Redis is an in-memory database. It stores data in RAM instead of on disk, which makes it incredibly fast β most operations take less than a millisecond.
Think of it as a giant, shared dictionary that any part of your application can read from and write to, and itβs blazing fast because everything lives in memory.
What itβs used for
Caching (most common use case): store the result of an expensive database query or API call so you donβt have to repeat it.
Without cache: User request β Query database (200ms) β Response
With Redis: User request β Check Redis (1ms) β Response
Session storage: store user sessions so they persist across server restarts and work across multiple servers.
Rate limiting: track how many requests a user has made in the last minute.
Queues: use Redis lists as a job queue (push tasks in, workers pop them out).
Real-time features: pub/sub for chat, notifications, live updates.
Leaderboards: sorted sets make rankings trivial.
How it works
Redis stores key-value pairs with different data types:
# String
SET user:name "Alice"
GET user:name β "Alice"
# Hash (like an object)
HSET user:1 name "Alice" email "alice@example.com"
HGETALL user:1 β name: Alice, email: alice@example.com
# List (queue)
RPUSH jobs "send-email" "resize-image"
LPOP jobs β "send-email"
# Set (unique values)
SADD online-users "alice" "bob"
SISMEMBER online-users "alice" β true
Caching example (Node.js)
import Redis from 'ioredis';
const redis = new Redis();
async function getUser(id) {
// Check cache first
const cached = await redis.get(`user:${id}`);
if (cached) return JSON.parse(cached);
// Cache miss β query database
const user = await db.query('SELECT * FROM users WHERE id = ?', [id]);
// Store in cache for 1 hour
await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 3600);
return user;
}
First request: ~200ms (database). Every request after that for the next hour: ~1ms (Redis).
Redis vs. a regular database
| Redis | PostgreSQL | |
|---|---|---|
| Storage | RAM | Disk |
| Speed | Microseconds | Milliseconds |
| Data size | Limited by RAM | Limited by disk |
| Persistence | Optional | Always |
| Use case | Cache, sessions, queues | Primary data store |
Redis is not a replacement for your database. It sits in front of it to make things faster.
When to use Redis
Good fit: caching, sessions, rate limiting, real-time features, job queues, leaderboards.
Not ideal: primary data storage (use PostgreSQL/MySQL), complex queries with joins, data larger than your available RAM.
For the full command reference, see the Redis cheat sheet.