> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Elasticache Overview

> Overview of Amazon ElastiCache, in-memory caching benefits, use cases, and guidance on choosing Redis or Memcached for performance, durability, and scalability.

This lesson explains Amazon ElastiCache and how in-memory caching improves application performance and scalability. You’ll learn common caching patterns, typical use cases (like database caching and session stores), and how to choose between Redis and Memcached for your workloads.

First, consider an application that relies only on a disk-backed database with no caching. Every user request produces a database query, increasing disk I/O and latency as traffic grows. As read volume rises, the database becomes a bottleneck and overall response times suffer.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/N4R4aUhm-yJiOGwk/images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-highload-diskstorage-performance-scalability.jpg?fit=max&auto=format&n=N4R4aUhm-yJiOGwk&q=85&s=754d464e41fdc1083fdbaca5a32d5224" alt="A slide titled &#x22;ElastiCache&#x22; showing four colored tiles with icons labeled: High Load, Disk-Based Storage, Performance Impact, and Scalability Issues." width="1920" height="1080" data-path="images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-highload-diskstorage-performance-scalability.jpg" />
</Frame>

Caching solves these problems by storing frequently accessed or transient data in fast, in-memory stores. When you offload read-heavy traffic to a cache you:

* Reduce the number of database reads
* Decrease latency for end users
* Improve application responsiveness and scalability

Cache lookup follows a simple pattern: check the cache first, then fall back to the database on misses.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/N4R4aUhm-yJiOGwk/images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-ecommerce-client-website-cache-db.jpg?fit=max&auto=format&n=N4R4aUhm-yJiOGwk&q=85&s=8229c51489e569ced7e65e2b6c003384" alt="A simple ElastiCache architecture diagram showing a client using an e-commerce website that queries a cache (with &#x22;Cache Hit&#x22; and &#x22;Cache Miss&#x22; paths) and falls back to a database. The flow arrows illustrate requests between client, website, cache, and database." width="1920" height="1080" data-path="images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-ecommerce-client-website-cache-db.jpg" />
</Frame>

Cache lookup pattern (high level):

1. Application queries the cache.
2. If the data exists (cache hit) — return it immediately.
3. If the data does not exist (cache miss) — query the database, return the result, and optionally store it back in the cache.

Example: simple cache-get pseudo-code (Node.js style)

```js theme={null}
// Pseudocode: cache-first lookup
async function getUserProfile(userId) {
  const cached = await cache.get(`user:${userId}`);
  if (cached) {
    return JSON.parse(cached); // cache hit
  }
  const profile = await db.queryUser(userId); // cache miss -> DB
  if (profile) {
    await cache.set(`user:${userId}`, JSON.stringify(profile), { ttl: 3600 });
  }
  return profile;
}
```

Caching is also ideal for session stores: storing short-lived session state (login tokens, shopping cart) in-memory is faster and simpler than persisting that data to disk.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/N4R4aUhm-yJiOGwk/images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-database-caching-session-store.jpg?fit=max&auto=format&n=N4R4aUhm-yJiOGwk&q=85&s=bd761b67420752f82f9708f0e12133f1" alt="An ElastiCache infographic showing two use cases: &#x22;Database Caching&#x22; on the left (to decrease read-heavy database loads) and &#x22;Session Store&#x22; on the right (to manage session information for web applications)." width="1920" height="1080" data-path="images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-database-caching-session-store.jpg" />
</Frame>

Amazon ElastiCache is AWS’s managed in-memory caching service. Key benefits include:

* Managed provisioning, engine and OS patching, and monitoring
* Built-in failure recovery and automated maintenance operations
* Support for Redis and Memcached as drop-in engines
* Backup/restore for Redis (Memcached is ephemeral)
* Scaling options (scale out/in via clusters or scale up/down instance types)
* High availability features (Multi-AZ replication and automatic failover for Redis depending on configuration)

When choosing between Redis and Memcached, consider the following differences. Use this to map engine capabilities to your application requirements (durability, data structures, replication, etc.).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/N4R4aUhm-yJiOGwk/images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-redis-memcached-comparison.jpg?fit=max&auto=format&n=N4R4aUhm-yJiOGwk&q=85&s=e4aa41b79d08b05ca2e34cdec36b902e" alt="A slide titled &#x22;ElastiCache&#x22; showing a two-column comparison of Redis (left) and Memcached (right) with rows listing differences such as data structures, persistence, replication/failover, Multi-AZ support, backups, partitioning, and threading." width="1920" height="1080" data-path="images/Introduction-to-AWS-Databases/AWS-Databases-Part-1/Elasticache-Overview/elasticache-redis-memcached-comparison.jpg" />
</Frame>

Feature comparison (high-level)

| Feature                  | Redis                                                                          | Memcached                                                    |
| ------------------------ | ------------------------------------------------------------------------------ | ------------------------------------------------------------ |
| Data model               | Rich data types (strings, hashes, lists, sets, sorted sets)                    | Simple key-value                                             |
| Persistence              | Optional persistence; ElastiCache supports snapshots/backups                   | No persistence; data is ephemeral                            |
| Replication and failover | Built-in replication, Multi-AZ, and automatic failover for replication groups  | No built-in replication or automatic failover                |
| Backups                  | Snapshot and restore supported                                                 | No backups                                                   |
| Partitioning / sharding  | Cluster mode provides sharding                                                 | Client-side partitioning; lacks centralized cluster sharding |
| Concurrency              | Single-threaded per shard/process (good for atomic ops and complex data types) | Multi-threaded (can use multiple CPU cores)                  |

When to choose Redis vs. Memcached

* Choose Redis if you need:
  * Advanced data structures (lists, sets, sorted sets, hashes)
  * Persistence or backup/restore capabilities
  * Built-in replication, automatic failover, and Multi-AZ high availability
  * Atomic operations and server-side scripting (Lua)

* Choose Memcached if you need:
  * A simple high-throughput, ephemeral cache
  * Multi-threaded performance to utilize multiple CPU cores
  * Lightweight key-value caching without persistence or replication concerns

<Callout icon="lightbulb" color="#1CB2FE">
  Redis is a better fit when you need advanced data structures, durability, replication, or backups. Memcached can be preferable for simple, high-performance ephemeral caching where multi-threaded CPU utilization matters.
</Callout>

Summary

* Amazon ElastiCache is a fully managed, in-memory caching service that supports Redis and Memcached.
* Use caching to reduce database load, lower latency, and improve application scalability.
* Select Redis for durability and complex data structures; select Memcached for simple, high-throughput ephemeral caches.

Links and references

* [Amazon ElastiCache documentation](https://docs.aws.amazon.com/elasticache/)
* [Redis](https://redis.io/)
* [Memcached](https://memcached.org/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-aws-databases/module/001734a9-f7c2-4943-83a3-d64621fedfd2/lesson/e70d44d3-2460-4344-8cc8-6b82e9be84e4" />
</CardGroup>
