Caches and copies Required
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.
Example
await db.none('UPDATE products SET price_cents = $1 WHERE id = $2', [price, id]);
await cache.del(`product:${id}`);
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: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/caches-and-copies/cache-freshness.