Skip to content

Horizontal scaling with Redis pub/sub #139

Description

@karutoil

Summary

The backend currently runs as a single instance. For horizontal scaling (multiple backend instances behind a load balancer), we need Redis pub/sub to synchronize WebSocket state, cache invalidation, and task scheduling across instances.

Current State

  • WebSocket gateway (src/websocket/gateway.ts) tracks agent connections in an in-memory CappedMap
  • Agent registry is in-process — only the instance that received the agent handshake can route commands to that agent
  • Task scheduler runs in-process — only one instance should schedule tasks to avoid duplicate execution
  • Permission caching uses in-memory 30-second TTL — no cross-instance invalidation
  • Redis is already in the Docker Compose stack but only used for future pub/sub (not currently connected)
  • catalyst-backend/package.json does not include ioredis or any Redis client yet

Requirements

  • Add Redis client (ioredis) to backend dependencies
  • Agent registry: publish agent connection/disconnection events via Redis pub/sub so all instances know which agent is connected where
  • WebSocket routing: when a client request needs to reach an agent on a different instance, forward the command via Redis pub/sub
  • Task scheduling: use Redis-based distributed lock (e.g., Redlock) so only one instance runs scheduled tasks at a time
  • Cache invalidation: broadcast permission/config cache invalidation across instances via Redis pub/sub
  • Session affinity: document that WebSocket connections require sticky sessions or that the pub/sub bridge handles cross-instance routing transparently

Acceptance Criteria

  • Redis client added to backend with connection configuration
  • Agent registry syncs across multiple backend instances via pub/sub
  • Server control commands route to the correct backend instance that holds the agent WS connection
  • Scheduled tasks execute exactly once across N instances (distributed lock)
  • Permission cache invalidation propagates across instances
  • Docker Compose example with 2+ backend instances
  • Health check endpoint reports Redis connectivity status

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backendBackend / API workarea:infraInfrastructure / DevOpsenhancementNew feature or requestin-progressCurrently being worked on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions