Designing Database Scaling Patterns

1. Designing Horizontal Database Scaling

TechniqueDescription
Read replicasScale read throughput
ShardingScale writes + storage
FederationSplit DBs by function
Caching tierOffload hot reads
CQRSSeparate write/read models

2. Designing Vertical Database Scaling

TunableNotes
CPU coresHelps query parallelism, connection load
RAMBuffer pool / page cache; aim cache > working set
StorageNVMe vs SSD; high IOPS for OLTP
Network10/25 GbE for replication

3. Designing Database Sharding Strategy

DecisionRecommendation
Shard keyTenant_id, user_id (high cardinality, even)
Shard countPower-of-2; over-provision (e.g., 1024 logical)
RoutingSmart client or proxy (Vitess/Citus/PgBouncer)
Cross-shard txnAvoid; use Saga or 2PC sparingly

4. Designing Consistent Hashing for Sharding

       0 ────────────────── 2^32-1   (hash ring)
         A    B    C    D
   key → hash → first node clockwise
   Add node E → only ~1/N keys move
      
ConceptDetail
Virtual nodes (vnodes)Each physical node owns many points → balance
ReplicationWalk N nodes clockwise from key
Used inCassandra, DynamoDB, Memcached clients

5. Designing Cross-Shard Query Strategy

PatternDescription
Scatter-gatherQuery all shards, merge in coordinator
Broadcast tablesReplicate small reference data to all shards
Co-located shardsSame shard key for joined tables
Materialized aggregatesAsync-built rollup tables

6. Designing Shard Rebalancing

Online Resharding

  1. Add new shard, mark as draining target
  2. Dual-write to old + new for moving keys
  3. Backfill historical data
  4. Cutover reads after verifying parity
  5. Stop writes to old shard, decommission

7. Designing Federation Pattern

ElementDescription
Function-based splitUsers DB, Orders DB, Inventory DB
ProsSmaller DBs, separate scaling, isolated failure
ConsNo cross-DB joins/transactions
IntegrationAPI calls, events, replicated read views

8. Designing Database Proxy Layer

CapabilityExamples
Connection poolingPgBouncer, RDS Proxy
Read/write splitProxySQL, MaxScale
Sharding routerVitess, Citus
Query rewritingCaching, masking PII
Failover managementAuto-redirect to new primary

9. Designing Write Amplification Solutions

CauseMitigation
LSM compactionTune levels, leveled vs tiered
Many indexesDrop unused; partial indexes
Wide rows in column storeSmaller batches; column projection
Replication factorRight-size; semi-sync

10. Designing Hot Spot Prevention

Hot SpotSolution
Sequential keys (time, autoinc)Prefix with hash / random salt
Power user / tenantSub-shard the hot tenant
Hot read rowCache, replicas, request coalescing
Hot write rowSharded counters, write batching