Back to Blog

Database Sharding: Horizontal Scaling Guide

December 21, 2025
Technical Tips5 min read
Database Sharding: Horizontal Scaling Guide

What Is Database Sharding?

Database sharding is a horizontal scaling technique that distributes data across multiple database instances (shards) based on a shard key. Each shard holds a subset of the data and operates independently. Instagram, for example, shards their PostgreSQL database across thousands of instances to serve 2 billion+ monthly active users.

Sharding is the go-to answer when an interviewer asks "How would you scale this database beyond a single server?" It enables near-linear horizontal scaling.

Sharding Strategies

  • Range-Based — Shard by value ranges (users A-M on shard 1, N-Z on shard 2). Simple but can create hotspots
  • Hash-Based — Hash the shard key (e.g., user_id % num_shards). Even distribution but resharding is complex
  • Directory-Based — Lookup table maps each key to its shard. Flexible but the directory becomes a single point of failure
  • Geographic — Shard by region for data locality and compliance (EU data on EU shards)

Choosing a Shard Key

The shard key determines data distribution and query routing. A good shard key has high cardinality (many unique values), even distribution, and aligns with your most common query patterns. For a social media app, user_id is often the best shard key because most queries are user-scoped.

Cross-shard queries (JOINs across shards) are expensive — minimize them through denormalization. Read designing scalable databases for schema strategies.

Share:
#TechnicalTips#InterviewPrep#CareerGrowth