Caching stores frequently accessed data in memory so later requests for that data are served faster. It is one of the most common ways to improve database performance because it reduces the number of requests that reach the underlying database.
Use cases
- Read-heavy workloads: many reads can be served from the cache instead of the database.
- Temporal data locality: the same data is requested repeatedly within short periods.
- Expensive computations: cache the results of slow queries or complex calculations.
- Rate limiting: fewer direct database accesses helps prevent overload.
The hard part: cache invalidation
Managing a cache yourself is often one of the hardest things to get right, because cached data can become outdated. If you implement caching yourself, design it carefully: decide what to cache, when to cache it, and how long it should live.
AWS caching options
- Amazon ElastiCache: a fully managed in-memory store supporting Redis and Memcached. Good for caching, session storage, gaming, geospatial services, real-time analytics, and queueing.
- Amazon DynamoDB Accelerator (DAX): a managed in-memory cache for DynamoDB that can improve eventually consistent reads by up to 10x, from milliseconds to microseconds.
- Amazon CloudFront: primarily a CDN, but it also caches static and occasionally changing dynamic content at edge locations.
- Aurora Buffer Pool: Amazon Aurora's built-in in-memory cache of frequently accessed data pages, reducing disk I/O.
Caching is fast but can cost more. If your reads don't benefit much from caching, consider read replicas instead.