How to Install Redis Server on Ubuntu 20.04

How to Install Redis Server on Ubuntu 20.04. In this post, we introduce Redis server, its advantages then show you how to install and secure Redis on Ubuntu 20.04.

Above all, Redis is an open source, in memory data store that works very well as a cache or a message broker. It can also be used as a database when you do not need all of the features of a traditional database.

Secondly, Redis supports five data types: String, Hash, List, Set, Sorted Set, and two special data types: Bitmap and HyperLogLog. Well, it is easily configured to automatically operate as a cache, and uses one of the popular eviction algorithms, like the LRU algorithm, that makes it operate as memcached databases.

As we go along, we will take a look at the data structures that Redis supports. Since Redis stores data on disk, it acts like a classic database for many use cases. Moreover, it also acts like a cache. Depending on your use case, Redis either persists your data by periodically flushing the in memory data set to disk, or appends every command to the disk based journal.

Shall we start with How to Install Redis Server on Ubuntu 20.04?

What is Redis?

Image Source: youtube

Primarily, redis (which stands for Remote Dictionary Service) is an open source NoSQL database known for being a fast, in memory, key value data store, cache, message broker, and queue. In Redis Session Store, session data is stored in the Redis database. Open source is commonly used for non relational key value databases and caches.

Due to the unique internal data structure of Redis, it is used to implement applications like caching, session management, chat servers, and queue systems.

Software in Redis is capable of handling a high volume of application requests, keeping data that is often accessed within server memory, that can be written and read rapidly. This simplicity makes Redis perfect for fast development and applications since core data structures are shared between processes and services with ease.

Although Redis supports the mapping of keys value based strings for data storage and retrieval (analogous to data models supported by conventional database types), it also supports other complicated data structures like lists, sets, etc.

In use cases, where backing up the file on disk is not sufficient, Redis still acts as a fast cache in front of another data store. You must have more memory than required by the data, as Redis will fork over every dump snapshot, potentially eating additional memory.

On the other hand, it also supports several data storage formats and can write data automatically to disk in two different ways (AOF and RDB). In fact, many high profile companies and reputed brands like Twitter, Snapchat, Uber, Pinterest, etc., use Redis.

Features of Redis Server

  • Data persistence: Redis server persists data by default. This means that if the computer crashes or there is a power outage, all the data is available when it starts up again.
  • Scalability: Redis server scales to handle high volumes of requests as well as large datasets. This makes it perfect for applications with large datasets or high traffic volumes such as social media sites and messaging apps.
  • Performance: Redis server performs more than 100,000 operations per second on average making it ideal for real time web applications like chat rooms or live feeds.

Advantages of Redis Server

Developers use Redis as a database, cache, or message broker for it provides high performance, reliability, and simplicity. There are many advantages to Redis which we have listed below in detail:

  • Allows you to store key and value pairs in good quantity – You can have enormous keys and values for objects up to 512MB, meaning Redis supports data as large as 1GB per record.
  • Uses Redis Hashing – Redis stores data in the form of string fields and string values, or a key and a map. In fact, it prefers its own hashing mechanism – Redis hash in codes to store user information.
  • Supports Data Replication – The process of building up master slave cache nodes is known as replication. Slave nodes always keep an eye on the master node, thus if the master node is updated, the slaves will also be updated immediately. Slaves can be updated by Redis asynchronously as well.
  • Offers sub messaging system – Redis’ pub/sub mechanism enables you to create high performance messaging applications in any language of your choice.
  • Supports installation in Raspberry Pi and ARM devices – Redis is easy to install on Raspberry Pi devices. Also, it has a tiny memory footprint that helps enable IoT based applications.

Up next with article blog How to Install Redis Server on Ubuntu 20.04 we have came to installation phase.

How to Install Redis Server on Ubuntu 20.04

In this section, we will show you how to install Redis server on Ubuntu 20.04.

Prerequisites

  • An Ubuntu 20.04 server is installed on your system.
  • A root user or a user with sudo privileges.

Update the System

First, you will need to update all the system packages to the latest version. You can update all the packages with the following command.

				
					apt update -y
apt upgrade -y
				
			

Once all the packages are upgraded, you can proceed to install the Redis server.

Install Redis Server

The latest version of Redis server is not included in the Ubuntu default repository. So you will need to install it from the Redis official repository.

Here and first of all, install the required dependencies using the following command.

				
					apt install curl gnupg2 software-properties-common -y
				
			

After installing all the dependencies, add the Redis official repository with the following command.

				
					add-apt-repository ppa:redislabs/redis
				
			

Next, update the repository cache using the following command.

				
					apt update -y
				
			

Then, install the latest version of Redis server with the following command.

				
					apt install redis-server -y
				
			

Once the Redis server is installed, you can proceed to configure Redis.

