The Cache Key With a Twist

Easy

· Database

Incident

SEV-3

Time to fix: 10 min

Failing checks on the shipped build

  • param order does not change the key

    same params, same key: expected "/api/list?b=2&a=1" but got "/api/list?a=1&b=2"

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

CDN edges

Inspired by every CDN's cache-hit-ratio mystery: ?a=1&b=2 and ?b=2&a=1 are the same request — but naive key building treats them as two, doubling origin load and halving the hit rate.

cacheKey.js must canonicalise the params.

Code map

// Builds the cache key for a request.
exports.cacheKey = function (path, params) {
  var parts = [];
  for (var k in params) {
    parts.push(k + "=" + params[k]);
  }
  return path + "?" + parts.join("&");
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Cache Key With a Twist — JavaScript Bug Hunt

Inspired by every CDN's cache-hit-ratio mystery: ?a=1&b=2 and ?b=2&a=1 are the same request — but naive key building treats them as two, doubling origin…

  • Language: JavaScript
  • Layer: Database
  • Difficulty: Easy
  • Concepts: Caching, Canonicalization
  • Modelled on: CDN edges
  • Visible tests: param order does not change the key; different values differ
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-3
  • Time to fix: 10 minutes, the target for an easy incident

Failing checks on the shipped build

  • param order does not change the key — same params, same key: expected "/api/list?b=2&a=1" but got "/api/list?a=1&b=2"

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Inspired by every CDN's cache-hit-ratio mystery: ?a=1&b=2 and ?b=2&a=1 are the same request — but naive key building treats them as two, doubling origin load and halving the hit rate.

cacheKey.js must canonicalise the params.

Bug report

BUG-CDN-KEY · Reported by: edge performance

cacheKey(path, params):

  • identical params in ANY order produce the IDENTICAL key
  • different values produce different keys

Observed: hit ratio 46% on an endpoint that should be ~95% — clients send params in whatever order their serialiser feels like.

Logs

[edge] MISS /api/list?a=1&b=2
[edge] MISS /api/list?b=2&a=1   <- same request!

The code as shipped

src/cdn/cacheKey.js (editable)

// Builds the cache key for a request.
exports.cacheKey = function (path, params) {
  var parts = [];
  for (var k in params) {
    parts.push(k + "=" + params[k]);
  }
  return path + "?" + parts.join("&");
};

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.