Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Solving Read-Your-Own-Writes
Replica Lag.

MySQL 8.0 InnoDB GTID replica lag (400 ms–3.2 s) causes users updating MFA numbers or RBAC roles to see stale pre-update state on immediate refresh.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When traffic spikes expose hidden coupling, combine a short Redis sticky-primary window (`ryow:user:{id}`) with GTID-verified reads (`WAIT_FOR_EXECUTED_GTID_SET`) and write-through cache invalidation.

TL;DR30-second simplified summary
▾

Core Mechanics: A MySQL 8.0 topology with 1 primary writer and 4 asynchronous GTID read replicas serves 8,500 RPS cleanly—until analytics exports or DDL push Seconds_Behind_Source to 400 ms–3.2 s.

Visual & Structural Flow: Depicts PATCH Primary -> Immediate GET Hits Lagging Replica (FastAPI Client -> MySQL Primary -> Read Replica); Sticky-Primary Window & Bounded GTID Wait (FastAPI + SQLAlchemy Router -> Redis Sticky Window (5s TTL) -> MySQL Primary (Writer) -> 4x Read Replicas (>=80% load)).

Trade-Offs & Production: Two naive workarounds break under load: Routing all reads to Primary: Destroys the >= 80% read offload across the 4 replicas, overloading the single writer. Causal session consistency is essential in read-heavy, single-writer relational architectures: Identity, MFA & RBAC Services: Guarantees revoked tokens/roles take effect immediately.

Failure Modes & Application: Causal session consistency is essential in read-heavy, single-writer relational architectures: Identity, MFA & RBAC Services: Guarantees revoked tokens/roles take effect immediately.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why async replica lag breaks Read-Your-Own-Writes.

Why this matters: The mechanism: understand how Why async replica lag breaks Read-Your-Own-Writes works from first principles.

Think of Why async replica lag breaks Read-Your-Own-Writes as the intuition layer for Solving Read-Your-Own-Writes Replica Lag: The mechanism: understand how Why async replica lag breaks Read-Your-Own-Writes works from first principles.

A MySQL 8.0 topology with 1 primary writer and 4 asynchronous GTID read replicas serves 8,500 RPS cleanly—until analytics exports or DDL push Seconds_Behind_Source to 400 ms–3.2 s. When a user updates their MFA phone number or RBAC permissions via PATCH /v1/users/{id} and immediately reloads GET /v1/users/{id}, routing the read to a lagging replica returns stale pre-update state.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Duplicate submissions and stale RBAC authorization checks.

Why this matters: The problem: see what breaks without Duplicate submissions and stale RBAC authorization checks under real concurrency or scale.

Breaking Read-Your-Own-Writes (RYOW) on identity and security paths causes immediate incidents:

  1. Security Vulnerability: Revoked RBAC permissions (GET /v1/users/{id}/permissions) continue to authorize requests for up to 3.2 seconds.
  2. UX Confusion & Retries: Users see their old MFA phone number after saving and resubmit the form repeatedly.
Async Replica Lag Race

PATCH Primary -> Immediate GET Hits Lagging Replica

FastAPI ClientMySQL PrimaryRead Replica1. PATCH /users/42 (revoke admin)2. 200 OK (GTID uuid:901)3. Async binlog lag (1.2s)4. GET /users/42/permissions5. Returns stale admin=true!

Routing an immediate post-write GET to an asynchronous replica exposes revoked permissions during the replication lag window.

Engineering Decision Framework
Problem
How to implement Solving Read-Your-Own-Writes Replica Lag without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When traffic spikes expose hidden coupling, combine a short Redis sticky-primary window (ryow:user:{id}) with GTID-verified reads (WAIT_FOR_EXECUTED_GTID_SET) and write-through cache invalidation.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Solving Read-Your-Own-Writes Replica Lag experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why Seconds_Behind_Source=0 and routing all reads to primary fail.

Why this matters: The problem: see what breaks without Why Seconds_Behind_Source=0 and routing all reads to primary fail under real concurrency or scale.

Two naive workarounds break under load:

  • Routing all reads to Primary: Destroys the >= 80% read offload across the 4 replicas, overloading the single writer.
  • Trusting Seconds_Behind_Source = 0: In MySQL, 0 only means the SQL thread caught up to the I/O thread's currently fetched relay log—if the I/O thread is stalled, the replica still serves stale data.
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Redis Sticky-Primary Window + GTID Causal Tokens.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Preserve >= 80% replica offload while guaranteeing RYOW and < 50 ms read bounds:

  • Sticky-Primary Redis Window (ryow:user:{id}): On PATCH, set a short Redis key (TTL = 5 s) after the DB commits. While present, GET routes directly to the Primary (zero replica lag).
  • GTID Causal Verification: Return gtid_executed from the Primary commit (source_uuid:tx_id). Replicas run SELECT WAIT_FOR_EXECUTED_GTID_SET(gtid, 0.05) (50 ms max); if it times out (!= 0), fail over that single read to Primary.
  • Write-Through Cache Invalidation: Delete/update Redis after DB commit (never before) so concurrent reads never repopulate Redis from a stale replica.
RYOW Routing Architecture

Sticky-Primary Window & Bounded GTID Wait

FastAPI + SQLAlchemy RouterChecks ryow:user:{id} + GTIDRedis Sticky Window (5s TTL)Routes recent writers to PrimaryMySQL Primary (Writer)Emits gtid_executed…4x Read Replicas…WAIT_FOR_EXECUTED_GTID_…

1.5s Sticky Window: Recent mutators read from Primary for 5s, eliminating replica lag on immediate page reloads.

2.50ms GTID Gate: If a replica has not applied the caller's GTID within 50ms, fall back to Primary immediately.

3.Security-Critical Paths: RBAC permission checks bypass stale replicas or read from Primary/consistent cache.

Combining a 5s Redis sticky-primary flag with a 50ms GTID wait guarantees RYOW without sacrificing 80%+ replica offload.

05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Read-Your-Own-Writes patterns are used in production.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Causal session consistency is essential in read-heavy, single-writer relational architectures:

  • Identity, MFA & RBAC Services: Guarantees revoked tokens/roles take effect immediately.
  • E-Commerce Cart & Profile Updates: Ensures users see their newly added address or payment method.
  • Multi-Region Read Replicas: Uses LSN/GTID watermarks in Postgres/MySQL to avoid global cross-region locks.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why is `Seconds_Behind_Source = 0` insufficient to guarantee Read-Your-Own-Writes on a MySQL 8.0 read replica?

Recommended Learning Flow