Back to roadmap
Module 4 · Edge: DNS, CDN, Load BalancersDay 03725 min

Sticky Sessions and Consistent Hashing

Route by key without thrashing the map when nodes change.

Day 037

Sticky Sessions and Consistent Hashing

N1
datastore
N2
datastore
N3
datastore
N4
datastore
Signal path
Hash ring with virtual nodes
Ring
note
async
N1
datastore
Ring
note
async
N2
datastore
Ring
note
async
N3
datastore
Memory hook

Sticky Sessions and Consistent Hashing: route by key without thrashing the map when nodes change

Mental model

move the first decision closer to the user

Design lens

Consistent hashing reduces churn massively.

Recall anchors
Sticky sessionsConsistent hashing

Why it matters

Sticky sessions pin a user to one server (e.g., for session affinity). Consistent hashing pins keys to nodes such that adding or removing a node only moves a small fraction of keys.

Deep dive

Naive mod-N hashing remaps every key when N changes; consistent hashing remaps only ~1/N.

Use cases: cache shards, distributed queues, partitioned services.

Virtual nodes balance load when ring is uneven.

Demo / scenario

Cache cluster of 5 nodes growing to 6.

  1. Mod-5 → mod-6 invalidates ~83% of keys.
  2. Consistent hash ring: only ~1/6 of keys move.
  3. Hot ranges still possible — add virtual nodes.
  4. Migration completes with negligible miss spike.

Tradeoffs

  • Consistent hashing reduces churn massively.
  • Virtual nodes balance but add overhead.
  • Some workloads still need explicit rebalancing.

Diagram

N1
N2
N3
N4
Ring
Hash ring with virtual nodes.

Mind map

Check yourself

Loading quiz…

Sources & further reading