← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Database Replication

database PHP 5.0+ Intermediate
debt(d7/e5/b7/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints show automated detection is 'no' — replication lag issues and stale reads typically surface only during production load or careful testing. Tools like datadog and prometheus can monitor lag after setup, but detecting the architectural misconfiguration (all reads hitting primary, no lag handling) requires manual review or runtime observation of production behavior.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix indicates configuring read/write splitting in Laravel/Symfony config plus adding monitoring infrastructure. While the config change itself may be localized, properly routing time-sensitive reads to primary requires auditing application code paths to identify which reads occur post-write, touching multiple files across the codebase.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (b7). Database replication is a cross-cutting architectural choice that affects every database interaction — applies_to shows it spans web and cli contexts. Once in place, every query must consider whether it should hit primary or replica, and the replication strategy shapes how the entire application handles data consistency. This is a persistent tax on development decisions.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception explicitly states developers assume read replicas have the same data as primary, but async replicas have lag. This contradicts the mental model from simpler database setups where reads always return fresh data. Reading from replica immediately after write returning stale data is a non-obvious failure mode that catches developers who expect database reads to be consistent.

About DEBT scoring →

Also Known As

read replica sync replication async replication logical replication

TL;DR

Copying data from one database server to replicas — synchronous replication guarantees zero data loss, asynchronous is faster but risks losing recent writes on failure.

Explanation

Synchronous replication: primary waits for at least one replica to confirm before acknowledging the write — zero data loss guarantee, but higher write latency. Asynchronous replication: primary acknowledges immediately, replica catches up — lower latency, but a primary failure can lose committed transactions. Logical replication copies row-level changes (compatible with different schema versions). Physical/streaming replication copies WAL binary — exact byte-for-byte replica. Use cases: read replicas (scale reads), failover (high availability), geographic distribution, and analytics offload.

Diagram

flowchart LR
    subgraph Sync Replication
        P1[Primary] -->|wait for ack| R1[Replica]
        R1 -->|confirmed| P1
        NOTE1[Zero data loss<br/>Higher write latency]
    end
    subgraph Async Replication
        P2[Primary] -->|fire and forget| R2[Replica]
        P2 -->|ack immediately| APP2[App]
        NOTE2[Low latency<br/>May lose recent writes on failure]
    end
    style NOTE1 fill:#238636,color:#fff
    style NOTE2 fill:#d29922,color:#fff

Common Misconception

Read replicas have the same data as the primary — async replicas have replication lag; reading from a replica immediately after a write may return stale data.

Why It Matters

Directing all application reads to the primary wastes replica capacity — but reading user profile from a replica after the user just updated it returns stale data if replication lag is not handled.

Common Mistakes

  • Reading from replica after write without accounting for replication lag.
  • Synchronous replication for all replicas — one slow replica blocks all writes.
  • No monitoring of replication lag — lag can grow silently to hours under heavy write load.
  • Failing over without promoting the most up-to-date replica — partial data loss on failover.

Code Examples

✗ Vulnerable
// Read after write from replica — stale data:
$this->primaryDb->update('users', ['name' => 'Alice'], ['id' => 42]);
// Replication lag: 200ms
$user = $this->replicaDb->find('users', 42); // Returns old name for 200ms
return $user['name']; // 'Bob' — stale! User just changed it to 'Alice'
✓ Fixed
// Read-your-writes: use primary for fresh data after writes:
public function updateAndReturn(int $id, array $data): array {
    $this->primaryDb->update('users', $data, ['id' => $id]);
    // Read from primary — guaranteed fresh:
    return $this->primaryDb->find('users', $id);
}

// Other reads from replica — fine with eventual consistency:
public function getPublicProfile(int $id): array {
    return $this->replicaDb->find('users', $id); // Stale OK for public data
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 55
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 3 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 17 Perplexity 12 Unknown AI 4 Google 4 Ahrefs 3 SEMrush 3 Majestic 2 ChatGPT 1 Meta AI 1
crawler 44 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
Route all writes to the primary and reads to replicas in your Laravel/Symfony config; add replica lag monitoring and route time-sensitive reads (post-write) to the primary
📦 Applies To
PHP 5.0+ web cli laravel symfony
🔗 Prerequisites
🔍 Detection Hints
All reads hitting primary DB under heavy read load; no read replica configured; replication lag not monitored
Auto-detectable: ✗ No mysql-replication datadog prometheus
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: High Context: File

✓ schema.org compliant