Sharding
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Layer 3 · Go deeper · 9 min

    Go deeper

    Size it, build it, then make the calls an interviewer will push you on.

    Back of the envelope

    Size it up

    Tap through each step of the arithmetic.

    How many primaries does the chat app need for writes alone?

      How many databases does a year of messages need?

        How many suffixes does a hot match counter need?

          Build-up · 5 steps

          Route one request to one shard

          Two small pieces turn many databases into one logical one, a router and a shard map.

          Step 1 of 5

          The request carries the key

          Per-user data is sharded by user_id because almost every request starts with “me”. The key has to be in the request for routing to work.

          4 interview moments

          You’re the candidate

          The interviewer pushes on the hard parts. Pick an approach and see how it holds up.

          1. Deep dive 1 of 4

            Interviewer

            “A regional food-delivery app takes 200,000 orders a day. The team wants to shard now, “before it gets hard”. What do you say?”

            Your move. Pick an approach:

          2. Deep dive 2 of 4

            Interviewer

            “You’re sharding a food-delivery app’s orders table. Which shard key do you choose?”

            The kitchen tablet polls the restaurant’s live orders constantly. Customers check “my orders” far less often.

            Your move. Pick an approach:

          3. Deep dive 3 of 4

            Interviewer

            “You run 32 database hosts and will need more soon. How should keys map to hosts so that growing is cheap?”

            Your move. Pick an approach:

          4. Deep dive 4 of 4

            Interviewer

            “Two users’ wallets live on different shards, and one sends the other money. How do you make the transfer safe?”

            Your move. Pick an approach:

          Build-up · 5 steps

          Add a host without downtime

          Growing a sharded database means moving whole logical shards. The sequence is the same whether you do it by hand or a tool does.

          Step 1 of 5

          Copy

          Host 5 joins empty. Copy a snapshot of logical shard 4 onto it. The key-to-logical mapping never changes, so no other shard moves.