Table of Contents

Redis consistently appears near the top of Stack Overflow’s most-loved databases survey. Not because it has the most features, or because it’s cheapest — because it does what it does extremely well, and predictably.

TL;DR

Redis (Remote Dictionary Server) is an in-memory data structure server:

  • Data lives in RAM → read/write latency in microseconds
  • Single-threaded architecture → atomic operations, no lock contention
  • Rich data types → String, Hash, List, Set, Sorted Set, Stream, and more
  • Optional persistence → not purely ephemeral

What Actually Is It?

Most engineers first encounter Redis as a cache, but that’s only a fraction of what it does. The official definition is “in-memory data structure store”:

  • In-memory: all data lives in RAM
  • Data structure store: not just key-value — supports many data types
  • Store: can be a database (with persistence), a cache, or a message queue

Why Is It So Fast?

Single-threaded event loop

Redis is single-threaded at its core, which sounds like a limitation. Traditional databases use multi-threading for concurrent requests — how does Redis handle load?

The key: Redis’s bottleneck isn’t CPU, it’s I/O. Since all data is in memory, every operation completes so quickly that multi-threading’s overhead would outweigh its benefits. Single-threaded means no locks, no deadlocks, no race conditions — every command executes atomically.

Redis 6.0 introduced multi-threaded network I/O while keeping business logic single-threaded — increasing throughput without sacrificing simplicity.

Memory access

Disk I/O latency is in the millisecond range. Memory access is in the nanosecond range — orders of magnitude apart. Redis in RAM + efficient data structure implementations keeps typical operations under 1ms.

Rich Data Types

This is what separates Redis from a simple key-value store:

String: The basic type. Supports INCR/DECR — great for counters and sequence generation.

Hash: Like an object. Store user data efficiently: HSET user:123 name Alice email alice@example.com

List: Ordered, supports push/pop from both ends. Good for task queues, activity feeds.

Set: Unordered, unique members. Supports intersection/union/difference. Good for tags, mutual friends.

Sorted Set (ZSet): Each member has a score, sorted by score. Leaderboards, priority queues.

HyperLogLog: Estimates set cardinality with minimal memory. “How many unique users today?”

Stream: Persistent message stream — a lightweight alternative to Kafka for many use cases.

Geospatial: Store coordinates, calculate distances, range queries.

Persistence: Not Just a Cache

Redis has two persistence mechanisms:

RDB (Redis Database): Periodic memory snapshots saved to disk. Fast recovery, compact files — but data since the last snapshot can be lost.

AOF (Append-Only File): Every write command appended to a log. Replay on restart for recovery. More complete data, larger files, slower recovery.

You can enable both: AOF for data safety, RDB to accelerate recovery.

Common Use Cases

Caching: Put Redis in front of your database to cache hot data and reduce query load.

Session management: Store user sessions in Redis — fast access with TTL for automatic expiration.

Rate limiting: INCR + EXPIRE for sliding window counters. Clean API rate limiting in a few lines.

Leaderboards: Sorted Set’s natural application — O(log N) score update, O(log N + M) top-K retrieval.

Pub/Sub: Simple publish-subscribe for notifications and lightweight event-driven architectures.

Distributed locks: SET NX EX for distributed locking. RedLock algorithm for high availability.

Redis vs. Memcached

Both are in-memory caches, but:

RedisMemcached
Data typesMultiple (String/Hash/List/Set/…)String only
PersistenceYes (RDB/AOF)No
ClusteringNative supportClient-side sharding
Pub/SubSupportedNot supported
Use casesCache + much morePure cache

Unless your use case is extremely simple — pure string cache, no persistence, no advanced features — there’s almost no reason to choose Memcached over Redis.

2026: Redis as AI Agent Memory Layer

Redis announced a Memory Layer for enterprise AI Agents in May 2026. The fit is natural: AI Agents need to maintain state across conversations and tasks, and Redis’s speed and rich data types make it an ideal store for agent working memory.

About 43% of enterprise AI agent stacks already include Redis as a component.

Summary

Redis’s popularity isn’t mysterious: it does one thing extremely well — letting you access data of various structures at memory speed. That foundation is solid enough to support a dozen use cases.

If any part of your system needs fast, reliable, low-latency data access, Redis is almost always worth considering first.

References

🇺🇸 English

