Handbooks  /  The Redis Handbook
Handbook~14 min readIntermediate
Deep Dive

Redis, the
data-structure server.

Calling Redis "a cache" undersells it. It's an in-memory server whose values are real data structures — sorted sets, hashes, lists — each with atomic operations at RAM speed. That's why one tool powers caches, rate limiters, leaderboards, sessions and queues. Here's how it works, and where it fits.

01

More than a key-value cache

Most people meet Redis as a cache: put a value under a key, read it back fast. That's true — it lives in memory, so reads and writes are microseconds, not milliseconds. But the deeper idea is that Redis is a data-structure server: a value isn't an opaque blob, it's a typed structure (a list, a set, a sorted set, a hash) with commands that operate on it atomically on the server.

That changes what you can do. Instead of reading a value, modifying it in your app, and writing it back (a race-prone round trip), you tell Redis "increment this counter," "add to this sorted set," "pop from this list" — and it happens atomically in one hop. The data structure lives where the data is. That's the difference between a cache and a Swiss-army server.

02

The core data structures

Five structures cover the vast majority of Redis usage. Knowing what each is for is most of knowing Redis.

StructureWhat it isUsed for
StringA value (text, number, bytes)Cache entries, counters (INCR), flags, small blobs
HashField → value map under one keyObjects/records (a user's fields) without serializing the whole thing
ListOrdered sequence (push/pop both ends)Simple queues, recent-items, timelines
SetUnordered unique membersMembership, tags, unions/intersections (who liked both)
Sorted setMembers ordered by a scoreLeaderboards, rate limiting, priority queues, ranges

The sorted set deserves a spotlight: it keeps members ordered by a numeric score with O(log N) add/update and rank queries — which is exactly a leaderboard, and also the backbone of sliding-window rate limiters and time-ordered indexes. Many "how would you build X in Redis?" answers are "a sorted set."

03

Single-threaded, and fast because of it

Surprisingly, core Redis executes commands on a single thread, via an event loop. That sounds like a limitation, but it's a feature: because one command runs to completion before the next, every operation is atomic with no locks, no race conditions, and no context-switching overhead. INCR, a sorted-set update, a multi-key transaction — all naturally atomic.

And it's still blazing fast because Redis is in-memory: the bottleneck is the network and memory bandwidth, not CPU concurrency. The practical lesson: keep individual commands cheap. A single expensive command (a huge KEYS * scan, a giant sorted-set range) blocks the whole server for its duration, so you avoid O(N) operations on big keys on the hot path.

→ The footgun

Because it's single-threaded, one slow command stalls every other client. Never run KEYS * or big blocking operations in production — use SCAN and keep hot-path commands O(1) or O(log N).

04

Persistence — it's in memory, so…

Redis keeps data in RAM, which is why it's fast — and why durability needs care. If the process dies, in-memory data is gone unless it was persisted. Redis offers two mechanisms with different tradeoffs.

RDB — snapshots

  • Periodic point-in-time dump of the dataset
  • Compact file, fast to load, low overhead
  • Risk: lose writes since the last snapshot

AOF — append-only file

  • Logs every write operation
  • Better durability (down to ~1s or per-write)
  • Larger files, slower to load/replay

Many deployments run both: AOF for durability, RDB for fast restarts and backups. The key mindset: Redis can be durable, but its sweet spot is a fast layer where losing a little recent data on a crash is acceptable. If you need database-grade durability guarantees, that's a signal to reconsider (section 08).

05

Memory limits: eviction & TTL

RAM is finite, so Redis has to decide what to keep. Set a maxmemory limit and an eviction policy for when it's hit — most commonly allkeys-lru (evict least-recently-used) or allkeys-lfu (least-frequently-used), which turns Redis into a proper LRU/LFU cache that automatically drops cold data. Other policies evict only keys with a TTL, or refuse writes when full.

Independently, any key can be given a TTL (time to live) so it expires automatically — essential for cache freshness, session timeouts, and rate-limit windows. Between eviction (memory pressure) and TTL (explicit expiry), Redis keeps its footprint bounded without you manually cleaning up.

06

What people actually build with it

The data structures map cleanly onto classic patterns — this is why Redis is everywhere.

CacheString + TTL — the read-through/aside cache in front of a slow database. The original use.
Rate limitINCR + TTL, or sorted set — count requests per window atomically; reject over the limit.
LeaderboardSorted set — O(log N) score updates and rank/top-K queries, live.
SessionHash + TTL — fast shared session store so app servers stay stateless.
QueueList or Streams — simple job queues (LPUSH/BRPOP) or durable Streams with consumer groups.
Pub/SubChannels — lightweight real-time fan-out (chat, notifications, live updates).

The pattern is always: pick the structure whose atomic operations are the feature. A leaderboard isn't "store scores and sort" — it's "a sorted set," and the rank query is one command.

07

Availability & scale

A single Redis is a single point of failure and capped by one machine's RAM. Three mechanisms address that. Replication: a primary asynchronously copies its data to replicas, which serve reads and stand ready as failover targets. Sentinel: monitors the primary and automatically promotes a replica if it dies, giving high availability for a single logical dataset. Cluster: shards the keyspace across many primaries (by hashing keys into slots), so both capacity and throughput scale horizontally, each shard with its own replicas.

→ Rule of thumb

Need HA but your data fits one machine? Replication + Sentinel. Outgrown one machine's memory or throughput? Redis Cluster to shard across nodes. Cluster adds constraints (multi-key ops must share a slot), so don't reach for it before you need it.

08

When Redis is the wrong tool

Redis is a fast layer, not a universal database. It's the wrong choice when your dataset is much larger than affordable RAM (memory is the cost ceiling), when you need rich relational queries or transactions spanning many entities (use a relational database), or when you need strong, guaranteed durability like a system of record (async replication and snapshotting can lose recent writes on failure).

Use it for what it's superb at: caching, counters, rate limiting, leaderboards, sessions, ephemeral queues, and real-time fan-out — a blazing-fast complement to your primary store, not a replacement for it. The right question isn't "can Redis do this?" (it usually can) but "is a fast, in-memory, structure-oriented layer the right shape for this data?"

Frequently asked

Quick answers

What is Redis?

An in-memory data-structure server: data lives in RAM for microsecond access, and values are typed structures (strings, hashes, lists, sets, sorted sets) with atomic commands. Used for caching, rate limiting, leaderboards, sessions, queues, and pub/sub.

Why single-threaded but fast?

Commands run on one event-loop thread, so each is atomic with no locks or context switches, and it's in-memory — the bottleneck is network/memory, not CPU. The catch: one slow command blocks everyone, so keep commands cheap.

RDB vs AOF?

RDB takes periodic snapshots (compact, fast load, can lose recent writes); AOF logs every write (better durability, larger/slower). Many use both, balancing durability against performance.

When not to use Redis?

As a primary store for data larger than RAM, for complex relational queries/transactions, or when you need database-grade durability. It's a fast layer, not a replacement for your system of record.

Finished this one? 0 / 29 Handbooks done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.