Learn how to configure Redis cache on Ubuntu 24.04 with secure settings, memory optimization, firewall rules, and production-ready configuration steps.
What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory data structure store used as a cache, database, message broker, and streaming engine. Unlike traditional databases that primarily store data on disk, Redis stores data in RAM, which allows it to deliver extremely fast read and write performance measured in microseconds.
Prerequisites
Before we begin, ensure we have the following:
- An Ubuntu 24.04 on dedicated server or KVM VPS.
- Basic Linux Command Line Knowledge.
Learn how to configure Redis cache on Ubuntu 24.04
Step 1: Update the System
Before installing any service, we ensure the system is fully updated. This reduces compatibility issues and strengthens security.
sudo apt update
sudo apt upgrade -y
Ubuntu 24.04 ships with a recent Redis package in its official repositories, maintained and tested for stability.
Step 2: Install Redis Server
We install Redis directly from Ubuntu’s official repository:
sudo apt install redis-server -y
After installation, Redis is automatically registered as a system service.
To verify the installation:
redis-server --version
Step 3: Start and Enable Redis
Redis typically starts automatically. We confirm its status:
sudo systemctl status redis-server
If it is not running:
sudo systemctl start redis-server
To ensure Redis starts automatically after reboot:
sudo systemctl enable redis-server
Systemd integration in Ubuntu 24.04 ensures stable service management and monitoring.
Step 4: Test Redis Connectivity
We validate the installation using the Redis CLI tool:
redis-cli
Inside the CLI, run:
ping
Expected response:
PONG
Exit with:
exit
This confirms Redis is operational and accepting connections locally.
Step 5: Configure Redis for Production
The main configuration file is located at:
/etc/redis/redis.conf
We recommend creating a backup before editing:
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.backup
Edit the configuration file:
sudo nano /etc/redis/redis.conf
1. Bind Address
For local-only usage (recommended for most web applications):
bind 127.0.0.1 ::1
If Redis must accept remote connections, restrict access using firewall rules and authentication.
2. Enable Password Authentication
Set a strong password:
requirepass StrongSecurePasswordHere
Restart Redis:
sudo systemctl restart redis-server
Now test authentication:
redis-cli
Inside CLI:
auth StrongSecurePasswordHere
Authentication protects Redis from unauthorized access, especially in cloud environments.
3. Configure Memory Management
Redis stores data in memory. Proper memory configuration prevents system instability.
Set a maximum memory limit:
maxmemory 512mb
Define eviction policy:
maxmemory-policy allkeys-lru
Common policies include:
- noeviction
- allkeys-lru
- volatile-lru
- allkeys-random
For caching workloads, allkeys-lru is generally recommended.
Restart the service after changes:
sudo systemctl restart redis-server
Step 6: Secure Redis with Firewall
If using UFW (default in Ubuntu):
Check firewall status:
sudo ufw status
Allow only internal access (if required):
sudo ufw allow from 127.0.0.1 to any port 6379
By default, Redis listens on port 6379.
Avoid exposing this port publicly unless absolutely necessary.
Step 7: Optimize Redis for Performance
For high-traffic production systems, we adjust kernel parameters.
Edit sysctl configuration:
sudo nano /etc/sysctl.conf
Add:
vm.overcommit_memory = 1
Apply changes:
sudo sysctl -p
Disable Transparent Huge Pages (THP):
sudo nano /etc/rc.local
Add:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
These optimizations improve Redis stability and reduce latency under heavy load.
Step 8: Enable Persistence (Optional but Recommended)
Redis supports two persistence methods:
- RDB (snapshotting)
- AOF (Append Only File)
In redis.conf, ensure at least one method is enabled.
For AOF:
appendonly yes
AOF provides better durability but slightly impacts performance. For caching-only setups, persistence may be disabled. For session storage or critical data, enable AOF.
Restart Redis after configuration.
Step 9: Monitor Redis Performance
We monitor Redis using built-in tools:
redis-cli info
For real-time monitoring:
redis-cli monitor
Important metrics include:
- used_memory
- connected_clients
- keyspace_hits
- keyspace_misses
Regular monitoring helps maintain performance and avoid memory saturation.
Step 10: Verify Redis is Secure
Run:
redis-cli
Without authentication, commands should be rejected if password protection is enabled.
We also confirm:
- Redis is not publicly exposed.
- Firewall rules are enforced.
- Strong password is configured.
- Unused modules are disabled.
Security misconfiguration is the most common Redis failure point in production.
Final Validation Checklist
Before deploying to live workloads, we confirm:
- Redis service is active and enabled
- Password authentication is configured
- Memory limit is defined
- Proper eviction policy is selected
- Firewall rules are applied
- Persistence is configured according to workload needs
When configured correctly, Redis on Ubuntu 24.04 provides fast, reliable, in-memory caching suitable for web applications, APIs, background jobs, and microservices architectures.
Conclusion
Redis remains one of the most efficient in-memory data stores available today. With Ubuntu 24.04’s stable packages and systemd integration, deployment is straightforward and reliable.
By following this structured configuration process, we ensure our Redis server is secure, optimized, and production-ready. Proper setup protects performance, strengthens infrastructure stability, and supports scalable application growth.
A well-configured Redis instance is not just a cache. It is a strategic performance layer in modern application architecture.
