agentleFS
openoctaopenocta/openocta_skills/mysql-replication-lag-resolver/SKILL.md

mysql-replication-lag-resolver

Skill166 starsChanged 5 months agoMITView on GitHub

Replication lag root cause analysis with automated resolution workflow. Detects lag, identifies blocking queries on the master, presents KILL candidates with safety classification, and executes KILL only after double user confirmation. Reduces recovery from 20 minutes to 2 minutes.

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    After bulk operations (data migration, batch updates) that may cause lag

    line 34
  2. Distinctive:

    After killing, check lag every 10-30 seconds:

    line 169

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

MySQL Replication Lag Resolver (Auto-Diagnose + Fix)

Overview

Replication lag is one of the most critical database incidents — it means read replicas are falling behind the master, serving stale data to applications. Traditional resolution requires DBA to: receive alert → open laptop → connect VPN → login to jumpbox → check slave status → find blocking queries on master → decide what to kill → execute KILL → monitor recovery. 20+ minutes at 2 AM.

This skill automates the entire diagnosis and presents a one-click resolution:

  1. Detect — Check Seconds_Behind_Master and replication health
  2. Root Cause — Find long-running queries on master blocking binlog sync
  3. Classify — Mark each query as safe/caution/dangerous to kill
  4. Present — Show candidates with impact analysis
  5. Resolve — Execute KILL with double user confirmation
  6. Verify — Monitor lag recovery in real-time

Real-world result: From 20 minutes to 2 minutes — 10x improvement. DBA only needs to say "confirm" on their phone.

When to Use

Apply this skill when:

  • Replication lag alert fires (Seconds_Behind_Master > threshold)
  • Application reports stale reads from replicas
  • Scheduled monitoring detects lag trend
  • After bulk operations (data migration, batch updates) that may cause lag
  • Proactive check during peak hours

Required MCP Server

ToolPurposeExample
db_queryCheck replication statusdb_query("SHOW SLAVE STATUS")
db_get_statusGet replication countersdb_get_status("Seconds_Behind_Master")
db_get_processlistFind blocking queriesdb_get_processlist()
db_kill_queryTerminate blocking queriesdb_kill_query(thread_id=123)

Required MySQL Privileges

  • REPLICATION CLIENT — View replication status
  • PROCESS — View all threads on master
  • SUPER or CONNECTION_ADMIN — KILL queries (on master only)

⚠️ CRITICAL: Safety Rules

NEVER Kill

  • Replication threads (system user on slaves)
  • DDL operations (ALTER, CREATE, DROP) — killing DDL on master can corrupt data dictionary
  • The diagnostic session itself
  • Any thread running < 60 seconds (may be legitimate)

KILL with Caution

TypeRiskGuidance
UPDATE/DELETEPartial rollback, breaks idempotent operationsKill only if > 300s AND lag is critical
INSERT...SELECTMay leave partial dataVerify target table before killing
Large SELECTSafe but wastes workPrefer killing SELECTs first

Mandatory Approval Protocol

This skill enforces double-confirmation for ALL KILL operations:

  1. Phase 1: Present findings + candidates
  2. Phase 2: User selects targets ("KILL 123, 124")
  3. Phase 3: AI reads back + user types "CONFIRM KILL"
  4. Phase 4: Execute KILL
  5. Phase 5: Monitor recovery

Diagnostic Workflow

Step 1: Assess Replication Health

-- Master status
SHOW MASTER STATUS;

-- Slave status (run on replica)
SHOW SLAVE STATUS\G

-- Key fields from SHOW SLAVE STATUS:
--   Seconds_Behind_Master: {lag_seconds}
--   Slave_IO_Running: Yes/No
--   Slave_SQL_Running: Yes/No
--   Last_Error: {error_message}
--   Relay_Master_Log_File: {master_log}
--   Exec_Master_Log_Pos: {executed_pos}
--   Read_Master_Log_Pos: {read_pos}

Classify lag severity:

Seconds_Behind_MasterSeverityAction
0-30✅ NormalNo action needed
30-120🟡 WarningMonitor, check trend
120-600🔴 HighDiagnose root cause
> 600🔴 CriticalImmediate intervention

Step 2: Identify Root Cause on Master

-- Find long-running queries on master that may block binlog sync
SELECT
  ID, USER, HOST, DB, COMMAND,
  TIME AS duration_sec,
  STATE,
  LEFT(INFO, 300) AS sql_fragment
FROM information_schema.PROCESSLIST
WHERE COMMAND != 'Sleep'
  AND TIME > 60
  AND USER != 'system user'
ORDER BY TIME DESC;

-- Check master binlog position vs what slaves have read
SHOW MASTER STATUS;
-- Compare with Exec_Master_Log_Pos from each slave

Root cause patterns:

PatternCauseFix
Long SELECT on masterBinlog events queued behind the SELECTKill the SELECT
Large UPDATE/DELETESingle big transaction blocks binlogKill if > 300s
Binlog Dump idleNetwork issue between master/slaveCheck network, not kill
Slave_SQL stoppedDuplicate key / constraint errorFix error, START SLAVE

Step 3: Classify Candidates

For each long-running query on master, classify:

IF SQL starts with ALTER/CREATE/DROP/TRUNCATE/RENAME:
  → Type: DDL, Kill: NOT RECOMMENDED (explain risk to user)
ELSE IF SQL starts with SELECT:
  → Type: SELECT, Kill: SAFE (kill first)
ELSE IF SQL starts with UPDATE/DELETE and duration > 300:
  → Type: DML, Kill: CAUTION (present risk)
