Redis
Fast Relational/Vector Database
For full documentation visit redis.io.
Over 10 Years | Multiple Projects | Preferred
The speed of Redis can't be denied.
Redis (Remote Dictionary Server) is an open-source, in-memory NoSQL database primarily used as a high-performance cache, session store, message broker, and streaming engine. Because it stores all data directly in RAM instead of a physical hard drive, it delivers sub-millisecond response times and handles hundreds of thousands of operations per second.
Key Characteristics of Redis
-
Extreme Speed: Serves reads and writes instantly by avoiding slow disk search bottlenecks.
-
Key-Value Schema: Operates like a massive dictionary where every data point maps to a specific unique key.
-
Rich Data Structures: Supports more than basic text strings, including Lists, Sets, Hashes, and JSON documents.
-
Data Expiration: Allows a Time-to-Live (TTL) parameter so data safely deletes itself automatically.
-
Optional Persistence: Keeps data secure via snapshots (RDB) or write logs (AOF) to restore memory if systems crash.
Core Data Types with CLI Examples
You can interact with Redis using basic CLI commands. Below are the primary data structures and how they function:
- 1. Strings (Basic Text/Numbers)
The most fundamental type used for simple text or counters.
# Set a key named "user:name" with a value of "Alice"
> SET user:name "Alice"
OK
# Retrieve the value
> GET user:name
"Alice"
- 2. Hashes (Objects/Dictionaries)
Perfect for storing flat objects with multiple fields, such as user profiles.
# Store a user profile object under one key
> HSET user:101 name "Bob" age "30" role "Developer"
(integer) 3
# Fetch the entire object
> HGETALL user:101
1) "name"
2) "Bob"
3) "age"
4) "30"
5) "role"
6) "Developer"
- 3. Lists (Ordered Collections)
Maintains the exact order of elements based on insertion time, functioning like queues.
# Push items into a queue from the right side
> RPUSH tasks "send_email" "resize_image"
(integer) 2
# Pop the first item out from the left side
> LPOP tasks
"send_email"
- 4. Sets (Unordered Unique Collections)
Stores values with a guarantee that no duplicate entries can exist.
# Add elements to a tag list
> SADD tags "tech" "news" "tech"
(integer) 2 # Returns 2 because "tech" was only added once
# View all members
> SMEMBERS tags
1) "news"
2) "tech"
Real-World Architecture Example (Application Caching)
The most popular way to use Redis is as a caching layer situated right in front of a slower primary database (like PostgreSQL or MongoDB).
[ Client ] ---> [ Backend API ] ---> (1. Check Cache) ---> [ Redis (RAM) ]
| |
| (2. If Cache Miss) | (3. Fast Return)
v v
[ Main Database (Disk) ] ----------------------------+
Practical Code Workflow (Python Example)
Using the official Python client library, this snippet demonstrates how an app checks Redis before pulling intensive data from a slow database:
import redis
import time
# Connect to the local Redis instance
# Learn setup details in the official Redis Quick Start Guide
# (https://redis.io/docs/latest/get-started/)
cache = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
def get_product_details(product_id):
# Step 1: Look for data inside the Redis cache
cached_data = cache.get(f"product:{product_id}")
if cached_data:
print("Cache Hit! Returning data instantly from RAM.")
return cached_data
# Step 2: Cache Miss – Simulate a slow database disk query
print("Cache Miss! Fetching from main database...")
time.sleep(2) # Simulates a slow 2-second database delay
db_result = f"Details for Product {product_id}"
# Step 3: Save to Redis with a 60-second expiration (TTL) for future requests
cache.setex(f"product:{product_id}", 60, db_result)
return db_result
# First run takes 2 seconds (Cache Miss)
print(get_product_details("45"))
# Second run finishes in microseconds (Cache Hit)
print(get_product_details("45"))
Modern Databases
Social Media
---
title: Redis
text: Very Fast Relational/Vector Database
image: /assets/svg/redis_logo_icon.svg
link:
target:
---