A cache that would have worked perfectly and never once hit
- Symptom
None, and that is the point. Every test passed. The cache wrote entries, reported healthy, and would have returned a hit rate of zero forever in production.
- Diagnosis
Cached answers were being encrypted with the per-questionnaire data key, because that is the key every other record in that flow uses and reaching for it was the consistent-looking choice. But the cache is a global corpus whose whole purpose is to be read by a different questionnaire later, and a different questionnaire has a different key. Every entry would have been undecryptable by every reader except the one that wrote it. Within a single questionnaire, which is what the tests exercised, it works flawlessly.
- Decision
This was caught in code review, not by me, and the fix was a decision rather than a patch: store the shared corpus unencrypted, matching the imported answers it derives from, and record in the code why. The repository still supports envelope encryption, and the note says plainly that if these entries ever need it they need a single shared key, never the per-questionnaire one. The wrong choice here is invisible in every test that does not span two questionnaires, so the comment is the control.
questionnaire A populate key=dek(questionnaire A) ok
questionnaire B lookup tier 1 hash match → 1 row
decrypt key=dek(questionnaire B)
error decrypt failed: wrong key
result MISS ← for every reader but the writer
hit rate (cross-questionnaire) 0%questionnaire A populate encrypted=false ok
questionnaire B lookup tier 1 hash match → 1 row
result HIT cost $0The bug I find most instructive of the four. It has no symptom, it passes review by consistency, since everything else in that flow is encrypted that way, and the tests that would catch it are exactly the ones nobody writes, because they span two runs.