Caching on AWS

Rushabh Trivedi
3 min readApr 7, 2023

--

Caching is a very critical feature in modern applications to reduce database operations and provide fast and cost efficient performance. Caching reduces the latency by avoiding amount of database calls and providing data from available the cache. AWS provides few services where you can store the session data which includes ElastiCache and Cloudfront.

What is Caching?

Caching is the process of storing frequently accessed data in a cache, which is a temporary storage area that can be accessed quickly. When a user makes a request, the web application first checks if the data is already cached. If the data is available in the cache, it is served to the user, and the web application avoids making a request to the database, thus saving time and resources.

AWS Services for Session Caching

Sample Architecture for Caching

I am here providing a very high level overview for two caching services i.e. ElastiCache and CloudFront.

Amazon ElastiCache

Amazon ElastiCache is a fully managed, in-memory data store that provides high performance and low latency. ElastiCache can be used to cache session data, such as user preferences/order deatils/product catalogues etc. ElastiCache comes in two varients
1. Memchached
2. Redis

With Memcached, ElastiCache provides a simple and scalable caching solution that can be used to store key-value pairs. Memcached is ideal for simple and frequently accessed data and it supports multi-threading.

With Redis, ElastiCache provides a more powerful caching solution that can be used to store complex data structures, such as lists, sets, and hashes. Redis is ideal for more complex data and use cases that require advanced data structures.

A typical flow for the caching works as depicted in the above diagram

  1. When user requests data, the server initially checks in the cache (if the data is found, it will be returned).
  2. If no data found, server queries the database (this is called cache miss).
  3. once data is required data is found from DB, server stores it to the cache
  4. For any subsequent requests, server returns from the cache only by avoiding database calls

Amazon CloudFront

Amazon CloudFront is a content delivery network (CDN) that can be used to cache frequently accessed data, such as images, videos, and session data. CloudFront can cache session data at edge locations around the world, making it ideal for global applications with a large user base.

To use CloudFront for session caching, you would configure your web application to set appropriate caching headers for session data. When a user logs into your web application, their session data would be cached at the edge location closest to the user. Subsequent requests for that user’s session could be served directly from the cache at the edge location.

Benefits of Caching with AWS

Session caching with AWS services provides several benefits, including:

  1. Improved performance: By reducing the number of requests to the database, session caching can significantly improve the performance of your web application.
  2. Cost-effective: Caching data in memory is less expensive than retrieving data from a database, which can help reduce the overall cost of running your web application.
  3. Scalability: AWS services are designed to scale as your application grows. With ElastiCache and CloudFront, you can easily scale your caching solution to meet the needs of your application.
  4. High availability: AWS services provide high availability and data durability, ensuring that your session data is always available when you need it.

Conclusion

Caching is a very critical aspect for an application. You should always take care of your specific usecase in order to select the right service for the caching.

I would like to hear your feedback and experiences on caching techniques that will help me and others to make right decisions.

--

--

Rushabh Trivedi
Rushabh Trivedi

Written by Rushabh Trivedi

AWS Certified Associate Architect, Cloud Solutions Lead, Angular Developer

No responses yet