AWS in 1 Min · Episode 178

Caching strategies on AWS, from CloudFront to prompt caching

Answer once, serve many. Watch the one-minute video, then read the details.

David Gomez1:09 video · 3 min read

"Add a cache and you're done." Not without a plan. A cache keeps a copy of a result close by, so the next request skips the slow, expensive work. But cache the wrong thing, or keep it too long, and users see stale data: an old price, a deleted post, yesterday's stock level. Good caching is a set of deliberate choices.

Cache at every layer

  • At the edge: Amazon CloudFront keeps copies of pages, images and API responses in edge locations near users.
  • In memory: Amazon ElastiCache (Valkey, Redis OSS or Memcached) holds results your application computes or reads often, in microseconds to low milliseconds.
  • In front of DynamoDB: DynamoDB Accelerator (DAX) is an in-memory cache that speaks the DynamoDB API, so reads hit memory first.

The closer to the user, the more work you skip, and the harder it is to invalidate.

Lazy loading (cache-aside)

  1. The application checks the cache first.
  2. On a miss, it reads the database.
  3. It stores the result in the cache for next time.

Only data someone actually asked for gets cached, and a cache failure is not fatal; the app just reads the database. The downsides: the first request is slow, and the cached copy can go stale when the database changes.

Write-through

With write-through, the application updates the cache every time it writes to the database. Cached data is never stale, but the cache fills with data that may never be read, and every write does twice the work. Many systems combine both: write-through for hot data, lazy loading for the rest.

Always set a time to live

Whichever strategy you use, give entries a TTL (time to live) so they expire on their own. A TTL caps how stale data can get and stops the cache from growing forever. A common mistake is caching without a TTL "because the data never changes", then spending a day hunting a bug caused by a value that did.

Where AI fits

For AI, two kinds of caching cut the bill:

  • Prompt caching in Amazon Bedrock. Many prompts start with the same long prefix: system instructions, tool definitions, a reference document. With prompt caching, supported models reuse that prefix instead of reprocessing it. Tokens read from the cache are billed at the model's lower cache-read rate and responses come back faster; writing to the cache can cost more than normal input, so it pays off when the prefix is reused. Put static content first and changing content last. The cache expires after a TTL that depends on the model (often 5 minutes, with a 1-hour option on supported Claude models), and a hit is never guaranteed.
  • Answer caching. If users ask the same questions, store the generated answer and return it without calling the model at all. A semantic cache goes further: it stores an embedding of each question, for example with vector search in ElastiCache for Valkey, and reuses an answer when a new question is close enough in meaning. Set a TTL and a similarity threshold, so answers stay fresh and relevant.

Quick recap

  • Cache close to the user: CloudFront, ElastiCache, DAX.
  • Lazy loading or write-through, chosen per kind of data.
  • Always set a TTL; for AI, cache the prompt prefix and repeated answers.
AWS in 1 Min #178 · 1:09

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS