← Lessons

quiz vs the machine

Gold1490

System Design

Sticky Routing for Stateful Connections

Pinning a client to the node holding its session state, and the tradeoffs that pinning introduces.

5 min read · core · beat Gold to climb

What it is

Sticky routing sends every message from a client to the same node for the life of its session. When a node holds in memory state such as an open WebSocket or a partial buffer, any other node would not know how to handle the message.

How stickiness is achieved

  • A consistent hash of the user id maps the client to a node.
  • Or a routing token issued at connect time names the node to return to.
  • The load balancer reads that key and pins the client.

The tradeoffs

  • A pinned node becomes a hot spot if its clients are unusually active.
  • When that node dies, its sessions are lost and must be rebuilt elsewhere.
  • Scaling out does not rebalance existing sessions, only new ones.

Reducing the pain

  • Keep node local state small and back it with a shared store so recovery is cheap.
  • Use consistent hashing so adding a node moves only a fraction of sessions.

Key idea

Sticky routing pins a client to the node holding its session state, trading away easy rebalancing and node failure tolerance for the ability to keep state in memory.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does a stateful connection need sticky routing?

2. What is a key drawback of sticky routing when a node fails?