Allow users to configure an external datastore for WebSocket connections so that session or connection information isn’t limited to a single pod’s memory #1872
Labels
enhancement
New feature or request
Currently, our application relies on sticky sessions to maintain websocket connections in a horizontally scaled environment. However, sticky sessions can sometimes fail to route clients to the correct pod, causing dropped or mismatched sessions. This creates potential downtime or data inconsistency when multiple pods are running.
To address this, we want the ability to configure a distributed state manager—such as Redis—so that websocket states are synchronized across all pods. This will ensure that any pod can handle websocket events without relying solely on sticky session routing.
Proposed Change
In server.py (around line 96), instead of creating the Socket.IO server without a shared state manager:
//Read the Redis URL from an environment variable (or some config).
// Example: "redis://redis-service:6379/0"
redis_url = os.getenv("REDIS_URL")
// Conditionally enable the Redis manager if configured.
if redis_url:
mgr = socketio.RedisManager(redis_url)
sio = socketio.Server(
async_mode='asgi', # or 'gevent', 'threading', etc.
client_manager=mgr
)
else:
// Fallback to default behavior with no external manager
sio = socketio.Server(
async_mode='asgi'
)
The text was updated successfully, but these errors were encountered: