Redis Commands & Data Structures

Lab
Easy
Medium
Hard

Strings & countersEasy

SET, GET and atomic INCR — the simplest cache and counter.

redis-cliclick a line to jump
>
Click to add a command
1 / 9clock 0s
Start
An empty Redis server. Redis keeps everything in memory, as keys that point to one of five basic data structures. Step forward to run each command.

Keyspace in memory (0 keys)

string list hash set zset
reply
(no command yet)
No keys yet — run a SET, RPUSH, HSET, SADD or ZADD.

Learn the theory

Redis Commands & Data Structures: how it works

4 min read · Updated

Redis is an in-memory data store that is much more than a key-value cache: every key can hold one of several data structures — strings, lists, hashes, sets, sorted sets — and Redis gives you atomic commands for each. Because the data lives in RAM and commands execute one at a time, operations take microseconds and counters, queues, leaderboards and locks become one-liners. Picking the right structure is most of the skill.

In short

  • Redis stores keys that point to values of a specific type; commands are type-specific (LPUSH works on lists, not strings).
  • Most commands are O(1) or O(log N), but a few (KEYS, SMEMBERS, HGETALL on huge values) are O(N) and can block the server.
  • Commands run one at a time, so INCR is atomic — no lost updates between clients.
  • Keys can have a TTL; expired keys disappear automatically. That makes Redis ideal for caches, sessions and rate limits.
  • Use strings for values/counters, lists for queues, hashes for objects, sets for unique collections, sorted sets for rankings.

The data model

Redis is a dictionary: key → value. Keys are binary-safe strings (convention: object:id:field, like user:42:name). The value's type decides which commands apply. Everything lives in memory, with optional persistence to disk (RDB snapshots and the AOF log).

Strings (and counters)

The simplest type: a byte string up to 512 MB. Store cached JSON, tokens, or numbers. INCR/INCRBY/DECR treat the value as an integer and change it atomically — perfect for counters.

SET user:1:name "Ada"
GET user:1:name            # "Ada"
INCR visits                # (integer) 1   <- atomic
SET lock:job "worker-A" NX EX 20   # only if absent, expires in 20 s

Lists

An ordered sequence you can push to and pop from both ends in O(1). RPUSH + LPOP gives a FIFO queue; LPUSH + LPOP gives a stack. LRANGE reads a slice (O(N) in the slice size).

RPUSH jobs "email:1" "email:2"
LPOP jobs                  # "email:1"  (first in, first out)
LRANGE jobs 0 -1           # everything left

Hashes

A map of field → value stored under one key: a compact way to represent an object (a user, a session) and to read or update single fields without rewriting the whole thing.

HSET user:7 name "Ravi" age 31 city "Pune"
HGET user:7 name
HGETALL user:7

Sets

An unordered collection of unique strings with O(1) add, remove and membership tests, plus set algebra: SINTER (in all sets), SUNION (in any), SDIFF. Great for tags, “who liked this”, unique visitors and mutual friends.

SADD friends:ann bob cara dan
SADD friends:bo cara dan eve
SINTER friends:ann friends:bo   # cara, dan

Sorted sets

Like a set, but every member has a numeric score and the set is always ordered by it (internally a skip list, so inserts and rank lookups are O(log N)). This is the structure behind leaderboards, priority queues, time-ordered feeds and rate-limit windows.

ZADD scores 120 "ann" 90 "bob" 150 "cara"
ZINCRBY scores 70 "bob"           # bob is now 160
ZREVRANGE scores 0 2 WITHSCORES    # top 3

TTL and expiry

EXPIRE key seconds (or SET … EX) gives a key a time to live. Redis removes expired keys lazily when you access them and actively in the background, so they never accumulate forever. TTL reports remaining seconds (-1 = no expiry, -2 = no such key).

Patterns you can build in a few commands

PatternRecipe
Counter / page viewsINCR page:home:views
Rate limiterINCR rate:ip then EXPIRE rate:ip 60 on the first hit; reject when the value passes the limit
Distributed lockSET lock:x <token> NX EX 30; release by deleting only if the token matches
Job queueRPUSH queue job to enqueue, LPOP (or blocking BLPOP) to consume
LeaderboardZADD/ZINCRBY to score, ZREVRANGE … WITHSCORES for the top N
Session storeA hash or string per session with a TTL

Frequently asked questions

What data structures does Redis support?
Strings, lists, hashes, sets and sorted sets are the core types. Redis also offers streams, bitmaps, HyperLogLog, geospatial indexes and more, built on top of the same key → value model.
Why is Redis so fast?
Data lives in memory, the data structures are simple and efficient, and commands run on a single thread without locking. Network round trips, not computation, usually dominate latency.
Is Redis single-threaded?
Command execution happens on one main thread, which makes every command atomic. Newer versions use extra threads for network I/O and background work, but commands still run one at a time.
How does Redis expire keys?
When a key has a TTL, Redis deletes it lazily the next time it is accessed and also runs a background process that samples keys and removes expired ones.
Why shouldn't I use the KEYS command in production?
KEYS scans the whole keyspace in a single O(N) operation and blocks all other clients while it runs. Use SCAN, which iterates incrementally.
How do I implement a leaderboard in Redis?
Use a sorted set: ZADD (or ZINCRBY) to set scores, ZREVRANGE with WITHSCORES for the top N and ZREVRANK to get a player's position — all O(log N).

Keep exploring

More interactive labs

All labs

Redis Caching: Cache-Aside, TTL & Eviction

Hit or miss? Watch LRU, LFU, TTLs and a cache stampede decide your database load.

Redis12 min · Intermediate

What Happens When 100 Reads Hit the Database

Connection pools, CPU workers, caches and replicas, as 100 requests arrive at once.

Databases12 min · Intermediate

JS Event Loop Visualizer

See the call stack, microtasks and timers run, frame by frame.

JavaScript10 min · Intermediate

CORS & Preflight Simulator

Edit the request and the server headers, then see exactly why the browser blocks it.

Networking12 min · Intermediate

HTTP Caching Explained

Cache-Control, ETag and 304s, one request at a time.

Networking12 min · Intermediate

What Happens When You Type a URL

DNS, TCP, TLS and HTTP — with a stopwatch.

Networking10 min · Beginner

How Database Indexes Work (B-Tree)

See a B-tree lookup beat a full table scan, and why some queries ignore your index.

Databases12 min · Intermediate

SQL Isolation Levels Lab

Run two transactions side by side and watch dirty reads, phantoms and write skew appear.

Databases15 min · Advanced

Sharding & Replication

Split data across servers, copy it for reads, and see what each choice costs.

Databases15 min · Advanced

Webhooks: Delivery, Retries & Signatures

Why you get duplicate deliveries, lost events and forged requests — and how to stop them.

Queues & Jobs12 min · Intermediate

Cron Jobs: Schedules, Overlaps & Missed Runs

Write a cron expression and watch what really happens when jobs overlap or the scheduler is down.

Queues & Jobs10 min · Beginner

Job Queues with BullMQ (Redis)

Watch jobs move through waiting, active, delayed, completed and failed — and see what BullMQ stores in Redis.

Queues & Jobs14 min · Intermediate

RabbitMQ: Exchanges, Queues, Acks & Dead Letters

Direct, topic and fanout routing, competing consumers, prefetch, redelivery and dead-letter queues, drawn.

Queues & Jobs14 min · Intermediate

Git Commands, HEAD & Refs (Visual)

Every core git command drawn with arrows: three areas, commits, branches, HEAD and remotes.

Git15 min · Beginner

Git Merge vs Rebase

Watch the commit graph change under merge, rebase and squash.

Git10 min · Beginner