Redis is an open-source in-memory data store for applications that need rapid access to data. It is widely used for caching, session management, real-time applications, message queues, and temporary data storage.
Traditionally, data was stored in databases on disk. An in-memory data store that provides quick access is Redis. This means that the applications may be able to get information faster, and it also helps to lighten the query burden on the primary database.
If you are running an application on an Ubuntu VPS, installing Redis may help you be more responsive and lessen the database work.
In this tutorial, you will learn how to install, configure, secure, and test Redis on Ubuntu VPS.
Why Redis?
Redis is beneficial for applications that want to access the same data again and again or need to read and write rapidly.
Where is Redis Used?
- Caching websites: Store high-demand memory content.
- Session management: Persist user sessions instead of querying the database every time.
- Message queues: They are excellent for asynchronous tasks and background jobs.
- Real-time communication: Support real-time chat apps, notifications, dashboards, etc.
- Database optimization: Reduce duplicate queries to MySQL, PostgreSQL, or other databases.
Prerequisites
Requirements prior to installing Redis: You must have:
- VPS-Ubuntu
- Root or sudo access
- SSH into the server.
- You will need a good internet connection for your application and for Redis memory.
You can connect to your VPS with an SSH client such as Terminal, Windows PowerShell, or PuTTY.
How to Install and Configure Redis on Ubuntu?
Step 1: Update Packages Ubuntu
First, update your VPS’s package index:
sudo apt-get update
The following packages were held back:
sudo apt upgrade -y
Before installing Redis, it’s a good idea to update your system. This ensures you’re working with the latest package information available.
Step 2: Set up Redis
Install Redis with Ubuntu’s APT package manager:
sudo apt install redis-server -y
Upon installation, verify the Redis version:
redis-server -v
The output should show you the installed version of Redis.
If a version number is returned by the command, then Redis has been installed successfully.
Step 3: Verify the Redis Service
Verify that the Redis service is running after installation:
sudo systemctl status redis
If Redis is not running, start the service:
sudo systemctl start redis.service
To have Redis start automatically on the boot of your VPS, enable the service:
sudo systemctl enable redis
You can try the service again with:
sudo systemctl status redis.service
Active (running) status expected.
Step 4: Set up Redis
Redis configuration is stored at:
/etc/redis.conf
Edit the configuration file with nano:
Edit the config file with Nano:
sudo nano /etc/redis/redis.conf
There are several options to configure depending on how Redis is to be used.
Step 5: Test the Redis Connection
Redis offers a command-line interface called redis-cli.
Run:
redis-cli
You must enter the Redis CLI. If authentication is enabled, run:
AUTH YourStrongPassword
A successful authentication returns:
OK
Now test the Redis server using the command:
PING
If Redis is working correctly, it returns:
PONG
It confirms that the Redis server is responding to commands.
Step 6: Store and Retrieve Test Data
You perform the basic read and write test using the Redis CLI.
Create a key:
SET test “Redis is up!”
Redis should return:
OK,
Now, get the stored value:
Now, get the saved value:
test GET
The answer should be
"Redis is running."
It confirms that you have installed Redis correctly to retrieve the data.
Leave the Redis CLI:
GIVE UP
Step 7: Redis Memory Usage Settings
It stores data in RAM, so it is a must considering how much memory Redis eats on your VPS.
You configure a maximum memory limit in the Redis configuration file:
sudo nano /etc/redis/redis.conf
Look for:
maxmemory
You specify a suitable limit based on the server’s available RAM.
For example:
maxmemory 256 MB
You configure what Redis should do while it reaches the memory limit. For a caching workload, an eviction policy such as the following must be appropriate:
maxmemory-policy allkeys-lru
Step 8: Configure the Firewall
It allows Redis to remove less recently used keys when it reaches a configured memory limit.
You don’t need to expose Redis’s default port to the internet if only applications on the same VPS use it.
Redis commonly uses:
6379
If remote access is needed, limit access to trusted IP addresses, and disallow connections from anywhere. For example, restrict access to port 6379.
UFW is a firewall that can help you control what connections are allowed on your VPS.
Redis Consumes Excessive RAM
If Redis is consuming too much memory, you need to check what data you store in Redis and set a reasonable maxmemory configuration. Eviction policy is useful for caching Redis workloads and removing older keys automatically with memory usage.
Redis Security Best Practices
Redis is fast and should be configured carefully on a production VPS.
Follow these practices:
- Do not expose Redis directly to the public internet unless needed.
- Authenticate where necessary.
- Restrict remote access with firewall rules.
- Have a strong and unique password.
- Keep Ubuntu and Redis packages up-to-date.
- Watch Redis memory usage.
- Do not run destructive commands on production data.
- Review Redis logs when diagnosing unexpected behavior.
Redis is a wonderful tool to improve the application performance by providing fast in-memory data access. On Ubuntu it installs smoothly using the APT package manager.
The basic process is to update. Install Ubuntu, install Redis, start the service, configure access, secure the installation, and test Redis with simple commands.
After successful configuration of Redis, hook it up with the application for caching, session management, queues, and other performance-sensitive workloads. If you are running Redis on a production Ubuntu VPS, focus on memory management, limited network access, authentication, and regular monitoring to make the service secure and reliable.
