Skip to content

Redis Basics

Use Redis as an in-memory key-value store: strings, lists, hashes, sets, and TTL.

What you'll learn

  • What Redis is and why it is so fast
  • Strings, lists, hashes, sets, and sorted sets data types
  • Setting expiration with TTL and EXPIRE
  • Common use cases: caching, sessions, rate limiting
  • Persistence and the trade-off of in-memory storage

Concept

What Is Redis?

Redis (Remote Dictionary Server) is an in-memory key-value data store. Because all data lives in RAM, reads and writes are extremely fast — typically sub-millisecond. Redis is commonly used for caching, session storage, real-time analytics, message queues, and leaderboards. It is not a replacement for your primary database; it sits alongside it to accelerate hot data.

Data Types

Redis is more than a simple key-value store — it supports rich data structures:

  • Strings — the simplest type; a key holds a string, number, or binary blob
  • Lists — ordered collections, push/pop from either end; good for queues
  • Hashes — maps of field-value pairs; like a mini object per key
  • Sets — unordered collections of unique strings; good for tags, unique visitors
  • Sorted Sets — sets with an associated score; perfect for leaderboards and rankings

Each data type has its own set of commands, and Redis executes them atomically.

TTL and Expiration

Every key can have a time-to-live (TTL) — after the TTL expires, the key is automatically deleted. This is essential for caching (cache entries expire and get refreshed) and sessions (sessions expire after inactivity). Set TTL with EXPIRE key seconds or at creation time with SET key value EX seconds.

Common Use Cases

  • Caching — store the result of an expensive database query; serve from Redis on subsequent requests
  • Session storage — web session data with automatic expiration
  • Rate limiting — count requests per user per minute using INCR with TTL
  • Leaderboards — sorted sets with scores for real-time rankings
  • Pub/Sub — real-time message broadcasting

Persistence

Although Redis is in-memory, it can persist data to disk: RDB (point-in-time snapshots) and AOF (append-only log of every write). This protects against data loss on restart, but persistence is asynchronous by default — a crash can lose the last few seconds of writes. Redis is designed for speed first, durability second.

Example

              -- Set a string with expiration (cache a value for 1 hour)
SET cache:user:42:profile '{"name":"Ada","role":"admin"}' EX 3600

-- Get the value
GET cache:user:42:profile

-- Work with a hash (object-like data)
HSET user:42 name "Ada Lovelace" email "[email protected]" role "admin"
HGET user:42 name
HGETALL user:42

-- Sorted set for a leaderboard
ZADD leaderboard 1500 "player_a" 2300 "player_b" 1800 "player_c"
ZREVRANGE leaderboard 0 2 WITHSCORES  -- top 3 players

-- Rate limiting: 100 requests per minute
INCR rate:api:user:42
EXPIRE rate:api:user:42 60  -- reset every 60 seconds
            

Five Redis patterns: caching a string with TTL, hash for object data, sorted set for a leaderboard, and INCR with TTL for rate limiting.

Try it

Common mistakes

The mistake

Storing large datasets in Redis without monitoring memory

The fix

Redis stores everything in RAM. Set a maxmemory policy (e.g. allkeys-lru) and monitor usage. Eviction policies prevent out-of-memory crashes.

The mistake

Using Redis as the only data store without persistence

The fix

Enable AOF or RDB persistence if data must survive restarts. For critical data, keep the primary copy in a durable database and use Redis as a cache.

Practice Redis Basics

2 exercises

Practice Redis Basics

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors