hardSystem Design

What is database sharding?

651 views
01

Understand the problem

Explain sharding and trade-offs.

shardingpartitioning
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Routing a key to a shard
Run Playground
import hashlib

SHARDS = ["db-0", "db-1", "db-2", "db-3"]

def shard_for(key):
    h = int(hashlib.md5(key.encode()).hexdigest(), 16)
    return SHARDS[h % len(SHARDS)]

for k in ["user:1001", "user:1002", "order:77", "order:78"]:
    print(k, "->", shard_for(k))

# NOTE: plain modulo means adding a shard remaps most keys.
# Use consistent hashing to limit how much data moves on resize.
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.