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

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 edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.