Redis consistently lands near the top of Stack Overflow's most-loved databases survey. Not because it has the most features, and definitely not because it's the cheapest option. It's there because it does what it does with almost eerie reliability — and once you understand *how* it works, the popularity stops being a mystery.

So let's get into it.

Redis stands for Remote Dictionary Server. The official description is "in-memory data structure store," and that phrase actually tells you everything you need to know — if you break it apart. In-memory means all your data lives in RAM. Data structure store means it's not just dumb key-value pairs — it supports a whole family of types. And store means it can act as a cache, a database, a message queue, or all three at once.

Most engineers first meet Redis as a cache layer in front of a database. That's a fine introduction, but it undersells what's actually there.

---

Here's the thing that surprises people: Redis is single-threaded. One thread, handling every command, one at a time. That sounds like a disaster for performance, right? Traditional databases throw threads at concurrency. How does Redis handle hundreds of thousands of requests per second with just one?

The answer is that Redis's bottleneck isn't the CPU — it's I/O. And since all the data is already in memory, every operation finishes so fast that the overhead of managing multiple threads would actually *hurt* more than it helps. Single-threaded means no locks, no deadlocks, no race conditions. Every command is atomic, automatically. You never have to worry about two operations stomping on each other.

Redis 6 introduced multi-threaded *network* I/O — so it can receive and send data on multiple threads — but the actual business logic, the part where commands execute and data changes, stays single-threaded. That's a clever line to walk: more throughput without giving up the simplicity.

And then there's just the physics. Disk access takes milliseconds. Memory access takes nanoseconds. That's not a small gap — it's orders of magnitude. Redis sitting entirely in RAM, with tight data structure implementations, routinely delivers sub-millisecond latency. Not as a best case. As the norm.

---

What really separates Redis from a simple key-value store is its type system. Let's walk through the important ones.

Strings are the baseline — but even they're more capable than you'd think. Redis strings support atomic increment and decrement operations, which makes them perfect for counters, rate limiters, and sequence generation.

Hashes let you store an object's fields under one key. Think of it like storing a user record — name, email, preferences — all grouped together efficiently.

Lists are ordered sequences where you can push and pop from either end. Classic use case: task queues, or an activity feed.

Sets store unique members with no ordering. The power here is set math — intersections, unions, differences. Great for things like "which users follow both of these topics" or "mutual friends."

Then there's the Sorted Set, which might be the most elegant data structure in Redis. Every member has a numeric score, and Redis keeps them sorted by that score automatically. Leaderboards are the textbook example — updating a score and fetching the top ten players are both fast operations regardless of how many players you have.

Beyond those, there's HyperLogLog for approximate cardinality — answering "how many unique visitors today" with very little memory. There's Streams, which gives you a persistent message log — a lightweight Kafka alternative for many use cases. And there's geospatial support for storing coordinates and running distance queries.

That breadth is why Redis ends up in so many different places in a stack.

---

One thing that surprises people who think of Redis purely as a cache: it has real persistence options.

The first is RDB — periodic snapshots of the entire dataset written to disk. Fast to restore, compact on disk, but if Redis goes down between snapshots, you lose whatever happened in that window.

The second is AOF — Append-Only File — where every write command gets logged as it happens. On restart, Redis replays the log to reconstruct state. More complete, but larger files and slower recovery.

The smart production setup uses both: AOF for data safety so you don't lose writes, RDB to make recovery fast when you need to bring the system back up. Redis isn't purely ephemeral — it can be a proper database if you need it to be.

---

Let's talk about the comparison everyone asks about: Redis versus Memcached.

They're both in-memory caches, so the surface similarity is real. But the gap in capability is significant. Memcached stores strings only. No persistence. No pub/sub. No native clustering — you shard on the client side. Redis does all of that, plus the rich data types we just covered.

The honest summary: unless your use case is so narrow that you only need a plain string cache with zero other requirements, there's almost no reason to reach for Memcached over Redis in 2026.

---

Speaking of 2026 — there's a new angle worth mentioning. Redis announced a Memory Layer specifically for enterprise AI agents. And the fit makes a lot of sense: AI agents need to maintain state across conversations and tasks, they need fast reads and writes, and they benefit from the kind of structured data storage Redis already provides. About 43% of enterprise AI agent stacks already include Redis as a component. It started as a cache layer for web apps, and it's quietly become infrastructure for AI systems too.

---

So what's the actual takeaway?

