TweakCode
Building Blocks lessons

Partitioning

How Partitioning works

Before you start

  • Caching

    Cache-aside is the read path everything here builds on.

    Open lesson

Why it matters

Redirect traffic no longer fits one cache node, so every short code needs a predictable owner among several nodes. Capacity changes and failures must not make clients disagree or turn the next request for nearly every code into a database miss. Adding one cache node must move only a small fraction of mappings, not flush the whole cache fleet.

Example

Hash the short codes

Redirect traffic no longer fits one cache. You will run a ring of cache nodes. The key is the short code.

Input
ring = {10: "A", 40: "B", 75: "C"}, keys = [5, 22, 50, 62, 80, 99]
Output
[["A", "B", "C", "C", "A", "A"], ["A", "B", "D", "C", "A", "A"]]

Approach 2 · Resolve the bottleneck

The idea

Keep one example in view

Hash the short codes

ring = {10: "A", 40: "B", 75: "C"}, keys = [5, 22, 50, 62, 80, 99]→[["A", "B", "C", "C", "A", "A"], ["A", "B", "D", "C", "A", "A"]]

The baseline, key mod N, is ≈ all keys move · O(1). The better model below keeps this exact input and removes that bottleneck.

Modulo N moves almost every sample key when node count changes. Place A, B, C, and short-code hashes on a ring instead; adding D at position 60 moves only key 50’s arc, while the mappings database still serves misses.

Put cache nodes A, B, and C at ring positions 10, 40, and 75. Hash each short code to the same 0–99 space; key 22 goes to the first node clockwise, B, while key 80 wraps to A. A binary search over sorted node positions finds that owner.

Adding D at 60 transfers only keys in its new arc, such as key 50 from C; the other sample keys stay put. Many virtual positions per physical cache smooth uneven arcs. The mappings database remains behind the ring for misses, so movement briefly costs fills rather than lost source data.

Visual anchor

Hash the short codes

A10B40C75D6052250628099
Add node D at 60. Only keys between 41 and 60 change owner, here just 50, from C to D. Every other key stays where it was.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.