This article discusses the limitations of caching for personalized data and introduces read replicas as a solution for scaling database read throughput. It explains how read replicas work, the concept of read/write splitting, and highlights the crucial trade-off of replication lag and eventual consistency, offering strategies to manage it.
Read original on Dev.to #systemdesignWhile caching is highly effective for reducing load from frequently accessed, *shared* data (e.g., trending articles), it offers limited benefits for *personalized*, unique data (e.g., user inboxes, personal feeds). For such data, each request is distinct, requiring a unique computation by the database. Caching merely shifts the storage of individual results without reducing the underlying computational work for the database, as the cache entry is only used once before potentially becoming stale.
When a single database is overwhelmed by legitimate, non-cacheable read requests, the solution is to distribute the read load across multiple database instances. A Read Replica is a secondary database server that maintains an identical copy of the primary database's data and is dedicated solely to serving read queries. This allows the primary database to focus exclusively on write operations.
Without Read Replicas:
App Servers
|
|
(all reads AND writes)
v
[ Primary Database ] <-- doing everything, overwhelmed
With Read Replicas:
App Servers
|
|-- reads --> [ Read Replica 1 ]
|-- reads --> [ Read Replica 2 ]
|-- reads --> [ Read Replica 3 ]
|
|-- writes --> [ Primary Database ]Read/write splitting is the architectural pattern where the application or a proxy layer intelligently routes write operations to the primary database and read operations to the read replicas. The replicas are kept synchronized with the primary through a continuous process called replication. Any data change on the primary is automatically propagated to all replicas, ensuring they eventually reflect the primary's state.
The Trade-off: Replication Lag and Eventual Consistency
Replication is not instantaneous. There is always a brief delay, known as replication lag, between a write occurring on the primary and that change appearing on the replicas. During this window, replicas may serve slightly stale data. This introduces eventual consistency, meaning data will eventually be consistent across all nodes, but might be inconsistent at any given moment. Developers must assess the tolerance for staleness for different types of data (e.g., a follower count can be slightly stale, but a financial transaction history should not).