What if your app could serve millions of users instantly without crashing?
Why Read replicas in HLD? - Purpose & Use Cases
Imagine a popular online store where thousands of customers browse products and place orders at the same time. The main database handles all these requests, but as traffic grows, it starts to slow down, making customers wait longer and sometimes causing errors.
Relying on a single database means every read and write request competes for the same resources. This causes slow responses and can crash the system under heavy load. Manually trying to fix this by upgrading hardware or limiting users is costly and frustrating.
Read replicas create copies of the main database that handle read requests separately. This spreads out the workload, so the main database focuses on writes while replicas serve reads quickly and reliably, improving performance and user experience.
SELECT * FROM products; -- all reads hit main database
SELECT * FROM products; -- reads directed to read replica
Read replicas enable systems to handle many more users smoothly by sharing the read workload across multiple database copies.
A social media app uses read replicas so millions of users can view posts and profiles instantly without slowing down the main database that saves new posts and comments.
Single databases struggle under heavy read traffic.
Read replicas copy data to serve read requests separately.
This improves speed, reliability, and scalability of applications.
Practice
read replicas in a system design?Solution
Step 1: Understand the role of read replicas
Read replicas are copies of the primary database used mainly to handle read requests.Step 2: Identify the benefit in system design
By directing read queries to replicas, the primary database is less loaded, improving read scalability.Final Answer:
To improve read scalability by offloading read queries from the primary database -> Option CQuick Check:
Read replicas = improve read scalability [OK]
- Thinking read replicas improve write performance
- Confusing read replicas with backup systems
- Assuming read replicas encrypt data
Solution
Step 1: Understand replication types
Read replicas usually use asynchronous replication, meaning data updates are sent with some delay.Step 2: Identify the correct replication behavior
Because replication is asynchronous, replicas may not always have the latest data instantly.Final Answer:
Replication is asynchronous, so replicas may lag behind the primary database. -> Option DQuick Check:
Replication = asynchronous lag possible [OK]
- Assuming replication is always synchronous
- Thinking replication triggers only on restart
- Confusing replication of queries with replication of data
Solution
Step 1: Separate reads and writes
Writes must go to the primary database to maintain data consistency.Step 2: Offload reads to replicas
All read requests should be directed to read replicas to reduce load on the primary and improve read response.Final Answer:
Send all 300 read requests to the read replicas and 100 writes to the primary -> Option AQuick Check:
Reads to replicas, writes to primary = best practice [OK]
- Sending writes to replicas causing data inconsistency
- Overloading primary with all requests
- Randomly distributing requests without separation
Solution
Step 1: Identify cause of stale data
Stale data usually happens because asynchronous replication delays updates to replicas.Step 2: Fix by changing replication method
Using synchronous replication ensures replicas have the latest data but may impact performance.Final Answer:
Replication is asynchronous causing delay; fix by using synchronous replication -> Option BQuick Check:
Stale data = async replication delay [OK]
- Restarting replicas without checking replication
- Adding replicas without fixing replication lag
- Reducing reads instead of addressing replication
Solution
Step 1: Understand scalability needs
Millions of reads require multiple replicas to distribute load effectively.Step 2: Balance data freshness and performance
Asynchronous replicas scale well but have lag; cache invalidation helps keep data fresh for users.Step 3: Evaluate other options
Single synchronous replica limits scalability; sending all to primary causes bottlenecks; no cache hurts freshness.Final Answer:
Use multiple asynchronous read replicas with eventual consistency and implement cache invalidation on writes -> Option AQuick Check:
Multiple async replicas + cache = scalable & fresh [OK]
- Choosing single synchronous replica causing bottlenecks
- Ignoring cache leading to stale reads
- Sending all traffic to primary causing overload
Ready to prove it?
Pass the test and this pattern moves unknown
