Question bankPricingSign in

Design a Database Sharding Strategy

Storage & File SystemsHard1:30

Design a database sharding strategy for a social network with 2 billion users, 500 billion posts, and 1 trillion friendship edges. The system handles 200,000 writes per second and 2 million reads per second. Data must be distributed across 1,000+ database servers. The most common queries are: fetch a user's profile, fetch a user's recent posts, and fetch a user's friends' recent posts (news feed).

In your 90-second answer, cover: - Sharding key selection and its impact on query patterns - How cross-shard queries work (especially the news feed query across friends' shards) - Rebalancing: how to add new shards without downtime or data loss - Hot shard mitigation for celebrity users with millions of followers

Constraints: single-shard queries (user profile, user's posts) must complete in under 10ms. Adding a shard must not require a full data migration. Global uniqueness constraints (usernames) must be enforced across all shards.

How to approach it

  • Hint 1

    Choose user_id as the sharding key. This co-locates a user's profile and their posts on the same shard, making the two most common queries (fetch profile, fetch posts) single-shard operations. But it means the news feed query (friends' posts) must fan out to multiple shards.

  • Hint 2

    Cross-shard queries like the news feed are expensive if done at read time. Consider pre-computing: when a user posts, fan out the post_id to each follower's feed on their respective shard. This makes the news feed a single-shard read at the cost of write amplification.

  • Hint 3

    For rebalancing, use consistent hashing with virtual nodes rather than range-based partitioning. When adding a new shard, it takes ownership of specific vnodes and only the data for those vnodes is migrated -- not the entire cluster. Use double-writes during migration: write to both old and new shard until migration is confirmed.

Ready to answer it out loud?

Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.

Practising needs an invite code. Join the waitlist and we’ll send you one.