General Amazon ElastiCache Concepts
Fully managed implementations of two popular in-memory data stores – Redis and Memcached.
ElastiCache is a web service that makes it easy to deploy and run Memcached or Redis protocol-compliant server nodes in the cloud.
The in-memory caching provided by ElastiCache can be used to significantly improve latency and throughput for many read-heavy application workloads or compute-intensive workloads.
It can be put in front of databases such as RDS and DynamoDB – sits between the application and the database.
Good if your database is particularly read-heavy and the data does not change frequently.
Also good for compute-heavy workloads such as recommendation engines and it can be used to store the results of I/O intensive database queries of compute-intensive calculations.
Elasticache can be used for storing session state.
Push-button scalability for memory, writes and reads.
In-memory key/value store.
Billed by node size and hours of use.
Elasticache EC2 nodes cannot be accessed from the Internet, nor can they be accessed by EC2 instances in other VPCs.
Exam tip: the key use cases for ElastiCache are offloading reads from a database, and storing the results of computations and session state. Also, remember that ElastiCache is an in-memory database and it’s a managed service (so you can’t run it on EC2).
There are two types of engine you can choose from: Memcached, Redis
- Simplest model and can run large nodes.
- It can be scaled in and out and cache objects such as DBs.
- Widely adopted memory object caching system.
- Open-source in-memory key-value store.
- Supports more complex data structures: sorted sets and lists.
- Supports master / slave replication and multi-AZ for cross-AZ redundancy.
- Supports automatic failover and backup/restore.
The following table provides a comparison of the different ElastiCache implementations:
The following diagram depicts Amazon ElastiCache Redis with Cluster Mode disabled:
The following diagram depicts Amazon ElastiCache Redis with Cluster Mode enabled:
There are two caching strategies available: Lazy Loading and Write-Through:
- Loads the data into the cache only when necessary (if a cache miss occurs).
- Lazy loading avoids filling up the cache with data that won’t be requested.
- If requested data is in the cache, ElastiCache returns the data to the application.
- If the data is not in the cache or has expired, ElastiCache returns a null.
- The application then fetches the data from the database and writes the data received into the cache so that it is available for next time.
- Data in the cache can become stale if Lazy Loading is implemented without other strategies (such as TTL).
- When using a write-through strategy, the cache is updated whenever a new write or update is made to the underlying database.
- Allows cache data to remain up-to-date.
- This can add wait time to write operations in your application.
- Without a TTL you can end up with a lot of cached data that is never read.
Dealing with stale data – Time to Live (TTL)
- The drawbacks of lazy loading and write through techniques can be mitigated by a TTL.
- The TTL specifies the number of seconds until the key (data) expires to avoid keeping stale data in the cache.
- When reading an expired key, the application checks the value in the underlying database.
- Lazy Loading treats an expired key as a cache miss and causes the application to retrieve the data from the database and subsequently write the data into the cache with a new TTL.
- Depending on the frequency with which data changes this strategy may not eliminate stale data – but helps to avoid it.
Exam tip: Compared to DynamoDB Accelerator (DAX) remember that DAX is optimized for DymamoDB specifically and only supports the write-through caching strategy (does not use lazy loading).