Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:APIJSON APIJSON Demo Cache Stampede Prevention On Delete

From Leeroopedia
Knowledge Sources
Domains Caching, Performance, Database
Last Updated 2026-10-14 00:00 GMT

Overview

Use delayed Redis cache expiry (60s) on DELETE operations instead of immediate deletion to prevent cache stampede from concurrent re-query bursts.

Description

The DemoSQLExecutor.removeCache() method handles cache invalidation differently based on the HTTP method. For DELETE requests, it uses REDIS_TEMPLATE.expire(sql, 60, TimeUnit.SECONDS) — setting a 60-second TTL instead of immediately deleting the key. This means stale data is served for up to 60 seconds while the delete propagates, but it prevents a thundering herd of database queries when multiple clients simultaneously request the same data after a delete. For POST and PUT operations, the cache is deleted immediately since the data has changed definitively.

Usage

Apply this heuristic when cache invalidation on DELETE could trigger cache stampede — typically when deleted records are frequently queried by multiple concurrent clients. Use immediate deletion for POST/PUT where data changes are clear-cut.

The Insight (Rule of Thumb)

  • Action: On DELETE, use expire(sql, 60, TimeUnit.SECONDS) instead of delete(sql).
  • Value: 60-second delayed expiry window.
  • Trade-off: Serves stale data for up to 60 seconds after a delete, in exchange for preventing a cache stampede that could overload the database.
  • Compatibility: Only applies to DELETE; POST/PUT still use immediate deletion.

Reasoning

When a record is deleted, the cached query result becomes stale. If the cache key is deleted immediately, the next request triggers a database query. If multiple clients query simultaneously (common in high-traffic scenarios), they all miss the cache and hit the database concurrently — a cache stampede. By setting a short TTL instead, the first miss triggers a database query, repopulates the cache, and subsequent requests within the 60-second window still hit the (stale) cache. This is especially important for DELETE because the deletion itself may involve cascading database operations that make the database temporarily slow.

Code Evidence

Delayed expiry on DELETE from `DemoSQLExecutor.java:110-122`:

@Override
public synchronized void removeCache(String sql, SQLConfig<Long, JSONObject, JSONArray> config) {
    super.removeCache(sql, config);
    try {
        if (config.getMethod() == RequestMethod.DELETE) { // 避免缓存击穿
            REDIS_TEMPLATE.expire(sql, 60, TimeUnit.SECONDS);
        } else {
            REDIS_TEMPLATE.delete(sql);
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

The Chinese comment 避免缓存击穿 literally means "avoid cache stampede/breakdown".

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment