Load balancers, sessions and Redis

Knowledge Videos

Short animated explainers, organised by subject.

Load balancers, sessions and Redis

How a load balancer spreads traffic, why in-memory sessions and sticky sessions break, how Redis as a shared session store fixes it, and how Services, Ingress and stateless pods fit together in Kubernetes.

Transcript

A load balancer spreads incoming requests across several servers, so no single server is overloaded, and one failure does not take you down.

It picks a server by a rule: round robin takes turns, least connections picks the quietest, and a hash keeps a client on the same server.

But if each server keeps sessions in its own memory, the next request may reach a server that has never seen you, and you are logged out.

Sticky sessions pin a user to one server, but load becomes uneven, and the session is lost when that server restarts or is replaced.

The better fix is stateless servers. Store sessions in Redis: the cookie holds only a session ID, and every server reads the session from Redis.

Redis is fast, and a TTL expires idle sessions automatically. Run it with replication, so the session store is not a single point of failure.

In Kubernetes, a Service load balances across ready pods. An Ingress adds HTTP routing, and a LoadBalancer service puts a cloud load balancer in front.

Services balance per connection, so long-lived connections like gRPC can stick to one pod. Use a layer 7 proxy to balance per request.

Pods come and go during rollouts and autoscaling, so keep them stateless, and point every pod at Redis through its Service name.

ClientIP affinity on a Service is only a stop-gap. Redis sessions let you scale and deploy without logging anyone out.

Balance. Share state. Scale.

More in this series