Cloud concept · Updated

Cache-Aside - Cache-aside is a caching pattern in which application code checks a cache first and, on a miss, reads from the authoritative data store and populates the cache.

In short

Also called lazy loading, cache-aside stores data in the cache only when an application requests it. It can reduce read latency and database load, but the application must manage cache population, expiration, invalidation, and failure handling.

Cloud Concepts Hub →

6 key aspects of Cache-Aside

Cache-aside trades application-managed caching and freshness controls for faster repeated reads, reduced database load, and more efficient scaling.

01
Application-managed read path

The application checks the cache, fetches missing data from the authoritative store, and populates the cache. Unlike read-through caching, the cache does not perform the database fetch on the application's behalf.

02
On-demand population

Entries are loaded when requested rather than preloading the entire dataset. This saves capacity for unused data, but initial requests and requests after expiration incur database-read latency.

03
Explicit freshness management

Choose expiration and invalidation rules based on the business tolerance for stale data. Database-first invalidation is common, but concurrent reads and writes still require attention when stronger consistency is needed.

04
Read scalability and cost control

Repeated reads can be served without repeatedly executing database queries. Compare the avoided database work with cache capacity, network, and operational costs rather than assuming any cache deployment saves money.

05
Controlled failure and miss handling

Cache outages, eviction, and synchronized expiration can create sudden database load. Combine bounded fallback with request coalescing, expiration jitter, and capacity planning.

06
Reconstructible, isolated cache entries

Keep authoritative records in durable storage and treat cached entries as replaceable copies. Include tenant, request variation, and data-version boundaries in keys where needed to prevent incorrect reuse.

Cloud services for Cache-Aside

A common design places CloudFront in front of an application that uses ElastiCache for shared cache-aside reads and Amazon RDS as its authoritative database. CloudFront can satisfy eligible HTTP requests without invoking the application. Requests that reach the application first check ElastiCache; misses trigger RDS reads and cache population. Writes commit to RDS and then invalidate affected application cache entries. If the same data also appears in cached HTTP responses, that layer needs its own freshness policy. Monitor each layer separately and ensure that cache misses or outages cannot overwhelm the database.

Amazon ElastiCacheAmazon ElastiCache

Provides a shared in-memory cache that application code can use for cache-aside lookups and population. The application remains responsible for selecting keys, handling misses, and coordinating expiration or invalidation with database writes.

Amazon RDSAmazon RDS

Can serve as the authoritative relational database behind a cache-aside implementation. The application reads RDS on cache misses and commits writes to the database before applying its chosen cache invalidation strategy; attaching an external cache does not automatically keep it synchronized.

Amazon CloudFrontAmazon CloudFront

Complements application cache-aside by caching eligible HTTP responses at edge locations and reducing traffic reaching the application. CloudFront is not an application-managed database cache; cache policies, response freshness, and invalidation must be designed separately.

Cache-Aside exam questions

Real practice exam questions on Cache-Aside, with the correct answer and its explanation. Answer them with every option in the exam simulator.

SOA-C03

A publishing company runs an article API on an Amazon EC2 Auto Scaling group. For each article ID, the application checks a shared Amazon ElastiCache for Valkey cache. If the key is absent, the application queries its database and caches any article it finds. If the database returns no article, the application returns HTTP 404 without caching the result.

During traffic spikes, clients repeatedly request article IDs that do not exist. These requests generate substantial database load even as the application scales out. Articles with these IDs might be published later. The engineer must allow repeated requests for a nonexistent article to reuse a shared result without querying the database. Each cached not-found result must expire within 30 seconds of being stored, regardless of how frequently it is requested.

Which application change meets these requirements?

Show answerCORRECT ANSWER

Store an explicit not-found marker under the article's cache key with a 30-second TTL. Return HTTP 404 when the marker is found, without querying the database or extending the TTL.

This extends the existing cache-aside flow to cache negative lookup results. A distinct marker separates a known not-found result from a cache miss. All application instances can reuse it, and the fixed TTL ensures that requests do not indefinitely delay a new database lookup.

SAP-C03

An industrial equipment company runs a reference-data API backed by Amazon Aurora PostgreSQL and an Amazon ElastiCache for Memcached cluster. The database contains specifications for millions of equipment models, but customers request only a small subset. Separate administrative applications update specifications directly in Aurora. Cached specifications can be stale for approximately 60 seconds, but frequently requested specifications must not remain stale indefinitely.

During a maintenance exercise, an empty node replaces a cache node. The API currently returns errors for records that were on the replaced node, even though those records remain available in Aurora. The company requires requests to continue succeeding during cache recovery without waiting for a bulk reload. Recovery must populate the cache only with requested records and restore low-latency access for repeatedly requested records, including records that are never updated. Load testing confirms that Aurora can support the temporary increase in reads during recovery.

