### Improve Read Performance
#### Cache Layer
- **Pros:** Fastest reads (for cached data), reduces database load.
- **Cons:** Data can be stale, limited storage, added application complexity for cache management.
- **Use When**:
1. a subset of data that is read very frequently and updated infrequently
2. a relatively quick win to offload the main database
3. tolerate slightly stale data
- **Example**: An e-commerce site caching its top-selling product information to speed up category and product pages.
#### Read Replicas
- **Pros:** Distributes read load, improves read throughput, offers read high availability.
- **Cons:** Replication lag (data might not be instantly current), increased infrastructure costs, doesn't help with write bottlenecks.
- **Use When**:
1. scale overall read throughput for the entire dataset, not just a small hot subset.
2. require higher availability for read operations
3. offload analytical queries to avoid transactional performance
- **Example**: A news website with many users Browse articles simultaneously. The content is written to the primary, and read replicas handle the high volume of article views.
#### Sharding
- **Pros:** Scales both reads and writes, handles massive datasets, improves fault isolation (reduces impact of failure).
- **Cons:** Highest implementation and management complexity (shard key selection, cross-shard queries, rebalancing), often requires significant application changes.
- **Use When**:
1. too large to fit on a single server
2. scale _both_ read and write operations
3. exhausted the benefits of caching and read replicas
- **Example**: A large social media platform where user data and their posts are distributed across many servers to handle millions of active users and a constant stream of new content.