user_id, username, and follower_count. Leo’s row will exist entirely on one shard, and Chris’s row will exist entirely on another, but both shards use the same schema.

- Compute
user_id % 4to get a remainder of0,1,2, or3. - Map that remainder to a shard:
0 → shard 01 → shard 12 → shard 23 → shard 3
1 % 4 = 1→ shard 14 % 4 = 0→ shard 07 % 4 = 3→ shard 3
Why teams shard: a single primary database has a finite write throughput. By splitting data across four machines, Chirp effectively has four primaries; each can accept writes for the rows it stores. If write capacity is still insufficient, you can add more shards (e.g., 8, 16, …) and distribute rows across them. This horizontal scaling increases write throughput by distributing load.
Pick a shard key that balances traffic. A good shard key spreads writes and reads evenly; a poor one can create “hot” shards. Consider user ID hashing or composite keys to avoid hotspots.

Routing decisions can be implemented in different places:
- Application layer: the app computes the shard and directs the query to the correct database.
- Middleware/proxy: a routing service sits between app and databases and forwards requests.
- Lookup service: a small directory service that the app queries to find the shard for a particular key.