[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-trigger-dev-staff-engineering-skills-thundering-herd":3,"mdc-6bo2rn-key":34,"related-org-trigger-dev-staff-engineering-skills-thundering-herd":3900,"related-repo-trigger-dev-staff-engineering-skills-thundering-herd":4065},{"slug":4,"name":4,"fn":5,"description":6,"org":7,"tags":12,"stars":23,"repoUrl":24,"updatedAt":25,"license":26,"forks":27,"topics":28,"repo":29,"sourceUrl":32,"mdContent":33},"staff-engineering-skills-thundering-herd","prevent thundering herd demand spikes","Prevent synchronized demand spikes that overwhelm backends. Use when implementing caching with TTL expiry, scheduling cron jobs, handling reconnection after failures, warming caches on startup, or invalidating cache entries. Activates on patterns like check-cache-miss-fetch-store without locking, fixed TTLs on popular keys, cron expressions at round times like :00 or :30, cache.clear() or cache.delete() on hot keys, or reconnection logic without jitter.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},"trigger-dev","Trigger.dev","https:\u002F\u002Fpexgzepcugksgbtrxkhf.supabase.co\u002Fstorage\u002Fv1\u002Fobject\u002Fpublic\u002Forg-logos\u002Ftrigger-dev.jpg","triggerdotdev",[13,17,20],{"name":14,"slug":15,"type":16},"Performance","performance","tag",{"name":18,"slug":19,"type":16},"Architecture","architecture",{"name":21,"slug":22,"type":16},"Caching","caching",3,"https:\u002F\u002Fgithub.com\u002Ftriggerdotdev\u002Fstaff-engineering-skills","2026-06-17T08:40:52.551886",null,1,[],{"repoUrl":24,"stars":23,"forks":27,"topics":30,"description":31},[],"Skills that give AI   coding agents staff-engineer instincts: recognizing and avoiding production failure modes like cardinality, idempotency, and race conditions.","https:\u002F\u002Fgithub.com\u002Ftriggerdotdev\u002Fstaff-engineering-skills\u002Ftree\u002FHEAD\u002Fskills\u002Fstaff-engineering-skills-thundering-herd","---\nname: staff-engineering-skills-thundering-herd\ndescription: Prevent synchronized demand spikes that overwhelm backends. Use when implementing caching with TTL expiry, scheduling cron jobs, handling reconnection after failures, warming caches on startup, or invalidating cache entries. Activates on patterns like check-cache-miss-fetch-store without locking, fixed TTLs on popular keys, cron expressions at round times like :00 or :30, cache.clear() or cache.delete() on hot keys, or reconnection logic without jitter.\n---\n\n# Thundering Herd Trap\n\nThe cache expired. Ten thousand requests hit the database at once. Before writing any cache-miss handler, cron schedule, or reconnection logic, ask: **what happens when every client does this at the same time?**\n\n## The Four Triggers\n\n| Trigger | What happens | Example |\n|---------|-------------|---------|\n| **Cache expiry** | Popular key expires, all concurrent requests miss and fetch simultaneously | Homepage feed cached for 5 min; on expiry, 10,000 requests hit the DB at once |\n| **Service recovery** | Service comes back up, all queued retries hit it simultaneously | Downstream API recovers from outage, backlogged clients all retry at once |\n| **Synchronized scheduling** | Every instance fires at the same second | Cron at `0 * * * *` on 50 instances = 50 identical queries at :00 |\n| **Connection storm** | All clients reconnect simultaneously after a failure | Database failover: 500 app servers all connect to the new primary at once |\n\n## Detection: When You're Creating a Thundering Herd\n\n**Stop and fix if you see:**\n\n1. **\"Check cache, miss, fetch, store\" with no coalescing** -- the textbook pattern. When 1,000 requests hit the miss simultaneously, 1,000 identical queries hit the database. Only one fetch is needed; the other 999 should wait for its result.\n\n2. **A fixed TTL on a popular cache key** -- `cache.set(key, data, { ttl: 300 })`. All keys set at similar times expire at similar times. Especially dangerous after a deployment or cache restart that repopulates everything at once.\n\n3. **Cron at round times** -- `0 * * * *`, `*\u002F30 * * * *`, `0 0 * * *`. Every instance fires at the same second. Every other system in the world also chose these times. Add jitter.\n\n4. **`cache.delete(key)` or `cache.clear()` on a hot path** -- invalidating a popular key causes an immediate stampede. Use write-through (set the new value) instead of delete, or use stale-while-revalidate.\n\n5. **Reconnection logic without jitter** -- `on(\"error\", () => connect())`. Every client detects the failure at the same time and reconnects at the same time. The new server gets overwhelmed before it can serve anyone.\n\n6. **Cache warming on startup with no stagger** -- 50 pods restart during a deploy, each makes 50 warmup queries. That's 2,500 queries in seconds on top of normal load.\n\n## Patterns\n\n### Request coalescing (single-flight)\n\nOnly one request fetches on a miss. Everyone else waits for that result.\n\n```typescript\nconst inflight = new Map\u003Cstring, Promise\u003Cany>>();\n\nasync function getWithCoalescing\u003CT>(key: string, fetcher: () => Promise\u003CT>, ttlSeconds: number): Promise\u003CT> {\n  const cached = await cache.get(key);\n  if (cached) return JSON.parse(cached);\n\n  \u002F\u002F If someone is already fetching this key, wait for their result\n  if (inflight.has(key)) return inflight.get(key) as Promise\u003CT>;\n\n  const promise = fetcher()\n    .then(async (data) => {\n      await cache.set(key, JSON.stringify(data), { EX: ttlSeconds + jitter(ttlSeconds) });\n      inflight.delete(key);\n      return data;\n    })\n    .catch((err) => {\n      inflight.delete(key);\n      throw err;\n    });\n\n  inflight.set(key, promise);\n  return promise;\n}\n\nfunction jitter(baseTtl: number): number {\n  return Math.floor(Math.random() * baseTtl * 0.2); \u002F\u002F 0-20% jitter\n}\n```\n\nThis is in-process only -- works for single-instance services. For multi-instance, use a distributed lock (Redis `SET NX PX`) so only one instance across the fleet fetches.\n\n### Stale-while-revalidate\n\nServe stale data immediately, refresh in the background. The cache never has a true miss.\n\n```typescript\nconst refreshing = new Set\u003Cstring>();\n\nasync function getWithSWR\u003CT>(key: string, fetcher: () => Promise\u003CT>, ttlSeconds: number): Promise\u003CT> {\n  const entry = await cache.getWithTTL(key); \u002F\u002F Returns { data, remainingTtl }\n\n  if (entry && entry.remainingTtl > 0) {\n    return entry.data; \u002F\u002F Fresh\n  }\n\n  if (entry) {\n    \u002F\u002F Stale but exists -- serve it, refresh in background\n    if (!refreshing.has(key)) {\n      refreshing.add(key);\n      fetcher()\n        .then((data) => cache.set(key, data, { EX: ttlSeconds + jitter(ttlSeconds) }))\n        .finally(() => refreshing.delete(key));\n    }\n    return entry.data; \u002F\u002F Return stale immediately\n  }\n\n  \u002F\u002F No data at all -- must fetch synchronously (with coalescing)\n  return fetchAndCache(key, fetcher, ttlSeconds);\n}\n```\n\nThe user gets a response in milliseconds (from stale cache) while the fresh data is fetched in the background. No stampede because there's never a moment where the cache is empty.\n\n**Tradeoff:** Users may briefly see stale data. Fine for most content. Not acceptable for financial data or security-critical reads.\n\n### Jittered TTLs\n\nPrevent mass expiry by spreading expirations across a time window.\n\n```typescript\nfunction setWithJitter(key: string, data: any, baseTtlSeconds: number) {\n  \u002F\u002F Add 0-20% random jitter\n  const ttl = baseTtlSeconds + Math.floor(Math.random() * baseTtlSeconds * 0.2);\n  return cache.set(key, JSON.stringify(data), { EX: ttl });\n}\n```\n\nThis doesn't prevent a stampede on a single hot key (all requests still miss at the same moment). But it prevents mass expiry -- 10,000 keys populated at the same time won't all expire in the same second.\n\n### Jittered cron scheduling\n\n```typescript\n\u002F\u002F BAD: all instances fire at exactly :00\ncron.schedule(\"0 * * * *\", generateReport);\n\n\u002F\u002F GOOD: each instance starts at a random offset within the first minute\nconst jitterMs = Math.floor(Math.random() * 60_000);\nsetTimeout(() => {\n  cron.schedule(\"0 * * * *\", generateReport);\n}, jitterMs);\n```\n\n### Jittered reconnection\n\n```typescript\n\u002F\u002F BAD: reconnect immediately on failure\ndb.on(\"error\", () => db.connect());\n\n\u002F\u002F GOOD: exponential backoff with jitter\ndb.on(\"error\", () => {\n  const baseDelay = 1000;\n  const attempt = reconnectAttempts++;\n  const delay = baseDelay * 2 ** Math.min(attempt, 6) + Math.random() * 1000;\n  setTimeout(() => db.connect(), delay);\n});\n```\n\nWithout jitter, all clients reconnect at the same time after the same backoff duration. Jitter spreads them across a window so the server isn't overwhelmed.\n\n### Write-through instead of delete on invalidation\n\n```typescript\n\u002F\u002F BAD: delete causes a stampede on the next read\nasync function updateConfig(newConfig: Config) {\n  await db.save(newConfig);\n  await cache.delete(\"config\"); \u002F\u002F Every next request stampedes\n}\n\n\u002F\u002F GOOD: write the new value directly -- no miss, no stampede\nasync function updateConfig(newConfig: Config) {\n  await db.save(newConfig);\n  await cache.set(\"config\", JSON.stringify(newConfig), { EX: 300 });\n  \u002F\u002F Cache goes from old value → new value with no gap\n}\n```\n\n## Anti-Patterns\n\n```typescript\n\u002F\u002F Naive cache-aside: 1,000 concurrent misses = 1,000 identical DB queries\nconst cached = await cache.get(key);\nif (!cached) { data = await db.query(key); await cache.set(key, data, { ttl: 300 }); }\n\n\u002F\u002F Fixed TTL, no jitter: all keys populated at deploy expire at the same time\ncache.set(key, data, { ttl: 300 }); \u002F\u002F Every key gets exactly 300s\n\n\u002F\u002F Cron at round time: 50 instances all fire at :00\ncron.schedule(\"0 * * * *\", expensiveJob);\n\n\u002F\u002F Cache clear in hot path: nuclear stampede\nawait cache.clear(); \u002F\u002F Everything stampedes at once\n\n\u002F\u002F Reconnect without backoff: connection storm\ndb.on(\"error\", () => db.connect());\n```\n\n## Related Traps\n\n- **Cache Invalidation** -- cache stampede is the thundering herd problem applied to caching. Stale-while-revalidate and write-through invalidation prevent the gap where the cache is empty.\n- **Retry Storms** -- retries are a specific form of thundering herd. When a service is slow and clients retry, the retries themselves create a herd on the struggling service. Exponential backoff with jitter is the same fix in both contexts.\n- **Hot Partitions** -- a thundering herd on a partitioned system overwhelms whichever partition holds the hot key. The two traps compound.\n- **Backpressure** -- a cache stampede is a sudden loss of backpressure. The cache was absorbing load; when it expires, the full load hits the backend with no buffering.\n",{"data":35,"body":36},{"name":4,"description":6},{"type":37,"children":38},"root",[39,48,60,67,193,199,207,322,328,335,340,1372,1385,1391,1396,2119,2124,2134,2140,2145,2405,2410,2416,2646,2652,3018,3023,3029,3379,3385,3844,3850,3894],{"type":40,"tag":41,"props":42,"children":44},"element","h1",{"id":43},"thundering-herd-trap",[45],{"type":46,"value":47},"text","Thundering Herd Trap",{"type":40,"tag":49,"props":50,"children":51},"p",{},[52,54],{"type":46,"value":53},"The cache expired. Ten thousand requests hit the database at once. Before writing any cache-miss handler, cron schedule, or reconnection logic, ask: ",{"type":40,"tag":55,"props":56,"children":57},"strong",{},[58],{"type":46,"value":59},"what happens when every client does this at the same time?",{"type":40,"tag":61,"props":62,"children":64},"h2",{"id":63},"the-four-triggers",[65],{"type":46,"value":66},"The Four Triggers",{"type":40,"tag":68,"props":69,"children":70},"table",{},[71,95],{"type":40,"tag":72,"props":73,"children":74},"thead",{},[75],{"type":40,"tag":76,"props":77,"children":78},"tr",{},[79,85,90],{"type":40,"tag":80,"props":81,"children":82},"th",{},[83],{"type":46,"value":84},"Trigger",{"type":40,"tag":80,"props":86,"children":87},{},[88],{"type":46,"value":89},"What happens",{"type":40,"tag":80,"props":91,"children":92},{},[93],{"type":46,"value":94},"Example",{"type":40,"tag":96,"props":97,"children":98},"tbody",{},[99,121,142,172],{"type":40,"tag":76,"props":100,"children":101},{},[102,111,116],{"type":40,"tag":103,"props":104,"children":105},"td",{},[106],{"type":40,"tag":55,"props":107,"children":108},{},[109],{"type":46,"value":110},"Cache expiry",{"type":40,"tag":103,"props":112,"children":113},{},[114],{"type":46,"value":115},"Popular key expires, all concurrent requests miss and fetch simultaneously",{"type":40,"tag":103,"props":117,"children":118},{},[119],{"type":46,"value":120},"Homepage feed cached for 5 min; on expiry, 10,000 requests hit the DB at once",{"type":40,"tag":76,"props":122,"children":123},{},[124,132,137],{"type":40,"tag":103,"props":125,"children":126},{},[127],{"type":40,"tag":55,"props":128,"children":129},{},[130],{"type":46,"value":131},"Service recovery",{"type":40,"tag":103,"props":133,"children":134},{},[135],{"type":46,"value":136},"Service comes back up, all queued retries hit it simultaneously",{"type":40,"tag":103,"props":138,"children":139},{},[140],{"type":46,"value":141},"Downstream API recovers from outage, backlogged clients all retry at once",{"type":40,"tag":76,"props":143,"children":144},{},[145,153,158],{"type":40,"tag":103,"props":146,"children":147},{},[148],{"type":40,"tag":55,"props":149,"children":150},{},[151],{"type":46,"value":152},"Synchronized scheduling",{"type":40,"tag":103,"props":154,"children":155},{},[156],{"type":46,"value":157},"Every instance fires at the same second",{"type":40,"tag":103,"props":159,"children":160},{},[161,163,170],{"type":46,"value":162},"Cron at ",{"type":40,"tag":164,"props":165,"children":167},"code",{"className":166},[],[168],{"type":46,"value":169},"0 * * * *",{"type":46,"value":171}," on 50 instances = 50 identical queries at :00",{"type":40,"tag":76,"props":173,"children":174},{},[175,183,188],{"type":40,"tag":103,"props":176,"children":177},{},[178],{"type":40,"tag":55,"props":179,"children":180},{},[181],{"type":46,"value":182},"Connection storm",{"type":40,"tag":103,"props":184,"children":185},{},[186],{"type":46,"value":187},"All clients reconnect simultaneously after a failure",{"type":40,"tag":103,"props":189,"children":190},{},[191],{"type":46,"value":192},"Database failover: 500 app servers all connect to the new primary at once",{"type":40,"tag":61,"props":194,"children":196},{"id":195},"detection-when-youre-creating-a-thundering-herd",[197],{"type":46,"value":198},"Detection: When You're Creating a Thundering Herd",{"type":40,"tag":49,"props":200,"children":201},{},[202],{"type":40,"tag":55,"props":203,"children":204},{},[205],{"type":46,"value":206},"Stop and fix if you see:",{"type":40,"tag":208,"props":209,"children":210},"ol",{},[211,222,240,271,295,312],{"type":40,"tag":212,"props":213,"children":214},"li",{},[215,220],{"type":40,"tag":55,"props":216,"children":217},{},[218],{"type":46,"value":219},"\"Check cache, miss, fetch, store\" with no coalescing",{"type":46,"value":221}," -- the textbook pattern. When 1,000 requests hit the miss simultaneously, 1,000 identical queries hit the database. Only one fetch is needed; the other 999 should wait for its result.",{"type":40,"tag":212,"props":223,"children":224},{},[225,230,232,238],{"type":40,"tag":55,"props":226,"children":227},{},[228],{"type":46,"value":229},"A fixed TTL on a popular cache key",{"type":46,"value":231}," -- ",{"type":40,"tag":164,"props":233,"children":235},{"className":234},[],[236],{"type":46,"value":237},"cache.set(key, data, { ttl: 300 })",{"type":46,"value":239},". All keys set at similar times expire at similar times. Especially dangerous after a deployment or cache restart that repopulates everything at once.",{"type":40,"tag":212,"props":241,"children":242},{},[243,248,249,254,256,262,263,269],{"type":40,"tag":55,"props":244,"children":245},{},[246],{"type":46,"value":247},"Cron at round times",{"type":46,"value":231},{"type":40,"tag":164,"props":250,"children":252},{"className":251},[],[253],{"type":46,"value":169},{"type":46,"value":255},", ",{"type":40,"tag":164,"props":257,"children":259},{"className":258},[],[260],{"type":46,"value":261},"*\u002F30 * * * *",{"type":46,"value":255},{"type":40,"tag":164,"props":264,"children":266},{"className":265},[],[267],{"type":46,"value":268},"0 0 * * *",{"type":46,"value":270},". Every instance fires at the same second. Every other system in the world also chose these times. Add jitter.",{"type":40,"tag":212,"props":272,"children":273},{},[274,293],{"type":40,"tag":55,"props":275,"children":276},{},[277,283,285,291],{"type":40,"tag":164,"props":278,"children":280},{"className":279},[],[281],{"type":46,"value":282},"cache.delete(key)",{"type":46,"value":284}," or ",{"type":40,"tag":164,"props":286,"children":288},{"className":287},[],[289],{"type":46,"value":290},"cache.clear()",{"type":46,"value":292}," on a hot path",{"type":46,"value":294}," -- invalidating a popular key causes an immediate stampede. Use write-through (set the new value) instead of delete, or use stale-while-revalidate.",{"type":40,"tag":212,"props":296,"children":297},{},[298,303,304,310],{"type":40,"tag":55,"props":299,"children":300},{},[301],{"type":46,"value":302},"Reconnection logic without jitter",{"type":46,"value":231},{"type":40,"tag":164,"props":305,"children":307},{"className":306},[],[308],{"type":46,"value":309},"on(\"error\", () => connect())",{"type":46,"value":311},". Every client detects the failure at the same time and reconnects at the same time. The new server gets overwhelmed before it can serve anyone.",{"type":40,"tag":212,"props":313,"children":314},{},[315,320],{"type":40,"tag":55,"props":316,"children":317},{},[318],{"type":46,"value":319},"Cache warming on startup with no stagger",{"type":46,"value":321}," -- 50 pods restart during a deploy, each makes 50 warmup queries. That's 2,500 queries in seconds on top of normal load.",{"type":40,"tag":61,"props":323,"children":325},{"id":324},"patterns",[326],{"type":46,"value":327},"Patterns",{"type":40,"tag":329,"props":330,"children":332},"h3",{"id":331},"request-coalescing-single-flight",[333],{"type":46,"value":334},"Request coalescing (single-flight)",{"type":40,"tag":49,"props":336,"children":337},{},[338],{"type":46,"value":339},"Only one request fetches on a miss. Everyone else waits for that result.",{"type":40,"tag":341,"props":342,"children":347},"pre",{"className":343,"code":344,"language":345,"meta":346,"style":346},"language-typescript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","const inflight = new Map\u003Cstring, Promise\u003Cany>>();\n\nasync function getWithCoalescing\u003CT>(key: string, fetcher: () => Promise\u003CT>, ttlSeconds: number): Promise\u003CT> {\n  const cached = await cache.get(key);\n  if (cached) return JSON.parse(cached);\n\n  \u002F\u002F If someone is already fetching this key, wait for their result\n  if (inflight.has(key)) return inflight.get(key) as Promise\u003CT>;\n\n  const promise = fetcher()\n    .then(async (data) => {\n      await cache.set(key, JSON.stringify(data), { EX: ttlSeconds + jitter(ttlSeconds) });\n      inflight.delete(key);\n      return data;\n    })\n    .catch((err) => {\n      inflight.delete(key);\n      throw err;\n    });\n\n  inflight.set(key, promise);\n  return promise;\n}\n\nfunction jitter(baseTtl: number): number {\n  return Math.floor(Math.random() * baseTtl * 0.2); \u002F\u002F 0-20% jitter\n}\n","typescript","",[348],{"type":40,"tag":164,"props":349,"children":350},{"__ignoreMap":346},[351,430,440,569,628,687,695,705,795,803,829,872,989,1023,1041,1055,1093,1125,1143,1159,1167,1208,1225,1234,1242,1284,1364],{"type":40,"tag":352,"props":353,"children":355},"span",{"class":354,"line":27},"line",[356,362,368,374,379,385,390,396,401,406,410,415,420,425],{"type":40,"tag":352,"props":357,"children":359},{"style":358},"--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA",[360],{"type":46,"value":361},"const",{"type":40,"tag":352,"props":363,"children":365},{"style":364},"--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8",[366],{"type":46,"value":367}," inflight ",{"type":40,"tag":352,"props":369,"children":371},{"style":370},"--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF",[372],{"type":46,"value":373},"=",{"type":40,"tag":352,"props":375,"children":376},{"style":370},[377],{"type":46,"value":378}," new",{"type":40,"tag":352,"props":380,"children":382},{"style":381},"--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF",[383],{"type":46,"value":384}," Map",{"type":40,"tag":352,"props":386,"children":387},{"style":370},[388],{"type":46,"value":389},"\u003C",{"type":40,"tag":352,"props":391,"children":393},{"style":392},"--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B",[394],{"type":46,"value":395},"string",{"type":40,"tag":352,"props":397,"children":398},{"style":370},[399],{"type":46,"value":400},",",{"type":40,"tag":352,"props":402,"children":403},{"style":392},[404],{"type":46,"value":405}," Promise",{"type":40,"tag":352,"props":407,"children":408},{"style":370},[409],{"type":46,"value":389},{"type":40,"tag":352,"props":411,"children":412},{"style":392},[413],{"type":46,"value":414},"any",{"type":40,"tag":352,"props":416,"children":417},{"style":370},[418],{"type":46,"value":419},">>",{"type":40,"tag":352,"props":421,"children":422},{"style":364},[423],{"type":46,"value":424},"()",{"type":40,"tag":352,"props":426,"children":427},{"style":370},[428],{"type":46,"value":429},";\n",{"type":40,"tag":352,"props":431,"children":433},{"class":354,"line":432},2,[434],{"type":40,"tag":352,"props":435,"children":437},{"emptyLinePlaceholder":436},true,[438],{"type":46,"value":439},"\n",{"type":40,"tag":352,"props":441,"children":442},{"class":354,"line":23},[443,448,453,458,462,467,472,478,483,488,492,497,501,506,511,515,519,523,528,533,537,542,547,551,555,559,564],{"type":40,"tag":352,"props":444,"children":445},{"style":358},[446],{"type":46,"value":447},"async",{"type":40,"tag":352,"props":449,"children":450},{"style":358},[451],{"type":46,"value":452}," function",{"type":40,"tag":352,"props":454,"children":455},{"style":381},[456],{"type":46,"value":457}," getWithCoalescing",{"type":40,"tag":352,"props":459,"children":460},{"style":370},[461],{"type":46,"value":389},{"type":40,"tag":352,"props":463,"children":464},{"style":392},[465],{"type":46,"value":466},"T",{"type":40,"tag":352,"props":468,"children":469},{"style":370},[470],{"type":46,"value":471},">(",{"type":40,"tag":352,"props":473,"children":475},{"style":474},"--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic",[476],{"type":46,"value":477},"key",{"type":40,"tag":352,"props":479,"children":480},{"style":370},[481],{"type":46,"value":482},":",{"type":40,"tag":352,"props":484,"children":485},{"style":392},[486],{"type":46,"value":487}," string",{"type":40,"tag":352,"props":489,"children":490},{"style":370},[491],{"type":46,"value":400},{"type":40,"tag":352,"props":493,"children":494},{"style":381},[495],{"type":46,"value":496}," fetcher",{"type":40,"tag":352,"props":498,"children":499},{"style":370},[500],{"type":46,"value":482},{"type":40,"tag":352,"props":502,"children":503},{"style":370},[504],{"type":46,"value":505}," ()",{"type":40,"tag":352,"props":507,"children":508},{"style":358},[509],{"type":46,"value":510}," =>",{"type":40,"tag":352,"props":512,"children":513},{"style":392},[514],{"type":46,"value":405},{"type":40,"tag":352,"props":516,"children":517},{"style":370},[518],{"type":46,"value":389},{"type":40,"tag":352,"props":520,"children":521},{"style":392},[522],{"type":46,"value":466},{"type":40,"tag":352,"props":524,"children":525},{"style":370},[526],{"type":46,"value":527},">,",{"type":40,"tag":352,"props":529,"children":530},{"style":474},[531],{"type":46,"value":532}," ttlSeconds",{"type":40,"tag":352,"props":534,"children":535},{"style":370},[536],{"type":46,"value":482},{"type":40,"tag":352,"props":538,"children":539},{"style":392},[540],{"type":46,"value":541}," number",{"type":40,"tag":352,"props":543,"children":544},{"style":370},[545],{"type":46,"value":546},"):",{"type":40,"tag":352,"props":548,"children":549},{"style":392},[550],{"type":46,"value":405},{"type":40,"tag":352,"props":552,"children":553},{"style":370},[554],{"type":46,"value":389},{"type":40,"tag":352,"props":556,"children":557},{"style":392},[558],{"type":46,"value":466},{"type":40,"tag":352,"props":560,"children":561},{"style":370},[562],{"type":46,"value":563},">",{"type":40,"tag":352,"props":565,"children":566},{"style":370},[567],{"type":46,"value":568}," {\n",{"type":40,"tag":352,"props":570,"children":572},{"class":354,"line":571},4,[573,578,583,588,594,599,604,609,615,619,624],{"type":40,"tag":352,"props":574,"children":575},{"style":358},[576],{"type":46,"value":577},"  const",{"type":40,"tag":352,"props":579,"children":580},{"style":364},[581],{"type":46,"value":582}," cached",{"type":40,"tag":352,"props":584,"children":585},{"style":370},[586],{"type":46,"value":587}," =",{"type":40,"tag":352,"props":589,"children":591},{"style":590},"--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic",[592],{"type":46,"value":593}," await",{"type":40,"tag":352,"props":595,"children":596},{"style":364},[597],{"type":46,"value":598}," cache",{"type":40,"tag":352,"props":600,"children":601},{"style":370},[602],{"type":46,"value":603},".",{"type":40,"tag":352,"props":605,"children":606},{"style":381},[607],{"type":46,"value":608},"get",{"type":40,"tag":352,"props":610,"children":612},{"style":611},"--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178",[613],{"type":46,"value":614},"(",{"type":40,"tag":352,"props":616,"children":617},{"style":364},[618],{"type":46,"value":477},{"type":40,"tag":352,"props":620,"children":621},{"style":611},[622],{"type":46,"value":623},")",{"type":40,"tag":352,"props":625,"children":626},{"style":370},[627],{"type":46,"value":429},{"type":40,"tag":352,"props":629,"children":631},{"class":354,"line":630},5,[632,637,642,647,652,657,662,666,671,675,679,683],{"type":40,"tag":352,"props":633,"children":634},{"style":590},[635],{"type":46,"value":636},"  if",{"type":40,"tag":352,"props":638,"children":639},{"style":611},[640],{"type":46,"value":641}," (",{"type":40,"tag":352,"props":643,"children":644},{"style":364},[645],{"type":46,"value":646},"cached",{"type":40,"tag":352,"props":648,"children":649},{"style":611},[650],{"type":46,"value":651},") ",{"type":40,"tag":352,"props":653,"children":654},{"style":590},[655],{"type":46,"value":656},"return",{"type":40,"tag":352,"props":658,"children":659},{"style":364},[660],{"type":46,"value":661}," JSON",{"type":40,"tag":352,"props":663,"children":664},{"style":370},[665],{"type":46,"value":603},{"type":40,"tag":352,"props":667,"children":668},{"style":381},[669],{"type":46,"value":670},"parse",{"type":40,"tag":352,"props":672,"children":673},{"style":611},[674],{"type":46,"value":614},{"type":40,"tag":352,"props":676,"children":677},{"style":364},[678],{"type":46,"value":646},{"type":40,"tag":352,"props":680,"children":681},{"style":611},[682],{"type":46,"value":623},{"type":40,"tag":352,"props":684,"children":685},{"style":370},[686],{"type":46,"value":429},{"type":40,"tag":352,"props":688,"children":690},{"class":354,"line":689},6,[691],{"type":40,"tag":352,"props":692,"children":693},{"emptyLinePlaceholder":436},[694],{"type":46,"value":439},{"type":40,"tag":352,"props":696,"children":698},{"class":354,"line":697},7,[699],{"type":40,"tag":352,"props":700,"children":702},{"style":701},"--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic",[703],{"type":46,"value":704},"  \u002F\u002F If someone is already fetching this key, wait for their result\n",{"type":40,"tag":352,"props":706,"children":708},{"class":354,"line":707},8,[709,713,717,722,726,731,735,739,744,748,753,757,761,765,769,773,778,782,786,790],{"type":40,"tag":352,"props":710,"children":711},{"style":590},[712],{"type":46,"value":636},{"type":40,"tag":352,"props":714,"children":715},{"style":611},[716],{"type":46,"value":641},{"type":40,"tag":352,"props":718,"children":719},{"style":364},[720],{"type":46,"value":721},"inflight",{"type":40,"tag":352,"props":723,"children":724},{"style":370},[725],{"type":46,"value":603},{"type":40,"tag":352,"props":727,"children":728},{"style":381},[729],{"type":46,"value":730},"has",{"type":40,"tag":352,"props":732,"children":733},{"style":611},[734],{"type":46,"value":614},{"type":40,"tag":352,"props":736,"children":737},{"style":364},[738],{"type":46,"value":477},{"type":40,"tag":352,"props":740,"children":741},{"style":611},[742],{"type":46,"value":743},")) ",{"type":40,"tag":352,"props":745,"children":746},{"style":590},[747],{"type":46,"value":656},{"type":40,"tag":352,"props":749,"children":750},{"style":364},[751],{"type":46,"value":752}," inflight",{"type":40,"tag":352,"props":754,"children":755},{"style":370},[756],{"type":46,"value":603},{"type":40,"tag":352,"props":758,"children":759},{"style":381},[760],{"type":46,"value":608},{"type":40,"tag":352,"props":762,"children":763},{"style":611},[764],{"type":46,"value":614},{"type":40,"tag":352,"props":766,"children":767},{"style":364},[768],{"type":46,"value":477},{"type":40,"tag":352,"props":770,"children":771},{"style":611},[772],{"type":46,"value":651},{"type":40,"tag":352,"props":774,"children":775},{"style":590},[776],{"type":46,"value":777},"as",{"type":40,"tag":352,"props":779,"children":780},{"style":392},[781],{"type":46,"value":405},{"type":40,"tag":352,"props":783,"children":784},{"style":370},[785],{"type":46,"value":389},{"type":40,"tag":352,"props":787,"children":788},{"style":392},[789],{"type":46,"value":466},{"type":40,"tag":352,"props":791,"children":792},{"style":370},[793],{"type":46,"value":794},">;\n",{"type":40,"tag":352,"props":796,"children":798},{"class":354,"line":797},9,[799],{"type":40,"tag":352,"props":800,"children":801},{"emptyLinePlaceholder":436},[802],{"type":46,"value":439},{"type":40,"tag":352,"props":804,"children":806},{"class":354,"line":805},10,[807,811,816,820,824],{"type":40,"tag":352,"props":808,"children":809},{"style":358},[810],{"type":46,"value":577},{"type":40,"tag":352,"props":812,"children":813},{"style":364},[814],{"type":46,"value":815}," promise",{"type":40,"tag":352,"props":817,"children":818},{"style":370},[819],{"type":46,"value":587},{"type":40,"tag":352,"props":821,"children":822},{"style":381},[823],{"type":46,"value":496},{"type":40,"tag":352,"props":825,"children":826},{"style":611},[827],{"type":46,"value":828},"()\n",{"type":40,"tag":352,"props":830,"children":832},{"class":354,"line":831},11,[833,838,843,847,851,855,860,864,868],{"type":40,"tag":352,"props":834,"children":835},{"style":370},[836],{"type":46,"value":837},"    .",{"type":40,"tag":352,"props":839,"children":840},{"style":381},[841],{"type":46,"value":842},"then",{"type":40,"tag":352,"props":844,"children":845},{"style":611},[846],{"type":46,"value":614},{"type":40,"tag":352,"props":848,"children":849},{"style":358},[850],{"type":46,"value":447},{"type":40,"tag":352,"props":852,"children":853},{"style":370},[854],{"type":46,"value":641},{"type":40,"tag":352,"props":856,"children":857},{"style":474},[858],{"type":46,"value":859},"data",{"type":40,"tag":352,"props":861,"children":862},{"style":370},[863],{"type":46,"value":623},{"type":40,"tag":352,"props":865,"children":866},{"style":358},[867],{"type":46,"value":510},{"type":40,"tag":352,"props":869,"children":870},{"style":370},[871],{"type":46,"value":568},{"type":40,"tag":352,"props":873,"children":875},{"class":354,"line":874},12,[876,881,885,889,894,898,902,906,910,914,919,923,927,931,935,940,945,949,953,958,963,967,972,976,981,985],{"type":40,"tag":352,"props":877,"children":878},{"style":590},[879],{"type":46,"value":880},"      await",{"type":40,"tag":352,"props":882,"children":883},{"style":364},[884],{"type":46,"value":598},{"type":40,"tag":352,"props":886,"children":887},{"style":370},[888],{"type":46,"value":603},{"type":40,"tag":352,"props":890,"children":891},{"style":381},[892],{"type":46,"value":893},"set",{"type":40,"tag":352,"props":895,"children":896},{"style":611},[897],{"type":46,"value":614},{"type":40,"tag":352,"props":899,"children":900},{"style":364},[901],{"type":46,"value":477},{"type":40,"tag":352,"props":903,"children":904},{"style":370},[905],{"type":46,"value":400},{"type":40,"tag":352,"props":907,"children":908},{"style":364},[909],{"type":46,"value":661},{"type":40,"tag":352,"props":911,"children":912},{"style":370},[913],{"type":46,"value":603},{"type":40,"tag":352,"props":915,"children":916},{"style":381},[917],{"type":46,"value":918},"stringify",{"type":40,"tag":352,"props":920,"children":921},{"style":611},[922],{"type":46,"value":614},{"type":40,"tag":352,"props":924,"children":925},{"style":364},[926],{"type":46,"value":859},{"type":40,"tag":352,"props":928,"children":929},{"style":611},[930],{"type":46,"value":623},{"type":40,"tag":352,"props":932,"children":933},{"style":370},[934],{"type":46,"value":400},{"type":40,"tag":352,"props":936,"children":937},{"style":370},[938],{"type":46,"value":939}," {",{"type":40,"tag":352,"props":941,"children":942},{"style":611},[943],{"type":46,"value":944}," EX",{"type":40,"tag":352,"props":946,"children":947},{"style":370},[948],{"type":46,"value":482},{"type":40,"tag":352,"props":950,"children":951},{"style":364},[952],{"type":46,"value":532},{"type":40,"tag":352,"props":954,"children":955},{"style":370},[956],{"type":46,"value":957}," +",{"type":40,"tag":352,"props":959,"children":960},{"style":381},[961],{"type":46,"value":962}," jitter",{"type":40,"tag":352,"props":964,"children":965},{"style":611},[966],{"type":46,"value":614},{"type":40,"tag":352,"props":968,"children":969},{"style":364},[970],{"type":46,"value":971},"ttlSeconds",{"type":40,"tag":352,"props":973,"children":974},{"style":611},[975],{"type":46,"value":651},{"type":40,"tag":352,"props":977,"children":978},{"style":370},[979],{"type":46,"value":980},"}",{"type":40,"tag":352,"props":982,"children":983},{"style":611},[984],{"type":46,"value":623},{"type":40,"tag":352,"props":986,"children":987},{"style":370},[988],{"type":46,"value":429},{"type":40,"tag":352,"props":990,"children":992},{"class":354,"line":991},13,[993,998,1002,1007,1011,1015,1019],{"type":40,"tag":352,"props":994,"children":995},{"style":364},[996],{"type":46,"value":997},"      inflight",{"type":40,"tag":352,"props":999,"children":1000},{"style":370},[1001],{"type":46,"value":603},{"type":40,"tag":352,"props":1003,"children":1004},{"style":381},[1005],{"type":46,"value":1006},"delete",{"type":40,"tag":352,"props":1008,"children":1009},{"style":611},[1010],{"type":46,"value":614},{"type":40,"tag":352,"props":1012,"children":1013},{"style":364},[1014],{"type":46,"value":477},{"type":40,"tag":352,"props":1016,"children":1017},{"style":611},[1018],{"type":46,"value":623},{"type":40,"tag":352,"props":1020,"children":1021},{"style":370},[1022],{"type":46,"value":429},{"type":40,"tag":352,"props":1024,"children":1026},{"class":354,"line":1025},14,[1027,1032,1037],{"type":40,"tag":352,"props":1028,"children":1029},{"style":590},[1030],{"type":46,"value":1031},"      return",{"type":40,"tag":352,"props":1033,"children":1034},{"style":364},[1035],{"type":46,"value":1036}," data",{"type":40,"tag":352,"props":1038,"children":1039},{"style":370},[1040],{"type":46,"value":429},{"type":40,"tag":352,"props":1042,"children":1044},{"class":354,"line":1043},15,[1045,1050],{"type":40,"tag":352,"props":1046,"children":1047},{"style":370},[1048],{"type":46,"value":1049},"    }",{"type":40,"tag":352,"props":1051,"children":1052},{"style":611},[1053],{"type":46,"value":1054},")\n",{"type":40,"tag":352,"props":1056,"children":1058},{"class":354,"line":1057},16,[1059,1063,1068,1072,1076,1081,1085,1089],{"type":40,"tag":352,"props":1060,"children":1061},{"style":370},[1062],{"type":46,"value":837},{"type":40,"tag":352,"props":1064,"children":1065},{"style":381},[1066],{"type":46,"value":1067},"catch",{"type":40,"tag":352,"props":1069,"children":1070},{"style":611},[1071],{"type":46,"value":614},{"type":40,"tag":352,"props":1073,"children":1074},{"style":370},[1075],{"type":46,"value":614},{"type":40,"tag":352,"props":1077,"children":1078},{"style":474},[1079],{"type":46,"value":1080},"err",{"type":40,"tag":352,"props":1082,"children":1083},{"style":370},[1084],{"type":46,"value":623},{"type":40,"tag":352,"props":1086,"children":1087},{"style":358},[1088],{"type":46,"value":510},{"type":40,"tag":352,"props":1090,"children":1091},{"style":370},[1092],{"type":46,"value":568},{"type":40,"tag":352,"props":1094,"children":1096},{"class":354,"line":1095},17,[1097,1101,1105,1109,1113,1117,1121],{"type":40,"tag":352,"props":1098,"children":1099},{"style":364},[1100],{"type":46,"value":997},{"type":40,"tag":352,"props":1102,"children":1103},{"style":370},[1104],{"type":46,"value":603},{"type":40,"tag":352,"props":1106,"children":1107},{"style":381},[1108],{"type":46,"value":1006},{"type":40,"tag":352,"props":1110,"children":1111},{"style":611},[1112],{"type":46,"value":614},{"type":40,"tag":352,"props":1114,"children":1115},{"style":364},[1116],{"type":46,"value":477},{"type":40,"tag":352,"props":1118,"children":1119},{"style":611},[1120],{"type":46,"value":623},{"type":40,"tag":352,"props":1122,"children":1123},{"style":370},[1124],{"type":46,"value":429},{"type":40,"tag":352,"props":1126,"children":1128},{"class":354,"line":1127},18,[1129,1134,1139],{"type":40,"tag":352,"props":1130,"children":1131},{"style":590},[1132],{"type":46,"value":1133},"      throw",{"type":40,"tag":352,"props":1135,"children":1136},{"style":364},[1137],{"type":46,"value":1138}," err",{"type":40,"tag":352,"props":1140,"children":1141},{"style":370},[1142],{"type":46,"value":429},{"type":40,"tag":352,"props":1144,"children":1146},{"class":354,"line":1145},19,[1147,1151,1155],{"type":40,"tag":352,"props":1148,"children":1149},{"style":370},[1150],{"type":46,"value":1049},{"type":40,"tag":352,"props":1152,"children":1153},{"style":611},[1154],{"type":46,"value":623},{"type":40,"tag":352,"props":1156,"children":1157},{"style":370},[1158],{"type":46,"value":429},{"type":40,"tag":352,"props":1160,"children":1162},{"class":354,"line":1161},20,[1163],{"type":40,"tag":352,"props":1164,"children":1165},{"emptyLinePlaceholder":436},[1166],{"type":46,"value":439},{"type":40,"tag":352,"props":1168,"children":1170},{"class":354,"line":1169},21,[1171,1176,1180,1184,1188,1192,1196,1200,1204],{"type":40,"tag":352,"props":1172,"children":1173},{"style":364},[1174],{"type":46,"value":1175},"  inflight",{"type":40,"tag":352,"props":1177,"children":1178},{"style":370},[1179],{"type":46,"value":603},{"type":40,"tag":352,"props":1181,"children":1182},{"style":381},[1183],{"type":46,"value":893},{"type":40,"tag":352,"props":1185,"children":1186},{"style":611},[1187],{"type":46,"value":614},{"type":40,"tag":352,"props":1189,"children":1190},{"style":364},[1191],{"type":46,"value":477},{"type":40,"tag":352,"props":1193,"children":1194},{"style":370},[1195],{"type":46,"value":400},{"type":40,"tag":352,"props":1197,"children":1198},{"style":364},[1199],{"type":46,"value":815},{"type":40,"tag":352,"props":1201,"children":1202},{"style":611},[1203],{"type":46,"value":623},{"type":40,"tag":352,"props":1205,"children":1206},{"style":370},[1207],{"type":46,"value":429},{"type":40,"tag":352,"props":1209,"children":1211},{"class":354,"line":1210},22,[1212,1217,1221],{"type":40,"tag":352,"props":1213,"children":1214},{"style":590},[1215],{"type":46,"value":1216},"  return",{"type":40,"tag":352,"props":1218,"children":1219},{"style":364},[1220],{"type":46,"value":815},{"type":40,"tag":352,"props":1222,"children":1223},{"style":370},[1224],{"type":46,"value":429},{"type":40,"tag":352,"props":1226,"children":1228},{"class":354,"line":1227},23,[1229],{"type":40,"tag":352,"props":1230,"children":1231},{"style":370},[1232],{"type":46,"value":1233},"}\n",{"type":40,"tag":352,"props":1235,"children":1237},{"class":354,"line":1236},24,[1238],{"type":40,"tag":352,"props":1239,"children":1240},{"emptyLinePlaceholder":436},[1241],{"type":46,"value":439},{"type":40,"tag":352,"props":1243,"children":1245},{"class":354,"line":1244},25,[1246,1251,1255,1259,1264,1268,1272,1276,1280],{"type":40,"tag":352,"props":1247,"children":1248},{"style":358},[1249],{"type":46,"value":1250},"function",{"type":40,"tag":352,"props":1252,"children":1253},{"style":381},[1254],{"type":46,"value":962},{"type":40,"tag":352,"props":1256,"children":1257},{"style":370},[1258],{"type":46,"value":614},{"type":40,"tag":352,"props":1260,"children":1261},{"style":474},[1262],{"type":46,"value":1263},"baseTtl",{"type":40,"tag":352,"props":1265,"children":1266},{"style":370},[1267],{"type":46,"value":482},{"type":40,"tag":352,"props":1269,"children":1270},{"style":392},[1271],{"type":46,"value":541},{"type":40,"tag":352,"props":1273,"children":1274},{"style":370},[1275],{"type":46,"value":546},{"type":40,"tag":352,"props":1277,"children":1278},{"style":392},[1279],{"type":46,"value":541},{"type":40,"tag":352,"props":1281,"children":1282},{"style":370},[1283],{"type":46,"value":568},{"type":40,"tag":352,"props":1285,"children":1287},{"class":354,"line":1286},26,[1288,1292,1297,1301,1306,1310,1315,1319,1324,1329,1334,1339,1344,1350,1354,1359],{"type":40,"tag":352,"props":1289,"children":1290},{"style":590},[1291],{"type":46,"value":1216},{"type":40,"tag":352,"props":1293,"children":1294},{"style":364},[1295],{"type":46,"value":1296}," Math",{"type":40,"tag":352,"props":1298,"children":1299},{"style":370},[1300],{"type":46,"value":603},{"type":40,"tag":352,"props":1302,"children":1303},{"style":381},[1304],{"type":46,"value":1305},"floor",{"type":40,"tag":352,"props":1307,"children":1308},{"style":611},[1309],{"type":46,"value":614},{"type":40,"tag":352,"props":1311,"children":1312},{"style":364},[1313],{"type":46,"value":1314},"Math",{"type":40,"tag":352,"props":1316,"children":1317},{"style":370},[1318],{"type":46,"value":603},{"type":40,"tag":352,"props":1320,"children":1321},{"style":381},[1322],{"type":46,"value":1323},"random",{"type":40,"tag":352,"props":1325,"children":1326},{"style":611},[1327],{"type":46,"value":1328},"() ",{"type":40,"tag":352,"props":1330,"children":1331},{"style":370},[1332],{"type":46,"value":1333},"*",{"type":40,"tag":352,"props":1335,"children":1336},{"style":364},[1337],{"type":46,"value":1338}," baseTtl",{"type":40,"tag":352,"props":1340,"children":1341},{"style":370},[1342],{"type":46,"value":1343}," *",{"type":40,"tag":352,"props":1345,"children":1347},{"style":1346},"--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C",[1348],{"type":46,"value":1349}," 0.2",{"type":40,"tag":352,"props":1351,"children":1352},{"style":611},[1353],{"type":46,"value":623},{"type":40,"tag":352,"props":1355,"children":1356},{"style":370},[1357],{"type":46,"value":1358},";",{"type":40,"tag":352,"props":1360,"children":1361},{"style":701},[1362],{"type":46,"value":1363}," \u002F\u002F 0-20% jitter\n",{"type":40,"tag":352,"props":1365,"children":1367},{"class":354,"line":1366},27,[1368],{"type":40,"tag":352,"props":1369,"children":1370},{"style":370},[1371],{"type":46,"value":1233},{"type":40,"tag":49,"props":1373,"children":1374},{},[1375,1377,1383],{"type":46,"value":1376},"This is in-process only -- works for single-instance services. For multi-instance, use a distributed lock (Redis ",{"type":40,"tag":164,"props":1378,"children":1380},{"className":1379},[],[1381],{"type":46,"value":1382},"SET NX PX",{"type":46,"value":1384},") so only one instance across the fleet fetches.",{"type":40,"tag":329,"props":1386,"children":1388},{"id":1387},"stale-while-revalidate",[1389],{"type":46,"value":1390},"Stale-while-revalidate",{"type":40,"tag":49,"props":1392,"children":1393},{},[1394],{"type":46,"value":1395},"Serve stale data immediately, refresh in the background. The cache never has a true miss.",{"type":40,"tag":341,"props":1397,"children":1399},{"className":343,"code":1398,"language":345,"meta":346,"style":346},"const refreshing = new Set\u003Cstring>();\n\nasync function getWithSWR\u003CT>(key: string, fetcher: () => Promise\u003CT>, ttlSeconds: number): Promise\u003CT> {\n  const entry = await cache.getWithTTL(key); \u002F\u002F Returns { data, remainingTtl }\n\n  if (entry && entry.remainingTtl > 0) {\n    return entry.data; \u002F\u002F Fresh\n  }\n\n  if (entry) {\n    \u002F\u002F Stale but exists -- serve it, refresh in background\n    if (!refreshing.has(key)) {\n      refreshing.add(key);\n      fetcher()\n        .then((data) => cache.set(key, data, { EX: ttlSeconds + jitter(ttlSeconds) }))\n        .finally(() => refreshing.delete(key));\n    }\n    return entry.data; \u002F\u002F Return stale immediately\n  }\n\n  \u002F\u002F No data at all -- must fetch synchronously (with coalescing)\n  return fetchAndCache(key, fetcher, ttlSeconds);\n}\n",[1400],{"type":40,"tag":164,"props":1401,"children":1402},{"__ignoreMap":346},[1403,1448,1455,1567,1621,1628,1681,1710,1718,1725,1748,1756,1802,1835,1847,1956,2010,2018,2046,2053,2060,2068,2112],{"type":40,"tag":352,"props":1404,"children":1405},{"class":354,"line":27},[1406,1410,1415,1419,1423,1428,1432,1436,1440,1444],{"type":40,"tag":352,"props":1407,"children":1408},{"style":358},[1409],{"type":46,"value":361},{"type":40,"tag":352,"props":1411,"children":1412},{"style":364},[1413],{"type":46,"value":1414}," refreshing ",{"type":40,"tag":352,"props":1416,"children":1417},{"style":370},[1418],{"type":46,"value":373},{"type":40,"tag":352,"props":1420,"children":1421},{"style":370},[1422],{"type":46,"value":378},{"type":40,"tag":352,"props":1424,"children":1425},{"style":381},[1426],{"type":46,"value":1427}," Set",{"type":40,"tag":352,"props":1429,"children":1430},{"style":370},[1431],{"type":46,"value":389},{"type":40,"tag":352,"props":1433,"children":1434},{"style":392},[1435],{"type":46,"value":395},{"type":40,"tag":352,"props":1437,"children":1438},{"style":370},[1439],{"type":46,"value":563},{"type":40,"tag":352,"props":1441,"children":1442},{"style":364},[1443],{"type":46,"value":424},{"type":40,"tag":352,"props":1445,"children":1446},{"style":370},[1447],{"type":46,"value":429},{"type":40,"tag":352,"props":1449,"children":1450},{"class":354,"line":432},[1451],{"type":40,"tag":352,"props":1452,"children":1453},{"emptyLinePlaceholder":436},[1454],{"type":46,"value":439},{"type":40,"tag":352,"props":1456,"children":1457},{"class":354,"line":23},[1458,1462,1466,1471,1475,1479,1483,1487,1491,1495,1499,1503,1507,1511,1515,1519,1523,1527,1531,1535,1539,1543,1547,1551,1555,1559,1563],{"type":40,"tag":352,"props":1459,"children":1460},{"style":358},[1461],{"type":46,"value":447},{"type":40,"tag":352,"props":1463,"children":1464},{"style":358},[1465],{"type":46,"value":452},{"type":40,"tag":352,"props":1467,"children":1468},{"style":381},[1469],{"type":46,"value":1470}," getWithSWR",{"type":40,"tag":352,"props":1472,"children":1473},{"style":370},[1474],{"type":46,"value":389},{"type":40,"tag":352,"props":1476,"children":1477},{"style":392},[1478],{"type":46,"value":466},{"type":40,"tag":352,"props":1480,"children":1481},{"style":370},[1482],{"type":46,"value":471},{"type":40,"tag":352,"props":1484,"children":1485},{"style":474},[1486],{"type":46,"value":477},{"type":40,"tag":352,"props":1488,"children":1489},{"style":370},[1490],{"type":46,"value":482},{"type":40,"tag":352,"props":1492,"children":1493},{"style":392},[1494],{"type":46,"value":487},{"type":40,"tag":352,"props":1496,"children":1497},{"style":370},[1498],{"type":46,"value":400},{"type":40,"tag":352,"props":1500,"children":1501},{"style":381},[1502],{"type":46,"value":496},{"type":40,"tag":352,"props":1504,"children":1505},{"style":370},[1506],{"type":46,"value":482},{"type":40,"tag":352,"props":1508,"children":1509},{"style":370},[1510],{"type":46,"value":505},{"type":40,"tag":352,"props":1512,"children":1513},{"style":358},[1514],{"type":46,"value":510},{"type":40,"tag":352,"props":1516,"children":1517},{"style":392},[1518],{"type":46,"value":405},{"type":40,"tag":352,"props":1520,"children":1521},{"style":370},[1522],{"type":46,"value":389},{"type":40,"tag":352,"props":1524,"children":1525},{"style":392},[1526],{"type":46,"value":466},{"type":40,"tag":352,"props":1528,"children":1529},{"style":370},[1530],{"type":46,"value":527},{"type":40,"tag":352,"props":1532,"children":1533},{"style":474},[1534],{"type":46,"value":532},{"type":40,"tag":352,"props":1536,"children":1537},{"style":370},[1538],{"type":46,"value":482},{"type":40,"tag":352,"props":1540,"children":1541},{"style":392},[1542],{"type":46,"value":541},{"type":40,"tag":352,"props":1544,"children":1545},{"style":370},[1546],{"type":46,"value":546},{"type":40,"tag":352,"props":1548,"children":1549},{"style":392},[1550],{"type":46,"value":405},{"type":40,"tag":352,"props":1552,"children":1553},{"style":370},[1554],{"type":46,"value":389},{"type":40,"tag":352,"props":1556,"children":1557},{"style":392},[1558],{"type":46,"value":466},{"type":40,"tag":352,"props":1560,"children":1561},{"style":370},[1562],{"type":46,"value":563},{"type":40,"tag":352,"props":1564,"children":1565},{"style":370},[1566],{"type":46,"value":568},{"type":40,"tag":352,"props":1568,"children":1569},{"class":354,"line":571},[1570,1574,1579,1583,1587,1591,1595,1600,1604,1608,1612,1616],{"type":40,"tag":352,"props":1571,"children":1572},{"style":358},[1573],{"type":46,"value":577},{"type":40,"tag":352,"props":1575,"children":1576},{"style":364},[1577],{"type":46,"value":1578}," entry",{"type":40,"tag":352,"props":1580,"children":1581},{"style":370},[1582],{"type":46,"value":587},{"type":40,"tag":352,"props":1584,"children":1585},{"style":590},[1586],{"type":46,"value":593},{"type":40,"tag":352,"props":1588,"children":1589},{"style":364},[1590],{"type":46,"value":598},{"type":40,"tag":352,"props":1592,"children":1593},{"style":370},[1594],{"type":46,"value":603},{"type":40,"tag":352,"props":1596,"children":1597},{"style":381},[1598],{"type":46,"value":1599},"getWithTTL",{"type":40,"tag":352,"props":1601,"children":1602},{"style":611},[1603],{"type":46,"value":614},{"type":40,"tag":352,"props":1605,"children":1606},{"style":364},[1607],{"type":46,"value":477},{"type":40,"tag":352,"props":1609,"children":1610},{"style":611},[1611],{"type":46,"value":623},{"type":40,"tag":352,"props":1613,"children":1614},{"style":370},[1615],{"type":46,"value":1358},{"type":40,"tag":352,"props":1617,"children":1618},{"style":701},[1619],{"type":46,"value":1620}," \u002F\u002F Returns { data, remainingTtl }\n",{"type":40,"tag":352,"props":1622,"children":1623},{"class":354,"line":630},[1624],{"type":40,"tag":352,"props":1625,"children":1626},{"emptyLinePlaceholder":436},[1627],{"type":46,"value":439},{"type":40,"tag":352,"props":1629,"children":1630},{"class":354,"line":689},[1631,1635,1639,1644,1649,1653,1657,1662,1667,1672,1676],{"type":40,"tag":352,"props":1632,"children":1633},{"style":590},[1634],{"type":46,"value":636},{"type":40,"tag":352,"props":1636,"children":1637},{"style":611},[1638],{"type":46,"value":641},{"type":40,"tag":352,"props":1640,"children":1641},{"style":364},[1642],{"type":46,"value":1643},"entry",{"type":40,"tag":352,"props":1645,"children":1646},{"style":370},[1647],{"type":46,"value":1648}," &&",{"type":40,"tag":352,"props":1650,"children":1651},{"style":364},[1652],{"type":46,"value":1578},{"type":40,"tag":352,"props":1654,"children":1655},{"style":370},[1656],{"type":46,"value":603},{"type":40,"tag":352,"props":1658,"children":1659},{"style":364},[1660],{"type":46,"value":1661},"remainingTtl",{"type":40,"tag":352,"props":1663,"children":1664},{"style":370},[1665],{"type":46,"value":1666}," >",{"type":40,"tag":352,"props":1668,"children":1669},{"style":1346},[1670],{"type":46,"value":1671}," 0",{"type":40,"tag":352,"props":1673,"children":1674},{"style":611},[1675],{"type":46,"value":651},{"type":40,"tag":352,"props":1677,"children":1678},{"style":370},[1679],{"type":46,"value":1680},"{\n",{"type":40,"tag":352,"props":1682,"children":1683},{"class":354,"line":697},[1684,1689,1693,1697,1701,1705],{"type":40,"tag":352,"props":1685,"children":1686},{"style":590},[1687],{"type":46,"value":1688},"    return",{"type":40,"tag":352,"props":1690,"children":1691},{"style":364},[1692],{"type":46,"value":1578},{"type":40,"tag":352,"props":1694,"children":1695},{"style":370},[1696],{"type":46,"value":603},{"type":40,"tag":352,"props":1698,"children":1699},{"style":364},[1700],{"type":46,"value":859},{"type":40,"tag":352,"props":1702,"children":1703},{"style":370},[1704],{"type":46,"value":1358},{"type":40,"tag":352,"props":1706,"children":1707},{"style":701},[1708],{"type":46,"value":1709}," \u002F\u002F Fresh\n",{"type":40,"tag":352,"props":1711,"children":1712},{"class":354,"line":707},[1713],{"type":40,"tag":352,"props":1714,"children":1715},{"style":370},[1716],{"type":46,"value":1717},"  }\n",{"type":40,"tag":352,"props":1719,"children":1720},{"class":354,"line":797},[1721],{"type":40,"tag":352,"props":1722,"children":1723},{"emptyLinePlaceholder":436},[1724],{"type":46,"value":439},{"type":40,"tag":352,"props":1726,"children":1727},{"class":354,"line":805},[1728,1732,1736,1740,1744],{"type":40,"tag":352,"props":1729,"children":1730},{"style":590},[1731],{"type":46,"value":636},{"type":40,"tag":352,"props":1733,"children":1734},{"style":611},[1735],{"type":46,"value":641},{"type":40,"tag":352,"props":1737,"children":1738},{"style":364},[1739],{"type":46,"value":1643},{"type":40,"tag":352,"props":1741,"children":1742},{"style":611},[1743],{"type":46,"value":651},{"type":40,"tag":352,"props":1745,"children":1746},{"style":370},[1747],{"type":46,"value":1680},{"type":40,"tag":352,"props":1749,"children":1750},{"class":354,"line":831},[1751],{"type":40,"tag":352,"props":1752,"children":1753},{"style":701},[1754],{"type":46,"value":1755},"    \u002F\u002F Stale but exists -- serve it, refresh in background\n",{"type":40,"tag":352,"props":1757,"children":1758},{"class":354,"line":874},[1759,1764,1768,1773,1778,1782,1786,1790,1794,1798],{"type":40,"tag":352,"props":1760,"children":1761},{"style":590},[1762],{"type":46,"value":1763},"    if",{"type":40,"tag":352,"props":1765,"children":1766},{"style":611},[1767],{"type":46,"value":641},{"type":40,"tag":352,"props":1769,"children":1770},{"style":370},[1771],{"type":46,"value":1772},"!",{"type":40,"tag":352,"props":1774,"children":1775},{"style":364},[1776],{"type":46,"value":1777},"refreshing",{"type":40,"tag":352,"props":1779,"children":1780},{"style":370},[1781],{"type":46,"value":603},{"type":40,"tag":352,"props":1783,"children":1784},{"style":381},[1785],{"type":46,"value":730},{"type":40,"tag":352,"props":1787,"children":1788},{"style":611},[1789],{"type":46,"value":614},{"type":40,"tag":352,"props":1791,"children":1792},{"style":364},[1793],{"type":46,"value":477},{"type":40,"tag":352,"props":1795,"children":1796},{"style":611},[1797],{"type":46,"value":743},{"type":40,"tag":352,"props":1799,"children":1800},{"style":370},[1801],{"type":46,"value":1680},{"type":40,"tag":352,"props":1803,"children":1804},{"class":354,"line":991},[1805,1810,1814,1819,1823,1827,1831],{"type":40,"tag":352,"props":1806,"children":1807},{"style":364},[1808],{"type":46,"value":1809},"      refreshing",{"type":40,"tag":352,"props":1811,"children":1812},{"style":370},[1813],{"type":46,"value":603},{"type":40,"tag":352,"props":1815,"children":1816},{"style":381},[1817],{"type":46,"value":1818},"add",{"type":40,"tag":352,"props":1820,"children":1821},{"style":611},[1822],{"type":46,"value":614},{"type":40,"tag":352,"props":1824,"children":1825},{"style":364},[1826],{"type":46,"value":477},{"type":40,"tag":352,"props":1828,"children":1829},{"style":611},[1830],{"type":46,"value":623},{"type":40,"tag":352,"props":1832,"children":1833},{"style":370},[1834],{"type":46,"value":429},{"type":40,"tag":352,"props":1836,"children":1837},{"class":354,"line":1025},[1838,1843],{"type":40,"tag":352,"props":1839,"children":1840},{"style":381},[1841],{"type":46,"value":1842},"      fetcher",{"type":40,"tag":352,"props":1844,"children":1845},{"style":611},[1846],{"type":46,"value":828},{"type":40,"tag":352,"props":1848,"children":1849},{"class":354,"line":1043},[1850,1855,1859,1863,1867,1871,1875,1879,1883,1887,1891,1895,1899,1903,1907,1911,1915,1919,1923,1927,1931,1935,1939,1943,1947,1951],{"type":40,"tag":352,"props":1851,"children":1852},{"style":370},[1853],{"type":46,"value":1854},"        .",{"type":40,"tag":352,"props":1856,"children":1857},{"style":381},[1858],{"type":46,"value":842},{"type":40,"tag":352,"props":1860,"children":1861},{"style":611},[1862],{"type":46,"value":614},{"type":40,"tag":352,"props":1864,"children":1865},{"style":370},[1866],{"type":46,"value":614},{"type":40,"tag":352,"props":1868,"children":1869},{"style":474},[1870],{"type":46,"value":859},{"type":40,"tag":352,"props":1872,"children":1873},{"style":370},[1874],{"type":46,"value":623},{"type":40,"tag":352,"props":1876,"children":1877},{"style":358},[1878],{"type":46,"value":510},{"type":40,"tag":352,"props":1880,"children":1881},{"style":364},[1882],{"type":46,"value":598},{"type":40,"tag":352,"props":1884,"children":1885},{"style":370},[1886],{"type":46,"value":603},{"type":40,"tag":352,"props":1888,"children":1889},{"style":381},[1890],{"type":46,"value":893},{"type":40,"tag":352,"props":1892,"children":1893},{"style":611},[1894],{"type":46,"value":614},{"type":40,"tag":352,"props":1896,"children":1897},{"style":364},[1898],{"type":46,"value":477},{"type":40,"tag":352,"props":1900,"children":1901},{"style":370},[1902],{"type":46,"value":400},{"type":40,"tag":352,"props":1904,"children":1905},{"style":364},[1906],{"type":46,"value":1036},{"type":40,"tag":352,"props":1908,"children":1909},{"style":370},[1910],{"type":46,"value":400},{"type":40,"tag":352,"props":1912,"children":1913},{"style":370},[1914],{"type":46,"value":939},{"type":40,"tag":352,"props":1916,"children":1917},{"style":611},[1918],{"type":46,"value":944},{"type":40,"tag":352,"props":1920,"children":1921},{"style":370},[1922],{"type":46,"value":482},{"type":40,"tag":352,"props":1924,"children":1925},{"style":364},[1926],{"type":46,"value":532},{"type":40,"tag":352,"props":1928,"children":1929},{"style":370},[1930],{"type":46,"value":957},{"type":40,"tag":352,"props":1932,"children":1933},{"style":381},[1934],{"type":46,"value":962},{"type":40,"tag":352,"props":1936,"children":1937},{"style":611},[1938],{"type":46,"value":614},{"type":40,"tag":352,"props":1940,"children":1941},{"style":364},[1942],{"type":46,"value":971},{"type":40,"tag":352,"props":1944,"children":1945},{"style":611},[1946],{"type":46,"value":651},{"type":40,"tag":352,"props":1948,"children":1949},{"style":370},[1950],{"type":46,"value":980},{"type":40,"tag":352,"props":1952,"children":1953},{"style":611},[1954],{"type":46,"value":1955},"))\n",{"type":40,"tag":352,"props":1957,"children":1958},{"class":354,"line":1057},[1959,1963,1968,1972,1976,1980,1985,1989,1993,1997,2001,2006],{"type":40,"tag":352,"props":1960,"children":1961},{"style":370},[1962],{"type":46,"value":1854},{"type":40,"tag":352,"props":1964,"children":1965},{"style":381},[1966],{"type":46,"value":1967},"finally",{"type":40,"tag":352,"props":1969,"children":1970},{"style":611},[1971],{"type":46,"value":614},{"type":40,"tag":352,"props":1973,"children":1974},{"style":370},[1975],{"type":46,"value":424},{"type":40,"tag":352,"props":1977,"children":1978},{"style":358},[1979],{"type":46,"value":510},{"type":40,"tag":352,"props":1981,"children":1982},{"style":364},[1983],{"type":46,"value":1984}," refreshing",{"type":40,"tag":352,"props":1986,"children":1987},{"style":370},[1988],{"type":46,"value":603},{"type":40,"tag":352,"props":1990,"children":1991},{"style":381},[1992],{"type":46,"value":1006},{"type":40,"tag":352,"props":1994,"children":1995},{"style":611},[1996],{"type":46,"value":614},{"type":40,"tag":352,"props":1998,"children":1999},{"style":364},[2000],{"type":46,"value":477},{"type":40,"tag":352,"props":2002,"children":2003},{"style":611},[2004],{"type":46,"value":2005},"))",{"type":40,"tag":352,"props":2007,"children":2008},{"style":370},[2009],{"type":46,"value":429},{"type":40,"tag":352,"props":2011,"children":2012},{"class":354,"line":1095},[2013],{"type":40,"tag":352,"props":2014,"children":2015},{"style":370},[2016],{"type":46,"value":2017},"    }\n",{"type":40,"tag":352,"props":2019,"children":2020},{"class":354,"line":1127},[2021,2025,2029,2033,2037,2041],{"type":40,"tag":352,"props":2022,"children":2023},{"style":590},[2024],{"type":46,"value":1688},{"type":40,"tag":352,"props":2026,"children":2027},{"style":364},[2028],{"type":46,"value":1578},{"type":40,"tag":352,"props":2030,"children":2031},{"style":370},[2032],{"type":46,"value":603},{"type":40,"tag":352,"props":2034,"children":2035},{"style":364},[2036],{"type":46,"value":859},{"type":40,"tag":352,"props":2038,"children":2039},{"style":370},[2040],{"type":46,"value":1358},{"type":40,"tag":352,"props":2042,"children":2043},{"style":701},[2044],{"type":46,"value":2045}," \u002F\u002F Return stale immediately\n",{"type":40,"tag":352,"props":2047,"children":2048},{"class":354,"line":1145},[2049],{"type":40,"tag":352,"props":2050,"children":2051},{"style":370},[2052],{"type":46,"value":1717},{"type":40,"tag":352,"props":2054,"children":2055},{"class":354,"line":1161},[2056],{"type":40,"tag":352,"props":2057,"children":2058},{"emptyLinePlaceholder":436},[2059],{"type":46,"value":439},{"type":40,"tag":352,"props":2061,"children":2062},{"class":354,"line":1169},[2063],{"type":40,"tag":352,"props":2064,"children":2065},{"style":701},[2066],{"type":46,"value":2067},"  \u002F\u002F No data at all -- must fetch synchronously (with coalescing)\n",{"type":40,"tag":352,"props":2069,"children":2070},{"class":354,"line":1210},[2071,2075,2080,2084,2088,2092,2096,2100,2104,2108],{"type":40,"tag":352,"props":2072,"children":2073},{"style":590},[2074],{"type":46,"value":1216},{"type":40,"tag":352,"props":2076,"children":2077},{"style":381},[2078],{"type":46,"value":2079}," fetchAndCache",{"type":40,"tag":352,"props":2081,"children":2082},{"style":611},[2083],{"type":46,"value":614},{"type":40,"tag":352,"props":2085,"children":2086},{"style":364},[2087],{"type":46,"value":477},{"type":40,"tag":352,"props":2089,"children":2090},{"style":370},[2091],{"type":46,"value":400},{"type":40,"tag":352,"props":2093,"children":2094},{"style":364},[2095],{"type":46,"value":496},{"type":40,"tag":352,"props":2097,"children":2098},{"style":370},[2099],{"type":46,"value":400},{"type":40,"tag":352,"props":2101,"children":2102},{"style":364},[2103],{"type":46,"value":532},{"type":40,"tag":352,"props":2105,"children":2106},{"style":611},[2107],{"type":46,"value":623},{"type":40,"tag":352,"props":2109,"children":2110},{"style":370},[2111],{"type":46,"value":429},{"type":40,"tag":352,"props":2113,"children":2114},{"class":354,"line":1227},[2115],{"type":40,"tag":352,"props":2116,"children":2117},{"style":370},[2118],{"type":46,"value":1233},{"type":40,"tag":49,"props":2120,"children":2121},{},[2122],{"type":46,"value":2123},"The user gets a response in milliseconds (from stale cache) while the fresh data is fetched in the background. No stampede because there's never a moment where the cache is empty.",{"type":40,"tag":49,"props":2125,"children":2126},{},[2127,2132],{"type":40,"tag":55,"props":2128,"children":2129},{},[2130],{"type":46,"value":2131},"Tradeoff:",{"type":46,"value":2133}," Users may briefly see stale data. Fine for most content. Not acceptable for financial data or security-critical reads.",{"type":40,"tag":329,"props":2135,"children":2137},{"id":2136},"jittered-ttls",[2138],{"type":46,"value":2139},"Jittered TTLs",{"type":40,"tag":49,"props":2141,"children":2142},{},[2143],{"type":46,"value":2144},"Prevent mass expiry by spreading expirations across a time window.",{"type":40,"tag":341,"props":2146,"children":2148},{"className":343,"code":2147,"language":345,"meta":346,"style":346},"function setWithJitter(key: string, data: any, baseTtlSeconds: number) {\n  \u002F\u002F Add 0-20% random jitter\n  const ttl = baseTtlSeconds + Math.floor(Math.random() * baseTtlSeconds * 0.2);\n  return cache.set(key, JSON.stringify(data), { EX: ttl });\n}\n",[2149],{"type":40,"tag":164,"props":2150,"children":2151},{"__ignoreMap":346},[2152,2222,2230,2310,2398],{"type":40,"tag":352,"props":2153,"children":2154},{"class":354,"line":27},[2155,2159,2164,2168,2172,2176,2180,2184,2188,2192,2197,2201,2206,2210,2214,2218],{"type":40,"tag":352,"props":2156,"children":2157},{"style":358},[2158],{"type":46,"value":1250},{"type":40,"tag":352,"props":2160,"children":2161},{"style":381},[2162],{"type":46,"value":2163}," setWithJitter",{"type":40,"tag":352,"props":2165,"children":2166},{"style":370},[2167],{"type":46,"value":614},{"type":40,"tag":352,"props":2169,"children":2170},{"style":474},[2171],{"type":46,"value":477},{"type":40,"tag":352,"props":2173,"children":2174},{"style":370},[2175],{"type":46,"value":482},{"type":40,"tag":352,"props":2177,"children":2178},{"style":392},[2179],{"type":46,"value":487},{"type":40,"tag":352,"props":2181,"children":2182},{"style":370},[2183],{"type":46,"value":400},{"type":40,"tag":352,"props":2185,"children":2186},{"style":474},[2187],{"type":46,"value":1036},{"type":40,"tag":352,"props":2189,"children":2190},{"style":370},[2191],{"type":46,"value":482},{"type":40,"tag":352,"props":2193,"children":2194},{"style":392},[2195],{"type":46,"value":2196}," any",{"type":40,"tag":352,"props":2198,"children":2199},{"style":370},[2200],{"type":46,"value":400},{"type":40,"tag":352,"props":2202,"children":2203},{"style":474},[2204],{"type":46,"value":2205}," baseTtlSeconds",{"type":40,"tag":352,"props":2207,"children":2208},{"style":370},[2209],{"type":46,"value":482},{"type":40,"tag":352,"props":2211,"children":2212},{"style":392},[2213],{"type":46,"value":541},{"type":40,"tag":352,"props":2215,"children":2216},{"style":370},[2217],{"type":46,"value":623},{"type":40,"tag":352,"props":2219,"children":2220},{"style":370},[2221],{"type":46,"value":568},{"type":40,"tag":352,"props":2223,"children":2224},{"class":354,"line":432},[2225],{"type":40,"tag":352,"props":2226,"children":2227},{"style":701},[2228],{"type":46,"value":2229},"  \u002F\u002F Add 0-20% random jitter\n",{"type":40,"tag":352,"props":2231,"children":2232},{"class":354,"line":23},[2233,2237,2242,2246,2250,2254,2258,2262,2266,2270,2274,2278,2282,2286,2290,2294,2298,2302,2306],{"type":40,"tag":352,"props":2234,"children":2235},{"style":358},[2236],{"type":46,"value":577},{"type":40,"tag":352,"props":2238,"children":2239},{"style":364},[2240],{"type":46,"value":2241}," ttl",{"type":40,"tag":352,"props":2243,"children":2244},{"style":370},[2245],{"type":46,"value":587},{"type":40,"tag":352,"props":2247,"children":2248},{"style":364},[2249],{"type":46,"value":2205},{"type":40,"tag":352,"props":2251,"children":2252},{"style":370},[2253],{"type":46,"value":957},{"type":40,"tag":352,"props":2255,"children":2256},{"style":364},[2257],{"type":46,"value":1296},{"type":40,"tag":352,"props":2259,"children":2260},{"style":370},[2261],{"type":46,"value":603},{"type":40,"tag":352,"props":2263,"children":2264},{"style":381},[2265],{"type":46,"value":1305},{"type":40,"tag":352,"props":2267,"children":2268},{"style":611},[2269],{"type":46,"value":614},{"type":40,"tag":352,"props":2271,"children":2272},{"style":364},[2273],{"type":46,"value":1314},{"type":40,"tag":352,"props":2275,"children":2276},{"style":370},[2277],{"type":46,"value":603},{"type":40,"tag":352,"props":2279,"children":2280},{"style":381},[2281],{"type":46,"value":1323},{"type":40,"tag":352,"props":2283,"children":2284},{"style":611},[2285],{"type":46,"value":1328},{"type":40,"tag":352,"props":2287,"children":2288},{"style":370},[2289],{"type":46,"value":1333},{"type":40,"tag":352,"props":2291,"children":2292},{"style":364},[2293],{"type":46,"value":2205},{"type":40,"tag":352,"props":2295,"children":2296},{"style":370},[2297],{"type":46,"value":1343},{"type":40,"tag":352,"props":2299,"children":2300},{"style":1346},[2301],{"type":46,"value":1349},{"type":40,"tag":352,"props":2303,"children":2304},{"style":611},[2305],{"type":46,"value":623},{"type":40,"tag":352,"props":2307,"children":2308},{"style":370},[2309],{"type":46,"value":429},{"type":40,"tag":352,"props":2311,"children":2312},{"class":354,"line":571},[2313,2317,2321,2325,2329,2333,2337,2341,2345,2349,2353,2357,2361,2365,2369,2373,2377,2381,2385,2390,2394],{"type":40,"tag":352,"props":2314,"children":2315},{"style":590},[2316],{"type":46,"value":1216},{"type":40,"tag":352,"props":2318,"children":2319},{"style":364},[2320],{"type":46,"value":598},{"type":40,"tag":352,"props":2322,"children":2323},{"style":370},[2324],{"type":46,"value":603},{"type":40,"tag":352,"props":2326,"children":2327},{"style":381},[2328],{"type":46,"value":893},{"type":40,"tag":352,"props":2330,"children":2331},{"style":611},[2332],{"type":46,"value":614},{"type":40,"tag":352,"props":2334,"children":2335},{"style":364},[2336],{"type":46,"value":477},{"type":40,"tag":352,"props":2338,"children":2339},{"style":370},[2340],{"type":46,"value":400},{"type":40,"tag":352,"props":2342,"children":2343},{"style":364},[2344],{"type":46,"value":661},{"type":40,"tag":352,"props":2346,"children":2347},{"style":370},[2348],{"type":46,"value":603},{"type":40,"tag":352,"props":2350,"children":2351},{"style":381},[2352],{"type":46,"value":918},{"type":40,"tag":352,"props":2354,"children":2355},{"style":611},[2356],{"type":46,"value":614},{"type":40,"tag":352,"props":2358,"children":2359},{"style":364},[2360],{"type":46,"value":859},{"type":40,"tag":352,"props":2362,"children":2363},{"style":611},[2364],{"type":46,"value":623},{"type":40,"tag":352,"props":2366,"children":2367},{"style":370},[2368],{"type":46,"value":400},{"type":40,"tag":352,"props":2370,"children":2371},{"style":370},[2372],{"type":46,"value":939},{"type":40,"tag":352,"props":2374,"children":2375},{"style":611},[2376],{"type":46,"value":944},{"type":40,"tag":352,"props":2378,"children":2379},{"style":370},[2380],{"type":46,"value":482},{"type":40,"tag":352,"props":2382,"children":2383},{"style":364},[2384],{"type":46,"value":2241},{"type":40,"tag":352,"props":2386,"children":2387},{"style":370},[2388],{"type":46,"value":2389}," }",{"type":40,"tag":352,"props":2391,"children":2392},{"style":611},[2393],{"type":46,"value":623},{"type":40,"tag":352,"props":2395,"children":2396},{"style":370},[2397],{"type":46,"value":429},{"type":40,"tag":352,"props":2399,"children":2400},{"class":354,"line":630},[2401],{"type":40,"tag":352,"props":2402,"children":2403},{"style":370},[2404],{"type":46,"value":1233},{"type":40,"tag":49,"props":2406,"children":2407},{},[2408],{"type":46,"value":2409},"This doesn't prevent a stampede on a single hot key (all requests still miss at the same moment). But it prevents mass expiry -- 10,000 keys populated at the same time won't all expire in the same second.",{"type":40,"tag":329,"props":2411,"children":2413},{"id":2412},"jittered-cron-scheduling",[2414],{"type":46,"value":2415},"Jittered cron scheduling",{"type":40,"tag":341,"props":2417,"children":2419},{"className":343,"code":2418,"language":345,"meta":346,"style":346},"\u002F\u002F BAD: all instances fire at exactly :00\ncron.schedule(\"0 * * * *\", generateReport);\n\n\u002F\u002F GOOD: each instance starts at a random offset within the first minute\nconst jitterMs = Math.floor(Math.random() * 60_000);\nsetTimeout(() => {\n  cron.schedule(\"0 * * * *\", generateReport);\n}, jitterMs);\n",[2420],{"type":40,"tag":164,"props":2421,"children":2422},{"__ignoreMap":346},[2423,2431,2479,2486,2494,2556,2580,2629],{"type":40,"tag":352,"props":2424,"children":2425},{"class":354,"line":27},[2426],{"type":40,"tag":352,"props":2427,"children":2428},{"style":701},[2429],{"type":46,"value":2430},"\u002F\u002F BAD: all instances fire at exactly :00\n",{"type":40,"tag":352,"props":2432,"children":2433},{"class":354,"line":432},[2434,2439,2443,2448,2452,2457,2462,2466,2470,2475],{"type":40,"tag":352,"props":2435,"children":2436},{"style":364},[2437],{"type":46,"value":2438},"cron",{"type":40,"tag":352,"props":2440,"children":2441},{"style":370},[2442],{"type":46,"value":603},{"type":40,"tag":352,"props":2444,"children":2445},{"style":381},[2446],{"type":46,"value":2447},"schedule",{"type":40,"tag":352,"props":2449,"children":2450},{"style":364},[2451],{"type":46,"value":614},{"type":40,"tag":352,"props":2453,"children":2454},{"style":370},[2455],{"type":46,"value":2456},"\"",{"type":40,"tag":352,"props":2458,"children":2460},{"style":2459},"--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D",[2461],{"type":46,"value":169},{"type":40,"tag":352,"props":2463,"children":2464},{"style":370},[2465],{"type":46,"value":2456},{"type":40,"tag":352,"props":2467,"children":2468},{"style":370},[2469],{"type":46,"value":400},{"type":40,"tag":352,"props":2471,"children":2472},{"style":364},[2473],{"type":46,"value":2474}," generateReport)",{"type":40,"tag":352,"props":2476,"children":2477},{"style":370},[2478],{"type":46,"value":429},{"type":40,"tag":352,"props":2480,"children":2481},{"class":354,"line":23},[2482],{"type":40,"tag":352,"props":2483,"children":2484},{"emptyLinePlaceholder":436},[2485],{"type":46,"value":439},{"type":40,"tag":352,"props":2487,"children":2488},{"class":354,"line":571},[2489],{"type":40,"tag":352,"props":2490,"children":2491},{"style":701},[2492],{"type":46,"value":2493},"\u002F\u002F GOOD: each instance starts at a random offset within the first minute\n",{"type":40,"tag":352,"props":2495,"children":2496},{"class":354,"line":630},[2497,2501,2506,2510,2514,2518,2522,2527,2531,2535,2539,2543,2548,2552],{"type":40,"tag":352,"props":2498,"children":2499},{"style":358},[2500],{"type":46,"value":361},{"type":40,"tag":352,"props":2502,"children":2503},{"style":364},[2504],{"type":46,"value":2505}," jitterMs ",{"type":40,"tag":352,"props":2507,"children":2508},{"style":370},[2509],{"type":46,"value":373},{"type":40,"tag":352,"props":2511,"children":2512},{"style":364},[2513],{"type":46,"value":1296},{"type":40,"tag":352,"props":2515,"children":2516},{"style":370},[2517],{"type":46,"value":603},{"type":40,"tag":352,"props":2519,"children":2520},{"style":381},[2521],{"type":46,"value":1305},{"type":40,"tag":352,"props":2523,"children":2524},{"style":364},[2525],{"type":46,"value":2526},"(Math",{"type":40,"tag":352,"props":2528,"children":2529},{"style":370},[2530],{"type":46,"value":603},{"type":40,"tag":352,"props":2532,"children":2533},{"style":381},[2534],{"type":46,"value":1323},{"type":40,"tag":352,"props":2536,"children":2537},{"style":364},[2538],{"type":46,"value":1328},{"type":40,"tag":352,"props":2540,"children":2541},{"style":370},[2542],{"type":46,"value":1333},{"type":40,"tag":352,"props":2544,"children":2545},{"style":1346},[2546],{"type":46,"value":2547}," 60_000",{"type":40,"tag":352,"props":2549,"children":2550},{"style":364},[2551],{"type":46,"value":623},{"type":40,"tag":352,"props":2553,"children":2554},{"style":370},[2555],{"type":46,"value":429},{"type":40,"tag":352,"props":2557,"children":2558},{"class":354,"line":689},[2559,2564,2568,2572,2576],{"type":40,"tag":352,"props":2560,"children":2561},{"style":381},[2562],{"type":46,"value":2563},"setTimeout",{"type":40,"tag":352,"props":2565,"children":2566},{"style":364},[2567],{"type":46,"value":614},{"type":40,"tag":352,"props":2569,"children":2570},{"style":370},[2571],{"type":46,"value":424},{"type":40,"tag":352,"props":2573,"children":2574},{"style":358},[2575],{"type":46,"value":510},{"type":40,"tag":352,"props":2577,"children":2578},{"style":370},[2579],{"type":46,"value":568},{"type":40,"tag":352,"props":2581,"children":2582},{"class":354,"line":697},[2583,2588,2592,2596,2600,2604,2608,2612,2616,2621,2625],{"type":40,"tag":352,"props":2584,"children":2585},{"style":364},[2586],{"type":46,"value":2587},"  cron",{"type":40,"tag":352,"props":2589,"children":2590},{"style":370},[2591],{"type":46,"value":603},{"type":40,"tag":352,"props":2593,"children":2594},{"style":381},[2595],{"type":46,"value":2447},{"type":40,"tag":352,"props":2597,"children":2598},{"style":611},[2599],{"type":46,"value":614},{"type":40,"tag":352,"props":2601,"children":2602},{"style":370},[2603],{"type":46,"value":2456},{"type":40,"tag":352,"props":2605,"children":2606},{"style":2459},[2607],{"type":46,"value":169},{"type":40,"tag":352,"props":2609,"children":2610},{"style":370},[2611],{"type":46,"value":2456},{"type":40,"tag":352,"props":2613,"children":2614},{"style":370},[2615],{"type":46,"value":400},{"type":40,"tag":352,"props":2617,"children":2618},{"style":364},[2619],{"type":46,"value":2620}," generateReport",{"type":40,"tag":352,"props":2622,"children":2623},{"style":611},[2624],{"type":46,"value":623},{"type":40,"tag":352,"props":2626,"children":2627},{"style":370},[2628],{"type":46,"value":429},{"type":40,"tag":352,"props":2630,"children":2631},{"class":354,"line":707},[2632,2637,2642],{"type":40,"tag":352,"props":2633,"children":2634},{"style":370},[2635],{"type":46,"value":2636},"},",{"type":40,"tag":352,"props":2638,"children":2639},{"style":364},[2640],{"type":46,"value":2641}," jitterMs)",{"type":40,"tag":352,"props":2643,"children":2644},{"style":370},[2645],{"type":46,"value":429},{"type":40,"tag":329,"props":2647,"children":2649},{"id":2648},"jittered-reconnection",[2650],{"type":46,"value":2651},"Jittered reconnection",{"type":40,"tag":341,"props":2653,"children":2655},{"className":343,"code":2654,"language":345,"meta":346,"style":346},"\u002F\u002F BAD: reconnect immediately on failure\ndb.on(\"error\", () => db.connect());\n\n\u002F\u002F GOOD: exponential backoff with jitter\ndb.on(\"error\", () => {\n  const baseDelay = 1000;\n  const attempt = reconnectAttempts++;\n  const delay = baseDelay * 2 ** Math.min(attempt, 6) + Math.random() * 1000;\n  setTimeout(() => db.connect(), delay);\n});\n",[2656],{"type":40,"tag":164,"props":2657,"children":2658},{"__ignoreMap":346},[2659,2667,2736,2743,2751,2798,2823,2849,2951,3003],{"type":40,"tag":352,"props":2660,"children":2661},{"class":354,"line":27},[2662],{"type":40,"tag":352,"props":2663,"children":2664},{"style":701},[2665],{"type":46,"value":2666},"\u002F\u002F BAD: reconnect immediately on failure\n",{"type":40,"tag":352,"props":2668,"children":2669},{"class":354,"line":432},[2670,2675,2679,2684,2688,2692,2697,2701,2705,2709,2713,2718,2722,2727,2732],{"type":40,"tag":352,"props":2671,"children":2672},{"style":364},[2673],{"type":46,"value":2674},"db",{"type":40,"tag":352,"props":2676,"children":2677},{"style":370},[2678],{"type":46,"value":603},{"type":40,"tag":352,"props":2680,"children":2681},{"style":381},[2682],{"type":46,"value":2683},"on",{"type":40,"tag":352,"props":2685,"children":2686},{"style":364},[2687],{"type":46,"value":614},{"type":40,"tag":352,"props":2689,"children":2690},{"style":370},[2691],{"type":46,"value":2456},{"type":40,"tag":352,"props":2693,"children":2694},{"style":2459},[2695],{"type":46,"value":2696},"error",{"type":40,"tag":352,"props":2698,"children":2699},{"style":370},[2700],{"type":46,"value":2456},{"type":40,"tag":352,"props":2702,"children":2703},{"style":370},[2704],{"type":46,"value":400},{"type":40,"tag":352,"props":2706,"children":2707},{"style":370},[2708],{"type":46,"value":505},{"type":40,"tag":352,"props":2710,"children":2711},{"style":358},[2712],{"type":46,"value":510},{"type":40,"tag":352,"props":2714,"children":2715},{"style":364},[2716],{"type":46,"value":2717}," db",{"type":40,"tag":352,"props":2719,"children":2720},{"style":370},[2721],{"type":46,"value":603},{"type":40,"tag":352,"props":2723,"children":2724},{"style":381},[2725],{"type":46,"value":2726},"connect",{"type":40,"tag":352,"props":2728,"children":2729},{"style":364},[2730],{"type":46,"value":2731},"())",{"type":40,"tag":352,"props":2733,"children":2734},{"style":370},[2735],{"type":46,"value":429},{"type":40,"tag":352,"props":2737,"children":2738},{"class":354,"line":23},[2739],{"type":40,"tag":352,"props":2740,"children":2741},{"emptyLinePlaceholder":436},[2742],{"type":46,"value":439},{"type":40,"tag":352,"props":2744,"children":2745},{"class":354,"line":571},[2746],{"type":40,"tag":352,"props":2747,"children":2748},{"style":701},[2749],{"type":46,"value":2750},"\u002F\u002F GOOD: exponential backoff with jitter\n",{"type":40,"tag":352,"props":2752,"children":2753},{"class":354,"line":630},[2754,2758,2762,2766,2770,2774,2778,2782,2786,2790,2794],{"type":40,"tag":352,"props":2755,"children":2756},{"style":364},[2757],{"type":46,"value":2674},{"type":40,"tag":352,"props":2759,"children":2760},{"style":370},[2761],{"type":46,"value":603},{"type":40,"tag":352,"props":2763,"children":2764},{"style":381},[2765],{"type":46,"value":2683},{"type":40,"tag":352,"props":2767,"children":2768},{"style":364},[2769],{"type":46,"value":614},{"type":40,"tag":352,"props":2771,"children":2772},{"style":370},[2773],{"type":46,"value":2456},{"type":40,"tag":352,"props":2775,"children":2776},{"style":2459},[2777],{"type":46,"value":2696},{"type":40,"tag":352,"props":2779,"children":2780},{"style":370},[2781],{"type":46,"value":2456},{"type":40,"tag":352,"props":2783,"children":2784},{"style":370},[2785],{"type":46,"value":400},{"type":40,"tag":352,"props":2787,"children":2788},{"style":370},[2789],{"type":46,"value":505},{"type":40,"tag":352,"props":2791,"children":2792},{"style":358},[2793],{"type":46,"value":510},{"type":40,"tag":352,"props":2795,"children":2796},{"style":370},[2797],{"type":46,"value":568},{"type":40,"tag":352,"props":2799,"children":2800},{"class":354,"line":689},[2801,2805,2810,2814,2819],{"type":40,"tag":352,"props":2802,"children":2803},{"style":358},[2804],{"type":46,"value":577},{"type":40,"tag":352,"props":2806,"children":2807},{"style":364},[2808],{"type":46,"value":2809}," baseDelay",{"type":40,"tag":352,"props":2811,"children":2812},{"style":370},[2813],{"type":46,"value":587},{"type":40,"tag":352,"props":2815,"children":2816},{"style":1346},[2817],{"type":46,"value":2818}," 1000",{"type":40,"tag":352,"props":2820,"children":2821},{"style":370},[2822],{"type":46,"value":429},{"type":40,"tag":352,"props":2824,"children":2825},{"class":354,"line":697},[2826,2830,2835,2839,2844],{"type":40,"tag":352,"props":2827,"children":2828},{"style":358},[2829],{"type":46,"value":577},{"type":40,"tag":352,"props":2831,"children":2832},{"style":364},[2833],{"type":46,"value":2834}," attempt",{"type":40,"tag":352,"props":2836,"children":2837},{"style":370},[2838],{"type":46,"value":587},{"type":40,"tag":352,"props":2840,"children":2841},{"style":364},[2842],{"type":46,"value":2843}," reconnectAttempts",{"type":40,"tag":352,"props":2845,"children":2846},{"style":370},[2847],{"type":46,"value":2848},"++;\n",{"type":40,"tag":352,"props":2850,"children":2851},{"class":354,"line":707},[2852,2856,2861,2865,2869,2873,2878,2883,2887,2891,2896,2900,2905,2909,2914,2918,2923,2927,2931,2935,2939,2943,2947],{"type":40,"tag":352,"props":2853,"children":2854},{"style":358},[2855],{"type":46,"value":577},{"type":40,"tag":352,"props":2857,"children":2858},{"style":364},[2859],{"type":46,"value":2860}," delay",{"type":40,"tag":352,"props":2862,"children":2863},{"style":370},[2864],{"type":46,"value":587},{"type":40,"tag":352,"props":2866,"children":2867},{"style":364},[2868],{"type":46,"value":2809},{"type":40,"tag":352,"props":2870,"children":2871},{"style":370},[2872],{"type":46,"value":1343},{"type":40,"tag":352,"props":2874,"children":2875},{"style":1346},[2876],{"type":46,"value":2877}," 2",{"type":40,"tag":352,"props":2879,"children":2880},{"style":370},[2881],{"type":46,"value":2882}," **",{"type":40,"tag":352,"props":2884,"children":2885},{"style":364},[2886],{"type":46,"value":1296},{"type":40,"tag":352,"props":2888,"children":2889},{"style":370},[2890],{"type":46,"value":603},{"type":40,"tag":352,"props":2892,"children":2893},{"style":381},[2894],{"type":46,"value":2895},"min",{"type":40,"tag":352,"props":2897,"children":2898},{"style":611},[2899],{"type":46,"value":614},{"type":40,"tag":352,"props":2901,"children":2902},{"style":364},[2903],{"type":46,"value":2904},"attempt",{"type":40,"tag":352,"props":2906,"children":2907},{"style":370},[2908],{"type":46,"value":400},{"type":40,"tag":352,"props":2910,"children":2911},{"style":1346},[2912],{"type":46,"value":2913}," 6",{"type":40,"tag":352,"props":2915,"children":2916},{"style":611},[2917],{"type":46,"value":651},{"type":40,"tag":352,"props":2919,"children":2920},{"style":370},[2921],{"type":46,"value":2922},"+",{"type":40,"tag":352,"props":2924,"children":2925},{"style":364},[2926],{"type":46,"value":1296},{"type":40,"tag":352,"props":2928,"children":2929},{"style":370},[2930],{"type":46,"value":603},{"type":40,"tag":352,"props":2932,"children":2933},{"style":381},[2934],{"type":46,"value":1323},{"type":40,"tag":352,"props":2936,"children":2937},{"style":611},[2938],{"type":46,"value":1328},{"type":40,"tag":352,"props":2940,"children":2941},{"style":370},[2942],{"type":46,"value":1333},{"type":40,"tag":352,"props":2944,"children":2945},{"style":1346},[2946],{"type":46,"value":2818},{"type":40,"tag":352,"props":2948,"children":2949},{"style":370},[2950],{"type":46,"value":429},{"type":40,"tag":352,"props":2952,"children":2953},{"class":354,"line":797},[2954,2959,2963,2967,2971,2975,2979,2983,2987,2991,2995,2999],{"type":40,"tag":352,"props":2955,"children":2956},{"style":381},[2957],{"type":46,"value":2958},"  setTimeout",{"type":40,"tag":352,"props":2960,"children":2961},{"style":611},[2962],{"type":46,"value":614},{"type":40,"tag":352,"props":2964,"children":2965},{"style":370},[2966],{"type":46,"value":424},{"type":40,"tag":352,"props":2968,"children":2969},{"style":358},[2970],{"type":46,"value":510},{"type":40,"tag":352,"props":2972,"children":2973},{"style":364},[2974],{"type":46,"value":2717},{"type":40,"tag":352,"props":2976,"children":2977},{"style":370},[2978],{"type":46,"value":603},{"type":40,"tag":352,"props":2980,"children":2981},{"style":381},[2982],{"type":46,"value":2726},{"type":40,"tag":352,"props":2984,"children":2985},{"style":611},[2986],{"type":46,"value":424},{"type":40,"tag":352,"props":2988,"children":2989},{"style":370},[2990],{"type":46,"value":400},{"type":40,"tag":352,"props":2992,"children":2993},{"style":364},[2994],{"type":46,"value":2860},{"type":40,"tag":352,"props":2996,"children":2997},{"style":611},[2998],{"type":46,"value":623},{"type":40,"tag":352,"props":3000,"children":3001},{"style":370},[3002],{"type":46,"value":429},{"type":40,"tag":352,"props":3004,"children":3005},{"class":354,"line":805},[3006,3010,3014],{"type":40,"tag":352,"props":3007,"children":3008},{"style":370},[3009],{"type":46,"value":980},{"type":40,"tag":352,"props":3011,"children":3012},{"style":364},[3013],{"type":46,"value":623},{"type":40,"tag":352,"props":3015,"children":3016},{"style":370},[3017],{"type":46,"value":429},{"type":40,"tag":49,"props":3019,"children":3020},{},[3021],{"type":46,"value":3022},"Without jitter, all clients reconnect at the same time after the same backoff duration. Jitter spreads them across a window so the server isn't overwhelmed.",{"type":40,"tag":329,"props":3024,"children":3026},{"id":3025},"write-through-instead-of-delete-on-invalidation",[3027],{"type":46,"value":3028},"Write-through instead of delete on invalidation",{"type":40,"tag":341,"props":3030,"children":3032},{"className":343,"code":3031,"language":345,"meta":346,"style":346},"\u002F\u002F BAD: delete causes a stampede on the next read\nasync function updateConfig(newConfig: Config) {\n  await db.save(newConfig);\n  await cache.delete(\"config\"); \u002F\u002F Every next request stampedes\n}\n\n\u002F\u002F GOOD: write the new value directly -- no miss, no stampede\nasync function updateConfig(newConfig: Config) {\n  await db.save(newConfig);\n  await cache.set(\"config\", JSON.stringify(newConfig), { EX: 300 });\n  \u002F\u002F Cache goes from old value → new value with no gap\n}\n",[3033],{"type":40,"tag":164,"props":3034,"children":3035},{"__ignoreMap":346},[3036,3044,3086,3123,3172,3179,3186,3194,3233,3268,3364,3372],{"type":40,"tag":352,"props":3037,"children":3038},{"class":354,"line":27},[3039],{"type":40,"tag":352,"props":3040,"children":3041},{"style":701},[3042],{"type":46,"value":3043},"\u002F\u002F BAD: delete causes a stampede on the next read\n",{"type":40,"tag":352,"props":3045,"children":3046},{"class":354,"line":432},[3047,3051,3055,3060,3064,3069,3073,3078,3082],{"type":40,"tag":352,"props":3048,"children":3049},{"style":358},[3050],{"type":46,"value":447},{"type":40,"tag":352,"props":3052,"children":3053},{"style":358},[3054],{"type":46,"value":452},{"type":40,"tag":352,"props":3056,"children":3057},{"style":381},[3058],{"type":46,"value":3059}," updateConfig",{"type":40,"tag":352,"props":3061,"children":3062},{"style":370},[3063],{"type":46,"value":614},{"type":40,"tag":352,"props":3065,"children":3066},{"style":474},[3067],{"type":46,"value":3068},"newConfig",{"type":40,"tag":352,"props":3070,"children":3071},{"style":370},[3072],{"type":46,"value":482},{"type":40,"tag":352,"props":3074,"children":3075},{"style":392},[3076],{"type":46,"value":3077}," Config",{"type":40,"tag":352,"props":3079,"children":3080},{"style":370},[3081],{"type":46,"value":623},{"type":40,"tag":352,"props":3083,"children":3084},{"style":370},[3085],{"type":46,"value":568},{"type":40,"tag":352,"props":3087,"children":3088},{"class":354,"line":23},[3089,3094,3098,3102,3107,3111,3115,3119],{"type":40,"tag":352,"props":3090,"children":3091},{"style":590},[3092],{"type":46,"value":3093},"  await",{"type":40,"tag":352,"props":3095,"children":3096},{"style":364},[3097],{"type":46,"value":2717},{"type":40,"tag":352,"props":3099,"children":3100},{"style":370},[3101],{"type":46,"value":603},{"type":40,"tag":352,"props":3103,"children":3104},{"style":381},[3105],{"type":46,"value":3106},"save",{"type":40,"tag":352,"props":3108,"children":3109},{"style":611},[3110],{"type":46,"value":614},{"type":40,"tag":352,"props":3112,"children":3113},{"style":364},[3114],{"type":46,"value":3068},{"type":40,"tag":352,"props":3116,"children":3117},{"style":611},[3118],{"type":46,"value":623},{"type":40,"tag":352,"props":3120,"children":3121},{"style":370},[3122],{"type":46,"value":429},{"type":40,"tag":352,"props":3124,"children":3125},{"class":354,"line":571},[3126,3130,3134,3138,3142,3146,3150,3155,3159,3163,3167],{"type":40,"tag":352,"props":3127,"children":3128},{"style":590},[3129],{"type":46,"value":3093},{"type":40,"tag":352,"props":3131,"children":3132},{"style":364},[3133],{"type":46,"value":598},{"type":40,"tag":352,"props":3135,"children":3136},{"style":370},[3137],{"type":46,"value":603},{"type":40,"tag":352,"props":3139,"children":3140},{"style":381},[3141],{"type":46,"value":1006},{"type":40,"tag":352,"props":3143,"children":3144},{"style":611},[3145],{"type":46,"value":614},{"type":40,"tag":352,"props":3147,"children":3148},{"style":370},[3149],{"type":46,"value":2456},{"type":40,"tag":352,"props":3151,"children":3152},{"style":2459},[3153],{"type":46,"value":3154},"config",{"type":40,"tag":352,"props":3156,"children":3157},{"style":370},[3158],{"type":46,"value":2456},{"type":40,"tag":352,"props":3160,"children":3161},{"style":611},[3162],{"type":46,"value":623},{"type":40,"tag":352,"props":3164,"children":3165},{"style":370},[3166],{"type":46,"value":1358},{"type":40,"tag":352,"props":3168,"children":3169},{"style":701},[3170],{"type":46,"value":3171}," \u002F\u002F Every next request stampedes\n",{"type":40,"tag":352,"props":3173,"children":3174},{"class":354,"line":630},[3175],{"type":40,"tag":352,"props":3176,"children":3177},{"style":370},[3178],{"type":46,"value":1233},{"type":40,"tag":352,"props":3180,"children":3181},{"class":354,"line":689},[3182],{"type":40,"tag":352,"props":3183,"children":3184},{"emptyLinePlaceholder":436},[3185],{"type":46,"value":439},{"type":40,"tag":352,"props":3187,"children":3188},{"class":354,"line":697},[3189],{"type":40,"tag":352,"props":3190,"children":3191},{"style":701},[3192],{"type":46,"value":3193},"\u002F\u002F GOOD: write the new value directly -- no miss, no stampede\n",{"type":40,"tag":352,"props":3195,"children":3196},{"class":354,"line":707},[3197,3201,3205,3209,3213,3217,3221,3225,3229],{"type":40,"tag":352,"props":3198,"children":3199},{"style":358},[3200],{"type":46,"value":447},{"type":40,"tag":352,"props":3202,"children":3203},{"style":358},[3204],{"type":46,"value":452},{"type":40,"tag":352,"props":3206,"children":3207},{"style":381},[3208],{"type":46,"value":3059},{"type":40,"tag":352,"props":3210,"children":3211},{"style":370},[3212],{"type":46,"value":614},{"type":40,"tag":352,"props":3214,"children":3215},{"style":474},[3216],{"type":46,"value":3068},{"type":40,"tag":352,"props":3218,"children":3219},{"style":370},[3220],{"type":46,"value":482},{"type":40,"tag":352,"props":3222,"children":3223},{"style":392},[3224],{"type":46,"value":3077},{"type":40,"tag":352,"props":3226,"children":3227},{"style":370},[3228],{"type":46,"value":623},{"type":40,"tag":352,"props":3230,"children":3231},{"style":370},[3232],{"type":46,"value":568},{"type":40,"tag":352,"props":3234,"children":3235},{"class":354,"line":797},[3236,3240,3244,3248,3252,3256,3260,3264],{"type":40,"tag":352,"props":3237,"children":3238},{"style":590},[3239],{"type":46,"value":3093},{"type":40,"tag":352,"props":3241,"children":3242},{"style":364},[3243],{"type":46,"value":2717},{"type":40,"tag":352,"props":3245,"children":3246},{"style":370},[3247],{"type":46,"value":603},{"type":40,"tag":352,"props":3249,"children":3250},{"style":381},[3251],{"type":46,"value":3106},{"type":40,"tag":352,"props":3253,"children":3254},{"style":611},[3255],{"type":46,"value":614},{"type":40,"tag":352,"props":3257,"children":3258},{"style":364},[3259],{"type":46,"value":3068},{"type":40,"tag":352,"props":3261,"children":3262},{"style":611},[3263],{"type":46,"value":623},{"type":40,"tag":352,"props":3265,"children":3266},{"style":370},[3267],{"type":46,"value":429},{"type":40,"tag":352,"props":3269,"children":3270},{"class":354,"line":805},[3271,3275,3279,3283,3287,3291,3295,3299,3303,3307,3311,3315,3319,3323,3327,3331,3335,3339,3343,3347,3352,3356,3360],{"type":40,"tag":352,"props":3272,"children":3273},{"style":590},[3274],{"type":46,"value":3093},{"type":40,"tag":352,"props":3276,"children":3277},{"style":364},[3278],{"type":46,"value":598},{"type":40,"tag":352,"props":3280,"children":3281},{"style":370},[3282],{"type":46,"value":603},{"type":40,"tag":352,"props":3284,"children":3285},{"style":381},[3286],{"type":46,"value":893},{"type":40,"tag":352,"props":3288,"children":3289},{"style":611},[3290],{"type":46,"value":614},{"type":40,"tag":352,"props":3292,"children":3293},{"style":370},[3294],{"type":46,"value":2456},{"type":40,"tag":352,"props":3296,"children":3297},{"style":2459},[3298],{"type":46,"value":3154},{"type":40,"tag":352,"props":3300,"children":3301},{"style":370},[3302],{"type":46,"value":2456},{"type":40,"tag":352,"props":3304,"children":3305},{"style":370},[3306],{"type":46,"value":400},{"type":40,"tag":352,"props":3308,"children":3309},{"style":364},[3310],{"type":46,"value":661},{"type":40,"tag":352,"props":3312,"children":3313},{"style":370},[3314],{"type":46,"value":603},{"type":40,"tag":352,"props":3316,"children":3317},{"style":381},[3318],{"type":46,"value":918},{"type":40,"tag":352,"props":3320,"children":3321},{"style":611},[3322],{"type":46,"value":614},{"type":40,"tag":352,"props":3324,"children":3325},{"style":364},[3326],{"type":46,"value":3068},{"type":40,"tag":352,"props":3328,"children":3329},{"style":611},[3330],{"type":46,"value":623},{"type":40,"tag":352,"props":3332,"children":3333},{"style":370},[3334],{"type":46,"value":400},{"type":40,"tag":352,"props":3336,"children":3337},{"style":370},[3338],{"type":46,"value":939},{"type":40,"tag":352,"props":3340,"children":3341},{"style":611},[3342],{"type":46,"value":944},{"type":40,"tag":352,"props":3344,"children":3345},{"style":370},[3346],{"type":46,"value":482},{"type":40,"tag":352,"props":3348,"children":3349},{"style":1346},[3350],{"type":46,"value":3351}," 300",{"type":40,"tag":352,"props":3353,"children":3354},{"style":370},[3355],{"type":46,"value":2389},{"type":40,"tag":352,"props":3357,"children":3358},{"style":611},[3359],{"type":46,"value":623},{"type":40,"tag":352,"props":3361,"children":3362},{"style":370},[3363],{"type":46,"value":429},{"type":40,"tag":352,"props":3365,"children":3366},{"class":354,"line":831},[3367],{"type":40,"tag":352,"props":3368,"children":3369},{"style":701},[3370],{"type":46,"value":3371},"  \u002F\u002F Cache goes from old value → new value with no gap\n",{"type":40,"tag":352,"props":3373,"children":3374},{"class":354,"line":874},[3375],{"type":40,"tag":352,"props":3376,"children":3377},{"style":370},[3378],{"type":46,"value":1233},{"type":40,"tag":61,"props":3380,"children":3382},{"id":3381},"anti-patterns",[3383],{"type":46,"value":3384},"Anti-Patterns",{"type":40,"tag":341,"props":3386,"children":3388},{"className":343,"code":3387,"language":345,"meta":346,"style":346},"\u002F\u002F Naive cache-aside: 1,000 concurrent misses = 1,000 identical DB queries\nconst cached = await cache.get(key);\nif (!cached) { data = await db.query(key); await cache.set(key, data, { ttl: 300 }); }\n\n\u002F\u002F Fixed TTL, no jitter: all keys populated at deploy expire at the same time\ncache.set(key, data, { ttl: 300 }); \u002F\u002F Every key gets exactly 300s\n\n\u002F\u002F Cron at round time: 50 instances all fire at :00\ncron.schedule(\"0 * * * *\", expensiveJob);\n\n\u002F\u002F Cache clear in hot path: nuclear stampede\nawait cache.clear(); \u002F\u002F Everything stampedes at once\n\n\u002F\u002F Reconnect without backoff: connection storm\ndb.on(\"error\", () => db.connect());\n",[3389],{"type":40,"tag":164,"props":3390,"children":3391},{"__ignoreMap":346},[3392,3400,3441,3577,3584,3592,3658,3665,3673,3717,3724,3732,3766,3773,3781],{"type":40,"tag":352,"props":3393,"children":3394},{"class":354,"line":27},[3395],{"type":40,"tag":352,"props":3396,"children":3397},{"style":701},[3398],{"type":46,"value":3399},"\u002F\u002F Naive cache-aside: 1,000 concurrent misses = 1,000 identical DB queries\n",{"type":40,"tag":352,"props":3401,"children":3402},{"class":354,"line":432},[3403,3407,3412,3416,3420,3424,3428,3432,3437],{"type":40,"tag":352,"props":3404,"children":3405},{"style":358},[3406],{"type":46,"value":361},{"type":40,"tag":352,"props":3408,"children":3409},{"style":364},[3410],{"type":46,"value":3411}," cached ",{"type":40,"tag":352,"props":3413,"children":3414},{"style":370},[3415],{"type":46,"value":373},{"type":40,"tag":352,"props":3417,"children":3418},{"style":590},[3419],{"type":46,"value":593},{"type":40,"tag":352,"props":3421,"children":3422},{"style":364},[3423],{"type":46,"value":598},{"type":40,"tag":352,"props":3425,"children":3426},{"style":370},[3427],{"type":46,"value":603},{"type":40,"tag":352,"props":3429,"children":3430},{"style":381},[3431],{"type":46,"value":608},{"type":40,"tag":352,"props":3433,"children":3434},{"style":364},[3435],{"type":46,"value":3436},"(key)",{"type":40,"tag":352,"props":3438,"children":3439},{"style":370},[3440],{"type":46,"value":429},{"type":40,"tag":352,"props":3442,"children":3443},{"class":354,"line":23},[3444,3449,3453,3457,3462,3467,3471,3475,3479,3483,3487,3492,3496,3500,3504,3508,3512,3516,3520,3524,3528,3532,3536,3540,3544,3548,3552,3556,3560,3564,3568,3572],{"type":40,"tag":352,"props":3445,"children":3446},{"style":590},[3447],{"type":46,"value":3448},"if",{"type":40,"tag":352,"props":3450,"children":3451},{"style":364},[3452],{"type":46,"value":641},{"type":40,"tag":352,"props":3454,"children":3455},{"style":370},[3456],{"type":46,"value":1772},{"type":40,"tag":352,"props":3458,"children":3459},{"style":364},[3460],{"type":46,"value":3461},"cached) ",{"type":40,"tag":352,"props":3463,"children":3464},{"style":370},[3465],{"type":46,"value":3466},"{",{"type":40,"tag":352,"props":3468,"children":3469},{"style":364},[3470],{"type":46,"value":1036},{"type":40,"tag":352,"props":3472,"children":3473},{"style":370},[3474],{"type":46,"value":587},{"type":40,"tag":352,"props":3476,"children":3477},{"style":590},[3478],{"type":46,"value":593},{"type":40,"tag":352,"props":3480,"children":3481},{"style":364},[3482],{"type":46,"value":2717},{"type":40,"tag":352,"props":3484,"children":3485},{"style":370},[3486],{"type":46,"value":603},{"type":40,"tag":352,"props":3488,"children":3489},{"style":381},[3490],{"type":46,"value":3491},"query",{"type":40,"tag":352,"props":3493,"children":3494},{"style":611},[3495],{"type":46,"value":614},{"type":40,"tag":352,"props":3497,"children":3498},{"style":364},[3499],{"type":46,"value":477},{"type":40,"tag":352,"props":3501,"children":3502},{"style":611},[3503],{"type":46,"value":623},{"type":40,"tag":352,"props":3505,"children":3506},{"style":370},[3507],{"type":46,"value":1358},{"type":40,"tag":352,"props":3509,"children":3510},{"style":590},[3511],{"type":46,"value":593},{"type":40,"tag":352,"props":3513,"children":3514},{"style":364},[3515],{"type":46,"value":598},{"type":40,"tag":352,"props":3517,"children":3518},{"style":370},[3519],{"type":46,"value":603},{"type":40,"tag":352,"props":3521,"children":3522},{"style":381},[3523],{"type":46,"value":893},{"type":40,"tag":352,"props":3525,"children":3526},{"style":611},[3527],{"type":46,"value":614},{"type":40,"tag":352,"props":3529,"children":3530},{"style":364},[3531],{"type":46,"value":477},{"type":40,"tag":352,"props":3533,"children":3534},{"style":370},[3535],{"type":46,"value":400},{"type":40,"tag":352,"props":3537,"children":3538},{"style":364},[3539],{"type":46,"value":1036},{"type":40,"tag":352,"props":3541,"children":3542},{"style":370},[3543],{"type":46,"value":400},{"type":40,"tag":352,"props":3545,"children":3546},{"style":370},[3547],{"type":46,"value":939},{"type":40,"tag":352,"props":3549,"children":3550},{"style":611},[3551],{"type":46,"value":2241},{"type":40,"tag":352,"props":3553,"children":3554},{"style":370},[3555],{"type":46,"value":482},{"type":40,"tag":352,"props":3557,"children":3558},{"style":1346},[3559],{"type":46,"value":3351},{"type":40,"tag":352,"props":3561,"children":3562},{"style":370},[3563],{"type":46,"value":2389},{"type":40,"tag":352,"props":3565,"children":3566},{"style":611},[3567],{"type":46,"value":623},{"type":40,"tag":352,"props":3569,"children":3570},{"style":370},[3571],{"type":46,"value":1358},{"type":40,"tag":352,"props":3573,"children":3574},{"style":370},[3575],{"type":46,"value":3576}," }\n",{"type":40,"tag":352,"props":3578,"children":3579},{"class":354,"line":571},[3580],{"type":40,"tag":352,"props":3581,"children":3582},{"emptyLinePlaceholder":436},[3583],{"type":46,"value":439},{"type":40,"tag":352,"props":3585,"children":3586},{"class":354,"line":630},[3587],{"type":40,"tag":352,"props":3588,"children":3589},{"style":701},[3590],{"type":46,"value":3591},"\u002F\u002F Fixed TTL, no jitter: all keys populated at deploy expire at the same time\n",{"type":40,"tag":352,"props":3593,"children":3594},{"class":354,"line":689},[3595,3600,3604,3608,3613,3617,3621,3625,3629,3633,3637,3641,3645,3649,3653],{"type":40,"tag":352,"props":3596,"children":3597},{"style":364},[3598],{"type":46,"value":3599},"cache",{"type":40,"tag":352,"props":3601,"children":3602},{"style":370},[3603],{"type":46,"value":603},{"type":40,"tag":352,"props":3605,"children":3606},{"style":381},[3607],{"type":46,"value":893},{"type":40,"tag":352,"props":3609,"children":3610},{"style":364},[3611],{"type":46,"value":3612},"(key",{"type":40,"tag":352,"props":3614,"children":3615},{"style":370},[3616],{"type":46,"value":400},{"type":40,"tag":352,"props":3618,"children":3619},{"style":364},[3620],{"type":46,"value":1036},{"type":40,"tag":352,"props":3622,"children":3623},{"style":370},[3624],{"type":46,"value":400},{"type":40,"tag":352,"props":3626,"children":3627},{"style":370},[3628],{"type":46,"value":939},{"type":40,"tag":352,"props":3630,"children":3631},{"style":611},[3632],{"type":46,"value":2241},{"type":40,"tag":352,"props":3634,"children":3635},{"style":370},[3636],{"type":46,"value":482},{"type":40,"tag":352,"props":3638,"children":3639},{"style":1346},[3640],{"type":46,"value":3351},{"type":40,"tag":352,"props":3642,"children":3643},{"style":370},[3644],{"type":46,"value":2389},{"type":40,"tag":352,"props":3646,"children":3647},{"style":364},[3648],{"type":46,"value":623},{"type":40,"tag":352,"props":3650,"children":3651},{"style":370},[3652],{"type":46,"value":1358},{"type":40,"tag":352,"props":3654,"children":3655},{"style":701},[3656],{"type":46,"value":3657}," \u002F\u002F Every key gets exactly 300s\n",{"type":40,"tag":352,"props":3659,"children":3660},{"class":354,"line":697},[3661],{"type":40,"tag":352,"props":3662,"children":3663},{"emptyLinePlaceholder":436},[3664],{"type":46,"value":439},{"type":40,"tag":352,"props":3666,"children":3667},{"class":354,"line":707},[3668],{"type":40,"tag":352,"props":3669,"children":3670},{"style":701},[3671],{"type":46,"value":3672},"\u002F\u002F Cron at round time: 50 instances all fire at :00\n",{"type":40,"tag":352,"props":3674,"children":3675},{"class":354,"line":797},[3676,3680,3684,3688,3692,3696,3700,3704,3708,3713],{"type":40,"tag":352,"props":3677,"children":3678},{"style":364},[3679],{"type":46,"value":2438},{"type":40,"tag":352,"props":3681,"children":3682},{"style":370},[3683],{"type":46,"value":603},{"type":40,"tag":352,"props":3685,"children":3686},{"style":381},[3687],{"type":46,"value":2447},{"type":40,"tag":352,"props":3689,"children":3690},{"style":364},[3691],{"type":46,"value":614},{"type":40,"tag":352,"props":3693,"children":3694},{"style":370},[3695],{"type":46,"value":2456},{"type":40,"tag":352,"props":3697,"children":3698},{"style":2459},[3699],{"type":46,"value":169},{"type":40,"tag":352,"props":3701,"children":3702},{"style":370},[3703],{"type":46,"value":2456},{"type":40,"tag":352,"props":3705,"children":3706},{"style":370},[3707],{"type":46,"value":400},{"type":40,"tag":352,"props":3709,"children":3710},{"style":364},[3711],{"type":46,"value":3712}," expensiveJob)",{"type":40,"tag":352,"props":3714,"children":3715},{"style":370},[3716],{"type":46,"value":429},{"type":40,"tag":352,"props":3718,"children":3719},{"class":354,"line":805},[3720],{"type":40,"tag":352,"props":3721,"children":3722},{"emptyLinePlaceholder":436},[3723],{"type":46,"value":439},{"type":40,"tag":352,"props":3725,"children":3726},{"class":354,"line":831},[3727],{"type":40,"tag":352,"props":3728,"children":3729},{"style":701},[3730],{"type":46,"value":3731},"\u002F\u002F Cache clear in hot path: nuclear stampede\n",{"type":40,"tag":352,"props":3733,"children":3734},{"class":354,"line":874},[3735,3740,3744,3748,3753,3757,3761],{"type":40,"tag":352,"props":3736,"children":3737},{"style":590},[3738],{"type":46,"value":3739},"await",{"type":40,"tag":352,"props":3741,"children":3742},{"style":364},[3743],{"type":46,"value":598},{"type":40,"tag":352,"props":3745,"children":3746},{"style":370},[3747],{"type":46,"value":603},{"type":40,"tag":352,"props":3749,"children":3750},{"style":381},[3751],{"type":46,"value":3752},"clear",{"type":40,"tag":352,"props":3754,"children":3755},{"style":364},[3756],{"type":46,"value":424},{"type":40,"tag":352,"props":3758,"children":3759},{"style":370},[3760],{"type":46,"value":1358},{"type":40,"tag":352,"props":3762,"children":3763},{"style":701},[3764],{"type":46,"value":3765}," \u002F\u002F Everything stampedes at once\n",{"type":40,"tag":352,"props":3767,"children":3768},{"class":354,"line":991},[3769],{"type":40,"tag":352,"props":3770,"children":3771},{"emptyLinePlaceholder":436},[3772],{"type":46,"value":439},{"type":40,"tag":352,"props":3774,"children":3775},{"class":354,"line":1025},[3776],{"type":40,"tag":352,"props":3777,"children":3778},{"style":701},[3779],{"type":46,"value":3780},"\u002F\u002F Reconnect without backoff: connection storm\n",{"type":40,"tag":352,"props":3782,"children":3783},{"class":354,"line":1043},[3784,3788,3792,3796,3800,3804,3808,3812,3816,3820,3824,3828,3832,3836,3840],{"type":40,"tag":352,"props":3785,"children":3786},{"style":364},[3787],{"type":46,"value":2674},{"type":40,"tag":352,"props":3789,"children":3790},{"style":370},[3791],{"type":46,"value":603},{"type":40,"tag":352,"props":3793,"children":3794},{"style":381},[3795],{"type":46,"value":2683},{"type":40,"tag":352,"props":3797,"children":3798},{"style":364},[3799],{"type":46,"value":614},{"type":40,"tag":352,"props":3801,"children":3802},{"style":370},[3803],{"type":46,"value":2456},{"type":40,"tag":352,"props":3805,"children":3806},{"style":2459},[3807],{"type":46,"value":2696},{"type":40,"tag":352,"props":3809,"children":3810},{"style":370},[3811],{"type":46,"value":2456},{"type":40,"tag":352,"props":3813,"children":3814},{"style":370},[3815],{"type":46,"value":400},{"type":40,"tag":352,"props":3817,"children":3818},{"style":370},[3819],{"type":46,"value":505},{"type":40,"tag":352,"props":3821,"children":3822},{"style":358},[3823],{"type":46,"value":510},{"type":40,"tag":352,"props":3825,"children":3826},{"style":364},[3827],{"type":46,"value":2717},{"type":40,"tag":352,"props":3829,"children":3830},{"style":370},[3831],{"type":46,"value":603},{"type":40,"tag":352,"props":3833,"children":3834},{"style":381},[3835],{"type":46,"value":2726},{"type":40,"tag":352,"props":3837,"children":3838},{"style":364},[3839],{"type":46,"value":2731},{"type":40,"tag":352,"props":3841,"children":3842},{"style":370},[3843],{"type":46,"value":429},{"type":40,"tag":61,"props":3845,"children":3847},{"id":3846},"related-traps",[3848],{"type":46,"value":3849},"Related Traps",{"type":40,"tag":3851,"props":3852,"children":3853},"ul",{},[3854,3864,3874,3884],{"type":40,"tag":212,"props":3855,"children":3856},{},[3857,3862],{"type":40,"tag":55,"props":3858,"children":3859},{},[3860],{"type":46,"value":3861},"Cache Invalidation",{"type":46,"value":3863}," -- cache stampede is the thundering herd problem applied to caching. Stale-while-revalidate and write-through invalidation prevent the gap where the cache is empty.",{"type":40,"tag":212,"props":3865,"children":3866},{},[3867,3872],{"type":40,"tag":55,"props":3868,"children":3869},{},[3870],{"type":46,"value":3871},"Retry Storms",{"type":46,"value":3873}," -- retries are a specific form of thundering herd. When a service is slow and clients retry, the retries themselves create a herd on the struggling service. Exponential backoff with jitter is the same fix in both contexts.",{"type":40,"tag":212,"props":3875,"children":3876},{},[3877,3882],{"type":40,"tag":55,"props":3878,"children":3879},{},[3880],{"type":46,"value":3881},"Hot Partitions",{"type":46,"value":3883}," -- a thundering herd on a partitioned system overwhelms whichever partition holds the hot key. The two traps compound.",{"type":40,"tag":212,"props":3885,"children":3886},{},[3887,3892],{"type":40,"tag":55,"props":3888,"children":3889},{},[3890],{"type":46,"value":3891},"Backpressure",{"type":46,"value":3893}," -- a cache stampede is a sudden loss of backpressure. The cache was absorbing load; when it expires, the full load hits the backend with no buffering.",{"type":40,"tag":3895,"props":3896,"children":3897},"style",{},[3898],{"type":46,"value":3899},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"items":3901,"total":1286},[3902,3921,3934,3944,3961,3976,3990,4007,4020,4032,4043,4052],{"slug":3903,"name":3903,"fn":3904,"description":3905,"org":3906,"tags":3907,"stars":3918,"repoUrl":3919,"updatedAt":3920},"trigger-authoring-chat-agent","author durable AI chat agents with Trigger.dev","Author and run a durable AI chat agent with chat.agent from @trigger.dev\u002Fsdk\u002Fai: the per-turn run loop, why you MUST spread ...chat.toStreamTextOptions() first, returning a StreamTextResult vs calling chat.pipe(), the two server actions (chat.createStartSessionAction + auth.createPublicToken), and wiring useChat to useTriggerChatTransport. Load this when building, modifying, or debugging a chat backend (the agent task or its lifecycle hooks) or its React transport, when declaring typed tools or custom data parts, or when migrating a plain AI SDK streamText route to chat.agent.\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3908,3911,3914,3915],{"name":3909,"slug":3910,"type":16},"Agents","agents",{"name":3912,"slug":3913,"type":16},"SDK","sdk",{"name":9,"slug":8,"type":16},{"name":3916,"slug":3917,"type":16},"Workflow Automation","workflow-automation",14401,"https:\u002F\u002Fgithub.com\u002Ftriggerdotdev\u002Ftrigger.dev","2026-07-02T17:12:52.307135",{"slug":3922,"name":3922,"fn":3923,"description":3924,"org":3925,"tags":3926,"stars":3918,"repoUrl":3919,"updatedAt":3933},"trigger-authoring-tasks","author backend Trigger.dev tasks","Covers writing backend Trigger.dev tasks with @trigger.dev\u002Fsdk: defining task() and schemaTask(), the run function and its ctx, retries, waits, queues and concurrency, idempotency keys, run metadata, logging, triggering other tasks (and the Result shape), scheduled\u002Fcron tasks, and the essentials of trigger.config.ts. Load this whenever you are authoring or editing code inside a \u002Ftrigger directory, defining a task, or writing backend code that triggers tasks. Realtime\u002FReact hooks and AI chat are covered by separate skills.\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3927,3930,3931,3932],{"name":3928,"slug":3929,"type":16},"Backend","backend",{"name":3912,"slug":3913,"type":16},{"name":9,"slug":8,"type":16},{"name":3916,"slug":3917,"type":16},"2026-07-02T17:12:48.396964",{"slug":3935,"name":3935,"fn":3936,"description":3937,"org":3938,"tags":3939,"stars":3918,"repoUrl":3919,"updatedAt":3943},"trigger-chat-agent-advanced","manage Trigger.dev chat sessions and transports","Advanced and operational chat.agent capabilities for Trigger.dev, loaded on demand. Load this when working on the raw Sessions primitive (sessions \u002F SessionHandle), a custom chat transport or the realtime wire protocol, durable sub-agents (AgentChat, chat.stream.writer), human-in-the-loop, steering, actions, background injection (chat.defer \u002F chat.inject), fast starts (preload, Head Start via @trigger.dev\u002Fsdk\u002Fchat-server), context resilience (compaction, recovery boot, OOM, large payloads), chat.local run-scoped state, offline testing with mockChatAgent, or prerelease\u002Fversion upgrades. For the everyday chat.agent({...}) definition and the useTriggerChatTransport happy path, use the trigger-authoring-chat-agent skill instead.\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3940,3941,3942],{"name":3909,"slug":3910,"type":16},{"name":9,"slug":8,"type":16},{"name":3916,"slug":3917,"type":16},"2026-07-02T17:12:51.03018",{"slug":3945,"name":3945,"fn":3946,"description":3947,"org":3948,"tags":3949,"stars":3918,"repoUrl":3919,"updatedAt":3960},"trigger-realtime-and-frontend","subscribe to Trigger.dev runs in realtime","Trigger.dev client\u002Ffrontend surface: subscribe to runs in realtime (runs.subscribeToRun and the @trigger.dev\u002Freact-hooks hook useRealtimeRun), consume metadata and AI\u002Ftext streams in React (useRealtimeStream), trigger tasks from the browser (useTaskTrigger, useRealtimeTaskTrigger), and mint scoped frontend credentials with auth.createPublicToken \u002F auth.createTriggerPublicToken. Load when wiring a frontend (React\u002FNext.js\u002FRemix) or backend-for-frontend to show live run progress, status badges, token streams, trigger buttons, or wait-token approval UIs. NOT for writing the backend task itself (streams.define \u002F metadata.set is trigger-authoring-tasks territory); this is the consumer side.\n",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3950,3953,3956,3959],{"name":3951,"slug":3952,"type":16},"Frontend","frontend",{"name":3954,"slug":3955,"type":16},"React","react",{"name":3957,"slug":3958,"type":16},"Real-time","real-time",{"name":9,"slug":8,"type":16},"2026-07-02T17:12:49.717706",{"slug":3962,"name":3962,"fn":3963,"description":3964,"org":3965,"tags":3966,"stars":3973,"repoUrl":3974,"updatedAt":3975},"trigger-agents","orchestrate AI agents with Trigger.dev","AI agent patterns with Trigger.dev - orchestration, parallelization, routing, evaluator-optimizer, and human-in-the-loop. Use when building LLM-powered tasks that need parallel workers, approval gates, tool calling, or multi-step agent workflows.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3967,3968,3971,3972],{"name":3909,"slug":3910,"type":16},{"name":3969,"slug":3970,"type":16},"Multi-Agent","multi-agent",{"name":9,"slug":8,"type":16},{"name":3916,"slug":3917,"type":16},30,"https:\u002F\u002Fgithub.com\u002Ftriggerdotdev\u002Fskills","2026-04-06T18:54:46.023553",{"slug":3977,"name":3977,"fn":3978,"description":3979,"org":3980,"tags":3981,"stars":3973,"repoUrl":3974,"updatedAt":3989},"trigger-config","configure Trigger.dev project settings","Configure Trigger.dev projects with trigger.config.ts. Use when setting up build extensions for Prisma, Playwright, FFmpeg, Python, or customizing deployment settings.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3982,3985,3988],{"name":3983,"slug":3984,"type":16},"Configuration","configuration",{"name":3986,"slug":3987,"type":16},"Deployment","deployment",{"name":9,"slug":8,"type":16},"2026-04-06T18:54:44.764258",{"slug":3991,"name":3991,"fn":3992,"description":3993,"org":3994,"tags":3995,"stars":3973,"repoUrl":3974,"updatedAt":4006},"trigger-cost-savings","optimize Trigger.dev task costs","Analyze Trigger.dev tasks, schedules, and runs for cost optimization opportunities. Use when asked to reduce spend, optimize costs, audit usage, right-size machines, or review task efficiency. Requires Trigger.dev MCP tools for run analysis.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[3996,3999,4002,4005],{"name":3997,"slug":3998,"type":16},"Analytics","analytics",{"name":4000,"slug":4001,"type":16},"Cost Optimization","cost-optimization",{"name":4003,"slug":4004,"type":16},"Operations","operations",{"name":9,"slug":8,"type":16},"2026-04-06T18:54:48.555552",{"slug":4008,"name":4008,"fn":4009,"description":4010,"org":4011,"tags":4012,"stars":3973,"repoUrl":3974,"updatedAt":4019},"trigger-realtime","monitor Trigger.dev tasks in real-time","Subscribe to Trigger.dev task runs in real-time from frontend and backend. Use when building progress indicators, live dashboards, streaming AI\u002FLLM responses, or React components that display task status.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4013,4014,4017,4018],{"name":3951,"slug":3952,"type":16},{"name":4015,"slug":4016,"type":16},"Observability","observability",{"name":3957,"slug":3958,"type":16},{"name":9,"slug":8,"type":16},"2026-04-06T18:54:47.293822",{"slug":4021,"name":4021,"fn":4022,"description":4023,"org":4024,"tags":4025,"stars":3973,"repoUrl":3974,"updatedAt":4031},"trigger-setup","set up Trigger.dev in projects","Set up Trigger.dev in your project. Use when adding Trigger.dev for the first time, creating trigger.config.ts, or initializing the trigger directory.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4026,4027,4030],{"name":3983,"slug":3984,"type":16},{"name":4028,"slug":4029,"type":16},"Local Development","local-development",{"name":9,"slug":8,"type":16},"2026-04-06T18:54:42.280816",{"slug":4033,"name":4033,"fn":4034,"description":4035,"org":4036,"tags":4037,"stars":3973,"repoUrl":3974,"updatedAt":4042},"trigger-tasks","build durable background tasks with Trigger.dev","Build AI agents, workflows and durable background tasks with Trigger.dev. Use when creating tasks, triggering jobs, handling retries, scheduling cron jobs, or implementing queues and concurrency control.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4038,4039,4040,4041],{"name":3909,"slug":3910,"type":16},{"name":3928,"slug":3929,"type":16},{"name":9,"slug":8,"type":16},{"name":3916,"slug":3917,"type":16},"2026-04-06T18:54:43.514369",{"slug":4044,"name":4044,"fn":4045,"description":4046,"org":4047,"tags":4048,"stars":23,"repoUrl":24,"updatedAt":4051},"staff-engineering-skills-backpressure","implement backpressure in streaming pipelines","Prevent unbounded resource growth when producers outpace consumers. Use when writing producer-consumer code, queue processing, batch operations, fan-out patterns, streaming pipelines, or any code where work is generated faster than it can be processed. Activates on patterns like Promise.all on dynamic-sized arrays, unbounded in-memory queues, producer loops without depth checks, fire-and-forget async calls in loops, or any buffer between a producer and consumer without a size limit.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4049,4050],{"name":18,"slug":19,"type":16},{"name":14,"slug":15,"type":16},"2026-06-17T08:40:42.723559",{"slug":4053,"name":4053,"fn":4054,"description":4055,"org":4056,"tags":4057,"stars":23,"repoUrl":24,"updatedAt":4064},"staff-engineering-skills-cache-invalidation","implement cache invalidation strategies","Prevent stale data bugs caused by missing or incomplete cache invalidation. Use when adding caching layers (Redis, in-memory Map, CDN, HTTP cache headers), optimizing read performance, or reviewing code that caches query results, API responses, or computed values. Activates on patterns like cache.set without cache.delete on write paths, unbounded Map\u002Fobject caches, TTL-only invalidation on security-sensitive data, Cache-Control headers on mutable content, or any caching where the write path doesn't invalidate the read cache.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4058,4059,4060,4063],{"name":18,"slug":19,"type":16},{"name":21,"slug":22,"type":16},{"name":4061,"slug":4062,"type":16},"Engineering","engineering",{"name":14,"slug":15,"type":16},"2026-06-17T08:40:45.194583",{"items":4066,"total":1057},[4067,4072,4079,4089,4101,4114,4126],{"slug":4044,"name":4044,"fn":4045,"description":4046,"org":4068,"tags":4069,"stars":23,"repoUrl":24,"updatedAt":4051},{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4070,4071],{"name":18,"slug":19,"type":16},{"name":14,"slug":15,"type":16},{"slug":4053,"name":4053,"fn":4054,"description":4055,"org":4073,"tags":4074,"stars":23,"repoUrl":24,"updatedAt":4064},{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4075,4076,4077,4078],{"name":18,"slug":19,"type":16},{"name":21,"slug":22,"type":16},{"name":4061,"slug":4062,"type":16},{"name":14,"slug":15,"type":16},{"slug":4080,"name":4080,"fn":4081,"description":4082,"org":4083,"tags":4084,"stars":23,"repoUrl":24,"updatedAt":4088},"staff-engineering-skills-cardinality","prevent cardinality traps in systems code","Detect and prevent cardinality traps in systems code. Use when writing code that iterates over collections, stores items in memory, creates per-entity resources, or fans out operations across a set of items. Activates on patterns like loops over query results, in-memory Maps\u002FSets populated from databases, or \"one X per Y\" resource allocation.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4085,4086,4087],{"name":18,"slug":19,"type":16},{"name":4061,"slug":4062,"type":16},{"name":14,"slug":15,"type":16},"2026-06-17T08:40:40.264608",{"slug":4090,"name":4090,"fn":4091,"description":4092,"org":4093,"tags":4094,"stars":23,"repoUrl":24,"updatedAt":4100},"staff-engineering-skills-clock-skew","prevent clock skew bugs in distributed systems","Prevent bugs caused by assuming clocks are synchronized or monotonic. Use when writing code that compares timestamps across machines, measures durations, sets lock expiry, orders distributed events, deduplicates by time window, or uses Date.now() for anything other than logging or display. Activates on patterns like Date.now() used for duration measurement, absolute timestamp expiry shared across machines, wall clock timestamps for event ordering, last-write-wins conflict resolution with timestamps, or timeout calculations using wall clock time.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4095,4096,4099],{"name":18,"slug":19,"type":16},{"name":4097,"slug":4098,"type":16},"Debugging","debugging",{"name":4061,"slug":4062,"type":16},"2026-06-17T08:40:37.803541",{"slug":4102,"name":4102,"fn":4103,"description":4104,"org":4105,"tags":4106,"stars":23,"repoUrl":24,"updatedAt":4113},"staff-engineering-skills-consistency-models","manage consistency models in distributed systems","Prevent stale read bugs caused by replica lag, cache staleness, and index delay. Use when writing code that reads data after writing it, uses read replicas, caches query results, reads from search indexes, or builds event-driven read models. Activates on patterns like create-then-redirect, update-then-read, cache-aside with TTL, search-after-create, or any write followed by a read that might hit a different data source.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4107,4108,4111,4112],{"name":18,"slug":19,"type":16},{"name":4109,"slug":4110,"type":16},"Database","database",{"name":4061,"slug":4062,"type":16},{"name":14,"slug":15,"type":16},"2026-06-17T08:40:46.442182",{"slug":4115,"name":4115,"fn":4116,"description":4117,"org":4118,"tags":4119,"stars":23,"repoUrl":24,"updatedAt":4125},"staff-engineering-skills-denormalization","prevent data model denormalization traps","Detect and prevent denormalization traps when designing data models. Use when writing code that copies fields between tables, embeds related data in documents, caches composed objects, or adds redundant columns to avoid joins. Activates on patterns like storing derived\u002Fcopied data, syncing fields across tables, or embedding nested objects in document stores.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4120,4121,4124],{"name":18,"slug":19,"type":16},{"name":4122,"slug":4123,"type":16},"Data Modeling","data-modeling",{"name":4109,"slug":4110,"type":16},"2026-06-17T08:40:53.835868",{"slug":4127,"name":4127,"fn":4128,"description":4129,"org":4130,"tags":4131,"stars":23,"repoUrl":24,"updatedAt":4137},"staff-engineering-skills-distributed-system-fallacies","design resilient distributed systems","Prevent failures caused by treating network calls like function calls. Use when writing code that calls external services, splits a monolith into microservices, chains multiple API calls, orchestrates distributed workflows, or handles requests that depend on other services. Activates on patterns like sequential service calls without timeouts, fetch\u002Faxios calls without error handling, multi-service writes without compensation, hardcoded service URLs, or any code that assumes the network is reliable, fast, or free.",{"slug":8,"name":9,"logoUrl":10,"githubOrg":11},[4132,4135,4136],{"name":4133,"slug":4134,"type":16},"API Development","api-development",{"name":18,"slug":19,"type":16},{"name":4061,"slug":4062,"type":16},"2026-06-17T08:40:47.666795"]