Database Replication Explained: Why a Saved Edit Can Look Missing

Database Replication Explained: Why a Saved Edit Can Look Missing

avatar
Charles Lee

You move the departure time in a shared trip itinerary from 9:00 to 8:30. The editor confirms the save. Then you open the trip overview and see 9:00 again. A few seconds later, the overview shows 8:30. Both screens may have asked the server for fresh data and received different answers.

One possible explanation is database replication. The app keeps copies of its data on separate database servers. In a common setup, one server accepts edits and others answer some of the reads. If a copy is still catching up, a fresh query can return an older version of the itinerary.

How an edit reaches a second database

The app sends the edit to the primary, the server responsible for writes in this setup. Once the database commits the change, the app can show the updated departure time in the editor. The primary also produces a record of the change so another server can reproduce it.

In PostgreSQL physical streaming replication, the primary sends records from its write-ahead log, or WAL. A standby receives those records and replays them to update its own copy. Once it has replayed the commit for the itinerary edit, new queries on that standby can see the change. A standby that accepts read queries can serve as a read replica.

The change log allows copies to follow an ordered history of updates. An application can use a message queue to hand off work elsewhere, such as sending a trip notification. That job has its own timing. A notification arriving says little about which version of the itinerary a particular replica can serve.

Where the old departure time came from

Amber and cyan glass tiles showing the same route with map pins in different positions

Suppose the database confirms the save before the replica applies it. The editor shows 8:30 using the result of the write. The overview sends a separate request, and the app routes its query to the replica. Since the replica has yet to replay the edit, it answers 9:00. That delay in propagating a change is replication lag. If several replicas serve reads, each can be at a different point in the change history.

A browser or shared web cache can produce the same old departure time by reusing an earlier response. Replica lag can occur even when the app builds a new response for every request. Both effects can happen on the same page, so the old value alone gives you little evidence about where the delay started.

For an app you maintain, start by recording the version returned by the save and the one returned by the follow-up request. Check whether the overview actually called the API, whether the response came from a cache, and which database server answered. Comparing the primary and replica against the same itinerary ID helps narrow down the source. A general lag metric is useful for spotting a pattern. To show that a particular edit is visible on a replica, check that it has replayed through the required point in the log.

What the primary waits for before confirming

With asynchronous replication, the primary can confirm a commit without waiting for another server to catch up. This avoids putting a trip across the network to the replica in the save path. The trade-off is a window in which the replica can serve older data. A burst of writes, a slow network, or a replica struggling to apply changes can extend that window.

With synchronous replication, the primary waits for the configured acknowledgement from one or more standbys before reporting success. The meaning of an acknowledgement matters. In PostgreSQL, one setting waits for the required standby to record the change in durable storage. Another, called remote_apply, waits for it to apply the change as well, making the update visible to new queries there.

Waiting for other machines adds time to the save. A required standby becoming unreachable can also leave writes waiting. Even with synchronous replication configured, a read routed to a different, asynchronous replica can still see 9:00.

Decide which screens need the new value immediately

A traveler who just changed the itinerary expects the next view to reflect that edit. One approach is to return the saved value in the write response and send the follow-up database query to the primary. This supports read-your-writes consistency for that flow, provided the read gets a fresh database view and the page serves a fresh response.

Teams that want the follow-up read to use a replica can carry a position in the database change log from the saved transaction. They can then choose a replica that has applied through that point, or wait until a selected replica catches up. If the wait times out, the app can try the primary. A fixed delay may hide the issue on a quiet day, but the amount of time alone gives no assurance that the copy is ready.

A daily report of completed trips may tolerate a short delay and benefit from running on a replica. The departure message that goes out to the group should use the confirmed itinerary version. Making that decision per operation lets the app reserve the primary for reads that need it and move other queries off the write server.

Prepare for both a failed server and a bad edit

Two cyan glass tiles on silver stands beside a separate amber tile in an open protective case

A standby can help the service recover if the primary fails. During failover, an eligible standby takes over as the writer, and the application reconnects to it. Hosting the servers in separate failure domains on cloud infrastructure can help one copy survive an outage that takes the other offline. The changeover can still interrupt requests. If the new primary never received a recently confirmed edit, that edit can be missing after failover.

Recovery from a bad edit is a different job. If someone accidentally deletes the whole itinerary, the standby may replay that deletion too. A separately retained backup gives the team an earlier state to restore. With point-in-time recovery configured and the necessary logs retained, the team may be able to restore to just before the deletion and recover the itinerary without rolling back everyone else's updates.

For the trip app, each test answers a different question. Save a new departure time and immediately open the overview to check what the traveler sees. Test whether connections and confirmed changes survive a controlled failover. Restore a backup separately and check whether the deleted itinerary can be recovered. Together, those tests show when a copy can serve a useful answer and when the app needs a different source of data.

The illustrations for this article were generated with AI.