Caching, Queues, and Background Work
Add a cache or background work only for a problem you have actually seen. Decide how stale data can be, who may see it, how many retries are allowed, and how you will watch it.
Content date: October 15, 2026.
Plain English
A cache reuses an earlier result. It only helps if that result is still right for this person and this task. A queue separates accepting work from doing it, so accepted work is not finished work. Both add moving parts that can fail in new ways. Start from a slowdown you measured or a job that truly must run in the background, and do not treat one more service as automatic progress.
Use this when
Use this when the same expensive read repeats and users feel the delay, or when slow work such as sending receipts must survive a crash.
If you are just starting
Decide how stale each piece of data may be and for how long. Check permissions, ownership, and paid access fresh when they matter, never from a cache. Before caching anything private, decide the cache key (the label each saved result is stored under) and how entries are cleared. For background jobs, save the pending state in the database with the original action, and show users an honest "processing" status. Use the free local practice files first, since they show the failure cases without a paid service.
When your project grows
Pick the cache's scope, a size limit and eviction rule, versioned keys, how freshness is rechecked, and what happens on a miss or outage. HTTP caching (browsers and CDNs) and your own application cache are separate mechanisms. The no-cache header allows storing a response but requires checking with the server before reuse. The no-store header tells caches not to store it. For queues, design how workers claim a job and how long they hold it. Make each job safe to run twice, limit delayed retries, and send jobs that keep failing to a dead-letter queue (a holding area for a person to review). Support cancellation and authorized replay, and watch the age of the oldest waiting job, throughput, failure rate, and jobs out of retries. A queue absorbs a burst but adds no capacity when work arrives faster than you finish it.
Worked example
A bakery's order system caches each customer's receipt. In the practice code, time counts in "ticks." The receipt label stays unchanged at tick 9 and refreshes at tick 10, and clearing the entry on purpose refreshes it sooner. Bob cannot read Alice's cached receipt, and once Alice's access is revoked she cannot use her still-warm entry. The practice code still checks current ownership on every read, so it teaches correctness, not speed. Its job worker records only one local effect when a finished job is replayed.
Practice
Make staleness and pending work observable
- Download the practice files and run the tests for cache expiry, clearing, warm-cache access checks, and job replay.
- Pick one set of data from your project, mark it public or private, decide how stale it may be, and write its cache key, clearing rule, and fallback.
- Draw the job states pending, working, retry, completed, and manual review, and mark which ones the practice code implements.
- For a real worker, write the attempts allowed, delay and deadline, stuck-job recovery, and who owns dead-letter review, cancellation, and replay.
- Add queue age, failure rate, and what users see while work is pending to your readiness report, keeping proposals separate from observations.
Deliverable: A cache decision table and a job-state runbook, with your local observations and the production checks still missing.
Common mistakes to watch for
- A cached page keeps showing a customer's receipt after her access was revoked.
- A 10-minute time-to-live (TTL) is mistaken for "updates show instantly," so a price change takes ten minutes to appear.
- Two customers' private results share one cache key, so one sees the other's order.
- A job delivered twice sends a customer two refunds that cannot be taken back.
Prompting
A weak prompt
Cache everything and put the slow stuff in a queue so the site is fast and never fails.
A better prompt
The slowdown I measured is [describe it], and data may be up to [time] stale. Define the cache scope, key, version, size limit, how entries are cleared, and the permission check on every hit. For background work, show "accepted" separately from "done," define how workers claim jobs and how many retries are allowed, and test duplicate delivery, interruption, stale data, revoked access, and retries running out. Add queue-age visibility and name an owner. List what my local practice does not cover.
Each cache and queue creates stale data or lost work that someone must own, so it should solve a problem you can point to.
Check before you move on
- The slowdown or job need is measured or clearly described.
- Acceptable staleness and how entries are cleared are written down.
- Permissions are checked every time a cached result is reused.
- The screen shows "accepted" separately from "done."
- Someone owns the queue and can see how old its oldest job is.
You are done when
- You can reproduce stale data, expiry, clearing, and a denied request that still hits a warm cache.
- Your report names what the local practice skips: job claiming, retry timing, a dead-letter queue, and outside-effect checks.
Questions to think about
A shop sets a cache TTL of ten minutes and then changes a price. Does every customer see the new price right away?
No. A TTL only sets how long an old copy may live, so some customers see the old price until it expires. Clear the entry when the price changes if that delay is not acceptable.
A developer sets "no-cache" on a page and assumes browsers won't store it. What does it actually do?
It lets the browser store the page but forces a check with the server before reuse. To forbid storing, the header is "no-store."
Orders arrive faster than the one worker can process them, every day. Does adding a queue fix it?
No. The queue just holds the backlog while waits keep growing. You need more processing capacity or less incoming work.