Sometimes the problem is not where to store the data, but how many times you request it. If your application queries the same information from the database over and over again, you are overloading and slowing it down. The solution is an in-memory cache, and in AWS it’s called ElastiCache. This subchapter explains what it is and why it can transform your application's performance.

The problem: asking for the same thing over and over

Imagine a news website where 100,000 people read the same popular article. Without a cache, each visit makes the application go to the database to fetch the same article, again and again. The database does the same work 100,000 times and gets saturated.

Databases store data on disk, which is relatively slow. What if we stored the most requested data in a much faster place so we don’t bother the database every time?

What is an in-memory cache

A cache is a temporary and very fast store where you keep the most frequently accessed data, to serve it instantly without going to the original source (the database).

The key: the cache stores data in RAM memory, which is much faster than disk. Reading from memory takes microseconds.

Analogy: Imagine a chef in a busy restaurant.

  • Without cache: every time they need salt, they go down to the basement storeroom (the database, on disk: slow). A thousand dishes = a thousand trips to the basement.
  • With cache: they have the salt and most-used ingredients on the countertop, at hand (the cache, in memory: super fast). They only go to the basement for the less common stuff.

The result: dishes are served much faster and the storeroom (the database) gets a break.

What is ElastiCache

ElastiCache is AWS’s managed service for running in-memory caches. It supports two very popular technologies:

Engine Features
Redis (ElastiCache for Redis / Valkey) Richer in features: advanced data structures, persistence, high availability, pub/sub
Memcached Simpler, just basic cache, very lightweight

In most modern cases, Redis is used for its additional features, but Memcached is still valid for simple caches.

As a managed service (just like RDS), AWS takes care of installation, patching, and infrastructure; you just use the cache.

How it works in practice

The most common pattern is called cache-aside. It works like this:

1. The app needs some data. It asks the CACHE first.

   Is it in the cache?
     ├── YES (cache hit)  → returns it instantly. Super fast!
     │
     └── NO (cache miss) → goes to the DATABASE (slow),
                           saves a copy in the cache for next time,
                           and returns the data.
  • The first time a piece of data is requested, it’s not in the cache (miss): it’s fetched from the database and stored in the cache.
  • The next times, it’s already in the cache (hit): it’s served instantly without touching the database.

Real example: On the news website, the first reader of the popular article causes it to be loaded from the database and stored in the cache. The next 99,999 readers get it directly from the cache, in microseconds, without bothering the database. The website flies and the database barely works.

What ElastiCache is used for

  • Speeding up frequent reads: “hot” data (the most requested) is served from memory.
  • Relieving the database: fewer repeated queries = a more rested and cheaper database.
  • Storing user sessions: each user’s session information, instantly accessible.
  • Leaderboards in games: Redis is excellent for real-time rankings.
  • Rate limiting: controlling how many times someone does something in a period.

The key concept: temporary data

The most important thing to understand: the cache is temporary and can be lost. It’s not the “source of truth” for your data; it’s a fast copy of what’s already in the database.

Therefore:

  • Cached data has a time to live (TTL): it expires after a period so as not to serve outdated information.
  • Never use the cache as the only place for important data. The source of truth is always the database; the cache only speeds up access.

The challenge of caches — invalidation: there’s a famous saying in computer science: “there are only two hard things in programming: cache invalidation and naming things.” The challenge is making sure that when data changes in the database, the cached copy is updated or deleted, so you don’t serve stale information. That’s why TTLs and update strategies are used.

What you should remember

  • An in-memory cache stores the most requested data in RAM (super fast) so you don’t have to go to the database (disk, slower) every time.
  • ElastiCache is AWS’s managed cache service, with Redis (more complete) and Memcached (simpler).
  • The typical pattern (cache-aside): check the cache first; if it’s there (hit), instant response; if not (miss), go to the database and save a copy.
  • Benefits: speeds up reads and relieves the database. Ideal for “hot” data, sessions, and leaderboards.
  • The cache is temporary: use TTL, keep it updated, and never make it the only place for important data.

In the last subchapter of the chapter (and of Part II) we’ll put everything in order: when to use each type of database we’ve seen.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved