One viral post sends every like to the same database shard

You have sharded the like counts by post id, and the interviewer agrees that the hash spreads posts evenly across the cluster. Then comes the follow-up: one post goes viral, and most of the likes in the next hour land on it. Every one of those writes goes to the same row on the same shard, and the other shards sit idle.

The requirement

Every post shows a like count. Posts are sharded by post_id, so all of one post's data lives together and a single read answers "how many likes". That is right for almost every post. The trouble is one key that takes far more writes than one shard can accept, and a better hash function cannot help: the hash spreads keys evenly, but the load sits on a single key.

Say it in those words before you propose anything. "The cluster has capacity. One key does not."

Two designs and what each one costs

Option A: split the hot counter into sub-counters. The hot post gets several counters under keys such as (post, 0) to (post, 3). Each like goes to one of them, chosen at random, and the count is their sum.

SHARDS = 4
HOT = {7: 4}  # post id -> number of sub-counters

def shard_of(post, sub):
    return (post + sub) % SHARDS

def shard_loads(likes):
    loads = [0] * SHARDS
    for i, post in enumerate(likes):
        sub = i % HOT.get(post, 1)  # round robin here; random in production
        loads[shard_of(post, sub)] += 1
    return loads

def read_count(counters, post):
    return sum(counters.get((post, s), 0) for s in range(HOT.get(post, 1)))

Run it on 60 likes for post 7 and 10 likes each for posts 1 to 4. Without the split, the loads are [10, 10, 10, 70]: the shard that holds post 7 takes seven times what each of the others takes. With four sub-counters they are [25, 25, 25, 25], and read_count still returns 60 for post 7.

Option B: add up likes before they reach the database. Each application server keeps a small in-memory tally per post and flushes it once a second as a single increment. The hot row now takes one write per server per second, however many likes arrive.

What goes wrong, and the input that shows it

Option A fails on reads. If you split every post to be safe, a feed page that shows thirty counts now makes four reads for each of them. If you split only the hot ones, a list of hot posts has to be kept, and it is always one viral post late.

Option B fails on loss. Restart one server during the busiest minute and its unflushed likes are gone. For likes, that is usually acceptable. For votes that decide something, or for anything that becomes money, it is not.

What to say out loud

Ask first: "Does every like have to be stored and counted the moment it happens, or may the number be a second behind and occasionally miss a few during a crash?" Then commit: "If every event must survive and the count must be exact, I split the hot counter and pay with fan-out on read. If a slightly late count is fine, I add up likes on the servers and flush once a second, and the write rate on that row stops depending on how viral the post is."

The exercise

  1. Pick a counter you know that one item can swamp: votes on a poll, viewers of a live stream, stock left for a flash sale.
  2. Write what one lost event costs in that product, in one sentence.
  3. Write option A and option B for it in one line each, with what each stores.
  4. Write the input that hurts each one: a feed of thirty counts for A, a crash in the busiest minute for B.
  5. Say the deciding question aloud, then your choice for both answers to it, in under a minute.

Where this fits in Gadfly

System design is one of Gadfly's four rooms, next to algorithms, behavioral and code review. The design runs on a shared board, guided or as a mock, one trade-off at a time. In guided mode the tutor asks the next question, such as what one lost like would cost, and does not draw the answer for you. In a mock it behaves like an interviewer, with no coaching during the round and a debrief at the end that quotes what was said, not a grade.

Published 11 November 2026 · System design: one trade-off · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].