Redis (Remote Dictionary Server) is a powerful, open-source, in-memory data structure store that functions as a database, cache, and message broker. Known for its exceptional performance and versatility, Redis has become an essential tool for modern applications requiring fast data access and real-time processing capabilities.
What is Redis?
Redis is an advanced key-value store that supports various data structures including strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, and geospatial indexes. Unlike traditional databases that store data on disk, Redis keeps all data in memory, making it incredibly fast with sub-millisecond response times.
Key Features of Redis
- In-Memory Storage: All data stored in RAM for lightning-fast access
- Data Persistence: Optional disk persistence with RDB snapshots and AOF logging
- Rich Data Types: Support for complex data structures beyond simple key-value pairs
- Atomic Operations: All operations are atomic, ensuring data consistency
- Pub/Sub Messaging: Built-in publish/subscribe messaging system
- Lua Scripting: Server-side scripting capabilities
- Clustering: Horizontal scaling with Redis Cluster
Installing Redis on Linux
Ubuntu/Debian Installation
The simplest way to install Redis on Ubuntu or Debian systems:
# Update package index
sudo apt update
# Install Redis server
sudo apt install redis-server
# Start Redis service
sudo systemctl start redis-server
# Enable Redis to start on boot
sudo systemctl enable redis-server
# Check Redis status
sudo systemctl status redis-server
Expected Output:
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2025-08-26 09:16:32 IST; 2min 15s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 12345 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 4915)
Memory: 2.3M
CPU: 125ms
CGroup: /system.slice/redis-server.service
└─12345 /usr/bin/redis-server 127.0.0.1:6379
CentOS/RHEL/Fedora Installation
# For CentOS/RHEL (with EPEL repository)
sudo yum install epel-release
sudo yum install redis
# For Fedora
sudo dnf install redis
# Start and enable Redis
sudo systemctl start redis
sudo systemctl enable redis
Compiling from Source
For the latest version or custom configurations:
# Install build dependencies
sudo apt install build-essential tcl
# Download Redis source
wget http://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
# Compile Redis
make
# Optional: Run tests
make test
# Install Redis
sudo make install
Redis Configuration
Redis configuration is managed through the redis.conf file, typically located at /etc/redis/redis.conf.
Important Configuration Parameters
# Bind to specific interface (default: 127.0.0.1)
bind 127.0.0.1
# Set port (default: 6379)
port 6379
# Set password authentication
requirepass your_secure_password
# Configure memory limit
maxmemory 2gb
maxmemory-policy allkeys-lru
# Enable persistence
save 900 1 # Save if at least 1 key changed in 900 seconds
save 300 10 # Save if at least 10 keys changed in 300 seconds
save 60 10000 # Save if at least 10000 keys changed in 60 seconds
After modifying the configuration, restart Redis:
sudo systemctl restart redis-server
Basic Redis Commands
Connecting to Redis
Use the Redis CLI to interact with the server:
# Connect to local Redis instance
redis-cli
# Connect to remote Redis instance
redis-cli -h hostname -p port -a password
Expected Output:
127.0.0.1:6379>
String Operations
Strings are the most basic Redis data type:
# Set a key-value pair
127.0.0.1:6379> SET name "CodeLucky"
OK
# Get a value
127.0.0.1:6379> GET name
"CodeLucky"
# Set with expiration (10 seconds)
127.0.0.1:6379> SETEX temp_key 10 "temporary_value"
OK
# Set multiple keys at once
127.0.0.1:6379> MSET key1 "value1" key2 "value2" key3 "value3"
OK
# Get multiple keys at once
127.0.0.1:6379> MGET key1 key2 key3
1) "value1"
2) "value2"
3) "value3"
# Increment numeric values
127.0.0.1:6379> SET counter 10
OK
127.0.0.1:6379> INCR counter
(integer) 11
127.0.0.1:6379> INCRBY counter 5
(integer) 16
Hash Operations
Hashes are perfect for storing objects:
# Set hash fields
127.0.0.1:6379> HSET user:1001 name "John Doe" email "[email protected]" age 30
(integer) 3
# Get specific field
127.0.0.1:6379> HGET user:1001 name
"John Doe"
# Get all fields and values
127.0.0.1:6379> HGETALL user:1001
1) "name"
2) "John Doe"
3) "email"
4) "[email protected]"
5) "age"
6) "30"
# Get multiple fields
127.0.0.1:6379> HMGET user:1001 name email
1) "John Doe"
2) "[email protected]"
# Check if field exists
127.0.0.1:6379> HEXISTS user:1001 phone
(integer) 0
List Operations
Lists are ordered collections of strings:
# Push elements to the left (beginning)
127.0.0.1:6379> LPUSH tasks "task3" "task2" "task1"
(integer) 3
# Push elements to the right (end)
127.0.0.1:6379> RPUSH tasks "task4" "task5"
(integer) 5
# Get list length
127.0.0.1:6379> LLEN tasks
(integer) 5
# Get range of elements
127.0.0.1:6379> LRANGE tasks 0 -1
1) "task1"
2) "task2"
3) "task3"
4) "task4"
5) "task5"
# Pop elements
127.0.0.1:6379> LPOP tasks
"task1"
127.0.0.1:6379> RPOP tasks
"task5"
Set Operations
Sets are unordered collections of unique strings:
# Add members to set
127.0.0.1:6379> SADD languages "Python" "JavaScript" "Go" "Python"
(integer) 3
# Get all members
127.0.0.1:6379> SMEMBERS languages
1) "Go"
2) "JavaScript"
3) "Python"
# Check membership
127.0.0.1:6379> SISMEMBER languages "Python"
(integer) 1
127.0.0.1:6379> SISMEMBER languages "Java"
(integer) 0
# Get set size
127.0.0.1:6379> SCARD languages
(integer) 3
# Remove member
127.0.0.1:6379> SREM languages "Go"
(integer) 1
Sorted Set Operations
Sorted sets combine the uniqueness of sets with ordering by score:
# Add members with scores
127.0.0.1:6379> ZADD leaderboard 100 "Alice" 95 "Bob" 110 "Charlie" 88 "David"
(integer) 4
# Get range by rank (lowest to highest)
127.0.0.1:6379> ZRANGE leaderboard 0 -1
1) "David"
2) "Bob"
3) "Alice"
4) "Charlie"
# Get range with scores
127.0.0.1:6379> ZRANGE leaderboard 0 -1 WITHSCORES
1) "David"
2) "88"
3) "Bob"
4) "95"
5) "Alice"
6) "100"
7) "Charlie"
8) "110"
# Get reverse range (highest to lowest)
127.0.0.1:6379> ZREVRANGE leaderboard 0 2
1) "Charlie"
2) "Alice"
3) "Bob"
# Get rank of member
127.0.0.1:6379> ZRANK leaderboard "Alice"
(integer) 2
Key Management Commands
# Check if key exists
127.0.0.1:6379> EXISTS name
(integer) 1
# Get key type
127.0.0.1:6379> TYPE name
string
# Set expiration time (seconds)
127.0.0.1:6379> EXPIRE name 300
(integer) 1
# Check time to live
127.0.0.1:6379> TTL name
(integer) 295
# Remove expiration
127.0.0.1:6379> PERSIST name
(integer) 1
# Delete keys
127.0.0.1:6379> DEL temp_key
(integer) 1
# Get all keys matching pattern
127.0.0.1:6379> KEYS user:*
1) "user:1001"
# Rename key
127.0.0.1:6379> RENAME old_key new_key
OK
Redis Persistence
Redis offers two persistence mechanisms:
RDB (Redis Database)
Point-in-time snapshots of your dataset:
# Manual snapshot
127.0.0.1:6379> BGSAVE
Background saving started
# Check last save time
127.0.0.1:6379> LASTSAVE
(integer) 1724662592
AOF (Append Only File)
Logs every write operation for better durability:
# Enable AOF in redis.conf
appendonly yes
appendfsync everysec # Options: always, everysec, no
# Rewrite AOF file
127.0.0.1:6379> BGREWRITEAOF
Background append only file rewriting started
Performance Monitoring
Server Information
# Get comprehensive server info
127.0.0.1:6379> INFO
# Get specific section
127.0.0.1:6379> INFO memory
# Memory
used_memory:2048576
used_memory_human:2.00M
used_memory_rss:8192000
used_memory_rss_human:7.81M
used_memory_peak:2048576
used_memory_peak_human:2.00M
Real-time Monitoring
# Monitor commands in real-time
127.0.0.1:6379> MONITOR
OK
1724662592.123456 [0 127.0.0.1:54321] "GET" "name"
1724662593.234567 [0 127.0.0.1:54322] "SET" "counter" "42"
# Get slow queries log
127.0.0.1:6379> SLOWLOG GET 10
# Reset statistics
127.0.0.1:6379> CONFIG RESETSTAT
OK
Database Management
Redis supports multiple databases (0-15 by default):
# Select database
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]>
# Get current database size
127.0.0.1:6379[1]> DBSIZE
(integer) 0
# Flush current database
127.0.0.1:6379[1]> FLUSHDB
OK
# Flush all databases
127.0.0.1:6379[1]> FLUSHALL
OK
Pub/Sub Messaging
Redis includes a publish/subscribe messaging system:
# Subscribe to channels (Terminal 1)
127.0.0.1:6379> SUBSCRIBE news sports
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "news"
3) (integer) 1
1) "subscribe"
2) "sports"
3) (integer) 2
# Publish messages (Terminal 2)
127.0.0.1:6379> PUBLISH news "Breaking: Redis 7.0 released!"
(integer) 1
127.0.0.1:6379> PUBLISH sports "World Cup finals today!"
(integer) 1
# Pattern subscription
127.0.0.1:6379> PSUBSCRIBE news:*
Reading messages... (press Ctrl-C to quit)
Transactions
Redis transactions allow atomic execution of multiple commands:
# Start transaction
127.0.0.1:6379> MULTI
OK
# Queue commands
127.0.0.1:6379> SET account:1 1000
QUEUED
127.0.0.1:6379> SET account:2 2000
QUEUED
127.0.0.1:6379> INCRBY account:1 -100
QUEUED
127.0.0.1:6379> INCRBY account:2 100
QUEUED
# Execute transaction
127.0.0.1:6379> EXEC
1) OK
2) OK
3) (integer) 900
4) (integer) 2100
# Discard transaction
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> SET test "value"
QUEUED
127.0.0.1:6379> DISCARD
OK
Security Configuration
Authentication
# Set password in redis.conf
requirepass your_secure_password
# Authenticate in CLI
127.0.0.1:6379> AUTH your_secure_password
OK
# Or connect with password
redis-cli -a your_secure_password
Command Renaming/Disabling
# In redis.conf
rename-command FLUSHDB "" # Disable command
rename-command FLUSHALL "FLUSH_ALL_SECRET" # Rename command
rename-command CONFIG "CONFIG_ABCD1234" # Rename sensitive command
Performance Optimization Tips
Memory Optimization
- Use appropriate data types: Choose the most memory-efficient structure
- Set memory limits: Configure
maxmemoryand eviction policies - Use key expiration: Set TTL for temporary data
- Optimize key names: Use shorter, consistent naming patterns
Connection Optimization
# Configure connection limits in redis.conf
maxclients 10000
timeout 300
tcp-keepalive 300
Common Use Cases
Caching
Redis excels as a cache layer:
# Cache user session
127.0.0.1:6379> SETEX session:abc123 3600 "user_id:1001"
OK
# Cache API response
127.0.0.1:6379> SET api:users:all "{\"users\":[{\"id\":1,\"name\":\"John\"}]}" EX 300
OK
Rate Limiting
# Simple rate limiting using counters
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR rate_limit:user:1001
QUEUED
127.0.0.1:6379> EXPIRE rate_limit:user:1001 60
QUEUED
127.0.0.1:6379> EXEC
1) (integer) 1
2) (integer) 1
Real-time Analytics
# Track page views
127.0.0.1:6379> INCR pageviews:2025-08-26
(integer) 1542
# Store user activity
127.0.0.1:6379> ZADD user:1001:activity 1724662592 "login"
(integer) 1
Troubleshooting Common Issues
Memory Issues
# Check memory usage
127.0.0.1:6379> INFO memory
# Find big keys
redis-cli --bigkeys
# Analyze memory usage by key pattern
redis-cli --memkeys --memkeys-samples 1000
Performance Issues
# Check slow queries
127.0.0.1:6379> SLOWLOG GET 10
# Monitor latency
redis-cli --latency
redis-cli --latency-history
# Check client connections
127.0.0.1:6379> CLIENT LIST
Backup and Recovery
Creating Backups
# Manual backup (RDB)
redis-cli BGSAVE
# Copy RDB file
sudo cp /var/lib/redis/dump.rdb /backup/location/
# Backup with custom name
redis-cli --rdb backup-$(date +%Y%m%d-%H%M%S).rdb
Restoring from Backup
# Stop Redis service
sudo systemctl stop redis-server
# Replace RDB file
sudo cp /backup/location/dump.rdb /var/lib/redis/dump.rdb
# Set correct ownership
sudo chown redis:redis /var/lib/redis/dump.rdb
# Start Redis service
sudo systemctl start redis-server
Conclusion
Redis is an incredibly powerful and versatile in-memory data store that can significantly enhance your application’s performance. From simple caching to complex real-time analytics, Redis provides the tools and data structures needed for modern, high-performance applications.
This comprehensive guide covered installation, basic operations, advanced features, and best practices for using Redis on Linux systems. As you continue to work with Redis, remember to monitor performance, implement proper security measures, and choose the right data structures for your specific use cases.
Whether you’re building a web application that needs fast session storage, implementing real-time features, or creating a high-performance caching layer, Redis on Linux provides the reliability and speed your applications demand.
- What is Redis?
- Installing Redis on Linux
- Redis Configuration
- Basic Redis Commands
- Key Management Commands
- Redis Persistence
- Performance Monitoring
- Database Management
- Pub/Sub Messaging
- Transactions
- Security Configuration
- Performance Optimization Tips
- Common Use Cases
- Troubleshooting Common Issues
- Backup and Recovery
- Conclusion







