Edge or in-memory cache

Updated 10:42

Should the product feed use an edge cache or an in-memory cache? Show me a typed example.

Assistant:

Short answer: cache at the edge for 60 seconds and revalidate in the background. Here is how the two options compare.

StrategyBest forTrade-off
Edge cacheMany regions, read-heavy pagesStale for up to the TTL
In-memoryOne region, low trafficCold after every deploy
BothMixed trafficTwo places to invalidate

A typed version of the loader:

TypeScript
type Feed = { items: { id: string; title: string }[]; updatedAt: number };
export async function readFeed(load: () => Promise<Feed>, ttl = 60_000) {
const cached = cache.get("feed");
if (cached && Date.now() - cached.updatedAt < ttl) return cached;
const fresh = await load();
cache.set("feed", fresh);
return fresh;
}

Before you ship it:

  • Key the cache by locale, not just by path.
  • Set a stale-while-revalidate window so readers never wait on a refresh.
  • Log the hit rate for a week, then tune the TTL.

Good. Purge the feed cache in every region so the correction goes out.

Assistant:

I need your approval before I run this. It clears the cached feed in three regions, and the next request in each one will be slow while it refills.

Input
{
"key": "feed",
"regions": [
"eu",
"us",
"apac"
],
"reason": "Editor published a correction"
}

This tool wants to run. Review the input, then allow or deny it.

AI can make mistakes. Check important info.