Skip to main content
In this lesson we cover how to choose a database and when — and how — to shard in production systems. Many modern databases already implement sharding and routing for you, so you rarely need to build hash rings, bucket maps, migration processes, and routing layers from scratch. The main decision is which family of database fits your workload and how that affects sharding, joins, and operational complexity.

High-level families: Relational vs NoSQL

Choose your database family first — relational (SQL) or NoSQL — because that choice determines where sharding is implemented and what trade-offs you accept.
  • Relational databases (SQL)
    • Examples: MySQL, PostgreSQL
    • Data model: fixed tables, columns, and explicit relationships (e.g., Users, Chirps, Likes)
    • Strengths: powerful joins and strong transactional guarantees via SQL
    • Sharding model: typically implemented by an additional layer (proxy/middleware) rather than built into the core database
  • NoSQL databases
    • Examples: Cassandra, DynamoDB, MongoDB
    • Data model: designed to scale across many machines from the start
    • Trade-offs: often relax cross-shard joins and strict cross-node transactions to achieve low-latency scale
    • Sharding model: routing and splitting are built into the database itself
A simple normalized relational layout (for context):
The real difference is where the splitting happens: relational systems usually require an external sharding layer; many NoSQL systems do the splitting and routing for you.

Relational world: Vitess and Citus

When you want to preserve SQL semantics and joins but still scale horizontally, you typically add a distributed layer that presents a single logical SQL endpoint.

Vitess (for MySQL)

  • Built at YouTube and used by other large services.
  • Sits between your application and MySQL servers and makes a MySQL cluster look like a single instance.
  • The application sends normal SQL to Vitess; Vitess routes queries to the correct shard.
  • When adding or rebalancing shards, Vitess performs online migrations using copy → catch up → cut over → delete steps.
A dark-themed diagram showing an app routed through a central "Vitess" layer down to five database shards (Shard 0–4) with dashed arrows and Shard 2 and 4 highlighted. A progress bar at the top marks step "2 Catch Up" as the current stage.
Every shard behind Vitess is a normal MySQL instance; Vitess provides routing, resharding, and online migration orchestration.

Citus (for Postgres)

  • Adds a distributed layer on top of Postgres so your app talks to a single Postgres endpoint.
  • You instruct Citus to shard tables by a key (for example, user_id). If you shard Users, Chirps, and Likes by the same key, rows for a given user are colocated, enabling local joins.
  • Small, frequently joined reference tables (e.g., country_codes) can be replicated to every shard to avoid cross-shard joins.
A dark-themed diagram illustrating a Citus sharded database architecture. An app connects to a routing layer that holds a small country_codes table and routes queries to four PostgreSQL shards labeled Shard 0–3.
Vitess and Citus preserve many relational strengths (joins and correctness) while providing an operational path to scale via sharding.

NoSQL world: Cassandra, DynamoDB, MongoDB

NoSQL systems typically implement sharding, routing, and rebalancing inside the database itself. They commonly favor denormalization to avoid expensive cross-shard operations.

Cassandra

  • Any node can accept a request, consult the hash ring, and forward internally to the nodes that own the data.
  • There is no single master; requests can be routed by any node.
  • Uses virtual nodes (vnodes): each physical node appears many times on the ring, which smooths rebalancing when adding machines.
  • Design pattern: encourages denormalization (e.g., store frequently needed attributes directly on referencing rows).
A dark-themed diagram titled "Cassandra" showing a circular ring of orange virtual nodes labeled "Virtual Nodes." To the right is a vertical stack of cylindrical database icons with the bottom one outlined and labeled "New Machine."

DynamoDB (managed by AWS)

  • Fully managed: Amazon runs machines and handles data splitting and rebalancing on your behalf.
  • You choose the partition key (shard key). For a Chirp app that might be user_id. AWS splits and rebalances data as it grows.
  • Important constraint: a single item is never split across machines. A single hot key has practical throughput limits (roughly 3,000 small reads/s and 1,000 small writes/s — approximate and dependent on item size and settings).
  • Effect: very hot keys can become bottlenecks; plan for caching, client-side sharding of hot keys, or adaptive partitioning.
A stylized DynamoDB diagram showing an app on the left routing requests to multiple partitions on the right, with the partition key labeled "user_id." One partition is highlighted and annotated with throughput bars and a "Lena 500" hotspot showing 3,000 reads/s and 1,000 writes/s.
DynamoDB removes much of the operational burden but makes partition/key design a first-class concern to avoid hot keys.
Hot-key limits matter in managed services. If a single partition key becomes extremely hot (many reads/writes), you will hit throughput limits even if the rest of the dataset is small. Design partition keys, caching, or key-splitting strategies up front.

MongoDB

  • Stores flexible documents (records with nested structures), which supports embedding related data inside a single document instead of joining.
  • Uses an external router (mongos) that proxies requests to the correct shards.
  • Sharding in MongoDB splits shard key ranges into chunks. When chunks become imbalanced, the balancer migrates chunks using copy → catch up → cut over → delete steps.
  • Queries that include the shard key are routed to a single shard:
  • Queries that don’t include the shard key must be scatter-gathered to all shards and then merged:
Scatter-gather works but increases cross-node network and CPU usage.

Quick comparison

Operational guidance and shard-key strategy

  • The single most important engineering decision is the shard/partition key. It determines:
    • Whether common queries target a single shard (fast) or require scatter-gather (slow).
    • Whether joins are local to a shard or require denormalization/replication.
  • Prefer shard keys that align with primary access patterns (for example, user_id if most queries are per-user).
  • Avoid monotonic keys (like auto-incrementing IDs) as they lead to hotspots.
  • For managed services, account for per-key throughput limits and plan caching or sharded client keys for very hot users.
Tip: If you must join frequently across entities, colocate related tables/documents on the same shard or replicate small reference tables. If you design to denormalize, accept the trade-off of extra writes for faster reads.

Summary

  • Relational databases ([MySQL], [Postgres]) usually need an added layer (Vitess, Citus) to scale via sharding while preserving SQL semantics.
  • NoSQL systems ([Cassandra], [DynamoDB], [MongoDB]) natively implement sharding and routing and tend toward denormalized schemas to avoid cross-shard joins.
  • The shard/partition key is the most critical schema design choice: it directly impacts latency, load distribution, and the feasibility of joins.
  • Managed services reduce operational overhead but do not remove the need for careful schema and partition key design to avoid hot keys and other limits.

Watch Video