iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Intro

Redis is an in-memory data store. Lightning-fast caches, queues, rate limiters, leaderboards, and pub/sub — all from one server.

Redis — what it is

EXAMPLE
# ===== The values =====
# - In-memory: microsecond reads/writes
# - Rich data types: string, hash, list, set, sorted set, stream, JSON, search
# - Optional persistence (RDB snapshots, AOF append log)
# - Clusterable; replicas for read scale; Sentinel for failover
# - Pub/Sub + Streams for messaging

# ===== Hello, redis =====
SET user:1 'Alex'
GET user:1
DEL user:1

# Hash:
HSET user:1 name 'Alex' email 'a@x.io'
HGETALL user:1

# List (queue):
LPUSH jobs:queue '{"id":1,"do":"send-email"}'
BRPOP jobs:queue 0     # block + pop

# Set:
SADD active 'u-1' 'u-2'
SISMEMBER active 'u-1'

# Sorted set (leaderboard):
ZADD scores 1500 'alice' 1700 'bob'
ZRANGE scores 0 -1 WITHSCORES

# ===== Caches with TTL =====
SET session:abc123 'user-1' EX 1800     # expire in 30 min
GET session:abc123
TTL session:abc123

# ===== Atomic counters (rate limiting) =====
INCR rl:login:198.51.100.7
EXPIRE rl:login:198.51.100.7 60          # set TTL on first hit

# ===== Pub/Sub =====
SUBSCRIBE chat:room-1
PUBLISH chat:room-1 'hello'

# ===== Streams (since 5.0; richer than pub/sub) =====
XADD events * type login user u-1
XREAD COUNT 10 STREAMS events 0

# ===== When Redis wins =====
# - Caches (the classic use case)
# - Rate limiting
# - Leaderboards and ephemeral counters
# - Real-time fan-out (chat, notifications)
# - Job queues for short-lived work

# ===== When Redis hurts =====
# - Primary durable storage of important data (use a real DB)
# - Datasets bigger than RAM
# - Strong relational queries

# ===== Patterns to internalise =====
# - <domain>:<entity>:<id> key naming
# - TTLs on every cache key (PERSIST only intentionally)
# - SCAN, not KEYS, in prod
# - Pipeline / MULTI for multi-key atomic-like behaviour

# ===== Pitfalls =====
# - Treating Redis as primary durable storage without backups + AOF
# - Keys without TTL bloating memory until OOM
# - KEYS * stalling the server
# - Tiny values in a hash with one field each (use STRING)

Why it matters

Redis is the swiss-army speed layer. Caches, queues, counters, leaderboards, pub/sub — all measured in microseconds. Treat it as ephemeral by default, keep keys tidy, and TTL everything. It is the most-deployed in-memory store on the planet for a reason.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Redis: in-memory, single-threaded, blazingly fast.
# Built-in data structures, persistence, pub/sub, streams.
Try it Yourself »

Exercise

Liveness check command.

Test yourself

Q1. Redis is best described as…
Q2. Redis is mostly…
Q3. A "ping → PONG" command is…

Discussion

Loading…