ElasticCache

Cache or Database? Choosing Your In-Memory Layer on AWS

Both deliver microsecond reads. Only one is designed to keep your data when a node fails. Here’s how to choose between an in-memory cache and a durable in-memory data store.

Isometric illustration comparing a temporary in-memory cache with a durable in-memory database protected across multiple availability zones

Key takeaways

  • The deciding question is whether your application can tolerate losing the data in memory.
  • A cache holds a copy of data that also exists in a primary database; a data store holds the authoritative copy.
  • Amazon MemoryDB writes to a Multi-AZ transaction log before acknowledging, making it suitable as a primary database.
  • Standard ElastiCache replication is asynchronous, so recent writes can be lost during a failover.
  • Caches need deliberate design for invalidation, eviction and stampede protection, not just a TTL.

The question that decides everything

Teams often choose between an in-memory cache and an in-memory data store based on speed. That’s the wrong test, because both are fast.

The right question is simpler: if every key in memory disappeared right now, what would happen?

  • If the application slows down briefly while it reloads data from a database, you need a cache.
  • If data is lost permanently, such as a shopping cart, a game score or a user session that exists nowhere else, you need a durable data store.

Getting this wrong in one direction wastes money. Getting it wrong in the other loses customer data.

Cache vs data store: the core difference

An in-memory cache holds a temporary copy of data whose source of truth lives elsewhere, typically a relational or NoSQL database. Its job is to reduce latency and database load. Losing it is an inconvenience, not a disaster.

An in-memory data store holds the authoritative copy of the data. It must survive node failures, Availability Zone outages and restarts without losing acknowledged writes.

The same engine can play either role. Valkey and Redis OSS are used both ways. What changes is the durability architecture underneath.

Your options on AWS

Amazon ElastiCache (Valkey, Redis OSS, Memcached)
The standard managed caching service, available as self-designed clusters or ElastiCache Serverless. Replication to replicas is asynchronous, so a primary node failure can lose the most recent writes. Snapshots provide backups, but not zero data loss. It is the right default for caching, and acceptable for data you can afford to lose.

Amazon MemoryDB (Valkey, Redis OSS)
A durable in-memory database. Every write is persisted to a distributed Multi-AZ transaction log before it is acknowledged, so a failover doesn’t lose persisted writes. AWS positions it for microsecond reads and single-digit millisecond writes, making it suitable as a primary database for sessions, leaderboards, carts and real-time features.

Amazon DynamoDB Accelerator (DAX)
A write-through cache built specifically for DynamoDB. It requires minimal application changes and suits read-heavy DynamoDB workloads that need microsecond response times.

AWS has also introduced a durability option for ElastiCache for Valkey built on a Multi-AZ transaction log, similar to MemoryDB. Check its current feature compatibility, such as with Global Datastore, before choosing it over MemoryDB.

Choosing a caching pattern

If you’re building a cache, the pattern determines consistency and complexity:

  • Cache-aside (lazy loading): the application checks the cache, reads from the database on a miss, then populates the cache. Simple and resilient, but the first request after expiry is always slow.
  • Write-through: the cache is updated whenever the database is written. Data stays fresher, but every write does extra work and rarely read data fills the cache.
  • Write-behind: writes go to the cache first and are flushed to the database asynchronously. Fast, but risky without a durable store, because unflushed writes can be lost.

Most production systems combine cache-aside for reads with explicit invalidation on writes.

The failure modes most teams miss

Cache stampedes. When a popular key expires, hundreds of requests miss at once and hit the database together. Prevent this with request coalescing or locks so only one request rebuilds the value, and by refreshing hot keys before they expire.

Synchronised expiry. If thousands of keys are written with the same TTL, they expire together. Add random jitter to TTLs to spread the load.

Wrong eviction policy. A cache should evict when full, typically with an LRU or LFU policy. A data store should never silently evict, so it should use noeviction and alert on memory usage instead.

Cold starts. After a failover or deployment, an empty cache sends full traffic to the database. Make sure the database can survive that load, or pre-warm critical keys.

Stale data. Every cached item needs a clear invalidation path. A TTL alone guarantees data will eventually be correct, not that it is correct now.

Decision guide

  • Reduce database load for read-heavy data: ElastiCache with cache-aside
  • Read-heavy DynamoDB workloads needing microsecond reads: DAX
  • Session data that can be recreated by logging in again: ElastiCache
  • Sessions, carts or state that exist nowhere else: MemoryDB
  • Leaderboards, counters and real-time features as the system of record: MemoryDB
  • Rate limiting and short-lived tokens: ElastiCache
  • Unpredictable or spiky cache traffic: ElastiCache Serverless

Quick checklist

  • Each dataset classified as cache (recoverable) or data store (authoritative)
  • Durable service chosen for any data that exists nowhere else
  • Caching pattern and invalidation path defined for every cached dataset
  • TTLs include random jitter
  • Stampede protection in place for hot keys
  • Eviction policy matches the role: LRU/LFU for caches, noeviction for data stores
  • Database capacity tested against a full cache miss
  • Hit ratio, memory usage and evictions monitored in CloudWatch

Conclusion

In-memory technology is one of the most effective ways to improve application performance, but speed isn’t the deciding factor. Durability is. Classify each dataset by what happens if it’s lost, choose a service whose durability matches that answer, and design your caching layer for the failure modes, not just the happy path.

CloudOps helps teams design in-memory architectures on AWS, from ElastiCache caching layers to durable MemoryDB deployments, sized and tested for real production failures. Contact us for an architecture review.

FAQ

Common Questions

  • A cache stores a temporary copy of data that also exists in a primary database. An in-memory database stores the authoritative copy and must not lose acknowledged writes.

  • Yes. Standard ElastiCache replication is asynchronous, so recent writes can be lost when a primary node fails. Use it for data that can be recovered or recomputed.

  • When the in-memory data is your system of record, such as sessions, carts or leaderboards that exist nowhere else, and you can’t afford to lose writes during a failure.

  • It’s when many requests miss the cache at the same time, usually after a popular key expires, and overload the database. Request coalescing, early refresh and TTL jitter help prevent it.

  • It can be either. The role depends on how it is deployed and whether the underlying service provides durable, Multi-AZ persistence.

Keep reading

View All Posts

Get started

Ready to Build on the Cloud?

Let's design a cloud environment on AWS, Azure or GCP that is secure, scalable, reliable and cost-effective.