What is Redis?
Redis is an in-memory, key-value data store that prioritizes speed above all else. The name stands for REmote DIctionary Server, and it's been the de facto standard for ultra-fast data access since 2009.
Unlike traditional databases (like PostgreSQL) that store data on disk, Redis keeps everything in RAM. This means queries return results in microseconds — thousands of times faster than disk-based systems. The trade-off? Redis can't store terabytes of data, but it's perfect for data you need instantly: cache layers, session storage, leaderboards, and real-time analytics.
Key characteristic: Redis is single-threaded. This means all operations are atomic (they can't be interrupted), making Redis predictable and eliminating complex locking problems.
A Brief History
Redis was created in 2009 by Salvatore Sanfilippo, an Italian programmer who needed a fast data structure store for a web analytics project. He open-sourced it in 2010, and it quickly became the go-to choice for companies needing sub-millisecond response times.
Today, Redis powers systems at:
- Twitter — Caching tweets and timelines
- GitHub — Rate limiting and sessions
- Stripe — Real-time counters and queue management
- Netflix — Caching recommendations and user data
- Instagram — Leaderboards and activity feeds
In the 2020s, Redis evolved to support clustering (sharding data across multiple servers), modules, and streams. Today it remains the fastest in-memory database in production use.
Real-World Use Cases
Redis excels in scenarios where speed and freshness matter more than durability. Here are the most common patterns:
Caching Layer
Session Storage
session:user123 = {name, email, login_time}. The session auto-expires after 1 hour.Leaderboards & Counters
ZREVRANGE shopstream:sales 0 9.Rate Limiting
rate:192.168.1.1. Increment on each request. Set TTL to 60 seconds. If counter exceeds 100, reject. This runs in microseconds.Pub/Sub (Real-Time Messaging)
Queues & Job Processing
Redis vs Traditional Databases
Redis and PostgreSQL serve different purposes. Use both:
| Aspect | Redis | PostgreSQL |
|---|---|---|
| Storage | In-memory (RAM) | On-disk (SSD/HDD) |
| Speed | Microseconds (1-100 μs) | Milliseconds (1-50 ms) |
| Data Size | Limited by RAM (GB) | Unlimited (TB+) |
| Persistence | Optional (RDB/AOF) | Always durable |
| Transactions | MULTI/EXEC (atomic) | ACID guarantees |
| Joins | N/A (key-value only) | Complex joins native |
| Use Cases | Cache, sessions, leaderboards, queue | Transactional data, reports, analytics |
Architecture pattern: Most production systems use both. PostgreSQL is the source of truth (durability). Redis sits in front as a cache layer and operational store (speed).
Persistence: How Redis Survives Restarts
Since Redis runs in RAM, all data is lost if the server crashes. To prevent this, Redis offers two persistence modes:
RDB (Redis Database Snapshots)
How it works: Redis saves a binary snapshot of all data to disk at intervals (e.g., every 1 minute or after 1000 writes).
Pros: Compact, fast recovery.
Cons: You lose data written between snapshots. Heavy snapshots can block the server.
AOF (Append-Only File)
How it works: Redis logs every write command to a file. On restart, it replays all commands to rebuild state.
Pros: Lose at most 1 second of data. Fine-grained durability.
Cons: AOF files grow large. Slower recovery on restart.
Hybrid: RDB + AOF
Modern Redis (7.0+) combines both: periodic RDB snapshots for efficiency, plus AOF for recent writes. Best of both worlds.
Your First Redis Commands
Let's try the most fundamental Redis operations. These assume a Redis server running on localhost:6379 and the redis-cli command-line tool installed.
Q1: SET a Key
What happened: The SET command stores a key-value pair. mykey is the key. "Hello, Redis!" is the value. The response OK means success.
Q2: GET a Key
What happened: GET retrieves the value for a key. Redis returns the string we stored.
Q3: Check if Key Exists
What happened: EXISTS returns 1 if the key exists, 0 if not. The key mykey exists, so the answer is 1.
Q4: Get the Data Type
What happened: TYPE returns the data type of a key's value. We stored a string, so the type is string. Redis has other types too: list, set, zset (sorted set), hash, stream.
Q5: Delete a Key
What happened: DEL deletes a key. The response 1 means 1 key was deleted. If the key didn't exist, DEL would return 0.
Q6: Verify Deletion
What happened: The key no longer exists. EXISTS returns 0.
ShopStream Example: Caching Product Data
Here's how ShopStream marketplace could use Redis to cache popular products:
Store a Product
Retrieve It (Instant)
Set Expiration (Cache for 1 Hour)
What happened: EXPIRE sets a TTL (time-to-live) of 3600 seconds (1 hour). After that time, Redis auto-deletes the key. This is perfect for cache data that should refresh periodically.
Test Your Knowledge
You're learning to scale. Build systems that millions use.
Join the Best AI Program: The AI-Era Data Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Data Engineer →