Agent Wranglers

SCL-35Recommended

Decide how stale a cache may be

Give every cached value an expiry or an invalidation rule, chosen for how stale it may safely be.

How to do it

Prefer short expiry times for data that changes, and delete the cache entry right after the write commits, in the same code path. Keep the expiry even then, so a missed delete heals itself. Never cache permissions, prices at checkout or anything that must be exact at the moment of use. Include the user or tenant in the cache key for private data. For HTTP responses, Cache files for a year and pages briefly sets the times.

Examples

JavaScript example

await db.none('UPDATE products SET price_cents = $1 WHERE id = $2', [price, id]);
await cache.del(`product:${id}`);

Python example

cursor.execute("UPDATE products SET price_cents = %s WHERE id = %s", (price, product_id))
cache.delete(f"product:{product_id}")

How to check

Change a cached value through the app. The next read shows the new value, or the stale one only within its stated expiry.

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/cache-freshness">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.