Skill 4.3.4

Implement caching by using AWS services (for example, Amazon ElastiCache, Amazon DynamoDB Accelerator [DAX], Amazon CloudFront).

Key Concepts

The simplest cache is in memory within the process. In Lambda, values stored outside the handler, such as configuration or secrets, persist across warm invocations. The Parameters and Secrets Lambda extension and the Secrets Manager caching clients cache values with a TTL.

Shared caches such as ElastiCache serve all instances, using lazy loading or write-through strategies with TTLs, while DAX caches DynamoDB reads. Caches must have an invalidation or expiry strategy, and the application should still work if the cache is empty or unavailable.

In Amazon CloudFront, a cache policy defines the cache key: which headers, query strings and cookies are included, plus TTLs. Only include what actually changes the response, such as Accept-Language for translated content, because every extra value reduces the cache hit ratio. An origin request policy forwards extra values to the origin without adding them to the cache key.

In API Gateway, REST API caching uses the method's cache key parameters, which can include headers, query strings and path parameters. Clients can send Cache-Control: max-age=0 to request a fresh response if the API allows cache invalidation.