{
  "license": "CC BY 4.0",
  "recipe": {
    "slug": "cache-hit-rate-and-stampede-risk",
    "title": "Find Cache Misses and Stampede Risk",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-07-28"
  },
  "contract": {
    "tableName": "cache_request_events",
    "schema": [
      {
        "name": "timestamp_utc",
        "type": "Timestamp",
        "description": "Cache lookup completion time in UTC."
      },
      {
        "name": "cache_key_pattern",
        "type": "Utf8",
        "description": "Bounded pattern such as product:{id}, never a raw key."
      },
      {
        "name": "cache_hit",
        "type": "Boolean",
        "description": "Whether the lookup returned a cached value."
      },
      {
        "name": "concurrent_misses",
        "type": "Int64",
        "description": "Observed in-flight fills for the same pattern."
      },
      {
        "name": "backend_duration_ms",
        "type": "Float64",
        "description": "Fallback duration; zero for a hit."
      },
      {
        "name": "environment",
        "type": "Utf8",
        "description": "Deployment environment."
      }
    ]
  },
  "query": "SELECT\n  cache_key_pattern,\n  COUNT(*) AS requests,\n  SUM(CASE WHEN cache_hit THEN 1 ELSE 0 END) AS hits,\n  SUM(CASE WHEN cache_hit THEN 0 ELSE 1 END) AS misses,\n  100.0 * SUM(CASE WHEN cache_hit THEN 0 ELSE 1 END)\n    / NULLIF(COUNT(*), 0) AS miss_rate_pct,\n  MAX(concurrent_misses) AS maximum_concurrent_misses,\n  AVG(CASE WHEN cache_hit THEN NULL ELSE backend_duration_ms END)\n    AS average_backend_duration_ms\nFROM cache_request_events\nWHERE timestamp_utc >= now() - INTERVAL '24 hours'\n  AND environment = 'production'\nGROUP BY cache_key_pattern\nORDER BY maximum_concurrent_misses DESC, cache_key_pattern;",
  "fixtureKind": "reproducible",
  "inputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 4,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 5,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 18,
      "backend_duration_ms": 180,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:56:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 24,
      "backend_duration_ms": 220,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:55:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 31,
      "backend_duration_ms": 250,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:54:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 28,
      "backend_duration_ms": 300,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:53:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 6,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:52:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 5,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:51:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 22,
      "backend_duration_ms": 210,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:40:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:39:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:38:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:37:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 4,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:36:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:35:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": false,
      "concurrent_misses": 7,
      "backend_duration_ms": 75,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:34:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:33:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    }
  ],
  "illustrativeInputRows": [
    {
      "timestamp_utc": "2026-07-28T18:00:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:59:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 4,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:58:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 5,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:57:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 18,
      "backend_duration_ms": 180,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:56:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 24,
      "backend_duration_ms": 220,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:55:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 31,
      "backend_duration_ms": 250,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:54:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 28,
      "backend_duration_ms": 300,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:53:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 6,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:52:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": true,
      "concurrent_misses": 5,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:51:00.000Z",
      "cache_key_pattern": "product:{id}",
      "cache_hit": false,
      "concurrent_misses": 22,
      "backend_duration_ms": 210,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:40:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:39:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:38:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:37:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 4,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:36:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:35:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": false,
      "concurrent_misses": 7,
      "backend_duration_ms": 75,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:34:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 3,
      "backend_duration_ms": 0,
      "environment": "production"
    },
    {
      "timestamp_utc": "2026-07-28T17:33:00.000Z",
      "cache_key_pattern": "feature-flags:{account}",
      "cache_hit": true,
      "concurrent_misses": 2,
      "backend_duration_ms": 0,
      "environment": "production"
    }
  ],
  "expectedOutput": {
    "columns": [
      "cache_key_pattern",
      "requests",
      "hits",
      "misses",
      "miss_rate_pct",
      "maximum_concurrent_misses",
      "average_backend_duration_ms"
    ],
    "rows": [
      {
        "cache_key_pattern": "product:{id}",
        "requests": 10,
        "hits": 5,
        "misses": 5,
        "miss_rate_pct": 50,
        "maximum_concurrent_misses": 31,
        "average_backend_duration_ms": 232
      },
      {
        "cache_key_pattern": "feature-flags:{account}",
        "requests": 8,
        "hits": 7,
        "misses": 1,
        "miss_rate_pct": 12.5,
        "maximum_concurrent_misses": 7,
        "average_backend_duration_ms": 75
      }
    ]
  },
  "notes": [
    "The included synthetic input rows generate the expected output when timestamps are shifted into the documented query window.",
    "Run this fixture in the public browser SQL playground; the selected query executes locally with DuckDB-Wasm.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}