Databases 9 min read

Why Your New Master‑Slave DB May Not Reflect Recent Writes—and How to Fix It

This article explains a real‑world case where a newly added mapping rule didn't appear immediately due to master‑slave replication lag, walks through the evolution of database architectures from single‑node to read‑write separation, and presents several practical strategies to mitigate consistency issues caused by replication delay.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Your New Master‑Slave DB May Not Reflect Recent Writes—and How to Fix It

Introduction

When I first joined the company I faced a problem with a database read‑write separation architecture: newly added mapping rules did not take effect immediately, and queries returned no data.

Database read‑write separation diagram
Database read‑write separation diagram

The issue was caused by the master‑slave setup where writes go to the master and reads go to the slave, which lags behind due to replication delay.

Database Architecture Evolution

Primary‑Backup (Master‑Standby)

Initially a single database is used, but it has a single point of failure. Adding a standby replica that synchronously mirrors the master solves availability but only activates on failure.

Master‑standby architecture
Master‑standby architecture

When the primary fails, the standby can be promoted manually to become the new master.

Master‑standby failover
Master‑standby failover

Master‑Slave (Read‑Write Separation)

As traffic grows, reads become a bottleneck. Adding read replicas (slaves) allows the master to handle writes while slaves serve reads, improving read performance. However, asynchronous replication introduces latency, causing temporary inconsistency.

Master‑slave architecture
Master‑slave architecture

During the latency window, reads from the slave may miss recent writes, which was the root cause of the observed problem.

Replication delay illustration
Replication delay illustration

Solutions to Master‑Slave Lag

1. Tolerate Inconsistency

If the application can accept stale data, no changes are needed.

Tolerate inconsistency
Tolerate inconsistency

2. Synchronous Replication

Configure the master‑slave link to be synchronous so the write returns only after the slave has applied the change, at the cost of higher write latency.

Synchronous replication
Synchronous replication

3. Force Reads to Master

For strongly consistent operations, route reads to the master, sacrificing some read scalability.

Force read master
Force read master

4. Middleware Routing

Introduce a middleware that records recent write keys; reads for those keys are sent to the master, otherwise to the slave.

Middleware routing
Middleware routing

5. Cache‑Based Routing

Use a cache to store keys of recent writes with an expiration equal to the replication delay; reads check the cache and decide whether to query the master or slave.

Cache routing
Cache routing

Conclusion

Read‑write separation solves read bottlenecks but brings replication delay and temporary inconsistency. Choose the mitigation strategy that best fits your consistency requirements and performance constraints.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

databasecachingMaster‑SlaveRead-Write SeparationConsistency
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.