feat: implement horizontal scaling optimizations for authz service

- Add /health and /ready endpoints for load balancer health checks
- Replace in-memory JWT token cache with Redis for multi-replica support
- Reduce DB connection pool from 100 to 25 connections per replica
- Add distributed rate limiting (100 req/min + 20 burst) using Redis
- Implement circuit breakers for DB and Redis to prevent cascading failures

This enables the service to scale horizontally with multiple replicas
behind a load balancer without exhausting database connections or
maintaining separate token caches per instance.
This commit is contained in:
2025-12-16 10:03:18 +08:00
parent ee8079e65c
commit 0d8f5b9600
9 changed files with 400 additions and 67 deletions
+14 -4
View File
@@ -1,17 +1,20 @@
// pkg/redisclient/redis.go
package redisclient
import (
"authorization/helper"
"context"
"fmt"
"os"
"time"
"github.com/redis/go-redis/v9"
)
var RDB *redis.Client
// RedisCircuitBreaker protects Redis operations
var RedisCircuitBreaker *helper.CircuitBreaker
func Init() {
redisHost := os.Getenv("REDIS_HOST")
if redisHost == "" {
@@ -39,9 +42,16 @@ func Init() {
RDB = redis.NewClient(opts)
// Test connection with authentication
// Initialize circuit breaker
RedisCircuitBreaker = helper.NewCircuitBreaker("redis", 5, 2*time.Second)
// Test connection with authentication using circuit breaker
ctx := context.Background()
if _, err := RDB.Ping(ctx).Result(); err != nil {
err := RedisCircuitBreaker.Call(func() error {
_, err := RDB.Ping(ctx).Result()
return err
})
if err != nil {
panic(fmt.Sprintf("Could not connect to Redis: %v", err))
}