First: Redis is fast because of where data lives, not just how it's built. Memory access versus disk access is a fundamental physical difference, and Redis is designed to exploit that gap.

Second: the single-threaded architecture isn't a bug — it's a feature. Atomic operations, no locking complexity, predictable performance. The threading model is what makes Redis reliable at scale.

Third: the data type richness is what makes Redis genuinely general-purpose. It's not just a cache in front of your database. It's a tool that can replace several different specialized systems for the right use cases.

If any layer of your system needs fast, structured, low-latency data access — Redis is almost always the first thing worth evaluating.

🇹🇼 中文

Redis 在 Stack Overflow 的開發者調查裡,「最受喜愛的資料庫」幾乎常駐前三名。這不是因為它功能最多,也不是因為它最便宜——是因為它把它能做的事,做得極好,而且做得可預測。

那 Redis 到底是什麼?很多人第一次接觸它是把它當快取用,但這只說對了一小部分。官方的定義是「in-memory data structure store」,拆開來看:資料全部存在 RAM 裡、不只是 key-value 而是支援多種資料型別、而且可以當資料庫用,不是「關機就消失」那種。

講到為什麼快,有兩個關鍵。第一個是單執行緒事件迴圈。聽起來像缺點,但 Redis 的瓶頸根本不在 CPU,在 I/O。所有資料都在記憶體,每個操作都極快,用不著多執行緒。而且單執行緒的好處是沒有鎖、沒有死鎖、沒有競態條件,每個命令原子執行,行為完全可預期。Redis 6.0 之後網路 I/O 改成多執行緒處理,吞吐量提升,但業務邏輯本身還是單執行緒。第二個原因更直觀:硬碟存取是毫秒等級,記憶體存取是奈秒等級,差了幾個數量級,這就是 Redis 一般操作可以壓在一毫秒以內的根本原因。

再來說它跟一般 key-value store 最大的差別——豐富的資料型別。String 支援原子遞增遞減,拿來做計數器很自然。Hash 像物件,適合存使用者資料。List 支援兩端 push 和 pop,天然的任務佇列。Set 是無序不重複集合,做標籤、共同好友很順手。Sorted Set 是最有趣的,每個成員帶一個分數、按分數排序,排行榜的實作幾乎就是為它量身設計的。除此之外還有 Stream 做輕量訊息流、HyperLogLog 用極少記憶體估算不重複使用者數量、以及內建地理座標和距離查詢的 Geospatial。

關於持久化,Redis 有兩種方式。RDB 是定期快照,恢復快、體積小,但最後一次快照之後的資料有可能遺失。AOF 是把每個寫入命令都追加到 log 裡,重啟時重放恢復,資料更完整但檔案比較大。實務上兩種可以同時開,用 AOF 保安全、用 RDB 加速恢復。

使用場景方面,最經典的當然是快取——在資料庫前放一層 Redis,熱點資料不用每次打到 DB。Session 管理也非常適合,支援 TTL 自動過期,Web 應用幾乎都這樣用。速率限制更是幾行程式碼就能搞定,用原子遞增加上過期時間做滑動視窗計數。分散式鎖也是常見用途,用原子性的 set if not exists 加過期時間實作,高可用版本搭配 RedLock 演算法。

那跟 Memcached 呢?兩個都是 in-memory,但 Memcached 只支援 String,沒有持久化,叢集需要客戶端自己分片。除非你的場景極度單純,只需要 String 快取、完全不需要進階功能,否則幾乎沒有選 Memcached 的理由。

最後一件有意思的事:Redis 在 2026 年 5 月宣布推出針對企業 AI Agent 的記憶層。AI Agent 需要在多輪對話和任務之間維持狀態,Redis 的速度加上豐富的資料型別,讓它成為存放 Agent 工作記憶的自然選擇。目前大約 43% 的企業 AI Agent 系統已經在用 Redis 作為元件之一。

整理一下核心要點。第一,Redis 快的根本原因是記憶體存取加上單執行緒原子操作,行為可預測才是它受歡迎的深層原因。第二,它的資料型別夠豐富,一個工具就能解決快取、計數器、排行榜、訊息佇列、分散式鎖等十幾種場景。第三,持久化讓它不只是快取,而是可以作為主要資料存儲使用,而且在 AI Agent 記憶層這個新方向上,它的定位只會越來越重要。

Tags

Related Articles