惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
云风的 BLOG
云风的 BLOG
小众软件
小众软件
雷峰网
雷峰网
博客园 - 【当耐特】
V
V2EX
WordPress大学
WordPress大学
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
The Cloudflare Blog
Jina AI
Jina AI
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I Fixed a 5s Database Bottleneck with CDC Dual-Writes
quarktimes · 2026-06-15 · via DEV Community

quarktimes

I Fixed a 5s Database Bottleneck with CDC Dual-Writes

We recently hit a critical bottleneck. While running a schema change on a billion-row order table during peak traffic, our P99 latency spiked to 5 seconds, triggering circuit breakers.

The culprit? MySQL's Online DDL. Even with the INPLACE algorithm, it briefly locks the table metadata to update dictionary files, blocking all incoming writes.

Here is how we solved this using a CDC (Change Data Capture) dual-write strategy and atomic table swapping, bringing P99 latency down to 200ms and achieving zero-downtime schema migrations.

The Architecture

The core idea is simple: instead of locking the live table, we create a shadow table and sync data asynchronously.

graph TD
    A[Client Request] --> B[Old Table]
    B --> C[Return Data]
    D[CDC Binlog Sync] --> E[New Table]
    F[Atomic Swap RENAME] -->|Swap Pointer| B
    F -->|Swap Pointer| E
    G[Validator Checksum] -->|Pass| F
    D -.->|Sync Data| E

The Root Cause

We discovered that the issue wasn't just the DDL itself, but how it interacted with MDL (Metadata Locks).

  1. Phenomenon: Business requests couldn't acquire MDL read locks and were blocked, draining the connection pool.
  2. Mechanism: Even INPLACE DDL requires an exclusive lock momentarily at the start and end to update FRM files.
  3. Solution: CDC dual-write moves the lock conflict from "Request vs DDL" to "Async Task vs DDL".

Solution 1: Fixing DDL Safety Checks

Our initial safety logic was flawed. It incorrectly flagged ALGORITHM=INPLACE as unsafe. We corrected this to explicitly allow INPLACE and INSTANT algorithms while banning explicit locks.

# Before (Error Logic)
def is_safe_ddl(sql):
    if 'ALGORITHM=INPLACE' in sql:
        return False  # Logic error: INPLACE is standard for Online DDL
    return True

# After (Fixed Logic)
def is_safe_ddl(sql):
    # Allow INPLACE, but forbid explicit locking syntax
    if 'LOCK=SHARED' in sql or 'LOCK=EXCLUSIVE' in sql:
        return False
    # Allow ALGORITHM=INPLACE or INSTANT
    return True

Solution 2: Atomic Table Swapping

We leveraged the atomic nature of MySQL's RENAME TABLE to switch traffic instantly. This operation only requires a brief exclusive lock, which is negligible compared to the original 5-second block.

-- Atomic swap operation
RENAME TABLE
    orders TO orders_old,
    orders_shadow TO orders;
-- Clean up the old table after swap
DROP TABLE orders_old;

Architecture Decisions

We evaluated a few alternatives before settling on this approach:

Decision Alternative Rationale
CDC Dual-Writes gh-ost gh-ost relies on simulating a replica and adds trigger overhead. Our existing CDC pipeline is more reusable and controllable.
Atomic RENAME App-layer Dual-Write App-layer logic is complex and prone to data inconsistency. DB-level atomicity is guaranteed by the engine and offers better P99 latency.

Production Takeaways

After rolling this out:

  1. Performance: P99 latency dropped from 5s to 200ms.
  2. Efficiency: Full data sync took 45 minutes; consistency validation took only 3 minutes.
  3. Validation: Relying solely on Binlog sync isn't enough. You must use checksum to perform a full differential comparison between the old and new tables to ensure zero data loss.

Understanding locking behavior is critical. By using shadow tables, we decoupled the lock conflict into the async link, keeping the main business completely unaffected.


Originally posted on my tech blog.