How to Build a MyBatis Plugin that Shields Databases from Sudden Traffic Spikes
This article explains the challenges of sudden traffic bursts on applications and databases, outlines a MyBatis plugin design that intercepts SQL, uses fingerprint‑based throttling with configurable policies, and details the development, optimization, testing, and documentation steps performed with pair‑programming assistance.
Background
In daily operations, occasional sudden traffic spikes generate short‑lived but extremely high load peaks that stress both applications and databases, causing CPU spikes, increased response times, higher TP99, elevated QPS, more slow SQL, and more active connections.
Problem Statement
Even with rate limiting, traffic shedding, scaling, or isolation, a large amount of internal traffic can still reach the database, rendering traditional protection methods such as KILL, master‑slave switchover, or restart ineffective when high‑frequency SQL continues to arrive.
Design Goal
The goal is to create a protection mechanism at the application‑database interaction layer. Using pair‑programming with Joycode, a MyBatis plugin is designed to intercept SQL, extract a fingerprint, look up a configurable threshold, and apply one of three handling policies when the threshold is exceeded.
Detailed Design Requirements
我要写一款MyBatis插件,有以下几方面诉求,请先规划设计,输出设计文档,图文结合,越详细越好:Intercept SQL and parse its fingerprint.
Define an SPI interface that, given a fingerprint, returns the configured threshold; the default implementation loads thresholds from a Spring configuration file, while other implementations can be provided via ServiceLoader.
If a fingerprint is found in the configuration and the accumulated count within a time window exceeds the threshold, trigger a handling strategy. The count is kept in memory.
Three handling strategies:
AbortPolicy : throw a RuntimeException to abort execution.
IgnorePolicy : silently discard the request without throwing.
DelayPolicy : sleep for a configurable period to throttle the load.
The strategy should respect a rate setting (e.g., 2/3) so that only the specified proportion of requests follows the chosen policy.
Coding Implementation
Based on the design document, the core class
com.jd.sword.mybatis.plugin.protector.SQLBasedDatabaseProtectorand related components are implemented. Maven coordinates were initially missing, causing compilation errors, which were manually added.
Optimization and Review
After the initial implementation, a code review identified several improvement areas:
Align code style with existing plugins (e.g., com.jd.sword.mybatis.plugin.sql.SQLMarkingInterceptor).
Standardize comment annotations, ensuring @date follows yyyy-MM-dd HH:mm:ss format.
Simplify complex logic in SlidingWindowCounter to reduce memory usage.
Remove redundant class SQLBasedDatabaseProtector if it provides no unique functionality.
Refactor package structure: split classes into sub‑packages for strategies, exceptions, and counters while keeping the main plugin entry point unchanged.
Analyze and possibly eliminate unused class OptimizedSlidingWindowCounter.
Testing and Documentation
Unit tests are written for SQLBasedDatabaseProtectorPlugin. The design and README documents are updated to reflect the latest implementation, including corrected Maven coordinates ( artifactId should be sword-mybatis-plugins) and version numbers.
请为com.jd.sword.mybatis.plugin.protector.SQLBasedDatabaseProtectorPlugin 写单测The updated documentation now includes design diagrams, usage instructions, threshold configuration examples, and deployment guidance.
JD Tech Talk
Official JD Tech public account delivering best practices and technology innovation.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
