Building a Durable In-Memory Database with Amazon MemoryDB for Redis - Unifying Cache and Primary Data Store
Learn how to operate a Redis-compatible in-memory database with MemoryDB, understand its durability mechanisms, and decide when to use it versus ElastiCache.
Overview of MemoryDB
MemoryDB for Redis is a durable in-memory database service compatible with Redis 7.x, capable of storing data across up to 500 nodes. While ElastiCache for Redis is specialized for caching (volatile data), MemoryDB persists writes to a Multi-AZ transaction log, making it suitable as a primary data store. Read latency is in the microsecond range, and write latency is in the single-digit millisecond range. In cluster mode, it scales out by adding shards, each consisting of a primary and replicas.
How Durability Works
MemoryDB synchronously records writes to a Multi-AZ transaction log, so written data can be recovered after a node failure. Write operations return an ACK to the client only after persistence to the transaction log is complete, providing strong consistency. On node restart, data is restored from the transaction log. Replication to replica nodes is asynchronous, so reads from replicas are eventually consistent. In addition to daily automatic snapshots, manual snapshots can be taken and used for recovery.
Use Cases and When to Choose Over ElastiCache
MemoryDB is well suited for use cases that need in-memory speed without risking data loss, such as session stores, user profiles, leaderboards, and real-time analytics. You can take full advantage of Redis data structures (Sorted Sets, Hashes, Streams) and position MemoryDB as a data layer that complements DynamoDB or RDS. ElastiCache is better suited as a front-end cache for RDS or DynamoDB, where some data loss is acceptable. ElastiCache uses asynchronous replication, which means recent writes may be lost during failover. As a selection criterion: if the data can be re-fetched from the source (such as RDS), choose ElastiCache; if the data in Redis is the sole source of truth, choose MemoryDB. If caching is the only goal, choose ElastiCache.
Redis Data Structures and Server-Side Processing
MemoryDB supports the Redis 7.x command set, including Strings, Hashes, Lists, Sets, Sorted Sets, Streams, HyperLogLog, and Geospatial indexes. Sorted Sets can be used to implement leaderboards and timelines with O(log N) ranking retrieval. Streams can serve as event logs or message queues with consumer group support for parallel processing. Lua scripting enables atomic server-side processing, Redis Functions allow registration of reusable logic, Pub/Sub provides real-time notifications, and transactions (MULTI/EXEC) support atomic operations. ACL v2 enables fine-grained access control per user and per command, with the ability to separate read and write permissions by key pattern. In cluster mode, data is distributed based on hash slots following the Redis Cluster protocol, and clients automatically connect to the correct shard via MOVED redirects. Large keys (several MB or more) concentrated on a single shard can cause hot spots, so design the data model with uniform key sizes.
Design Best Practices and Considerations
With MemoryDB, design so that your data size fits within node memory. High memory utilization causes swapping or eviction that degrades performance, so plan capacity with headroom in memory utilization. Shard count changes (resharding) can be performed online, but write latency temporarily increases during data redistribution. Encryption is supported both at rest (KMS) and in transit (TLS), and ACLs (Access Control Lists) provide command-level authorization control. The standard architecture places MemoryDB in a VPC with security groups restricting source connections. A Multi-AZ configuration provides high availability.
Pricing and Limit Considerations
MemoryDB nodes are billed by the hour, and pricing varies by node type. This is more expensive than an equivalent ElastiCache node, but includes transaction log storage for durability. Data transfer within the same region is free, while cross-region transfer follows standard data transfer pricing. Snapshot storage beyond the free allowance is billed based on the amount stored. Reserved nodes (1-year/3-year) can reduce costs. Eviction behavior is controlled by setting maxmemory-policy in a parameter group.
Summary
MemoryDB is a Redis-compatible, durable in-memory database. It achieves write persistence through a Multi-AZ transaction log, and unlike ElastiCache (which is designed for caching), it can serve as a primary database. With microsecond read latency, it suits use as a data layer for session stores, leaderboards, and real-time analytics. The key design principle is the separation: ElastiCache for caching, MemoryDB when the data stored is the sole source of truth.
References (Official AWS Resources)
The primary sources for this page are the official AWS website and documentation. Check the official pages below for the latest specifications and pricing.
- Amazon MemoryDB for Redis official page
- Amazon ElastiCache official page
- AWS KMS official page
- AWS Documentation (official)
If this page and the official documentation disagree, treat the official documentation as authoritative.