DEV Community

Rashwan Lazkani
Rashwan Lazkani

Posted on

Caching Strategies in AWS

Hi,

In this post I will provide you some caching strategies that could be useful when using AWS. Before we start I just want to explain the term TTL which will be used in this article:
Time To Live (TTL) is the time that an object is stored in a caching system before it's deleted or refreshed.

Scenario

A website that users enters which fetches data from a database through API Gateway and some app logic (in this case through Lambda but could be EC2 etc.)

Image description

What caching possibilities do we have?

Let's start from the left:

Amazon CloudFront uses cache at edge which basically means caching as close at the user as possible. If the user do hit the cache that means that they will get a response immediately. Remember to set a TTL depending on how long refresh you want on your cached data.

In Amazon API Gateway you can enable caching by going to the API Gateway console, you configure caching in the Settings tab of a named Stage Editor. The default TTL value for API caching is 300 seconds. The maximum TTL value is 3600 seconds. TTL=0 means caching is disabled.

Note that API Gateway can be used without CloudFront too.

In our app logic we don't have any caching.

Database cache, you don't always want to query and make heavy load on your database. You can have as a thumb rule to always cache frequent- and complex queries.

What database cache options do we have?

  • Redis a fast, open source, in-memory, key-value data store
  • Memcached an easy-to-use, high-performance, in-memory data store
  • Amazon DynamoDB Accelerator (DAX) is a fully managed, highly available, in-memory cache for Amazon DynamoDB that delivers up to a 10 times performance improvement—from milliseconds to microseconds—even at millions of requests per second

After implementing our caches our architecture could look something like this:
Image description

The more you move to the right side the more computation cost and it’s gonna be a latency back to the user.

Summary
Cache is a great way to help browsers and apps load data faster but remember to check where you want to cache and how long you want to cache.

Any comments, questions or discussions are always welcome!

Top comments (0)