HomeRoadmapsDB Migration Day 3
Database Migrations & Zero-Downtime Schema Evolution Β· Day 3 Β· 20 min

Idempotent Chunked Backfill & Lag Throttling
Zero-Downtime Schema Evolution β€” Day 3

Backfill millions of historical rows in deterministic primary-key batches with checkpoint resumption, idempotency guarantees, and replica-lag-aware throttling.

⚑ Runtime: Node.js 22 LTS Β· VitestπŸ›‘οΈ Database Reliability & Schema Evolution QE Architect
πŸ“¦Sample Codebase & Working Solution (Day 3)Available Now
View on GitHub

The official reference codebase for Day 3 is hosted in the repository. Clone the repository and navigate to tests/day3-chunked-backfill.spec.ts to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/777abhi/db-migration-qe-roadmap.git
cd db-migration-qe-roadmap/tests/day3-chunked-backfill.spec.ts
🏭Production Context & Enterprise Implementation
πŸ’‘ Why We Are Doing This: A single unbounded UPDATE table SET new_col = old_col bloats WAL logs, exhausts IOPS, and spikes read-replica lag into minutes.
🏒 Real-World Production Usecase: Backfilling 50M+ row tables in bounded primary-key chunks with automatic pause/backoff when replica lag exceeds 150ms.
βš™οΈ How Implemented in Production: Keyset-paginated batch runner with backfillVersion idempotency guard and replica-lag feedback loop.
Phase 1

Run Idempotent Backfill & Throttle Tests

Step 3.1: Run Day 3 Tests
Terminal β€” Day 3 Lab
npm run test:day3
Step 3.2: Sample Codebase & Working Solution (Day 3)
src/backfill/chunkedBackfillRunner.ts
const lagMs = getReplicaLagMs(batchIndex);
if (lagMs > config.maxReplicaLagMs) {
  throttleEvents++;
}
for (const row of batch) {
  if (row.migratedValue !== null && row.backfillVersion >= 1) {
    skippedAlreadyMigrated++;
  } else {
    row.migratedValue = row.legacyValue.trim();
    row.backfillVersion = 1;
    processedCount++;
  }
  cursor = row.id;
}
Day 3 Key Learnings
  • Replaced unbounded table-wide UPDATEs with deterministic primary-key cursor batches.
  • Proved 100% idempotency when re-running or resuming from a checkpoint cursor.
  • Verified replica-lag throttling records backoff events whenever lag exceeds SLA.

🧠 Knowledge Check

Why should a backfill worker use primary-key cursor pagination (WHERE id > cursor LIMIT N) instead of OFFSET pagination?