What is Replication Lag?
Replication lag is the delay between a write operation on a primary database and its application on a read replica, causing temporary data inconsistency.
Replication lag is the measurable time difference between when a write transaction commits on a primary database and when that same change is applied to one or more read replicas. It is a natural consequence of asynchronous replication, where the primary does not wait for replicas to acknowledge the write before returning success to the client. The lag can be expressed in seconds or as a count of transactions queued for replay.
Replication lag arises from several factors. Network latency between the primary and replica adds a base delay. The replica's own processing capacity, especially under heavy read load, can slow the application of incoming changes. Large transactions, such as bulk updates or schema changes, take longer to replay and increase lag. In systems like MySQL's asynchronous replication or PostgreSQL's streaming replication, the lag is typically sub-second in well-tuned environments but can grow to seconds or minutes during bursts of write activity or replica resource contention.
In the database stack, replication lag is a key concern for applications that require strong read-after-write consistency. If an application writes to the primary and immediately reads from a replica, it may see stale data if the lag is nonzero. Engineers monitor lag using tools like SHOW SLAVE STATUS in MySQL or pg_stat_replication in PostgreSQL. Mitigation strategies include using synchronous replication (which reduces lag to zero at the cost of write latency), routing critical reads to the primary, or implementing session-level consistency guarantees.
Key facts
- Replication lag is measured in time or as a backlog of transactions.
- It is inherent to asynchronous replication architectures.
- Common causes include network delay, replica load, and large transactions.
- High lag can lead to stale reads and inconsistent application behavior.
- Monitoring tools like SHOW SLAVE STATUS expose current lag values.
How it works in practice
Related terms
References
More in Databases
ACID
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties guaranteeing reliable database transaction processing, ensuring data integrity despite concurrent access or failures.
B-Tree Index
A B-Tree index is a self-balancing tree data structure that maintains sorted data for efficient insertion, deletion, and range queries in database systems.
BASE
BASE is a design philosophy for distributed databases that prioritizes availability and partition tolerance over immediate consistency, making it a looser alternative to ACID in NoSQL systems.
CAP Theorem
CAP theorem states that a distributed data system cannot simultaneously provide consistency, availability, and partition tolerance; it can only guarantee two of the three.
Connection Pool
A managed cache of database connections that applications reuse rather than opening and closing a connection for each query, reducing latency and server load.
Failover
Failover is the process of automatically or manually promoting a replica database to primary when the active node fails, ensuring continued availability.
Foreign Key
A column or set of columns in a database table whose values must match a primary key in another table, ensuring referential integrity between the two tables.
Hash Index
A data structure that maps keys to storage locations using a hash function, providing constant-time equality lookups but no ordered or range scans.
Materialized View
A database object that stores the precomputed result of a query as a table, refreshed periodically or on demand to improve read performance and reduce computational overhead.
NoSQL
NoSQL is a family of non-relational database systems designed for flexible schemas, horizontal scaling, and high-throughput data access that traditional SQL databases cannot easily provide.