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:
+14
-4
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user