Agent Wranglers

SCL-12Recommended

Put a format version in every cache key

Build each server cache key from a prefix, the data's format version and every input that changes the value. Add the tenant or user for private data.

How to do it

When a deploy changes the shape of a cached object, old copies still sit in the cache. The new code then reads a shape it does not expect, and a rollback reads the new one. Raise the version with each change of shape, and old keys expire on their own. Hash free text, such as a search term, so one key cannot collide with another. The same rule for data cached in the browser is Key cached data by every input.

Examples

JavaScript example

import { createHash } from 'node:crypto';

const PRODUCT_CACHE_VERSION = 3;
const hash = text => createHash('sha256').update(text).digest('hex').slice(0, 16);

export const productKey = (tenantId, productId) => `product:v${PRODUCT_CACHE_VERSION}:${tenantId}:${productId}`;
export const searchKey = (tenantId, term) => `search:v${PRODUCT_CACHE_VERSION}:${tenantId}:${hash(term)}`;

Python example

import hashlib

PRODUCT_CACHE_VERSION = 3

def _hash(text: str) -> str:
    return hashlib.sha256(text.encode()).hexdigest()[:16]

def product_key(tenant_id: str, product_id: str) -> str:
    return f"product:v{PRODUCT_CACHE_VERSION}:{tenant_id}:{product_id}"

def search_key(tenant_id: str, term: str) -> str:
    return f"search:v{PRODUCT_CACHE_VERSION}:{tenant_id}:{_hash(term)}"

How to check

Each key builder includes its version constant. A test fills the cache with entries of the old version, raises the version and reads again. It gets a miss, not a parse error.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/caching/versioned-cache-keys">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.