DesignTheSystem/dev
System Design (HLD) 6 min read

Hot keys and partitioning

Split data across nodes with a good key, and spot one key taking all the load.

Partitioning

When one node can't hold the data or the traffic, split it: each node owns a slice of keys. A good key spreads load evenly; userId usually does.

A bad key piles work on one node. Partitioning by date puts every write for today on the same node.

Hot keys

Even with a good key, one item can be far more popular: a celebrity's post, the one product in a flash sale. All its traffic lands on a single node.

Fixes: cache the hot item in memory; split its counter into many sub-counters and add them up on read; or queue its writes so they arrive at a steady pace.

// one hot counter becomes 10, spread across nodes
increment(postId):
  shard = random(0..9)
  counters[postId + ":" + shard] += 1

likes(postId) = sum of counters[postId + ":0..9"]

Key takeaways

  • Partition by a key that spreads load evenly.
  • One popular key can overload one node.
  • Cache it, split it into sub-counters, or queue its writes.

Try it in the simulator: Spread the load with partitions

Put this concept into practice on Push Notification Pipeline:

  1. Build the pipeline with Kafka set to 1 partition and run it. Watch the queue become the bottleneck.
  2. Raise Kafka's partitions to 4, then 8, then more, running after each change. Add Workers to match.

What to watch: One partition is a single lane, so the queue saturates and requests fail. Each partition adds a lane that another Worker can read in parallel, and availability climbs as partitions are added until the queue keeps up.

Check your understanding

Answer these questions interactively to test what you learned and track progress on your skill map:

  1. Orders are partitioned by order date. What goes wrong?
  2. A single post gets a million likes a minute. What's a sound fix?

Continue learning