The CAP Theorem & Database Horizontal Sharding
Analyze Consistency, Availability, Partition Tolerance (CAP Theorem) trade-offs, and scale databases horizontally using Range and Hash Sharding.
CAP Theorem & Database Sharding
### The CAP Theorem (Brewer's Theorem)
In a distributed network partition (P), a system MUST choose between:
- **Consistency (CP)**: Every read receives the most recent write or an error.
- **Availability (AP)**: Every non-failing node returns a non-error response without guaranteeing latest data.
### Database Sharding
Sharding partitions large tables horizontally across multiple distinct database instances based on a **Shard Key** (`user_id`).