Redis, short for Remote Dictionary Server, is a high-performance, open-source, in-memory data store known for its speed and versatility. It is primarily used in caching or as a “quick-access” database. Unlike traditional databases that rely on disk-based storage, Redis operates entirely in-memory, enabling the fastest operations with minimum latency. It’s based on a NoSQL database where data structures like strings, hashes, lists, sets, etc., are stored as key-value pairs where the key is the unique identifier and stores its corresponding value. Giants like X (formerly Twitter), Uber, and Pinterest rely on Redis to power time-sensitive workloads that demand speed and scalability. This makes Redis an essential tool for modern application development, especially in scenarios that require real-time performance.
When deploying Redis in production, however, it’s important to consider not just the tool itself but also the infrastructure supporting it. In this guide, we’ll walk through the step-by-step process of installing Redis on an Ubuntu VM, configuring the server, creating and retrieving data, and setting up authentication for the Redis server to secure its usage to only authorized clients.
Prerequisites
- A Virtual Machine (such as the ones provided by NodeShift) with at least:
- 1 vCPUs
- 1 GB RAM
- 10 GB SSD
- Ubuntu 22.04 VM
Note: The prerequisites for this are highly variable across use cases. A high-end configuration could be used for a large-scale deployment.
Step-by-step process to install Redis on Ubuntu 22.04
For this tutorial, we’ll use a CPU-powered Virtual Machine by NodeShift, which provides high-compute Virtual Machines at a very affordable cost on a scale that meets GDPR, SOC2, and ISO27001 requirements. It also offers an intuitive and user-friendly interface, making it easier for beginners to get started with Cloud deployments. However, feel free to use any cloud provider you choose and follow the same steps for the rest of the tutorial.
Step 1: Setting up a NodeShift Account
Visit app.nodeshift.com and create an account by filling in basic details, or continue signing up with your Google/GitHub account.
If you already have an account, login straight to your dashboard.
Step 2: Create a Compute Node (CPU Virtual Machine)
After accessing your account, you should see a dashboard (see image), now:
- Navigate to the menu on the left side.
- Click on the Compute Nodes option.
- Click on Start to start creating your very first compute node.
These Compute nodes are CPU-powered virtual machines by NodeShift. These nodes are highly customizable and let you control different environmental configurations, such as vCPUs, RAM, and storage, according to your needs.
Step 3: Select configuration for VM
- The first option you see is the Reliability dropdown. This option lets you choose the uptime guarantee level you seek for your VM (e.g., 99.9%).
- Next, select a geographical region from the Region dropdown where you want to launch your VM (e.g., United States).
- Most importantly, select the correct specifications for your VM according to your workload requirements by sliding the bars for each option.
Step 4: Choose VM Configuration and Image
- After selecting your required configuration options, you’ll see the available VMs in your region and as per (or very close to) your configuration. In our case, we’ll choose a ‘1 vCPUs/1GB/25GB SSD’ Compute node.
- Next, you’ll need to choose an image for your Virtual Machine. For the scope of this tutorial, we’ll select Ubuntu, as we will deploy the Redis server on Ubuntu.
Step 5: Choose the Billing cycle and Authentication Method
- Two billing cycle options are available: Hourly, ideal for short-term usage, offering pay-as-you-go flexibility, and Monthly for long-term projects with a consistent usage rate and potentially lower cost.
- Next, you’ll need to select an authentication method. Two methods are available: Password and SSH Key. We recommend using SSH keys, as they are a more secure option. To create one, head over to our official documentation.
Step 6: Finalize Details and Create Deployment
Finally, you can also add a VPC (Virtual Private Cloud), which provides an isolated section to launch your cloud resources (Virtual machine, storage, etc.) in a secure, private environment. We’re keeping this option as the default for now, but feel free to create a VPC according to your needs.
Also, you can deploy multiple nodes at once with the Quantity option.
That’s it! You are now ready to deploy the node. Finalize the configuration summary; if it looks good, go ahead and click Create to deploy the node.
Step 7: Connect to active Compute Node using SSH
As soon as you create the node, it will be deployed in a few seconds or a minute. Once deployed, you will see a status Running in green, meaning that our Compute node is ready to use!
Once your node shows this status, follow the below steps to connect to the running VM via SSH:
- Open your terminal and run the below SSH command:
(replace root
with your username and paste the IP of your VM in place of ip
after copying it from the dashboard)
ssh root@ip
2. In some cases, your terminal may take your consent before connecting. Enter ‘yes’.
3. A prompt will request a password. Type the SSH password, and you should be connected.
Output:
Step 8: Install Redis
- Start by updating the package source-list to keep up with the latest changes and security versions.
apt update
Output:
2. Install the Redis server with the given command
apt install redis-server -y
Output:
Step 9: Configure and start the Redis server
Once the installation is complete, we will need to edit the Redis configuration file to instruct it to use systemd
to initialize and run the Redis service.
- Open the configuration file in the Nano editor (or any editor of your choice).
nano /etc/redis/redis.conf
2. Scroll down in the file and you’ll see a supervised
directive. It has been set to no
, so we will change it to “systemd
“, because we are on Ubuntu and it will use systemd
to start the Redis service.
The content in the file should look like this:
Once done, save and exit the editor (Ctrl+O > Enter > Ctrl+X
).
3. Start the Redis service
After doing the above changes, restart the service for changes to take effect,
systemctl restart redis
and check the status to confirm it’s running:
systemctl status redis
Output:
4. Enable the service so from next time it starts automatically on system start or reboot.
sudo systemctl enable --now redis-server
Output:
5. Check the installed version.
redis-server -v
Output:
Step 10: Access the Redis CLI
- Run the following command to start the Redis shell.
redis-cli
Output:
The output above indicates that our Redis server is working perfectly fine and we can start using it through the Redis shell.
2. Check the host connectivity by running the ping
command. If it shows the output as PONG
, it means Redis is operating as expected.
Output:
3. Set a key in Redis
In Redis, keys are the fundamental data structure to identify, store, and retrieve data. Each key in Redis maps to a corresponding value assigned to it.
Moving forward, we’ll set up a key named “view” with the value “Mountains” using the following command.
set view "Mountains"
Output:
Getting the output OK
indicates that the data key has been successfully created and saved in Redis.
4. Retrieve the data (key).
Use the get
command along with the name of the key to retrieve the key that you’ve created.
(replace view
with the name of your key)
get view
Output:
Step 11: Set up Authentication for Redis server
Redis server is not configured for authentication by default. For organizations using Redis, it’s important to configure the authentication system so that only authorized clients can access the data stored on the server.
Follow the below-mentioned steps to set it up:
- Open the configuration file using an editor of your choice.
nano /etc/redis/redis.conf
2. Scroll to the requirepass
directive, which is set to foobared
by default and is commented.
Uncomment this line and change the value with a strong password, which you will use later to authenticate to Redis before using it. The edited version should look similar to this:
Save and exit the editor (Ctrl+O > Enter > Ctrl+X
).
3. Test the authentication setup
First, restart the Redis service to ensure the changes take effect.
systemctl restart redis.service
Then proceed to invoke the Redis shell by running redis-cli. Once invoked, run the get command to see if you can retrieve and see the data stored in the server.
Output:
As you may observe, the output shows “Authentication required” error, which means that our authentication is working as expected and it won’t let you work with the Redis server until you are authenticated.
To access the server, use the command below to authenticate yourself.
(replace <password>
with the server’s password that you saved in the configuration file)
AUTH <password>
Once authenticated, run the get
command again to check if you can access the key.
Output:
As shown in the output above, after getting authenticated, we can successfully work with the server.
Conclusion
In this guide, we have covered setting up Redis on an Ubuntu 22.04 VM in a simple approach. However, you can optimize the configuration in a production environment and benefit from its production-level features. While Redis offers exceptional performance, choosing the right server to host it can significantly impact reliability and ease of deployment. With NodeShift, you get reliability with high uptime from several compute providers aggregated under one hood in the platform, combined with the ease of use and affordability for businesses of all scales.