Getting Started

Redis Introduction

Learn Redis — the blazing-fast in-memory data store used for caching, real-time analytics, and session management.

What is Redis?

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store. It operates primarily in RAM, making it orders of magnitude faster than disk-based databases. Redis can act as a database, cache, message broker, and streaming engine.

Originally created by Salvatore Sanfilippo in 2009, Redis is now one of the most popular databases in the world. It is used by Twitter, GitHub, Snapchat, Craigslist, and countless others for high-performance use cases.

Why is Redis Fast?

Redis is fast for several reasons:

  • In-memory storage — All data lives in RAM. Memory access is measured in nanoseconds; disk access in milliseconds. The difference is roughly 100,000x.
  • Single-threaded core — Redis processes one command at a time, avoiding lock contention. I/O is handled asynchronously.
  • Efficient data structures — Redis implements hash tables, skiplists, and compressed list encodings that minimize memory and maximize throughput.
  • Simple protocol — The RESP (Redis Serialization Protocol) is a lightweight text protocol with minimal overhead.

What Redis is Used For

Caching

The most common use case. Store the results of expensive database queries or API calls in Redis. When the same data is requested again, serve it from Redis in sub-millisecond time instead of re-computing it.

Session Storage

Web applications store user sessions in Redis. Sessions need to be fast (read on every request), temporary (expire after inactivity), and shared across multiple server instances.

Rate Limiting

Count requests per user per time window. Redis atomic increment operations make this trivial.

Real-time Leaderboards

Sorted Sets let you maintain a ranked list of scores, updated in real time, with O(log n) inserts and rank queries.

Pub/Sub Messaging

Redis supports publish/subscribe patterns for real-time notifications between services.

Job Queues

Libraries like BullMQ use Redis as a persistent job queue for background processing.

Redis in the AI Era

Redis has become critical infrastructure for AI applications:

  • Caching LLM responses — Large language model API calls are expensive (both time and money). Caching identical or near-identical prompts avoids redundant calls.
  • Semantic caching — Using Redis as a vector store (via Redis Stack), similar prompts — not just identical ones — can be matched and cached.
  • Rate limiting AI APIs — Controlling how many tokens or requests are consumed per user.
  • Real-time AI feature stores — Serving pre-computed features to ML models with millisecond latency.

Installing Redis

Mac:

bash
brew install redis
brew services start redis

Ubuntu/Debian:

bash
sudo apt update
sudo apt install redis-server
sudo systemctl start redis

Docker:

bash
docker run -d -p 6379:6379 redis:7-alpine

Connecting with redis-cli

bash
redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET hello "world"
OK
127.0.0.1:6379> GET hello
"world"

Redis listens on port 6379 by default.

Example

bash
# Basic Redis commands
SET user:1:name "Alice"
GET user:1:name
# "Alice"

SET counter 0
INCR counter
# (integer) 1

INCR counter
# (integer) 2

# Set with expiry (TTL in seconds)
SET session:abc123 "user:1" EX 3600
TTL session:abc123
# (integer) 3600

# Check if key exists
EXISTS user:1:name
# (integer) 1

Want to run this code interactively?

Try in Compiler