ELSE IF Command = 'Binlog Dump':
  → Type: REPLICATION, Kill: NEVER
ELSE:
  → Type: OTHER, Kill: REVIEW

Step 4: Present to User

Show candidates using the template below. Wait for user response.

Step 5: Execute KILL (After Confirmation)

-- Kill confirmed threads
KILL {thread_id};

-- If KILL fails (thread already gone), report it
-- Alternative: KILL QUERY {thread_id} (kills query, keeps connection)

Step 6: Monitor Recovery

After killing, check lag every 10-30 seconds:

-- Recheck slave status
SHOW SLAVE STATUS\G

-- Expected recovery pattern:
-- Seconds_Behind_Master: 623 → 412 → 89 → 0

Report recovery timeline to user.

Output Templates

Initial Diagnosis

🔄 Replication Lag Alert
═══════════════════════
📊 Current Status
  Master: {host}:{port} (Binlog: {file}, Pos: {pos})
  Slave:  {host}:{port}
  Lag: {seconds}s 🔴 CRITICAL
  IO Thread: {io_status}
  SQL Thread: {sql_status}

🔍 Root Cause Analysis
  Cause: {count} long-running queries on master blocking binlog sync
  Impact: Slave is {seconds}s behind, serving stale data to {app_count} applications

📋 Blocking Queries (candidates for KILL)
  | Thread | User | Duration | Type | SQL | Kill Safety |
  |--------|------|----------|------|-----|-------------|
  | 23451  | app1 | 892s     | SELECT | SELECT ... FROM order_detail | ✅ Safe |
  | 23487  | app2 | 567s     | SELECT | SELECT ... FROM user_session | ✅ Safe |
  | 23502  | app3 | 423s     | SELECT | SELECT ... FROM payment_record | ✅ Safe |

  ℹ️ Skipped: 2 Binlog Dump threads (replication, NEVER kill)

⚠️ Action Required (Step 1 of 2):
  Reply "KILL {thread_ids}" to select targets,
  "KILL ALL" for all candidates,
  or "SKIP" to cancel.
  ⚠️ A final confirmation will be required before execution.

After User Selects (Double Confirmation)

⚠️ DOUBLE CONFIRMATION REQUIRED (Step 2 of 2)
══════════════════════════════════════════════
You are about to KILL {count} queries on MASTER:

  | Thread | User | Duration | Type | SQL |
  |--------|------|----------|------|-----|
  | 23451  | app1 | 892s     | SELECT | ... |
  | 23487  | app2 | 567s     | SELECT | ... |
  | 23502  | app3 | 423s     | SELECT | ... |

  ❗️ Please type EXACTLY "CONFIRM KILL" to proceed.
     Any other response cancels this operation.

Recovery Report

✅ KILL Execution Complete
═════════════════════════
  Thread 23451 (892s, SELECT): ✅ Killed
  Thread 23487 (567s, SELECT): ✅ Killed
  Thread 23502 (423s, SELECT): ✅ Killed

📈 Recovery Timeline
  0s:   Lag = 623s (KILL executed)
  30s:  Lag = 412s ↓
  60s:  Lag = 89s ↓
  90s:  Lag = 0s ✅ Replication normalized

💡 Root Cause Summary:
  3 concurrent full-table-scan queries on master blocked binlog sync.
  Recommended: Add indexes on order_detail(status, create_time) and
  user_session(last_active) to prevent recurrence.

Example Usage

User: "Replication lag alert on db-slave-01"

AI (Phase 1):

🔄 Replication Lag Alert
═══════════════════════
  Slave: db-slave-01:3306
  Lag: 623s 🔴 CRITICAL
  Root Cause: 3 long-running SELECT queries on master blocking binlog sync

  KILL candidates:
    23451 · app1 · 892s · SELECT * FROM order_detail WHERE ...
    23487 · app2 · 567s · SELECT * FROM user_session WHERE ...
    23502 · app3 · 423s · SELECT * FROM payment_record WHERE ...

  Reply "KILL ALL" or specify thread IDs.

User: "KILL ALL"

AI (Phase 3 — Double check):

⚠️ CONFIRM: Kill all 3 queries on master?
Type "CONFIRM KILL" to proceed.

User: "CONFIRM KILL"

AI (Phase 4+5):

✅ 3 queries killed. Lag recovering: 623 → 412 → 89 → 0. Normalized.

Best Practices

Preventing Replication Lag

PreventionHow
Set long_query_time lowSET GLOBAL long_query_time = 1
Monitor lag proactivelyCheck every 30s, alert at > 60s
Use row-based replicationRBR is more efficient for most workloads
Batch operationsSplit large UPDATEs into 1000-row batches
Parallel replicationSET GLOBAL slave_parallel_workers = 4 (MySQL 5.7+)

After Resolving Lag

  1. Check data consistency — Compare row counts on master vs slave for critical tables
  2. Root cause remediation — Add missing indexes to prevent recurrence
  3. Update monitoring — Lower alert threshold if this was a recurring issue
  4. Notify stakeholders — Applications may have cached stale data

Quick Reference

ActionCommand
Check slave statusSHOW SLAVE STATUS\G
Check master statusSHOW MASTER STATUS
Start slaveSTART SLAVE
Stop slaveSTOP SLAVE
Skip one replication errorSET GLOBAL sql_slave_skip_counter = 1; START SLAVE
Show long queriesSELECT * FROM information_schema.PROCESSLIST WHERE TIME > 60
Kill a threadKILL {thread_id}
Kill query onlyKILL QUERY {thread_id}

Use this skill when replication lag fires — diagnose, present options, resolve with one confirmation.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.