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
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.

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.

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).

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.

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:
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_idif 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.