1. Introduction to Redis
Redis is an open-source, in-memory data structure store used as a:
-
Caching system
-
Database
-
Message broker
It is designed for high performance and low latency, making it ideal for modern web applications.
2. Why Redis is Important
Redis is widely used because it solves common performance problems in applications.
Key Advantages:
-
Extremely fast (data stored in memory)
-
Reduces database load
-
Supports multiple data structures
-
Built-in replication and persistence
-
Easy to integrate with backend frameworks
3. How Redis Works
Redis follows a simple key–value model:
SET user:1 "John"
GET user:1
-
SETstores a value -
GETretrieves a value
All operations happen in memory, which is why Redis is much faster than disk-based systems.
4. Redis Data Structures
Redis supports several powerful data types:
4.1 Strings
SET name "Alice"
GET name
4.2 Lists
LPUSH tasks "Task1"
LPUSH tasks "Task2"
4.3 Sets
SADD users "John"
SADD users "Jane"
4.4 Hashes
HSET user:1 name "John" age 25
4.5 Sorted Sets
ZADD leaderboard 100 "Player1"
These structures allow Redis to handle complex use cases efficiently.
5. Installing Redis
macOS (Homebrew)
brew install redis
brew services start redis
Ubuntu
sudo apt update
sudo apt install redis-server
Verify Installation
redis-cli ping
Expected output:
PONG
6. Basic Redis Commands
| Command | Description |
|---|---|
| SET | Store a value |
| GET | Retrieve a value |
| DEL | Delete a key |
| EXISTS | Check if key exists |
| EXPIRE | Set expiration time |
Example:
SET session:1 "active"
EXPIRE session:1 60
7. Using Redis for Caching
Caching is the most common Redis use case.
Workflow:
-
Check if data exists in Redis
-
If yes → return immediately
-
If no → fetch from database
-
Store result in Redis
Example (PHP):
$value = Redis::get('user:1');
if (!$value) {
$value = DB::table('users')->find(1);
Redis::set('user:1', $value, 60);
}
This improves performance and reduces database queries.
8. Data Persistence in Redis
Redis can persist data to disk using:
RDB (Snapshotting)
-
Saves data at intervals
AOF (Append Only File)
-
Logs every write operation
These options provide a balance between performance and data safety.
9. Redis Pub/Sub System
Redis supports real-time messaging.
Publisher:
PUBLISH notifications "Hello"
Subscriber:
SUBSCRIBE notifications
Common use cases:
-
Chat systems
-
Notifications
-
Real-time dashboards
10. Redis Use Cases
Redis is commonly used for:
-
Session management
-
API response caching
-
Real-time analytics
-
Leaderboards
-
Rate limiting
11. Redis with Laravel
Redis integrates easily with Laravel.
Configuration (.env)
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
Example:
Cache::put('key', 'value', 60);
Cache::get('key');
12. Redis vs Traditional Databases
| Feature | Redis | Relational Databases |
|---|---|---|
| Storage | In-memory | Disk-based |
| Speed | Very fast | Moderate |
| Data Model | Key-value | Tables |
| Use Case | Caching, real-time | Persistent storage |
Redis is typically used alongside databases, not as a replacement.
13. Limitations of Redis
Redis may not be suitable when:
-
Data must be permanently stored without risk
-
Dataset size exceeds available memory
-
Complex relational queries are required
14. Best Practices
-
Use expiration (TTL) for cached data
-
Keep values small and efficient
-
Use clear key naming conventions
-
Monitor memory usage regularly
15. Conclusion
Redis is a powerful tool for improving application performance:
-
Fast and efficient
-
Flexible data structures
-
Ideal for caching and real-time systems
For modern applications, Redis is an essential component in building scalable systems.
