Scaling Real-Time Systems with Redis & Celery
How we use Redis, Celery and PgBouncer to remove connection bottlenecks and keep real-time systems available under load.
Live applications such as seat locking for a show or real-time vehicle tracking have a shared problem: many clients ask for the same small piece of state at the same moment. A relational database is built for durable, consistent records, not for answering thousands of tiny, short-lived questions a second. When connection spikes arrive, the database runs out of connections and memory before it runs out of CPU.
Here is the pattern we use across our Python backends, and why each part is there.
Keep short-lived state in Redis
Data that only matters for a few seconds does not belong in PostgreSQL. A driver's latest GPS position, or a seat that is held while someone pays, can live in Redis with a time-to-live, so it expires on its own.
- Pub/Sub for live updates. A WebSocket gateway subscribes to a Redis channel and pushes each message to the clients that care, so the application servers do not poll the database.
- Atomic locks for contested resources. Setting a key only if it does not exist gives a distributed mutex. Two people tapping the same seat cannot both win, and the lock releases itself if the process holding it dies.
Move slow work out of the request
A request should answer quickly and hand everything else to a worker. With Celery, sending an email, a push notification, a receipt or a heavy calculation becomes a queued task. API threads stay free, and a burst of work turns into a longer queue instead of timeouts.
Two details matter in practice: give urgent tasks (payments, confirmations) their own queue so a flood of low-priority work cannot delay them, and make tasks safe to run twice, because a retry will eventually happen.
Pool database connections
Each PostgreSQL connection costs memory, and many application processes opening their own connections can exhaust the server. PgBouncer sits in front of the database and shares a small set of real connections between many clients, which keeps memory use predictable when traffic spikes.
What we measure
We do not trust a design until it has been load tested against the real workload. For each system we track connection counts, queue length, task latency and the time to answer the slowest requests, and we change one thing at a time so we know what helped.
These ideas run through projects like Saadhan Charge, where chargers and drivers send a steady stream of live updates, and the auditorium ticketing app, where seats must never be sold twice. If you are planning a real-time product, talk to our team.
- Redis
- Celery
- Distributed Systems
- Performance
