Welcome to my Redis learning journey! This repository contains comprehensive notes and practical examples for learning Redis, the powerful in-memory data structure store.
- What is Redis?
- Why Use Redis?
- Key Features
- Data Structures
- Getting Started
- Basic Commands
- Practical Examples
- Use Cases
- License
Redis (REmote DIctionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and sorted sets with range queries.
- Blazing Fast: Redis stores data in memory, making it significantly faster than disk-based databases
- Versatile: Supports multiple data structures and operations
- Atomic Operations: Ensures data integrity with atomic operations
- Persistence: Can persist data to disk if needed
- Pub/Sub: Built-in publish/subscribe messaging system
- Replication: Supports master-slave replication
- In-memory data storage
- Data persistence options
- Built-in replication
- Lua scripting
- Transactions
- High availability via Redis Sentinel
- Automatic partitioning with Redis Cluster
Redis supports various data structures:
| Data Structure | Description | Common Use Cases |
|---|---|---|
| Strings | Simple key-value pairs | Caching, counters |
| Lists | Linked lists of strings | Message queues, activity feeds |
| Sets | Unordered collections of unique strings | Tags, social graphs |
| Hashes | Maps between string fields and values | Storing objects, user profiles |
| Sorted Sets | Sets with a score for ordering | Leaderboards, priority queues |
| Streams | Log-like data structure | Message queues, event sourcing |
| Geospatial | Location-based data | Nearby locations, geofencing |
- Docker (recommended) or Redis installed locally
- Node.js (for Node.js examples)
docker run --name redis-learning -p 6379:6379 -d redisdocker exec -it redis-learning redis-cliSET user:1 "John Doe"
GET user:1
INCR counter
EXPIRE user:1 3600 # Set expiration in seconds
LPUSH tasks "task1"
RPUSH tasks "task2"
LPOP tasks
LRANGE tasks 0 -1
SADD tags "redis" "database" "cache"
SMEMBERS tags
SISMEMBER tags "redis"
HSET user:1000 name "John" age 30 email "john@example.com"
HGET user:1000 name
HGETALL user:1000
ZADD leaderboard 100 "player1" 200 "player2"
ZRANGE leaderboard 0 -1 WITHSCORES
ZREVRANGE leaderboard 0 -1 WITHSCORES
// Example of caching with Node.js and Redis
const redis = require('redis');
const client = redis.createClient();
async function getCachedData(key) {
const cachedData = await client.get(key);
if (cachedData) return JSON.parse(cachedData);
// If not in cache, fetch from database
const data = await fetchFromDatabase(key);
// Cache for 1 hour
await client.setEx(key, 3600, JSON.stringify(data));
return data;
}// Simple rate limiter using Redis
async function isRateLimited(userId, limit = 10, windowInSeconds = 60) {
const key = `rate_limit:${userId}`;
const current = await client.incr(key);
if (current === 1) {
await client.expire(key, windowInSeconds);
}
return current > limit;
}- Caching: Speed up your application by caching frequently accessed data
- Session Storage: Store user session data
- Real-time Analytics: Track user activity in real-time
- Message Queues: Implement background job processing
- Leaderboards: Track and display top scores or rankings
- Geospatial Indexing: Find locations near a point
- Pub/Sub Messaging: Implement real-time notifications
This project is open source and available under the MIT License.
Happy coding with Redis! 🚀 If you find this repository helpful, consider giving it a ⭐
Created with ❤️ as part of my learning journey