Configure Redis Server

By default, the Redis service is not managed by systemd. So you need to configure the Redis server to enable the systemd.

First step is to edit the Redis configuration file using the following command.

				
					nano /etc/redis/redis.conf
				
			

Uncomment and change the following line.

				
					supervised systemd
				
			

Save and close the file when you are finish. Then, restart the Redis service to apply the changes.

				
					systemctl restart redis-server
				
			

Please verify the Redis service status using the following command.

				
					systemctl status redis-server
				
			

You should see the Redis service status in the following output.

				
					● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-12-24 14:15:38 UTC; 16s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 55083 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 1066)
     Memory: 2.6M
     CGroup: /system.slice/redis-server.service
             └─55083 /usr/bin/redis-server 127.0.0.1:6379

Dec 24 14:15:38 ubuntu2004 systemd[1]: Starting Advanced key-value store...
Dec 24 14:15:38 ubuntu2004 systemd[1]: Started Advanced key-value store.
				
			

How to Connect Redis CLI

At this point, the Redis server is started and listens on port 6379. Please verify it with the following command.

				
					ss -antpl | grep redis
				
			

You should see the Redis listening port in the following output.

				
					LISTEN    0         511              127.0.0.1:6379             0.0.0.0:*        users:(("redis-server",pid=55083,fd=6))                                        
				
			

Now, run the Redis command line utility to connect to the Redis shell.

				
					redis-cli
				
			

Once you are connected, you will get the Redis console in the following output.

				
					127.0.0.1:6379>
				
			

In this step, please run the following command to test the Redis.

				
					127.0.0.1:6379> ping
				
			

If everything is fine, you will get the following output.

				
					PONG
				
			

Now, please set some value to Redis using the following command.

				
					127.0.0.1:6379> SET server:name "ubuntu"
				
			

After, please retrieve the stored value using the following command.

				
					127.0.0.1:6379> GET server:name
				
			

You will get the following output.

				
					"ubuntu"
				
			

Now, exit from the Redis shell with the following command.

				
					127.0.0.1:6379> exit
				
			

Secure Redis with Password Authentication

At this point, Redis is installed and running but there is not any password authentication when connecting to Redis. For security purposes, it is recommended to secure the Redis with password based authentication.

You can enable the Redis password authentication by editing the Redis configuration file.

				
					nano /etc/redis/redis.conf
				
			

Change the following line.

				
					requirepass securepassword
				
			

Save and close the fiel then restart the Redis service to apply the configuration changes.

				
					systemctl restart redis-server
				
			

Next, connect to the Redis console with the following command.

				
					redis-cli
				
			

Once you are connect, set a key value using the following command.

				
					127.0.0.1:6379> set key 100
				
			

You will get the authorization error.

				
					(error) NOAUTH Authentication required.
				
			

To resolve this error, you will need to authenticate Redis before running any command. Run the following command to authenticate Redis.

				
					127.0.0.1:6379> auth securepassword
				
			

You can now set the key value using the following command.

				
					127.0.0.1:6379> set key 100
				
			

Next, retrive the key value with the following command.

				
					127.0.0.1:6379> get key
				
			

You will get the following output.

				
					"100"
				
			

Secure Redis Built in Commands

Also, it is recommended to secure the Redis built in commands to protect your server. Do it by editing the Redis configuration file.

				
					nano  /etc/redis/redis.conf
				
			

Add the following lines to rename two commands.

				
					rename-command SHUTDOWN SHUTDOWN_DONT
rename-command CONFIG DO_CONFIG
				
			

Save and close the file then restart the Redis service to apply the changes.

				
					systemctl restart redis-server
				
			

Next, connect to the Redis console and authenticate it with the following commands.

				
					redis-cli
127.0.0.1:6379> auth securepassword

				
			

Then, run the config command to retrieve the Redis password.

				
					127.0.0.1:6379> config get requirepass
				
			

You will get the error bucause we rename this command to do_config. Next, run the do_config command again.

				
					127.0.0.1:6379> do_config get requirepass
				
			

You will get your Redis password in the following output.

				
					1) "requirepass"
2) "securepassword"

				
			

Thank you for reading How to Install Redis Server on Ubuntu 20.04. We shall conclude this article blog. 

How to Install Redis Server on Ubuntu 20.04 Conclusion

In this post, we have explained how to install Redis on Ubuntu 20.04. We also showed you how to secure Redis with password authentication.
Lastly, Redis is known to be a phenomenally fast database in memory, used as a database, cache, and message broker, and is written in C.

The Redis server is a key value store, which is an in memory database that stores data in the form of “key” and “value”. Most commonly used as a cache, storing frequently accessed data in memory to allow applications to respond to users.

Would you like to read more Redis content, please navigate to this section of our blog. 

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x