1. Introduction to Redis
This chapter starts from first principles. It covers what Redis is, why keeping data in memory changes how an application performs, and how the key-value model differs from relational databases and from other caches. It then shows how to install, run and configure a server, and how to talk to it through the CLI, the wire protocol and a GUI. Every later chapter builds on the server, client and command model introduced here.
<a id="11-core-concepts"></a>
1.1 Core Concepts
This subchapter explains what Redis is, the design decisions behind it, and where it fits among other data stores.
What Is Redis
Most applications keep reading and writing small pieces of state: a user's session, a page-view counter, a cached product page, a job waiting to be processed. That state has to be shared across every application instance, and reading it should take microseconds to a few milliseconds. Process-local memory is fast but not shared: each server has its own copy, and the copy disappears on restart. A relational database is shared, but every query pays for SQL parsing, planning, page lookups, locking and often disk I/O. Redis fills the gap between them. It is a shared, networked store that keeps its data in memory.
Redis (REmote DIctionary Server) is an in-memory data structure store. Salvatore Sanfilippo ("antirez") created it in 2009 to power a real-time web analytics tool. People often call it a data structure server. Values are not opaque blobs: they are typed structures (strings, lists, hashes, sets, sorted sets, streams and more), and Redis exposes operations on those structures that run on the server, where the data lives.
An analogy: picture a giant hash map, like a Python dict or a Java HashMap, running in its own process. Every application instance can reach it over the network, its values can be lists, sets or ranked collections as well as strings, and you call methods on those values remotely. LPUSH queue job1 is the remote version of queue.insert(0, "job1").
How it works at a high level:
- Clients connect over TCP (default port 6379) and send commands encoded in the RESP protocol.
- The server runs commands one at a time on its main thread, inside an event loop. Each command is therefore atomic: no other command can run halfway through it.
- The dataset lives in RAM. Writing it to disk (RDB snapshots, an append-only file) is optional and serves recovery, not reads.
- Replication, Sentinel and Cluster add redundancy, failover and sharding.
+-------------+ +-------------+ +-------------+
| App node 1 | | App node 2 | | Worker |
+------+------+ +------+------+ +------+------+
| TCP + RESP | |
+-------------------+-------------------+
|
+---------------v----------------+
| Redis server |
| main thread executes one |
| command at a time against |
| the in-memory dataset |
| |
| strings lists hashes |
| sets sorted sets streams |
+---------------+----------------+
| optional
+-------v-------+
| RDB / AOF | persistence files on disk
+---------------+
Example
A redis-cli session that uses several data types:
127.0.0.1:6379> SET greeting "hello"
OK
127.0.0.1:6379> GET greeting
"hello"
127.0.0.1:6379> INCR page:views
(integer) 1
127.0.0.1:6379> LPUSH tasks "send-email" "resize-image"
(integer) 2
127.0.0.1:6379> ZADD leaderboard 100 alice 85 bob 92 carol
(integer) 3
127.0.0.1:6379> ZRANGE leaderboard 0 1 REV WITHSCORES
1) "alice"
2) "100"
3) "carol"
4) "92"
The same operations from Python, using the redis-py client:
import redis
# decode_responses=True turns the bytes coming back from the server into str
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r.set("greeting", "hello")
print(r.get("greeting")) # hello
r.incr("page:views") # the increment runs atomically on the server
r.zadd("leaderboard", {"alice": 100, "bob": 85, "carol": 92})
# Ranking happens inside Redis; only the top 2 entries cross the network
print(r.zrange("leaderboard", 0, 1, desc=True, withscores=True))
# [('alice', 100.0), ('carol', 92.0)]
The main capabilities, all of which later chapters cover:
| Capability | What it gives you | Chapter |
|---|---|---|
| Rich data types | Server-side operations on lists, sets, hashes, sorted sets, streams | 2, 3 |
| Atomic commands | Each command runs without interleaving | 4, 5 |
| Persistence | RDB snapshots and append-only file | 6 |
| Replication and failover | Replicas and Sentinel | 7 |
| Sharding | Redis Cluster with hash slots | 7 |
| Messaging | Pub/Sub and Streams | 3, 4 |
| Programmability | Lua scripts, Functions, modules | 4, 12 |
| Search and vectors | Query engine, vector search | 9 |
Key Takeaways
- Redis is an in-memory, networked data structure server, not just a string cache.
- Operations run where the data lives, so less data crosses the network and fewer round trips are needed.
- Commands run one at a time on a main thread, which makes each one atomic.
- Persistence, replication and clustering are optional layers on top of the in-memory core.
Practice
- (Beginner) In your own words, explain why Redis is called a "data structure server" and not just a key-value cache. Name two data types and one server-side operation for each.
- (Intermediate) Using
redis-cli, create a sorted set of five players with scores and fetch the top three. Then explain why ranking inside Redis beats fetching every score into the application and sorting there, in terms of both network traffic and concurrency.- (Interview) "Redis runs commands on a single thread. How can it still serve hundreds of thousands of operations per second?" Hint: think about where a typical request spends its time (memory access, network I/O, disk) and what an event loop with I/O multiplexing gives you.
In-Memory Data Stores
The reason to keep data in memory is physics. Each layer of the storage hierarchy is orders of magnitude slower than the one above it:
| Operation | Approximate latency |
|---|---|
| CPU L1 cache reference | ~1 ns |
| Main memory (RAM) reference | ~100 ns |
| NVMe SSD random read | ~20-100 µs |
| HDD random seek | ~5-10 ms |
| Network round trip within a data center | ~100-500 µs |
When the data is in RAM, finding a value takes nanoseconds to microseconds. The network round trip, not storage, becomes the bottleneck.
An in-memory data store keeps the primary copy of its dataset in RAM and serves every read and write from there. Disk, if used at all, is for durability and recovery. A disk-based database works the other way round: its data structures (B-tree pages, heap files) live on disk, and a buffer pool caches hot pages in memory. A cache miss there means disk I/O.
An analogy: a chef's countertop against a basement pantry. The countertop (RAM) holds far less, but everything on it is within arm's reach. The pantry (disk) holds everything, but each trip takes time. Redis keeps the whole working set on the countertop.
Disk-based RDBMS read (buffer pool miss):
client -> network -> parse SQL -> plan -> buffer pool lookup -> disk I/O -> row -> network -> client
Redis read:
client -> network -> parse RESP -> hash table lookup in RAM -> network -> client
Keeping data in memory has trade-offs:
| Benefit | Cost |
|---|---|
| Sub-millisecond latency, very high throughput | Capacity limited by RAM, which costs more per GB than disk |
| Data structures tuned for memory (pointers, skiplists, compact encodings) | Data written since the last persistence point can be lost in a crash |
| No disk I/O on the request path | Restarts must reload the dataset from disk, which takes time |
When memory fills up, Redis follows its maxmemory setting and an eviction policy: it either rejects writes or evicts keys (chapter 5.3).
Example
Watch latency and memory on a running instance:
# Continuously sample round-trip latency to the server (min/max/avg in ms)
redis-cli --latency
# Quick throughput test for SET and GET; results depend heavily on hardware and network
redis-benchmark -t set,get -n 100000 -q
# Inspect memory usage
redis-cli INFO memory | grep -E "used_memory_human|maxmemory_human|maxmemory_policy"
Load 100,000 keys quickly with the bulk-loading pipe mode, then check memory again:
# Generate commands and stream them to Redis in pipe mode (much faster than one call per key)
for i in $(seq 1 100000); do echo "SET key:$i value-$i"; done | redis-cli --pipe
redis-cli INFO memory | grep used_memory_human
redis-cli DBSIZE # (integer) 100000 if the database started empty
Key Takeaways
- RAM access is roughly 1,000 times faster than SSD and 100,000 times faster than HDD seeks.
- With in-memory storage, network round trips usually dominate request latency.
- Disk exists for durability and restart recovery, not for serving requests.
- The dataset must fit in memory, so plan capacity and choose an eviction policy.
Practice
- (Beginner) Using the latency table, estimate how many sequential reads per second one client could do against (a) a store that seeks on an HDD for every read and (b) Redis over a 0.5 ms network round trip. What dominates the cost in case (b)?
- (Intermediate) Record
used_memory_human, load 100,000 keys with the--pipeloop above, and record it again. Work out the average memory per key and explain why it is larger than the raw size of the key and value strings.- (Interview) "If Redis keeps everything in memory, what happens to your data on restart, and how do you decide whether that is acceptable?" Hint: separate "cache in front of a source of truth" from "primary store", and think about the data-loss window each persistence mode allows.
Key-Value Model
The most common access pattern in applications is "give me the thing with this identifier": the session for this token, the profile for this user ID. If every access goes through a known key, a store can drop query planners, schemas and joins and use a hash table instead. Lookups are O(1) on average, and sharding becomes simple because each key can be routed to a server on its own.
In Redis, each logical database is one flat keyspace: a dictionary that maps keys to values.
- A key is a binary-safe string of up to 512 MB. In practice, keep keys short and readable.
- A value is a typed data structure. The type belongs to the value, and commands are type-specific: running a list command on a hash returns a
WRONGTYPEerror. - There are no tables, schemas or built-in secondary indexes. You get structure through key naming conventions, usually colon-separated namespaces such as
user:1001:profile(chapter 2.1). - Keys are created implicitly on the first write. Aggregate types (lists, sets, hashes, sorted sets) are deleted automatically when their last element is removed. Streams are the exception and can exist empty.
- Any key can have a time to live (TTL), after which Redis deletes it.
An analogy: a coat check. You hand over a ticket (the key) and get exactly your coat (the value) back immediately. The attendant cannot efficiently find "all blue coats" unless you organized a separate rack for them yourself, which is the role of a secondary index.
Keyspace (database 0)
+---------------------+-------------+----------------------------------+
| Key | Type | Value |
+---------------------+-------------+----------------------------------+
| greeting | string | "hello" |
| user:1001 | hash | {name: "Ada", plan: "pro"} |
| queue:emails | list | ["msg3", "msg2", "msg1"] |
| post:42:likes | set | {1001, 1002, 1007} |
| leaderboard:2026 | sorted set | {ada: 950, bob: 870} |
| session:9f2c | string | "<serialized session>" TTL 1800 |
+---------------------+-------------+----------------------------------+
Example
Types, type errors, implicit creation and deletion, and TTLs:
127.0.0.1:6379> HSET user:1001 name "Ada" plan "pro"
(integer) 2
127.0.0.1:6379> TYPE user:1001
hash
127.0.0.1:6379> GET user:1001
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> RPUSH tmp a
(integer) 1
127.0.0.1:6379> RPOP tmp
"a"
127.0.0.1:6379> EXISTS tmp
(integer) 0
127.0.0.1:6379> SET session:9f2c "payload" EX 1800
OK
127.0.0.1:6379> TTL session:9f2c
(integer) 1800
In the transcript, the list key tmp disappeared when its last element was popped.
Because you can only look things up by key, you design keys around your queries. To find users by email, keep a second key that points to the primary one, and update both atomically:
import redis
r = redis.Redis(decode_responses=True)
def create_user(user_id: int, name: str, email: str) -> None:
pipe = r.pipeline(transaction=True) # MULTI/EXEC: both writes apply together
pipe.hset(f"user:{user_id}", mapping={"name": name, "email": email})
pipe.set(f"user:email:{email.lower()}", user_id) # secondary "index" key: email -> id
pipe.execute()
def find_user_by_email(email: str) -> dict | None:
user_id = r.get(f"user:email:{email.lower()}") # first lookup: email -> id
return r.hgetall(f"user:{user_id}") if user_id else None # second lookup: id -> data
Key Takeaways
- The keyspace is a flat dictionary. Keys are binary-safe strings, values are typed structures.
- Commands are type-specific, and a mismatch produces a
WRONGTYPEerror. - Keys appear on the first write, and empty aggregates disappear automatically.
- Design keys around access patterns. Any extra lookup path needs extra keys that you maintain.
Practice
- (Beginner) Propose key names for: a user's profile, the set of a user's followers, and a per-day page-view counter for article 77.
- (Intermediate) Create a set with three members, remove them one by one with
SREM, and runEXISTSafter each removal. Explain the result, and explain why this behavior matters when your code usesEXISTSto check whether something was "initialized".- (Interview) "How would you support looking up users both by ID and by email in a pure key-value store?" Hint: consider storing more than one key per entity, what happens if the email changes, and how to keep the keys consistent.
Redis vs Relational Databases
Redis and relational databases (PostgreSQL, MySQL) are usually complementary. Knowing where each is strong keeps you from forcing one to do the other's job.
A relational database is built for flexible querying over durable, structured data. You declare a schema, model relationships with foreign keys, and ask arbitrary questions in SQL. Redis is built for very fast operations along access paths you design in advance. You decide the keys and structures up front, and every command touches known keys.
An analogy: a library with a full catalog against a bank of numbered lockers. In the library you can find books by author, subject or year, but each search takes some effort. A locker opens instantly if you know its number, and you cannot ask the lockers to "list everything about databases".
| Aspect | Relational database | Redis |
|---|---|---|
| Data model | Tables, rows, typed columns, relations | Keys mapped to data structures |
| Schema | Declared and enforced | None. The application defines structure |
| Querying | Declarative SQL, ad hoc queries, joins | One command per operation on known keys. No joins |
| Primary storage | Disk, with a memory buffer cache | Memory, with optional disk persistence |
| Latency | Milliseconds (higher for complex queries) | Sub-millisecond |
| Durability | Write-ahead log flushed on commit | Configurable. Some data loss is possible by default |
| Transactions | ACID, isolation levels, rollback | MULTI/EXEC runs a queued batch atomically. No rollback |
| Integrity | Constraints, foreign keys | None. The application enforces it |
| Capacity | Terabytes on disk | Bounded by RAM (or the RAM of a cluster) |
| Scaling writes | Mostly vertical. Sharding is harder | Built-in sharding with Redis Cluster |
A common architecture uses both: the relational database is the system of record, and Redis sits in front of it as a cache. Redis also acts as the primary store for data that is ephemeral, high-churn or naturally key-based, such as sessions, rate-limit counters and leaderboards.
Example
A leaderboard is natural in Redis and costly in SQL under heavy write load:
-- Relational: needs an index on score. Every update maintains the index,
-- and each read runs a query through the planner.
UPDATE scores SET score = score + 10 WHERE player = 'ada';
SELECT player, score FROM scores ORDER BY score DESC LIMIT 10;
127.0.0.1:6379> ZINCRBY scores 10 ada
"960"
127.0.0.1:6379> ZRANGE scores 0 9 REV WITHSCORES
1) "ada"
2) "960"
...
The reverse case is an ad hoc analytical question:
-- Trivial in SQL, but in plain Redis you would have to precompute indexes for every filter
SELECT u.city, COUNT(*), SUM(o.total)
FROM users u JOIN orders o ON o.user_id = u.id
WHERE u.signup_date >= '2026-09-01' AND o.total > 100
GROUP BY u.city;
Key Takeaways
- Relational databases are good at durable, ad hoc querying and integrity. Redis is good at fast operations on known keys.
- In Redis, you design keys and structures around the queries before writing data.
- Redis transactions do not roll back, and default persistence can lose recent writes.
- Most production systems use both: SQL as the source of truth, and Redis for caching and real-time state.
Practice
- (Beginner) For each workload, choose Redis, a relational database, or both, and justify the choice: a bank ledger, a shopping cart for anonymous visitors, a product catalog with faceted filters, API rate-limit counters, monthly financial reports.
- (Intermediate) Write the SQL and Redis versions of "increment a page-view counter". Discuss what happens at 10,000 concurrent increments per second in each system, considering row locks, the write-ahead log and atomic
INCR.- (Interview) "Why not use Redis as your only database?" Hint: consider durability guarantees, cost per GB, ad hoc querying, referential integrity, and what happens to replicated writes during a failover.
Redis vs Memcached
Before Redis, Memcached (2003) was the default distributed cache for web applications. Both keep data in memory and both are used for caching, so teams often compare them. They come from different philosophies: Memcached is a deliberately minimal cache of opaque blobs, while Redis is a feature-rich data structure server.
Memcached's main traits:
- It is multithreaded and scales across CPU cores on a single node.
- Values are opaque byte strings (1 MB maximum item size by default), and keys are limited to 250 bytes.
- It has no persistence, no built-in replication and no server-side data structures.
- Distribution is client-side: clients use consistent hashing to pick a server.
- Its command set is small:
get,set,add,replace,delete,incr/decr,append,cas,touch.
| Feature | Memcached | Redis |
|---|---|---|
| Data types | Opaque strings | Strings, lists, hashes, sets, sorted sets, streams, JSON, and more |
| Max value size | 1 MB default (configurable) | 512 MB |
| Threading | Multithreaded | Single-threaded command execution, optional I/O threads |
| Persistence | None | RDB snapshots, AOF |
| Replication and failover | None built in | Replicas, Sentinel, Cluster |
| Sharding | Client-side | Client-side or Redis Cluster |
| Eviction | LRU (segmented) | Several policies (LRU, LFU, TTL, random, none) |
| Messaging and scripting | None | Pub/Sub, Streams, Lua, Functions |
| Partial updates | No (read-modify-write the whole blob) | Yes (for example, HSET on one field) |
Analogy: Memcached is a fleet of identical storage boxes. You put sealed packages in and take sealed packages out. Redis is a workshop where you can open a package, change one part and close it again without carrying it home.
Example
Updating one field of a cached user object:
import json
# Memcached (pymemcache): read-modify-write of an opaque blob, guarded by CAS
from pymemcache.client.base import Client
mc = Client(("localhost", 11211))
value, cas_token = mc.gets("user:1001") # fetch the value and its CAS token
user = json.loads(value)
user["plan"] = "pro"
ok = mc.cas("user:1001", json.dumps(user), cas_token) # fails if another client wrote first
# if not ok: re-read and retry, adding round trips under contention
# Redis: update one field in place, atomically, on the server
import redis
r = redis.Redis(decode_responses=True)
r.hset("user:1001", "plan", "pro")
Memcached is still a reasonable choice for a pure, simple cache of large serialized objects, where multithreaded throughput per node and operational simplicity matter most, or when it is already in your stack. For anything that needs data structures, persistence or replication, Redis is the usual choice.
Key Takeaways
- Memcached is a minimal, multithreaded cache of opaque values. Redis is a data structure server.
- Redis supports partial, server-side updates. Memcached needs read-modify-write cycles.
- Redis has persistence, replication, clustering, messaging and scripting. Memcached has none of these built in.
- Multithreading helps CPU-bound workloads, but most cache workloads are bound by network round trips.
Practice
- (Beginner) List three Redis features you would lose by migrating a cache to Memcached, and name an application feature that depends on each.
- (Intermediate) Explain why Memcached's multithreading does not automatically make it faster than Redis for every workload. Compare a workload of many tiny
GETs from many clients with one where each request does heavy CPU work on the server.- (Interview) "Your team uses Memcached and now wants a real-time leaderboard. Would you build it on Memcached or introduce Redis?" Hint: identify the operation a leaderboard needs on every update and every read, and what it costs to do that on the client.
Common Use Cases
Knowing the standard use cases helps you recognize which data type a problem needs. Most Redis deployments combine a few well-known patterns, each of which maps to specific structures and commands:
| Use case | Why Redis fits | Main types and commands | Chapter |
|---|---|---|---|
| Caching | Sub-millisecond reads, TTLs, automatic eviction | Strings or hashes. SET ... EX, GET | 8.1 |
| Session storage | Fast per-request lookup with expiration | Hashes. HSET, EXPIRE | 8.3 |
| Rate limiting | Atomic counters with expiry | INCR, EXPIRE, sorted sets | 8.2 |
| Leaderboards | Always-sorted collections | Sorted sets. ZADD, ZINCRBY, ZRANGE | 2.6 |
| Queues and jobs | Push/pop, blocking reads, consumer groups | Lists, Streams | 2.3, 3.4 |
| Real-time messaging | Fan-out to subscribers | Pub/Sub | 4.1 |
| Distributed locks | Atomic set-if-absent with expiry | SET key val NX PX ms | 8.2 |
| Unique counting | Approximate cardinality in 12 KB | HyperLogLog | 3.2 |
| Proximity search | Radius and box queries | Geo commands | 3.3 |
| Vector search and AI | Embeddings, semantic caching | Query engine, vector sets | 9 |
Redis is a poor fit for large analytical scans, datasets far bigger than your memory budget, strict multi-entity ACID requirements, and data with complex relationships queried in unpredictable ways.
Example
Cache-aside, the most common caching pattern:
1. GET product:42
App --------------------------> Redis
| <-- hit: return the value
| <-- miss: (nil)
|
| 2. SELECT ... WHERE id = 42
+------------------------------> PostgreSQL
|
| 3. SET product:42 <json> EX 300
+------------------------------> Redis
import json
import time
import redis
r = redis.Redis(decode_responses=True)
def get_product(product_id: int, db) -> dict:
key = f"product:{product_id}"
cached = r.get(key)
if cached is not None: # cache hit: no database work at all
return json.loads(cached)
product = db.fetch_product(product_id) # cache miss: read from the source of truth
r.set(key, json.dumps(product), ex=300) # 5-minute TTL so stale entries age out
return product
A fixed-window rate limiter, which allows limit requests per user per minute:
def allow_request(user_id: str, limit: int = 100) -> bool:
window = int(time.time() // 60) # current minute number
key = f"ratelimit:{user_id}:{window}" # one counter per user per minute
pipe = r.pipeline() # transaction=True by default: MULTI/EXEC
pipe.incr(key) # atomic increment, creates the key at 1
pipe.expire(key, 60) # the counter cleans itself up
count, _ = pipe.execute()
return count <= limit
Key Takeaways
- Each common use case maps to a specific data type. Learning those mappings is most of the work in Redis design.
- Caching, sessions, rate limiting, leaderboards and queues make up most real-world usage.
- TTLs and atomic commands are what make Redis good at short-lived, high-churn state.
- Avoid Redis for ad hoc analytics, very large cold datasets, and complex relational integrity.
Practice
- (Beginner) Name the Redis data type you would use for: "users online right now", "the last 50 notifications per user", and "an approximate count of unique visitors per day".
- (Intermediate) Implement
get_productabove against a fake database (a Python dict with an artificialtime.sleep). Add anupdate_productfunction that invalidates the cache entry, and measure latency on a hit and on a miss.- (Interview) "Design a rate limiter that allows 100 requests per minute per API key." Hint: start with the
INCR+EXPIREfixed window, then discuss the burst allowed at window boundaries and how a sorted set of timestamps fixes it.
Redis Licensing and Forks (Valkey)
Licensing decides whether you may use, modify or offer Redis as a service. It is the reason cloud providers now offer two engine families, and it determines which binaries you install. A Redis engineer should know the history and the practical consequences.
Timeline
| Date | Event |
|---|---|
| 2009-2024 | Redis core is released under the permissive BSD 3-Clause license |
| 2018 | Redis Labs relicenses some add-on modules (Commons Clause, later RSAL). The core stays BSD |
| March 2024 | Starting with Redis 7.4, the core is dual-licensed under RSALv2 and SSPLv1. Neither is an OSI-approved open-source license |
| March-April 2024 | The Linux Foundation launches Valkey, a BSD-licensed fork of Redis 7.2.4 backed by AWS, Google Cloud, Oracle, Ericsson, Snap and others |
| Sept 2024 onward | Valkey 8.x adds performance work (improved I/O threading, a new hash table). Valkey 9.0 follows with features such as hash field expiration and multiple databases in cluster mode |
| May 2025 | Redis 8.0 adds AGPLv3 as a third license option (RSALv2 / SSPLv1 / AGPLv3) and folds the former Redis Stack capabilities (JSON, query engine, time series, probabilistic structures) plus vector sets into "Redis Open Source" |
What the licenses mean in practice
| License | Key condition (simplified, not legal advice) |
|---|---|
| BSD 3-Clause | Use, modify and redistribute freely, keeping the copyright notice |
| RSALv2 | Free to use in your own products, but you may not offer Redis itself as a managed service to third parties |
| SSPLv1 | If you offer it as a service, you must release the source of your whole service stack |
| AGPLv3 | If users interact with a modified version over a network, you must provide the modified source |
For a typical company that runs Redis inside its own application, any of these licenses usually works. The restrictions mostly affect vendors that sell Redis as a hosted service, which is why the major clouds moved to Valkey.
Other forks and compatible servers
| Project | Relationship to Redis | License |
|---|---|---|
| Valkey | Fork of Redis 7.2.4, Linux Foundation | BSD 3-Clause |
| Redict | Fork of Redis 7.2.4 | LGPLv3 |
| KeyDB | Older multithreaded fork | BSD 3-Clause |
| Dragonfly | Independent reimplementation, protocol-compatible | BSL 1.1 |
| Microsoft Garnet | Independent reimplementation (C#), RESP-compatible | MIT |
Valkey kept the RESP protocol and the full command set of Redis 7.2, so existing clients and tools work unchanged. Since the fork, the two projects have diverged. Redis 8 has integrated modules and vector sets. Valkey has its own modules (such as valkey-json, valkey-bloom and valkey-search) and its own performance and cluster improvements. Check each feature you rely on against the specific engine and version.
Example
Valkey works as a drop-in replacement for 7.2-era workloads:
# Run Valkey next to Redis on a different host port
docker run -d --name valkey -p 6380:6379 valkey/valkey:latest
# The same redis-cli and client libraries work
redis-cli -p 6380 PING # PONG
# Valkey reports a Redis-compatible version for older clients, plus its own version
redis-cli -p 6380 INFO server | grep -E "server_name|redis_version|valkey_version"
Key Takeaways
- Redis was BSD-licensed until 7.2. Version 7.4 moved to RSALv2/SSPLv1, and 8.0 added AGPLv3 as an option.
- Valkey is the BSD-licensed, Linux Foundation fork of Redis 7.2.4 that most cloud providers back.
- Clients and commands from the 7.2 era work on both, but newer features differ between the engines.
- License restrictions mainly affect anyone offering the database as a service. Internal use is usually unaffected.
Practice
- (Beginner) Run
redis-server --versionon your installation and find out which license applies to that version.- (Intermediate) Your company sells a hosted "cache-as-a-service" product. Explain which Redis licenses would constrain you and why Valkey might be the safer engine. Then describe the opposite case: an internal application that needs Redis 8's integrated query engine.
- (Interview) "Why did the major cloud providers back Valkey after the 2024 license change?" Hint: read what RSALv2 and SSPLv1 say about offering the software as a managed service, and consider who sells managed Redis.
<a id="12-installation-and-setup"></a>
1.2 Installation and Setup
This subchapter shows how to get a working server on a laptop, in containers and in the cloud, and how its behavior is configured.
Installing on Linux and macOS
There are several ways to install Redis, and they differ in how current the version is and who controls upgrades. Distribution packages are convenient but often several releases behind. Official vendor repositories track current releases. Building from source gives full control and is the standard path for development and testing of specific versions.
| Method | Pros | Cons |
|---|---|---|
Distribution package (apt install redis-server) | One command, integrated with systemd | Often an older version. Some distributions (for example Fedora 41+) now ship Valkey as the default "redis" |
| Official Redis APT/RPM repository | Current releases, systemd integration | Requires adding a third-party repository |
| Homebrew (macOS) | Simple, current | For development, not production |
| Build from source | Any version, full control | Manual upgrades and service setup |
| Docker | Isolated, reproducible (next topic) | Requires a container runtime |
Windows has no native Redis server. Use WSL2 or Docker.
Example
Ubuntu or Debian, using the official Redis repository:
sudo apt-get install -y lsb-release curl gpg
# Add Redis's signing key and repository
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install -y redis
# The package registers a systemd service
sudo systemctl enable --now redis-server
sudo systemctl status redis-server
redis-cli ping # PONG
redis-server --version
macOS with Homebrew:
brew install redis
brew services start redis # run in the background, start at login
redis-cli ping # PONG
# Or run in the foreground using Homebrew's config file
redis-server "$(brew --prefix)/etc/redis.conf"
From source:
wget https://download.redis.io/redis-stable.tar.gz
tar -xzf redis-stable.tar.gz
cd redis-stable
make # builds src/redis-server, src/redis-cli, src/redis-benchmark
make test # optional. Runs the test suite (requires Tcl)
sudo make install # copies the binaries to /usr/local/bin
Building Redis 8 with its bundled modules (JSON, query engine and so on) needs extra toolchain dependencies and the BUILD_WITH_MODULES=yes flag. A plain make builds the core server only.
Typical file locations for a Debian/Ubuntu package install:
| Item | Path |
|---|---|
| Configuration | /etc/redis/redis.conf |
| Data directory (RDB/AOF) | /var/lib/redis |
| Log file | /var/log/redis/redis-server.log |
| Binaries | /usr/bin/redis-server, /usr/bin/redis-cli |
You can always ask a running server where its files are:
redis-cli INFO server | grep -E "redis_version|config_file|executable"
redis-cli CONFIG GET dir # the working directory, where persistence files live
Key Takeaways
- Prefer the official repository or a pinned container image over a possibly outdated distribution package.
- On Linux, package installs run as a systemd service with config in
/etc/redis/redis.conf. - Check the version and file locations with
redis-server --version,INFO serverandCONFIG GET dir. - On Windows, use WSL2 or Docker.
Practice
- (Beginner) Install Redis using one of the methods above. Verify it with
redis-cli pingand record the version and mode (redis_mode) fromINFO server.- (Intermediate) For your installation, find the config file path, log file path and data directory using only
redis-clicommands. Then confirm each path exists on disk.- (Interview) "Why might you avoid the distribution's default Redis package in production?" Hint: think about version lag, security patch timing, missing features, and who controls the upgrade schedule.
Running Redis with Docker
Containers give you a reproducible Redis with a pinned version that starts in seconds, runs in isolation, and can be thrown away. You can run several versions side by side, and the same images run on Kubernetes in production. For development, Docker is often the fastest way to get Redis.
The official redis image stores data in /data. Unless you mount a volume there, data disappears when the container is removed. You can pass server options as arguments, or mount a full config file.
A security detail: the official image turns protected mode off by default, because it expects the container network to provide isolation. If you publish the port on all interfaces of a public machine without a password, anyone can connect. Publish to 127.0.0.1 for local development, and always set authentication for anything reachable from outside.
| Image | Contents |
|---|---|
redis:8, redis:8-alpine | Redis Open Source 8, including the formerly separate Stack capabilities |
redis:7.2 | Last BSD-licensed Redis line |
redis/redis-stack | Legacy pre-8 bundle (Redis + modules + Redis Insight) |
valkey/valkey | The Valkey fork |
Example
# Quick start, reachable only from this machine
docker run -d --name redis -p 127.0.0.1:6379:6379 redis:8
# Use the redis-cli bundled inside the container
docker exec -it redis redis-cli
# Persistent data in a named volume, with the append-only file enabled
docker run -d --name redis-persist \
-p 127.0.0.1:6379:6379 \
-v redis-data:/data \
redis:8 redis-server --appendonly yes
# Use your own config file (mounted read-only)
docker run -d --name redis-conf \
-v "$PWD/redis.conf:/usr/local/etc/redis/redis.conf:ro" \
redis:8 redis-server /usr/local/etc/redis/redis.conf
A Docker Compose setup with an application, a password and a health check:
services:
redis:
image: redis:8 # pin a minor version in real projects
command: ["redis-server", "--appendonly", "yes", "--requirepass", "${REDIS_PASSWORD}"]
ports:
- "127.0.0.1:6379:6379" # publish to the host's loopback only
volumes:
- redis-data:/data # persistence survives container recreation
healthcheck:
test: ["CMD", "redis-cli", "--no-auth-warning", "-a", "${REDIS_PASSWORD}", "ping"]
interval: 5s
timeout: 3s
retries: 5
app:
build: .
environment:
REDIS_URL: "redis://:${REDIS_PASSWORD}@redis:6379/0" # "redis" resolves on the Compose network
depends_on:
redis:
condition: service_healthy # wait until PING succeeds
volumes:
redis-data:
Key Takeaways
docker run redis:8is the fastest route to a working server. Pin versions for reproducibility.- Mount a volume at
/data, or data is lost when the container is removed. - The official image disables protected mode. Bind published ports to localhost and set a password.
- In Compose, services reach Redis by service name, and health checks control startup order.
Practice
- (Beginner) Start a Redis 8 container with a named volume, write a key, then remove the container with
docker rm -f. Start a new container on the same volume and confirm the key survived. Which persistence mechanism saved it?- (Intermediate) Run
redis:7.2on port 6379 andvalkey/valkeyon port 6380 at the same time. Compare theirINFO serveroutput and list three differences.- (Interview) "A containerized Redis was found reachable from the internet with no password. What went wrong, and how do you fix it?" Hint: consider how the port was published, the image's protected-mode default, firewall rules, and authentication.
Starting redis-server
Knowing how the server process starts, where its configuration comes from, and how it stops lets you run multiple instances, debug startup problems and avoid losing data on shutdown.
redis-server reads its configuration in this order:
- Built-in defaults.
- The config file, if you pass a path as the first argument.
- Command-line options in
--directive valueform, which override the file.
Started with no arguments, Redis logs a warning that no config file was specified and runs with defaults: port 6379 and default RDB snapshot rules.
Ways to run it:
| Mode | How | When |
|---|---|---|
| Foreground | redis-server | Development and containers. Logs go to stdout |
| Daemonized | daemonize yes | Legacy init systems |
| systemd-supervised | supervised systemd plus a unit file | Linux servers. systemd handles restarts and logging |
How it stops matters for data safety:
| Action | Behavior |
|---|---|
SHUTDOWN or SIGTERM | Graceful: saves an RDB snapshot if save points are configured, flushes the AOF, then exits |
SHUTDOWN NOSAVE | Exits without saving a snapshot |
SHUTDOWN SAVE | Forces a snapshot even with no save points configured |
SIGKILL (kill -9) or a crash | Immediate. Anything not yet persisted is lost |
Example
# Foreground with defaults (Ctrl+C sends SIGINT, which triggers a graceful shutdown)
redis-server
# Config file plus overrides: other port, RDB snapshots disabled
redis-server ./redis.conf --port 6380 --save ""
# A throwaway instance with no persistence at all
redis-server --port 6390 --save "" --appendonly no
# A second instance on the same host needs its own port, data directory and pidfile
redis-server --port 6391 --dir /tmp/redis-6391 --pidfile /tmp/redis-6391.pid
# Check and stop
redis-cli -p 6380 ping # PONG
redis-cli -p 6380 shutdown
A typical startup log:
12345:C 28 Oct 2026 10:00:00.000 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12345:C 28 Oct 2026 10:00:00.000 * Redis version=8.x.x, bits=64, commit=00000000, modified=0, pid=12345, just started
12345:C 28 Oct 2026 10:00:00.000 * Configuration loaded
12345:M 28 Oct 2026 10:00:00.001 * Running mode=standalone, port=6380.
12345:M 28 Oct 2026 10:00:00.002 * Server initialized
12345:M 28 Oct 2026 10:00:00.002 * Ready to accept connections tcp
Each line starts with the PID and a role letter: M for primary, S for replica, C for a child process or early startup, X for Sentinel. Watch for startup warnings about vm.overcommit_memory and Transparent Huge Pages (chapter 12.2).
Key Takeaways
- Configuration is layered: defaults, then the config file, then command-line overrides.
- Run in the foreground in containers, and under
supervised systemdon Linux hosts. SHUTDOWNandSIGTERMpersist before exiting.SIGKILLloses unpersisted writes.- Multiple instances on one host need distinct ports, data directories, pidfiles and log files.
Practice
- (Beginner) Start two instances on ports 6379 and 6380 with different
--dirvalues. Write different keys to each and confirm they do not see each other's data.- (Intermediate) Start an instance with
--save "" --appendonly no, write keys,kill -9it, restart it and check the keys. Repeat with--appendonly yesand explain the difference.- (Interview) "What is the difference between
SHUTDOWN,SIGTERMandSIGKILLfor a Redis process in terms of data safety?" Hint: ask which of them give Redis a chance to run its persistence routine before exiting.
The redis.conf File
redis.conf controls the server's networking, security, persistence, memory limits and more. Keeping it under version control is how you make instances reproducible. The reference redis.conf that ships with the source is fully self-documented, and reading it once is worth the time.
Format rules
- One directive per line:
directive arg1 arg2 .... - Lines starting with
#are comments. Inline comments are not supported.port 6379 # defaultis parsed as extra arguments and fails at startup. - Memory units:
1k= 1,000 bytes,1kb= 1,024 bytes, and likewisem/mbandg/gb. Units are case-insensitive. include /path/file.confpulls in other files. When a directive appears more than once, the last occurrence wins, so putincludelines where you want their overrides to apply.
| Area | Directive | Purpose |
|---|---|---|
| Network | bind, port, protected-mode, timeout, tcp-keepalive | Where and how clients connect |
| Security | requirepass, aclfile | Authentication (chapter 11) |
| Process | daemonize, supervised, pidfile, logfile, loglevel | Process management and logging |
| Keyspace | databases | Number of logical databases (default 16) |
| Persistence | dir, dbfilename, save, appendonly, appendfsync | RDB/AOF behavior (chapter 6) |
| Memory | maxmemory, maxmemory-policy | Memory limit and eviction (chapter 5.3) |
| Replication | replicaof, masterauth | Replica setup (chapter 7) |
| Limits | maxclients | Maximum concurrent connections |
Example
A minimal production-style config (excerpt). Note that every comment is on its own line:
# Listen on loopback only. The "-" prefix means: do not fail if IPv6 is unavailable
bind 127.0.0.1 -::1
port 6379
protected-mode yes
# Simple shared password. ACL users are preferable (chapter 11)
requirepass change-me-to-a-long-random-secret
# Let systemd manage the process
daemonize no
supervised systemd
logfile /var/log/redis/redis-server.log
loglevel notice
databases 16
# Persistence: working directory for RDB and AOF files
dir /var/lib/redis
dbfilename dump.rdb
# Snapshot if >=1 change in 3600 s, >=100 changes in 300 s, or >=10000 changes in 60 s
save 3600 1 300 100 60 10000
appendonly yes
appendfsync everysec
# Memory limit and eviction behavior
maxmemory 2gb
maxmemory-policy allkeys-lru
# Host-specific overrides go last so they win
include /etc/redis/local-overrides.conf
Inspecting and changing configuration at runtime:
127.0.0.1:6379> CONFIG GET maxmemory maxmemory-policy
1) "maxmemory"
2) "2147483648"
3) "maxmemory-policy"
4) "allkeys-lru"
127.0.0.1:6379> CONFIG SET maxmemory 4gb
OK
127.0.0.1:6379> CONFIG REWRITE
OK
CONFIG SET changes only the running process. CONFIG REWRITE writes the current settings back into the config file so they survive a restart. Some directives (for example daemonize and databases) can only be set at startup, and sensitive ones such as dir are blocked from CONFIG SET by default through enable-protected-configs.
Key Takeaways
redis.confholds one directive per line, comments only on their own lines, and the last occurrence of a directive wins.- Know the core groups: network, security, process, persistence, memory, replication.
CONFIG GETandCONFIG SETwork at runtime. UseCONFIG REWRITEto persist changes to the file.- Keep config files under version control so a runtime change cannot silently drift from the file.
Practice
- (Beginner) Write a config for a development instance: port 6400, no persistence (
save "",appendonly no),maxmemory 100mb,allkeys-lru, logging to stdout. Start it and verify each setting withCONFIG GET.- (Intermediate) Change
maxmemorywithCONFIG SET, runCONFIG REWRITE, and diff the file before and after. What did Redis change, and where did it place the new lines?- (Interview) "A teammate changed a setting with
CONFIG SETand it disappeared after a restart. Why, and how do you prevent configuration drift across a fleet?" Hint: compare runtime state with file state, and considerCONFIG REWRITEalongside configuration management tooling.
Managed Cloud Offerings
Running Redis in production means handling replication, failover, patching, backups, monitoring, scaling and security. Managed services take on most of that work in exchange for cost and some loss of control: restricted commands, limited configuration, and the provider's choice of versions.
| Provider | Offering | Engines | Notable traits |
|---|---|---|---|
| Redis Ltd. | Redis Cloud | Redis | Runs on AWS, GCP or Azure. Active-Active geo-replication, the latest Redis features |
| AWS | ElastiCache | Valkey, Redis OSS, Memcached | Node-based or serverless. Cluster mode, Multi-AZ |
| AWS | MemoryDB | Valkey, Redis OSS | Durable: writes go to a multi-AZ transaction log |
| Google Cloud | Memorystore | Valkey, Redis, Redis Cluster | Integrated with VPC and IAM |
| Microsoft Azure | Azure Managed Redis | Redis (Enterprise-based) | Successor to Azure Cache for Redis, which is being retired |
| Upstash | Serverless Redis | Redis-compatible | Pay per request. HTTP/REST API for edge and serverless functions |
| Aiven | Aiven for Valkey | Valkey | Multi-cloud managed open source |
What to compare when choosing:
- Engine and version. Which features you get (Redis 8 vs Valkey), and how fast upgrades arrive.
- High availability. Replica placement, failover time, SLA.
- Durability. RDB/AOF support, backups, point-in-time recovery, or transaction-log durability (MemoryDB).
- Scaling. Cluster mode, online resharding, maximum memory per node.
- Security. Private networking (VPC), TLS, ACL support.
- Restrictions. Disabled or renamed commands (often
CONFIG,DEBUG,MODULE, sometimesKEYS), and whether modules are supported. - Pricing model. Per node-hour, per GB, or per request.
Managed service (single region, Multi-AZ)
+-------------------------------------------------------+
| Zone A Zone B |
| +-----------+ replication +-----------+ |
| | Primary | ------------> | Replica | |
| +-----+-----+ +-----------+ |
| ^ automatic failover promotes the replica |
+--------|----------------------------------------------+
| stable DNS endpoint, TLS + AUTH
+----+----+
| App |
+---------+
Example
Connecting to a managed instance with TLS and an ACL user:
# --askpass prompts for the password, keeping it out of shell history and the process list
redis-cli -h my-cache.example.cache.amazonaws.com -p 6379 --tls --user app --askpass
import os
import redis
# The "rediss://" scheme (two s's) means TLS. The URL carries user, password, host, port and db
r = redis.Redis.from_url(os.environ["REDIS_URL"]) # e.g. rediss://app:secret@host:6379/0
r.ping()
Key Takeaways
- Managed services handle failover, patching and backups, but restrict commands and configuration.
- Engine choice (Redis or Valkey) differs by provider and decides which features you get.
- Compare HA, durability, scaling, security, restrictions and pricing, not just price per GB.
- Always connect over TLS (
rediss://) with authentication, and keep instances on private networks.
Practice
- (Beginner) Pick two providers from the table and compare them on engine, HA model and pricing model for a workload with 20 GB of data, a high-availability requirement and a single region.
- (Intermediate) Find one provider's list of restricted commands. For each restricted command, explain why a provider would block it.
- (Interview) "When would you run Redis yourself on VMs or Kubernetes instead of using a managed service?" Hint: consider cost at large scale, custom modules, version control, compliance requirements and in-house expertise.
<a id="13-interacting-with-redis"></a>
1.3 Interacting with Redis
This subchapter covers the tools and the protocol you use to talk to a Redis server, and the command conventions shared by everything that follows.
redis-cli Basics
redis-cli is the official command-line client and the tool you will use most for exploration, debugging and operations. It ships with every Redis install. Besides sending commands, it has built-in diagnostic modes for latency, big keys, live stats and bulk loading, and on a production server it is often the only tool available.
It works in two modes:
- Interactive (REPL). Run
redis-cliand type commands. You get tab completion, inline syntax hints andHELP. - One-shot. Run
redis-cli <command> <args>, which executes one command and exits. This is useful in scripts.
| Flag | Purpose |
|---|---|
-h host, -p port | Server address (default 127.0.0.1:6379) |
-u redis://user:pass@host:port/db | Connect using a URL |
--user name, --askpass | ACL username, and a prompt for the password |
-a password | Password on the command line (discouraged: visible in ps output and shell history) |
-n db | Select a logical database number |
--tls | Connect over TLS |
--raw / --no-raw | Raw output for scripts, or formatted output for humans |
-r count -i seconds | Repeat a command (-r -1 repeats forever) |
-x | Read the last argument from stdin |
--scan --pattern 'p*' | Iterate keys safely with SCAN |
--bigkeys, --memkeys | Sample the keyspace for the largest keys |
--latency, --stat | Live latency sampling and server stats |
--pipe | Bulk-load commands from stdin |
To avoid exposing a password, set the REDISCLI_AUTH environment variable or use --askpass.
Example
Interactive use, including quoting rules:
$ redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET msg "hello world"
OK
127.0.0.1:6379> SET multiline "line1\nline2"
OK
127.0.0.1:6379> GET multiline
"line1\nline2"
127.0.0.1:6379> HELP SET
SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
summary: Sets the string value of a key, ignoring its type. ...
In double quotes, \n and other escapes are interpreted. Single quotes keep text literal. redis-cli shows non-printable bytes as escapes, and with --raw it prints the actual bytes.
One-shot and diagnostic use:
# One-shot commands
redis-cli SET counter 10
redis-cli INCRBY counter 5 # (integer) 15
redis-cli --raw GET counter # 15 (no quotes or type prefix, script-friendly)
# Password via environment variable, which stays out of the process list
export REDISCLI_AUTH='s3cret'
redis-cli -h 10.0.0.5 PING
# Use a file's contents as the value (-x reads the last argument from stdin)
redis-cli -x SET config:json < config.json
# Iterate keys safely (SCAN under the hood, unlike KEYS) and delete them in batches of 100
redis-cli --scan --pattern 'demo:*' | xargs -n 100 redis-cli DEL
# Live monitoring
redis-cli --stat # keys, memory, clients, requests per second
redis-cli -r -1 -i 1 INFO stats | grep instantaneous_ops_per_sec
redis-cli --bigkeys # largest key per type (sampled with SCAN)
Key Takeaways
redis-cliworks interactively for exploration and in one-shot mode for scripts.- Use
--rawfor script-friendly output and--scaninstead ofKEYSto list keys. - Keep passwords off the command line: use
REDISCLI_AUTHor--askpass. - The diagnostic modes (
--stat,--latency,--bigkeys,--pipe) are a built-in ops toolkit.
Practice
- (Beginner) Write a shell script that uses only one-shot
redis-clicommands to create keysdemo:1throughdemo:10, list them with--scan, and delete them.- (Intermediate) Run
redis-benchmark -n 1000000 -qin one terminal. In another, watch the load with--statand with-r -1 -i 1 INFO stats. Compare the two views and describe what each reports.- (Interview) "Why is
redis-cli -a secretdiscouraged, and what are the alternatives?" Hint: think about where command-line arguments are visible on a multi-user Linux system, and where shells record commands.
RESP Protocol
Clients and servers need a common format for requests and replies. Redis's designers wanted a protocol that is simple to implement in any language, fast to parse, and readable enough to debug by eye. The result is RESP (REdis Serialization Protocol). Knowing RESP helps you debug client issues, understand pipelining (chapter 4.3) and write minimal clients.
How a request is encoded. A client sends each command as a RESP array of bulk strings. Every element is prefixed with its byte length, and every line ends with \r\n:
SET mykey hello is sent as:
*3\r\n array of 3 elements
$3\r\nSET\r\n bulk string, 3 bytes: SET
$5\r\nmykey\r\n bulk string, 5 bytes: mykey
$5\r\nhello\r\n bulk string, 5 bytes: hello
Length prefixes make RESP binary-safe: the parser reads exactly N bytes and never scans for delimiters, so values can contain \r\n, null bytes or arbitrary binary data.
Reply types. The first byte of a reply identifies its type:
| Prefix | Type | Example | Version |
|---|---|---|---|
+ | Simple string | +OK\r\n | RESP2 |
- | Error | -ERR unknown command\r\n | RESP2 |
: | Integer | :42\r\n | RESP2 |
$ | Bulk string ($-1 = null in RESP2) | $5\r\nhello\r\n | RESP2 |
* | Array | *2\r\n... | RESP2 |
_ | Null | _\r\n | RESP3 |
# | Boolean | #t\r\n | RESP3 |
, | Double | ,3.14\r\n | RESP3 |
( | Big number | (3492890328409238509324850943850943825024385\r\n | RESP3 |
! | Bulk error | !21\r\nSYNTAX invalid syntax\r\n | RESP3 |
= | Verbatim string | =15\r\ntxt:Some string\r\n | RESP3 |
% | Map | %2\r\n... | RESP3 |
~ | Set | ~3\r\n... | RESP3 |
> | Push (out-of-band data) | >3\r\n... | RESP3 |
| | Attribute (metadata) | |1\r\n... | RESP3 |
RESP2 vs RESP3. RESP2 is the default. RESP3 (Redis 6+) adds semantic types: maps instead of flat arrays for HGETALL, real doubles for scores, and push messages, which let Pub/Sub notifications and client-side caching invalidations share a connection with normal replies. A client opts in with HELLO 3.
Redis also accepts inline commands: plain space-separated text, which is why you can type PING into telnet or nc.
Example
Talking raw RESP with nc:
# SET mykey hello
printf '*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$5\r\nhello\r\n' | nc localhost 6379
# +OK
# GET mykey
printf '*2\r\n$3\r\nGET\r\n$5\r\nmykey\r\n' | nc localhost 6379
# $5
# hello
# GET on a missing key returns a RESP2 null bulk string
printf '*2\r\n$3\r\nGET\r\n$7\r\nmissing\r\n' | nc localhost 6379
# $-1
# Inline command form
printf 'PING\r\n' | nc localhost 6379
# +PONG
Depending on your nc variant, you may need -q 1 (GNU) or -N (OpenBSD) so it exits after the reply.
A minimal client in Python:
import socket
def encode_command(*args: str) -> bytes:
"""Encode a command as a RESP array of bulk strings."""
out = [f"*{len(args)}\r\n".encode()] # array header: element count
for arg in args:
data = arg.encode()
out.append(f"${len(data)}\r\n".encode() + data + b"\r\n") # length-prefixed, binary-safe
return b"".join(out)
with socket.create_connection(("localhost", 6379)) as s:
s.sendall(encode_command("SET", "mykey", "hello"))
print(s.recv(1024)) # b'+OK\r\n' simple string
s.sendall(encode_command("INCR", "visits"))
print(s.recv(1024)) # b':1\r\n' integer
s.sendall(encode_command("GET", "mykey"))
print(s.recv(1024)) # b'$5\r\nhello\r\n' bulk string
s.sendall(encode_command("HELLO", "3")) # switch this connection to RESP3
print(s.recv(4096)[:20]) # b'%7\r\n$6\r\nserver...' a map reply
The reply sizes here fit easily in one recv. A real client must buffer and parse incrementally, because one reply can span several TCP reads.
Key Takeaways
- Requests are RESP arrays of length-prefixed bulk strings, which makes the protocol binary-safe and fast to parse.
- The first byte of a reply gives its type:
+simple string,-error,:integer,$bulk string,*array. - RESP3 (
HELLO 3) adds maps, doubles, booleans, nulls and push messages. - Inline commands let you debug with
telnetornc.
Practice
- (Beginner) Hand-encode
LPUSH queue a bcin RESP, send it withnc, and decode the reply.- (Intermediate) Extend the Python client with a recursive parser that handles simple strings, errors, integers, bulk strings (including
$-1null) and arrays. Test it withLRANGEandHGETALL.- (Interview) "Why does RESP use length-prefixed bulk strings instead of delimiter-terminated values?" Hint: think about storing binary data such as images or serialized objects, and about the cost of scanning and escaping.
Redis Insight GUI
Command-line tools are precise, but some tasks are easier with a visual tool: browsing a large keyspace, inspecting nested structures, seeing memory distribution or profiling traffic. Redis Insight is the official free GUI from Redis. It runs as a desktop app (Windows, macOS, Linux) or as a Docker container served in the browser.
Main features:
| Feature | What it does |
|---|---|
| Browser | Lists keys by pattern and type, with editors for strings, hashes, lists, sets, sorted sets, streams and JSON. Shows TTL and memory per key |
| Workbench | Command editor with autocomplete, inline docs and visual results for query engine and JSON output |
| Database Analysis | Samples the keyspace to report memory by key pattern, top keys, TTL distribution and recommendations |
| Slow Log | Visual view of SLOWLOG entries |
| Profiler | Live stream of commands (uses MONITOR) |
| Pub/Sub | Subscribe to channels and publish messages |
| Bulk Actions | Delete keys by pattern, upload command files |
| Tutorials and AI assistant | Guided tutorials and an assistant for commands and data modeling |
Insight also connects to Valkey and other RESP-compatible servers for core features. Features that depend on Redis-specific modules or commands may not be available there.
Example
Running Insight in Docker on the same network as a Redis container:
docker network create redis-net
docker run -d --name redis --network redis-net redis:8
# Insight's web UI listens on port 5540
docker run -d --name redisinsight --network redis-net \
-p 127.0.0.1:5540:5540 \
-v redisinsight:/data \
redis/redisinsight:latest
# Open http://localhost:5540 and add a database with host "redis" and port 6379.
# To reach a Redis running directly on the host instead, use host "host.docker.internal"
# (Docker Desktop) or the host's IP address.
A typical first session:
1. Add database -> host, port, optional username/password/TLS
2. Browser -> filter "user:*", open a hash, edit a field, set a TTL
3. Workbench -> run: HGETALL user:1001
4. Analysis Tools -> "Database Analysis" -> review memory by key prefix
5. Slow Log -> look for commands above the slowlog threshold
Key Takeaways
- Redis Insight is the official free GUI, available as a desktop app or Docker image (port 5540).
- It is most useful for browsing and editing data, Workbench queries, and memory analysis.
- The Profiler uses
MONITOR, which is expensive. Avoid running it for long on busy production servers. - Inside Docker, connect to Redis by container name on a shared network, or via
host.docker.internal.
Practice
- (Beginner) Connect Redis Insight to your local server, create one key of each core type (string, list, hash, set, sorted set), and inspect each in the Browser.
- (Intermediate) Load 100,000 keys across three prefixes (
user:*,session:*,cache:*) with different value sizes. Run Database Analysis and find which namespace uses the most memory and which keys have no TTL.- (Interview) "What are the risks of running a GUI profiler against a production instance?" Hint: find out what
MONITORdoes with every command the server runs, and what that costs at high throughput.
Command Syntax and Documentation
Redis has hundreds of commands, and many take several options. Reading command syntax quickly, and knowing where to find time complexity and edge cases, prevents subtle bugs and production incidents caused by an O(N) command run against a huge key.
General shape
COMMAND [SUBCOMMAND] key [key ...] [argument ...] [OPTION value]
- Command names are case-insensitive (
set=SET). Key names and values are case-sensitive. - Container commands group subcommands:
CONFIG GET,CLIENT LIST,OBJECT ENCODING,XINFO STREAM. - Commands are grouped by data type or area: generic (
DEL,EXPIRE,SCAN), string, list, hash, set, sorted set, stream, Pub/Sub, scripting, connection, server, cluster.
Reading the syntax notation in the docs
| Notation | Meaning | Example |
|---|---|---|
[ ... ] | Optional | [GET] |
a | b | Choose one | [NX | XX] |
... | Repeatable | key [key ...] |
UPPERCASE | Literal token | EX |
lowercase | Value you provide | seconds |
SET key value [NX | XX] [GET] [EX seconds | PX milliseconds |
EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
Each page on the official command reference (redis.io/docs/latest/commands) lists:
- Time complexity, for example O(1), O(log N) or O(N), with what N means for that command.
- ACL categories, for example
@write,@string,@slow,@dangerous. - Available since, plus a history of added options and deprecations.
- Return values for RESP2 and RESP3.
Built-in documentation from the server
| Command | Shows |
|---|---|
HELP SET (in redis-cli) | Syntax and summary |
HELP @hash | All commands in a group |
COMMAND DOCS SET | Full docs: summary, complexity, arguments, history |
COMMAND INFO SET | Arity, flags, key positions, ACL categories |
COMMAND COUNT | Number of commands the server supports |
COMMAND LIST FILTERBY ACLCAT dangerous | Commands in an ACL category |
COMMAND GETKEYS SET k v | Which arguments are keys (useful for Cluster) |
Deprecated forms to recognize in older code
| Deprecated | Modern replacement |
|---|---|
GETSET key value | SET key value GET |
SETNX, SETEX, PSETEX | SET with NX / EX / PX |
HMSET | HSET with several field-value pairs |
ZREVRANGE, ZRANGEBYSCORE | ZRANGE ... REV / ZRANGE ... BYSCORE |
SLAVEOF | REPLICAOF |
Example
Combining SET options, with typical errors:
127.0.0.1:6379> SET lock:order:42 worker-7 NX PX 30000
OK
127.0.0.1:6379> SET lock:order:42 worker-9 NX PX 30000
(nil)
127.0.0.1:6379> SET counter 10
OK
127.0.0.1:6379> SET counter 20 GET
"10"
127.0.0.1:6379> SET counter 30 XX KEEPTTL
OK
127.0.0.1:6379> GET
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> FOO bar
(error) ERR unknown command 'FOO', with args beginning with: 'bar'
127.0.0.1:6379> COMMAND DOCS LRANGE
1) "lrange"
2) 1) "summary"
2) "Returns a range of elements from a list."
3) "since"
4) "1.0.0"
5) "group"
6) "list"
7) "complexity"
8) "O(S+N) where S is the distance of start offset from HEAD for small lists, ..."
...
What happened in that session:
- The second
SET ... NXreturned(nil)because the key already existed, so the lock was not acquired. SET ... GETreturned the old value ("10") while writing the new one.XX KEEPTTLupdates the key only if it exists, and keeps any TTL it had.
Common error prefixes are ERR (generic), WRONGTYPE (type mismatch), NOAUTH / WRONGPASS (authentication), NOPERM (ACL), OOM (maxmemory reached under noeviction), and MOVED / ASK (Cluster redirects).
Key Takeaways
- Command names are case-insensitive. Keys and values are not.
- Learn to read
[ ],|and...in syntax lines, and always check the time complexity and what N means. - Use
HELP,COMMAND DOCSandCOMMAND INFOto get documentation from the server you are actually running. - Prefer modern forms (
SET ... NX EX,ZRANGE ... REV) over deprecated commands.
Practice
- (Beginner) Using the
SETsyntax, write one command that updates keyprofile:7only if it already exists, preserves its TTL, and returns the previous value.- (Intermediate) Use
COMMAND DOCSto find the time complexity ofLRANGE,SMEMBERS,ZADD,HGETALLandDEL. For each, describe an input that would make it slow on a production server.- (Interview) "How do you decide whether a command is safe to run on a production instance holding 50 million keys?" Hint: combine time complexity (and what N refers to), ACL categories such as
@slowand@dangerous, and whether the command blocks the main thread.
Databases and SELECT
Before key prefixes became the norm, Redis offered logical databases as a lightweight way to separate datasets on one server. They still exist and are useful to know, but their limits are widely misunderstood, and many teams avoid them in production.
How they work
- A server has
databaseslogical databases (default 16), numbered0to15. - Each database is a separate keyspace: the key
colorin db 0 is unrelated tocolorin db 1. - The selected database is per connection. New connections start in db 0, and
SELECT nswitches. redis-cli -n 2or a URL path such asredis://host:6379/2selects a database at connect time. The CLI prompt shows the current one as[2].
| Command | Effect |
|---|---|
SELECT index | Switch this connection to another database |
DBSIZE | Number of keys in the current database |
MOVE key db | Move a key to another database (fails if the key exists there) |
SWAPDB a b | Swap two databases atomically for all clients |
FLUSHDB [ASYNC | SYNC] | Delete all keys in the current database |
FLUSHALL [ASYNC | SYNC] | Delete all keys in every database |
INFO keyspace | Key counts and expiry info per database |
What they do not give you. Databases are namespaces, not isolation:
| Shared across all databases | Consequence |
|---|---|
Memory and maxmemory | One database can push another's keys out through eviction |
| The main thread | A slow command in db 3 blocks clients of db 0 |
| Config, persistence files, replication | Everything is backed up, replicated and restored together |
| ACLs | No per-database permissions. ACLs work on key patterns and commands |
| Cluster mode | Redis Cluster supports only db 0 (recent Valkey releases lift this) |
The usual advice is to use key prefixes for namespacing within an application and separate instances when you need real isolation. Databases still fit a few cases: test data on a development server, and atomic dataset swaps with SWAPDB.
Example
127.0.0.1:6379> SET color red
OK
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> GET color
(nil)
127.0.0.1:6379[1]> SET color blue
OK
127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> MOVE color 1
(integer) 0
127.0.0.1:6379> INFO keyspace
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
MOVE returned 0 because color already exists in db 1, so nothing was moved. Newer server versions add extra fields to the INFO keyspace lines.
Atomic dataset refresh with SWAPDB: load a new dataset into a staging database, then swap it in.
import redis
loader = redis.Redis(db=1) # this pool's connections run SELECT 1 on connect
admin = redis.Redis(db=0)
loader.flushdb() # clear the staging database
pipe = loader.pipeline(transaction=False) # plain pipeline: batching only, no MULTI/EXEC
for sku, price in fetch_new_prices(): # fetch_new_prices() is your data source
pipe.set(f"price:{sku}", price)
pipe.execute()
admin.swapdb(0, 1) # all db 0 clients now see the new data at once
A common bug with connection pools: if code runs SELECT by hand on a pooled connection, that connection goes back to the pool still pointing at the other database, and later users read or write the wrong keyspace. Always set the database in the client or pool configuration, never with an ad hoc SELECT.
Key Takeaways
- Redis has 16 logical databases by default. Each has its own keyspace, and the selection is per connection.
- They share memory, CPU, configuration, persistence and ACLs, so they offer no real isolation.
- Redis Cluster supports only database 0. Use key prefixes or separate instances instead.
SWAPDBgives you an atomic switch to a freshly loaded dataset. Set the database in the pool config, not with ad hocSELECT.
Practice
- (Beginner) Write a few keys to db 3, check
INFO keyspace, runFLUSHDBwhile in db 3, and confirm that db 0 is untouched. Then explain whatFLUSHALLwould have done instead.- (Intermediate) Implement a nightly reload of a product catalog using db 1 as staging and
SWAPDB. Describe what clients connected to db 0 see before, during and after the swap, and what happens to the old data.- (Interview) "Why do many teams avoid Redis logical databases in production, and what do they use instead?" Hint: consider isolation of memory and CPU, Cluster support, ACL granularity and per-tenant monitoring.
<a id="2-keys-and-core-data-types"></a>