Interview: we added a fourth node and most of the data moved

Hash mod N moves about N of N+1 keys. Compare fixed partitions and consistent hashing on 120 real keys, then watch one partition move, and decide auto vs…

More on Ticket interviews

Keep learning

Overview

hash mod N fixed partitions consistent hashing ui-monospace, SFMono-Regular, Menlo, monospace A canteen with three cashier lines. Students pick a line by their card number divided by 3. When a fourth cashier opens and the rule becomes divide by 4, nine of twelve students must change line. Before: card number % 3 picks the line After: card number % 4 picks the line Only 0, 1 and 2 stayed. 9 of 12 moved, to make room for one cashier. Hash values 0 to 11 placed on 3 nodes and then on 4 nodes. Only hashes 0, 1 and 2 stay on the same node. hash % N 4.5 TB · about 10 h fixed partitions, or ring 1.5 TB · about 3.3 h Adding a fourth node to 6 terabytes of data moves about 4.5 terabytes with hash mod N, but only about 1.5 terabytes with fixed partitions or consistent hashing. all 6 TB 1 · Ask any node it forwards for you 2 · A routing tier knows the map, holds no data 3 · Smart client has the map itself Coordinator keeps the map: partition → node one extra hop if wrong node one extra hop, always no hop, but must learn of moves Node 2 is busy and slow to answer Others decide it is dead Automatic rebalance moves its data away Network and the other nodes get extra load More nodes look slow. Go back to step 2 A busy node looks dead, automatic rebalancing moves its data, the extra load makes other nodes look slow, and the loop repeats. A human in the loop breaks the cycle. The divisor is N, the cluster size. example: hash 7 12 partitions. A key always stays in its partition. A key belongs to the first node clockwise from it. keys on each node prev ? 'Adding' : n Starting with Placement scheme Fixed partitions Ring points per node Add a node Remove a node Keys that moved Least possible Busiest node vs fair share Nothing has moved yet. Press "Add a node". What to notice: hash mod N moves most keys every time. Fixed partitions and the ring move about 1 key in N. A ring with 1 point per node is lumpy. Try 8. Three nodes. The routing table says partition P11 lives on C. Node D joins D is empty. Nothing moves yet. Requests for P11 still go to C. Copy in the background D copies P11 from C. C keeps serving reads and writes meanwhile. Catch up D also receives the writes that reached C during the copy, until it is level with C. The routing table now says P11 lives on D. In-flight requests to C get forwarded or retried. Clean up C deletes its old copy. The cluster is balanced again. = 2 && step caught up Routing table (kept by a coordinator) Requests for P11 go to The switch is one small update. Clients keep working during the copy. A real rebalance moves several partitions. We follow one. Start the move Next step What to notice: the data is copied first and the routing table changes last. Clients are never pointed at a node that does not have the data yet. Keys were placed with hash(key) % nodes. Adding a fourth node copied most of the data. virtual nodes auto vs manual A canteen that opens a new cashier Adding one node moved most keys. Place data so a new node takes only its fair share. Who decides when to move? 6 TB (illustrative) about 3 in 4 Today: 3 nodes. The data doubles in a year, so you will add nodes every few months. No downtime allowed. Which placement do you pick? Many fixed partitions, plus a routing table kept by a coordinator A new node takes whole partitions, about 1/N of the data. Pick a partition count with many factors and room to grow. Data moved per node added Extra bookkeeping Guess needed up front Consistent hashing with many points per node Keep hash % N and schedule a maintenance window for each new node Simple code, huge copy every time: 3 in 4 keys now, 9 in 10 at ten nodes. And downtime is ruled out. Why does changing the number of nodes move almost every key? explain simply The node is hash % N, so changing N changes almost every remainder. Hashes 0–11, from 3 to 4 nodes: only 0, 1, 2 stay. 9 of 12 move. From N to N+1 nodes, only about 1 key in N+1 stays. Goal: the new node takes only its fair share, 1 in 4 here. depends on N worked example N of N+1 move What is the catch with a fixed number of partitions? what breaks if You choose the count on day one, and it caps your node count. Partitions grow with data: too big moves slowly, too small adds overhead. A wrong guess means a costly reshard. Pick a count with many factors. nodes ≤ partitions reshard is expensive We have 6 TB on 3 nodes and add a 4th. How much data moves, and how long does it take? At 125 MB/s (1 Gbit/s): ~3.3 h vs ~10 h (illustrative). Real moves are throttled to protect traffic, so they take longer. 1/N fair share does the arithmetic mentions throttling After the move, how does a client find the right node? what would you do instead Ask any node, use a routing tier, or give clients the map. A coordinator (ZooKeeper, etcd, or built-in consensus) owns the map. Cutover risk: in-flight requests to the old node need forward or retry. routing options who owns the map cutover window Should the cluster rebalance itself automatically? A rebalance moves lots of data and loads network and nodes. With auto failure detection: busy node looks dead, data moves, others overload. Humans can also move data ahead of a known peak. cost of a move cascading failure human in the loop One customer key gets most of the traffic. Does adding nodes fix it? staff curveball No. One key always lives on one node, whatever the scheme. Salting: append a random 00–99 to spread its writes over 100 keys. Or give the hot key its own shard or machine. hot key ≠ placement salting and its read cost own shard We are adding a node. How do you avoid copying most of the data? The problem Fix 1 Fix 2 The cutover Copy, keep serving, then flip the map. Who pulls the trigger Suggested plan, human approves. hash % N on a growing cluster Hurts the first time you add a node. Too few partitions Auto rebalance + auto failover A slow node can trigger a spiral. Consistent hashing fixes hot keys It spreads keys, not one key’s traffic. hash % N moves ~N of N+1 keys Fixed partitions and ring, with a cost each Sized the move: ~1/N Human or throttle on rebalancing Hot key ≠ placement problem partitions chosen on day one per node at 10 nodes max nodes, ever hot key + random 00–99 100 keys spread over shards the price

We added a fourth node and most of the data moved

Adding one node copied most data all night. hash % N: changing N moves almost every key. 1 · The story in plain words 3 lines card % 3 picks line Open cashier 4 rule becomes card % 4 9 of 12 move nobody asked them to Better rule new cashier takes a few from each The rule depended on the number of cashiers. That is the bug.

Adding one node kept the cluster slow all night

Database on-call Keys placed with hash(key) % nodes. We added a 4th node to 3. The database copied data until morning. Lead asks: could it move less? Should the cluster rebalance itself? Keys that moved

Add a node and count the keys that move

Now follow one partition as it moves, with no downtime.

Four ways to place data

Moves ~1/N No up-front guess Range scans Even load hash % N Fixed partitions Consistent hashing Key-range splits Fixed size forever, or a refillable cache. Nodes come and go often.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.