Redis Intro
Redis is an in-memory data structure server. A program connects to it over the network, sends short text commands such as SET, GET or ZADD, and gets replies back in well under a millisecond. Unlike a relational database, Redis does not store rows in tables: it stores keys, and every key holds one value of a specific type, such as a string, a list, a hash, a set or a sorted set.
The name stands for REmote DIctionary Server. You can picture the whole database as one giant dictionary (a hash map) that lives in RAM and that many programs can share.
SET greeting "Hello, Redis!"
GET greeting
RPUSH tasks "write lesson" "test examples"
LRANGE tasks 0 -1
TYPE tasksredis> SET greeting "Hello, Redis!"
OK
redis> GET greeting
"Hello, Redis!"
redis> RPUSH tasks "write lesson" "test examples"
(integer) 2
redis> LRANGE tasks 0 -1
1) "write lesson"
2) "test examples"
redis> TYPE tasks
list
Each line is one command. The transcript shows the prompt redis> with the command you typed, followed by the reply: OK for a successful write, a quoted string for a text value, (integer) 2 for a number, and a numbered list for an array.
Why is Redis so fast?
| Reason | What it means |
|---|---|
| Data lives in memory | Reading RAM takes nanoseconds; reading a disk takes micro- to milliseconds |
| Simple operations | Most commands touch one key and run in constant or logarithmic time |
| One main thread | Commands run one after another, so there are no locks to wait for |
| Compact encodings | Small lists, hashes and sets are packed into tight memory blocks |
| A simple protocol | The RESP protocol is cheap to parse on both sides |
Because commands run one at a time, every single command is atomic: no other client can see a half-finished INCR or RPUSH. That property is the base of many patterns in this course.
What is Redis used for?
Redis rarely replaces your main database. It sits next to it and handles the jobs that need speed or special data structures:
- Caching: keep the result of slow queries or API calls for a few minutes.
- Sessions: store logged-in user sessions that every web server can read.
- Counters and rate limiting: count page views or requests per user per minute.
- Leaderboards: sorted sets keep players ordered by score automatically.
- Queues and streams: hand jobs from web servers to background workers.
- Real-time features: pub/sub messages, presence, live dashboards.
- Search and AI: vector similarity search for embeddings (vector sets, Redis 8).
# A page-view counter and a tiny leaderboard
INCR views:home
INCR views:home
INCR views:home
ZADD scores 120 ada 95 linus 140 grace
ZRANGE scores 0 -1 REV WITHSCORESredis> INCR views:home
(integer) 1
redis> INCR views:home
(integer) 2
redis> INCR views:home
(integer) 3
redis> ZADD scores 120 ada 95 linus 140 grace
(integer) 3
redis> ZRANGE scores 0 -1 REV WITHSCORES
1) "grace"
2) "140"
3) "ada"
4) "120"
5) "linus"
6) "95"
Notice that INCR created the key views:home the first time it was used. Redis creates keys on demand and removes empty collections automatically, so you never declare a schema.
Redis versus a relational database
| Relational database (PostgreSQL, MySQL) | Redis | |
|---|---|---|
| Storage | Disk first, cached in memory | Memory first, optionally saved to disk |
| Data model | Tables, rows, columns, joins | Keys holding typed values |
| Query language | SQL | One command per operation |
| Typical latency | Milliseconds | Sub-millisecond |
| Dataset size | Larger than RAM is normal | Usually must fit in RAM |
| Best at | Complex queries, durable records | Hot data, counters, queues, caches |
Note: Redis can persist data to disk (see the Persistence lesson), so a restart does not have to lose everything. Still, design as if memory is the limit: it is the most expensive resource Redis uses.
A short history
Salvatore Sanfilippo wrote Redis in 2009 to speed up a real-time web analytics product. It became one of the most popular databases in the world. Redis 8 (2025) folded features that used to be separate modules, such as JSON, the query engine, time series and probabilistic types, into the Redis Open Source distribution, and added vector sets. Several forks and compatible servers also exist, for example Valkey and KeyDB; the commands in this course work the same on all of them unless a lesson says otherwise.
How the playgrounds work
The examples in this course are transcripts of a real Redis 8.8 server. Each example starts from an empty server and sends the commands one per line, like typing them in redis-cli. Lines starting with # are comments and are not sent. Redis itself does not run inside your browser: the playground shows the saved, verified transcript. To experiment with your own commands, install Redis locally as shown in the next lesson and type them into redis-cli.
Test yourself
Where does Redis keep its working data set?
- On disk, with a small memory cache
- In memory (RAM)
- In the client application
- In a cloud object store
Why is a single Redis command such as INCR atomic?
- Redis locks the key on disk
- Commands are executed one at a time by the main thread
- Clients must call LOCK first
- It is not atomic
Which job is Redis usually NOT used for?
- Caching query results
- Storing sessions
- Being the only store for terabytes of rarely read archive data
- Leaderboards