Which application design meets these requirements?

Show answerCORRECT ANSWER

Read from Memcached first. On a miss, retrieve the record from Aurora, return it, and attempt to cache it with a 60-second TTL. Do not extend the TTL on cache hits. If a cache operation fails, serve the database result without requiring the cache write to succeed.

This design populates the cache on demand. An empty replacement node causes database reads and temporarily higher latency, rather than application errors. Requested records are cached again, including records that never change. A TTL that is not renewed on hits prevents popular entries from remaining cached indefinitely, and cache write failures do not make the authoritative database unavailable.

DVA-C02

A developer is adding Amazon ElastiCache to an application that retrieves immutable build metadata from an Amazon DynamoDB table. The table contains millions of records, but users repeatedly request only a small subset. The cache cannot hold the entire table. After cache entries expire or the cache is restarted, repeated requests for the same records must again benefit from caching. Requests must also succeed during a cache outage, provided DynamoDB is available and has sufficient capacity.

Which application implementation meets these requirements?

Show answerCORRECT ANSWER

Check the cache for each requested record. On a miss or cache error, retrieve the record from DynamoDB. After a miss, attempt to cache the retrieved record with a TTL, but return the record even if the cache write fails.

This application-managed read path populates the cache as records are requested. Subsequent requests can reuse cached records, including after expiration or a restart. Falling back to DynamoDB and tolerating cache write failures keeps the optional cache from becoming a dependency for successful reads.

Practice Cache-Aside in a full exam

Questions on Cache-Aside in each practice exam, with every answer option and an explanation for each:

Start a free practice exam

What is Cache-Aside

3 min read

Cache-aside separates the authoritative data store from an optional, faster copy of frequently requested data. The application controls both access paths. For each cacheable read, it constructs a cache key and checks the cache. A hit returns the cached value. On a miss, the application reads the database, places the result in the cache with an appropriate expiration policy, and returns the result. The database remains the source of truth; the cache should contain data that can be reconstructed.

For example, a product API might use a key such as product:123 to cache a product description for five minutes. The first request after expiration reads the database and populates the cache. Subsequent requests reuse the cached description. This works well for frequently read information that changes relatively slowly. A payment authorization or inventory reservation generally requires authoritative validation rather than relying on a potentially stale cached value.

In cloud architectures, application instances commonly share a network-accessible cache such as Amazon ElastiCache. Newly added application instances can reuse existing cached data rather than each warming a private cache. By reducing database queries, cache-aside can help an application handle increased traffic without proportionally increasing database capacity. However, scaling application instances does not automatically scale cache capacity, and a single frequently accessed key can still become a bottleneck.

Writes require an explicit consistency strategy. A common approach commits the database update first and then invalidates the corresponding cache entry. A later read reloads the value. Updating the cache before committing the database can expose uncommitted data or leave an incorrect value after a failed write. Even database-first invalidation has race conditions: a concurrent reader can fetch an older value before the commit and populate it after invalidation. Time-to-live limits how long an entry remains cached, but does not by itself guarantee consistency. Strict freshness may require version-aware coordination or bypassing the cache for critical reads.

Cache-aside differs from read-through caching because application code, rather than the caching layer, fetches data from the database on a miss. It also differs from write-through caching, which synchronously updates the cache as part of the write path. Adding a cache does not automatically synchronize database changes, improve write throughput, or make cached reads strongly consistent. Expiration and memory-pressure eviction can both cause misses, even when the application has not explicitly invalidated an entry.

Cache misses usually cost more than hits because they involve both a cache lookup and a database read. When many requests miss the same key simultaneously, they can overwhelm the database with duplicate work, a condition called a cache stampede. Request coalescing, carefully implemented locks, and randomized expiration times can reduce this risk. Plan for cold starts, cache replacement, and failover because sudden loss of cached data can rapidly restore the full read workload to the database.

Treat cache failure as a controlled degradation scenario, not an assumption that unlimited database fallback is safe. Use short cache timeouts, bounded retries, circuit breakers, and database concurrency limits. Load shedding may be necessary when the database cannot serve uncached peak traffic. Cache replication and failover improve availability, but a cache is not a substitute for database backups, durable storage, or disaster recovery.

Evaluate the pattern using cache hit ratio, end-to-end latency, database utilization, evictions, and total operating cost. A high hit ratio for expensive, repeated queries can avoid database scaling costs; low-reuse data can instead add network calls and cache expense with little benefit. Design cache keys to include tenant and authorization-relevant boundaries. Edge caching with Amazon CloudFront can complement application cache-aside by serving cacheable HTTP responses before requests reach the application, but it is a separate caching layer with its own freshness and invalidation rules.

Learn Cache-Aside in lessons

6 short lessons on Cache-Aside in 3 certification courses.

Open the lessons