- 01 Aug 2024
- 4 Minutes to read
- Print
- PDF
Install Redis on Ubuntu
- Updated on 01 Aug 2024
- 4 Minutes to read
- Print
- PDF
Redis is an in-memory data structure store that may be used as a message broker, database, and cache on Ubuntu systems. Many data structures are supported, including strings, hashes, lists, sets, and more. Redis on Ubuntu offers real-time applications that need quick access to and manipulation of data a reliable, high-performance solution. It is renowned for its strength, simplicity, and support for replication, persistence, and high availability.
Install Redis on Ubuntu
To install Redis (Version 6.0.16) on Ubuntu, perform the following steps:
- Update your local package index to ensure you have the latest version of available packages.
sudo apt update - Download Redis 6.0.16 using the below link.
curl -O http://download.redis.io/releases/redis-6.0.16.tar.gz - Extract the downloaded tar file using the command.
tar xzvf redis-6.0.16.tar.gz - After the "tar" file is extracted, use the below command to navigate to the downloaded tar file.
cd redis-6.0.16 - Install the Redis server package from the official Ubuntu repositories.
sudo apt install redis-server =6:6.0.16-1 - To edit the Redis configuration use the below command.
sudo vi /etc/redis/redis.conf - Scroll down and locate the supervised directive. By default, this is set to no. Set this directive to systemd. supervised systemd.
Figure: General - If any changes are made to the Redis configuration file, restart the Redis service to apply the changes.
sudo systemctl restart redis - To know the status of the Redis services use the below command.
sudo systemctl status redis
Figure: Redis status
To enable the Redis server to start automatically upon system startup or a reboot.
sudo systemctl enable --now redis-server
To check the version of Redis installed.
redis-server -v
Figure: Redis version
Configure Authentication for Redis
- To configure authentication for Redis use the below command.
sudo vi /etc/redis/redis.conf - In the configuration file, locate and uncomment the requirepass directive. By default, this is set to foobared, which is a placeholder. Change this to a strong password. This is the password that clients will authenticate with using the AUTH command.
Figure: Require pass - Use the command to restart the Redis service.
sudo systemctl restart redis.service
To test if password authentication works, invoke the Redis client.
Figure: Authentication
Allow Remote Connection
By default, Redis listens to port 6397 and is only accessible from localhost. However, you can configure it to allow remote connections from anywhere.
To allow remote connection to Redis, perform the below steps:
- Use the below command, to open the Redis configuration.
sudo vi /etc/redis/redis.conf - Set the bind attribute to 0.0.0.0 to allow remote connections from anywhere and save the changes.
Figure: Bind attribute - After the changes are made, use the below command to restart.
sudo systemctl restart redis.service - To confirm that Redis can be accessed remotely, use the below command.
sudo ss -an | grep 6379
Figure: SS command - If the Firewall is enabled, use the below commands.
sudo ufw allow 6379/tcp
sudo ufw reload - To access the Redis server remotely, use the below commands.
redis-cli -h host-ip-address -p port
redis-cli -h 192.168.50.83 -p 6379
As a best practice, ensure the below keys are accommodated in redis.config file.
An example file with recommended initial settings is displayed below.
Figure: Recommended settings
To know more about the fields in the recommended settings page, refer to the table.
Field | Description | Use |
bind 127.0.0.1 | Specifies the IP address that Redis will listen to for incoming connections. | By setting it to 127.0.0.1, Redis will only accept connections from the localhost, enhancing security by restricting external access. |
protected-mode yes | Enables a safe mode that protects Redis from being accessed remotely when no authentication or binding to a public IP address is configured. | Prevents accidental exposure of the Redis server to the internet, providing an additional layer of security. Note Disable the protected mode in production environments where you have controlled access (protected-mode no). |
requirepass your_strong_password | Sets a password for the Redis server. | Clients must authenticate with this password before being able to execute commands, enhancing security. |
appendonly yes | Enables Append-Only File (AOF) persistence mode. | Ensures that every write operation is logged, allowing for data recovery in case of a server crash. |
appendfsync everysec | Configures the frequency at which the AOF file is synced to disk. | By setting it to everysec, Redis will sync the AOF file to disk every second, balancing durability and performance. |
save 900 1 | Configures Redis to perform a snapshot every 900 seconds (15 minutes) if at least 1 key has changed. | Periodic snapshot for data persistence. |
save 300 10 | Configures Redis to perform a snapshot every 300 seconds (5 minutes) if at least 10 keys have changed. | More frequent snapshots when moderate changes occur. |
save 60 10000 | Configures Redis to perform a snapshot every 60 seconds (1 minute) if at least 10,000 keys have changed. | High-frequency snapshots during periods of high write activity. |
maxmemory 2gb | Sets the maximum amount of memory Redis is allowed to use. A good starting point might be 75% of your total RAM. | Limits memory usage to 2GB, ensuring Redis does not exceed the specified memory and potentially affects other applications. |
maxmemory-policy allkeys-lru | Defines the eviction policy to apply when the maximum memory limit is reached. | allkeys-lru evicts the least recently used keys to make space for new data, applicable to all keys. |
tcp-backlog 511 | Sets the backlog size for TCP connections. | Determines the queue size for pending connections, set to 511 to handle a moderate load. |
tcp-keepalive 300 | Configures the TCP keepalive setting. | Sends a keepalive packet every 300 seconds (5 minutes) to maintain a connection with clients. |
logfile /var/log/redis/redis-server.log | Specifies the file where Redis logs its activity. | Directs logs to /var/log/redis/redis-server.log for monitoring and troubleshooting. |
replicaof <master-ip> <master-port> | Configures the server to act as a replica of another Redis instance. | Makes the current server a replica of the master located at <master-ip> and <master-port>, enabling data replication for redundancy and high availability. |
Redis details are to be passed in Appsettings.production.JSON.
"Redis":{ "Hostname":"X.X.X.X", // redis IP "Port":"6379", "UserName":"default", "Password":"Password", // redis password "Topic":"Devapexsanity101", // Instance name "Database":12, "Enable":true }, |