Think of a read replica like an extra cashier lane that can look up orders but cannot change inventory. Interviewers love this topic because it tests whether you understand both scaling and consistency.
Question: What are read replicas in SQL databases, and why would you use them?
Answer: A read replica is a copy of the main database that receives changes from the primary database and is used mostly for SELECT queries. It helps spread read traffic across more machines so the primary can focus on writes. The trade-off is that replicas can lag behind, so they may temporarily return older data.
Interview-Ready Answer: I’d say a read replica is a secondary database that continuously receives changes from the primary and serves read-only traffic. I use replicas to scale reads, lower load on the primary, and sometimes place data closer to users. The main thing I watch is replication lag: after a write, a replica may not show the new value immediately, so for read-after-write paths I either query the primary or use a consistency strategy that guarantees freshness.
A read replica is a database copy that is kept in sync with the primary database. The primary accepts writes; the replica is usually read-only. The key idea is simple: instead of forcing every dashboard, report, and product search to hit one busy server, you spread those reads across replicas.
SELECT queries to the replica, while INSERT/UPDATE/DELETE stay on the primary.Use read replicas when reads are much heavier than writes, like product catalogs, analytics dashboards, feed pages, or “my recent orders” screens. They are also useful for geographic latency: a replica closer to Europe can answer European reads faster than a primary in another region.
Replicas do not make writes faster. Every write still lands on the primary, and the primary must ship those changes to each replica. So replicas reduce read pressure, but they do not magically remove a write bottleneck.
Also, replicas do not guarantee fresh data unless the system uses synchronous replication. In the more common asynchronous setup, the primary commits first and replicas catch up a moment later. That delay is called replication lag.
| Tool | Best for | Main trade-off |
|---|---|---|
| Read replica | More read traffic | Stale reads |
| Index | Faster query lookups | Slower writes, extra storage |
| Cache | Hot repeated results | Invalidation complexity |
A helpful rule: indexes make one database query cheaper, replicas give you more databases to answer reads, and caches skip the database entirely for hot data.
From an application perspective, a read query on a replica is still usually O(1) for lookup work, but end-to-end latency depends on network distance, replica load, and disk speed. Replication itself adds extra work to the primary: every change must be streamed to each replica, so write amplification grows with the number of replicas. In practice, teams often start with 1–2 replicas and add more only when they see sustained read pressure and acceptable lag.
Memory hook: Primary is the cashier, replicas are the look-up desks. They can answer questions fast, but only the cashier changes the receipt.
Real-World Story: Imagine a checkout service for an online store. The primary database stores orders, payments, and inventory changes. The app sends the customer’s “order history” page and the admin dashboard to read replicas so thousands of users can browse without slowing down checkout. A common outage happens when the team assumes a replica is instantly fresh: right after payment succeeds, the app asks a replica whether the order exists, but replication is 2 seconds behind. The user sees “payment pending,” support tickets spike, and logs show the order row exists on the primary but not yet on the replica. The fix is to route critical read-after-write checks to the primary, or wait for confirmed replication before showing the final success state.
-- PostgreSQL-flavored SQL example.
-- This script demonstrates the main idea: writes happen on the primary,
-- while read replicas are read-only and are safe for SELECT-heavy traffic.
DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL,
status TEXT NOT NULL
);
-- Simulate the writable primary database.
INSERT INTO orders (order_id, customer_name, status) VALUES
(1, 'Asha', 'paid'),
(2, 'Ben', 'pending'),
(3, 'Chao', 'paid');
-- A normal read on the primary.
SELECT order_id, customer_name, status
FROM orders
ORDER BY order_id;
-- A read-replica session is effectively read-only.
START TRANSACTION READ ONLY;
-- This confirms the session cannot write.
SELECT current_setting('transaction_read_only') AS transaction_mode;
-- Safe query: this is exactly the kind of traffic a replica should handle.
SELECT order_id, customer_name, status
FROM orders
WHERE status = 'paid'
ORDER BY order_id;
-- Edge case: uncommenting the next line would fail in a real read-only replica session.
-- UPDATE orders SET status = 'cancelled' WHERE order_id = 2;
ROLLBACK;Follow-up & Tricky Questions:
Common Mistakes:
Memory Hook: “Primary changes the truth; replicas repeat the truth later.”
Cheat Sheet:
SELECT traffic.Practice Tasks: