How Redis Works

Intermediate
7 min read· Backend & Databases

Redis is an in-memory data structure store that can act as a cache, message broker, session store, rate limiter, and real-time leaderboard — all at the same time. Because it stores everything in RAM and uses a single-threaded event loop, Redis can handle over 1 million operations per second on a single instance. It's one of the most widely deployed pieces of infrastructure in the world — used by Twitter, GitHub, Airbnb, and almost every large backend system.

Think of it like a whiteboard next to your desk

Your filing cabinet (the database) has everything, but looking something up takes time. Redis is the whiteboard where you write down the things you check most often. The whiteboard is fast to read and write, but space is limited and it gets wiped on reboot unless you take a photo of it first (persistence). The key insight: not everything needs to live in the filing cabinet every single time.

Step by Step

1 / 6

Key Concepts

String

The simplest Redis type. Can hold text, integers, or binary data up to 512MB. Used for caching serialised objects (JSON), counters (INCR), and feature flags. INCR is atomic — safe for distributed counters.

Hash

A map of field-value pairs stored under a single key. Perfect for representing objects: HSET user:123 name "Akshay" age 30. More memory-efficient than storing a JSON string when only a few fields need updating.

Sorted Set (ZSet)

A set of unique members each with a floating-point score. Members are always sorted by score. ZADD adds members, ZRANGE retrieves them in order, ZRANK returns a member's rank. Power use case: real-time leaderboards and rate limiting with sliding windows.

Pub/Sub

Redis's publish/subscribe messaging. Publishers send messages to a channel; subscribers receive them in real time. No message persistence — if a subscriber is offline when a message is published, it misses it. For persistence, use Redis Streams instead.

RDB Snapshot

Redis forks the process and writes a compact binary snapshot of all data to a .rdb file. This happens in the background without blocking the main process. Useful for backups and fast restarts, but risks losing up to minutes of data on crash.

AOF (Append-Only File)

Every write command is appended to a log file. On restart, Redis replays the log to reconstruct state. With fsync=always, you can have durability similar to a relational database at the cost of write performance.

LRU Eviction

When Redis runs out of memory (maxmemory limit), it evicts keys based on a configurable policy. allkeys-lru evicts the least recently used keys first. volatile-lru only evicts keys with a TTL set. Choosing the right policy prevents OOM crashes.

Redis Cluster

Horizontal scaling of Redis by sharding data across multiple nodes. The keyspace is divided into 16,384 hash slots distributed across nodes. Clients must be cluster-aware, or use a proxy. Supports replication within the cluster.

Key Facts

  • Redis can execute over 1,000,000 GET/SET operations per second on commodity hardware because all data lives in RAM.
  • The name Redis stands for Remote Dictionary Server.
  • Redis is single-threaded for command processing but uses multiple threads for I/O and background tasks since Redis 6.0.
  • Twitter used Redis Sorted Sets to power its "who to follow" recommendations and timeline fanout at massive scale.
  • A Redis key can hold a maximum value of 512MB — but in practice, keeping values under a few kilobytes is a performance best practice.
  • GitHub uses Redis to store session data and as a work queue backend for background jobs via Sidekiq.

Real-World Applications

Database query result caching

The most common use case. Before querying the database, check Redis for a cached result using a key like "user:123:profile". On miss, query the database, store the result in Redis with a TTL, and return it. This can reduce database load by 80–95% for read-heavy endpoints.

Session storage

HTTP is stateless. After login, store the session token as a Redis key with a TTL: SET session:abc123 "{userId: 1}" EX 86400. On every request, look up the token in Redis. This is faster than querying a database and works across multiple backend instances.

Rate limiting

Use INCR and EXPIRE to implement a simple rate limiter: increment a counter key per user per minute, expire it after 60 seconds. If the counter exceeds the limit, reject the request. Sorted Sets enable more sophisticated sliding window rate limiters.

Job queues

Libraries like BullMQ (Node.js) and Sidekiq (Ruby) use Redis Lists or Streams as job queues. Workers BRPOP (blocking right-pop) from a list, process the job, and acknowledge completion. Redis Streams add consumer groups and message acknowledgement for more reliable delivery.

Frequently Asked Questions

When should I use Redis vs a regular database?

Use Redis for data that benefits from sub-millisecond access, has a natural TTL, or requires specialised data structures (leaderboards, counters, queues). Use a relational database as the source of truth. Redis is not a replacement for a database — it's a complement to one.

What happens to data when Redis restarts?

Without persistence, all data is lost on restart. With RDB enabled, Redis restores the last snapshot (potentially minutes of data loss). With AOF and fsync=always, data loss is limited to at most one write operation. Most production setups use both RDB and AOF for a balance of fast restarts and durability.

Is Redis really single-threaded? How can it be so fast?

Redis is single-threaded for executing commands, which eliminates lock contention. Its speed comes from storing all data in RAM (no disk I/O for reads), a minimal protocol (RESP), and highly optimised C data structures. Modern Redis (6.0+) uses multiple threads for network I/O while keeping command execution single-threaded.

What is the difference between Redis and Memcached?

Memcached is a simpler cache that only supports string values. Redis supports rich data types (lists, sets, sorted sets, hashes, streams), optional persistence, pub/sub, replication, and clustering. Memcached is multi-threaded and slightly faster for simple get/set workloads, but Redis wins on versatility.

Related Topics