14 / 25 · Data and consistency
Database replication
Replication keeps copies of database data. It can improve resilience or serve reads, but asynchronous copies may lag. A replica is not the same as an independent backup.
What you will practise
- Explain replication lag
- route read-after-write
- distinguish replica and backup
Before you start: Write acknowledgment, primary and read replica.
About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.
Download MP4CaptionsListen without the video
Download audioChapters and full transcript
Let us understand database replication. Two students copy notes from one teacher. If copying takes time, one notebook may lack the last sentence. If the teacher accidentally deletes an important page and the students copy that deletion, every notebook can lose it. You still need a recoverable older copy. Replication keeps copies of database data. It can improve resilience or serve reads, but asynchronous copies may lag. A replica is not the same as an independent backup.
Follow the normal path. A booking write reaches the current primary. Its acknowledgment policy determines what the application can assume about durability and replication at that moment. Changes travel to replicas. With asynchronous replication, the primary can reply before all copies have applied the latest write. SeatSpot may serve public catalog reads from replicas while routing authoritative reservations to the write owner. A user reading immediately after a write must not be surprised by a stale confirmation state.
Now change one condition. A confirmation read reaches Replica A before the booking arrives there. The user may see an older state even though the primary accepted the write. A common misunderstanding is this: Replication protects against every accidental deletion. A replica can copy the same deletion or corruption. Keep restore-tested recovery points separately. Think about this. The primary confirms at 12:00:00; the replica applies at 12:00:02. What can a read at 12:00:01 show? Pause the video and make your prediction. The earlier state, even though the primary accepted the write. Read-your-writes requires a suitable authoritative or documented consistency path.
Let us reason with a concrete example. The primary confirms a booking at 12:00:00. A read replica applies it at 12:00:02. A read at 12:00:01 may see the earlier state. Two seconds here is fictional teaching lag, not a product guarantee. Read the confirmation from an authoritative path or implement a documented consistency mechanism. Do not tell the user their purchase disappeared simply because a replica is behind. These figures are teaching examples, not production guarantees.
Turn the idea into a design decision. Use the primary or an explicitly supported consistency mechanism for confirmation. Public catalog reads can have a different staleness allowance. If the primary fails, promotion needs a clear election or orchestration process and fencing. Avoid letting an isolated old writer accept conflicting new changes. When you try the practice task, look for this result. Lag is different from a failed purchase; a replica is not an independent historical backup.
Check your understanding. Replication also copies many mistakes. Maintain tested backups and recovery points independently rather than counting live copies as protection against every kind of data loss. Here is a challenge. Why can a replica show an order as missing just after checkout? Pause before the answer. Asynchronous replication may not have applied the write yet. Route that confirmation to an appropriate authoritative path or use a supported read-your-writes mechanism. Distinguish lag from a real failed write. For an interview, compare acknowledgment, read consistency and failover fencing. Say what you would measure, not only which tool you would buy.
Start with an everyday picture
Two students copy notes from one teacher. If copying takes time, one notebook may lack the last sentence. If the teacher accidentally deletes an important page and the students copy that deletion, every notebook can lose it. You still need a recoverable older copy.
Why SeatSpot needs this
SeatSpot may serve public catalog reads from replicas while routing authoritative reservations to the write owner. A user reading immediately after a write must not be surprised by a stale confirmation state.
Trace one step at a time
Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.
Write at the owner
A booking write reaches the current primary. Its acknowledgment policy determines what the application can assume about durability and replication at that moment.
Read all six mechanism steps
Write at the owner
A booking write reaches the current primary. Its acknowledgment policy determines what the application can assume about durability and replication at that moment.
Ship changes
Changes travel to replicas. With asynchronous replication, the primary can reply before all copies have applied the latest write.
Read a lagging copy
A confirmation read reaches Replica A before the booking arrives there. The user may see an older state even though the primary accepted the write.
Choose a consistent path
Use the primary or an explicitly supported consistency mechanism for confirmation. Public catalog reads can have a different staleness allowance.
Coordinate failover
If the primary fails, promotion needs a clear election or orchestration process and fencing. Avoid letting an isolated old writer accept conflicting new changes.
Keep recovery separate
Replication also copies many mistakes. Maintain tested backups and recovery points independently rather than counting live copies as protection against every kind of data loss.
Pause and make a prediction
The primary confirms at 12:00:00; the replica applies at 12:00:02. What can a read at 12:00:01 show?
Correct a common misunderstanding
Claim to question: Replication protects against every accidental deletion.
Better explanation: A replica can copy the same deletion or corruption. Keep restore-tested recovery points separately.
Draw the write/read timeline
- Mark primary acknowledgment at second zero.
- Mark replica application at second two.
- Place a confirmation read at second one and choose a safer read path.
Check what you can explain
This is your self-check, not an automated grade. Try again without the diagram tomorrow.
0 of 3 goals checked. Choose one remaining idea to practise next.
Teach it back
Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.
Engineering model and trade-offs
In primary/replica arrangements, writes are accepted by the primary and changes are shipped to replicas. Asynchronous acknowledgment can be faster but may leave an acknowledged write unavailable after a primary loss before replication.
Synchronous replication can wait for specified remote durability/visibility guarantees, adding latency or reducing write availability when required replicas cannot reply. Inspect the database-specific guarantee rather than using the word synchronous loosely.
Failover needs promotion rules, client reconnection and fencing of the old writer. Two independent primaries can diverge. Monitor replication lag and use a read-your-writes strategy, such as routing a user to the primary temporarily, when necessary.
Work through an example
The primary confirms a booking at 12:00:00. A read replica applies it at 12:00:02.
Reason with the numbers
A read at 12:00:01 may see the earlier state. Two seconds here is fictional teaching lag, not a product guarantee.
Make a decision
Read the confirmation from an authoritative path or implement a documented consistency mechanism. Do not tell the user their purchase disappeared simply because a replica is behind.
Implementation sketch
-- Inspect your configured PostgreSQL replica in a safe environment:
SELECT pg_is_in_recovery();
-- Primary-side monitoring depends on permissions/configuration:
SELECT application_name, state, sync_state
FROM pg_stat_replication;Read-only diagnostic examples, not a complete replication setup. Permissions, monitoring access and column details depend on the actual database version/service.
Where this is used
- Read distribution where staleness is acceptable.
- Maintaining copies for defined failover scenarios.
Common mistakes
- Using replication instead of backups.
- Promoting a new writer without fencing the old one.
- Ignoring read-after-write behavior.
Practise a reasoned answer
Answer aloud or write notes before opening the reference. These prompts teach reasoning and trade-offs; they are not claimed questions from a particular company.
Synchronous or asynchronous replication?
Choose a practice depth
- Foundation
- Explain copying changes and temporary lag.
- Practical
- Compare acknowledgment, read consistency and failover fencing.
- Advanced
- Defend recent-write loss windows, promotion safety and measured recovery procedures.
Self-check rubric
- State the required user outcome and the relevant invariant.
- Explain the normal path and one failure path.
- Name a cost or consistency/latency trade-off.
- Say what you would measure or test before trusting it.
For deeper practice, connect this topic to the previous lesson and identify a shared bottleneck. A longer answer is not automatically a better answer.
Try it before viewing the solution
Why can a replica show an order as missing just after checkout?
Check one key idea
Your explanation and interview notes
Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.
Loading your local notes…
Official references and next reading
The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.