Redis
An in-memory data store used as a cache, message broker, and database. Blazing fast because everything lives in RAM.
What is Redis?
In short
Redis is an open source in-memory data store that keeps all of its data in RAM, which lets it answer reads and writes in well under a millisecond. It is used as a cache, a message broker, and sometimes a primary database, and it supports rich data types like strings, hashes, lists, sorted sets, and streams rather than just plain key-value pairs.
What Redis actually is
Redis stands for Remote Dictionary Server. At its core it is a single key-value store, but the values are not limited to strings. A value can be a hash (a small map of fields), a list, a set, a sorted set ordered by a score, a bitmap, a HyperLogLog, a stream, or a geospatial index. This is why Redis is often called a data structures server instead of just a cache.
Everything lives in memory. A lookup does not touch a disk, walk a B-tree, or cross a network of storage nodes, so a typical operation finishes in a few microseconds on the server and a fraction of a millisecond once you add network round-trip. A single Redis node can serve well over 100,000 operations per second on commodity hardware.
Redis is single-threaded for command execution. One command runs to completion before the next starts, so individual operations are atomic with no locks to manage. Modern versions use extra threads for network I/O, but the command processing model stays simple and predictable.
How it works under the hood
Because RAM is volatile, Redis offers two ways to survive a restart. RDB takes point-in-time snapshots of the whole dataset on a schedule, which is compact and fast to load but can lose the writes since the last snapshot. AOF (append-only file) logs every write command and replays it on startup, which loses less data but produces larger files. Many production setups run both.
Single-key atomicity covers simple cases, but Redis also gives you MULTI/EXEC transactions and Lua scripts that run server-side as one atomic block. Scripts are the common way to do read-then-write logic, like a rate limiter or a leaderboard update, without a race between two clients.
For scale and availability, Redis replicates from a primary to one or more read replicas asynchronously. Redis Sentinel watches the primary and promotes a replica if it dies. Redis Cluster goes further by sharding the keyspace across 16,384 hash slots spread over multiple primaries, so the dataset can outgrow a single machine's memory.
Keys can carry a TTL. Set a key to expire in 60 seconds and Redis removes it for you, which is how it behaves as a cache. When memory fills up, an eviction policy such as allkeys-lru or volatile-ttl decides what to drop.
When to use it and the trade-offs
Reach for Redis when you need very low latency and the access pattern is key-based: caching database query results or rendered pages, session storage, rate limiting, real-time leaderboards, counters, and pub/sub or stream-based message passing. It shines as a layer in front of a slower system of record like PostgreSQL or DynamoDB.
The main constraint is that your working set has to fit in RAM, and RAM is far more expensive per gigabyte than disk. A 100 GB dataset that costs little on disk can be costly to hold entirely in memory, so Redis is usually for hot data, not the full archive.
Durability is the other trade-off. Even with AOF set to fsync every second, a crash can lose up to a second of writes, and asynchronous replication means a failover can lose recently acknowledged writes. For data you cannot afford to lose, keep the authoritative copy in a durable database and treat Redis as an accelerator. Also avoid commands like KEYS in production, because scanning the whole keyspace on a single-threaded server blocks every other client.
A concrete example
Picture a news site that shows article view counts. Every page load would otherwise issue an UPDATE and a SELECT against the main database, which buckles under a traffic spike. Instead, each view runs INCR article:1234:views in Redis, an atomic in-memory increment that takes microseconds. A background job flushes the counts to PostgreSQL every minute.
The same site caches each rendered article: SET article:1234:html with a 5 minute TTL. The web server checks Redis first, and only on a miss does it render from the database and store the result. With a good hit rate the database sees a tiny fraction of the original load, and readers get pages served straight from memory.
Where it is used in production
Twitter (X)
Used Redis to serve timelines, storing each user's home feed as a list of tweet IDs in memory for fast fan-out reads.
GitHub
Runs Redis for background job queues (via Resque) and for caching, keeping web request latency low.
Stack Overflow
Uses a small number of Redis servers as a shared cache layer across the whole site, handling billions of operations a day.
AWS ElastiCache and Amazon MemoryDB
Offer fully managed Redis-compatible services so teams get clustering, failover, and backups without running the servers themselves.
Frequently asked questions
- Is Redis a database or a cache?
- It is both. Most teams use it as a cache in front of a durable database, but with AOF persistence and replication enabled it can serve as a primary store for data that can tolerate a small risk of loss on failover.
- Does Redis lose all my data if the server restarts?
- Not if you configure persistence. RDB snapshots and AOF logging let Redis rebuild its dataset on startup. With AOF you might lose up to about a second of writes; with snapshots alone you lose everything since the last snapshot.
- Why is Redis so fast?
- All data lives in RAM, so reads and writes never wait on disk. Command execution is single-threaded, which removes lock contention, and the protocol and data structures are tuned so most operations finish in microseconds.
- What is the difference between Redis and Memcached?
- Memcached is a simpler in-memory cache that stores plain strings. Redis adds rich data types, persistence, replication, clustering, pub/sub, transactions, and Lua scripting, so it does much more than caching.
- How does Redis handle datasets bigger than one machine's memory?
- Redis Cluster shards keys across 16,384 hash slots spread over multiple primary nodes, so the combined memory of the cluster holds the data and clients are routed to the node owning each key.
Learn Redis hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Redis as part of a larger topic.
Redis Cache
The Swiss Army knife of caching, data structures, persistence, clustering, and why nearly every major tech company runs Redis
foundation · caching strategies
Application-Level Caching
Distributed caching with Redis and Memcached, the shared brain across your servers
foundation · caching strategies
Snapshotting
Capture a consistent point-in-time view of distributed state without stopping the system
advanced · consistency models
Design a Rate Limiter
Design a distributed rate limiting system - token bucket, sliding window, and protecting services at massive scale
capstone · capstone
Remote Cache
A shared cache that lives on a separate server, trade local speed for cross-instance consistency
foundation · caching strategies
See also
Related glossary terms you might want to look up next.
Memcached
A simple, high-performance distributed memory caching system. Stores key-value pairs in RAM. Simpler than Redis but less feature-rich.
Caching
Storing frequently accessed data in a faster storage layer so you don't have to fetch it from the original (slower) source every time.
Rate Limiting
Controlling how many requests a client can make in a given time window. Protects your API from abuse and ensures fair usage.
Document Database
A NoSQL database that stores data as flexible JSON-like documents. MongoDB and CouchDB let each document have a different structure.
Key-Value Store
The simplest NoSQL model: store data as key-value pairs. Blazing fast lookups by key. Redis, DynamoDB, and etcd are key-value stores.
Column-Family Store
A NoSQL database that groups columns into families, optimized for reading and writing large amounts of data across many machines. Cassandra and HBase use this model.