Redis is a free and open-source in-memory key-value database. It is an fantastic tool that is used as a cache, a message broker, a streaming engine and many more. It offers a wide range of tools and data structures that almost applicable in a wide range of environments.
Redis also offers async replication and blazingly fast non-blocking synchronization and auto-reconnection model.
Let's dive in.
Installing Redis on Linux(Debian Based)
One of the best things about Redis is that its open-source. Hence most of the Linux distributions will provide a Redis package using the default package manager.
For this tutorial though, we can install the Redis server by following a few steps.
Start by importing the key to the APT repository as shown:
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
Next, add the repo to
apt packages index by running the command:
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
Finally, refresh the packages index and install redis:
sudo apt-get update sudo apt-get install redis
Once completed, we can manage the Redis server using
Installing Redis on macOS(Home-brew)
One of the best ways to setup Redis server on macOS, is using the Homebrew package manager. In this section, we will show you how to install and manage the Redis server using Homebrew.
Before running the commands in this tutorial, ensure you habe the Homebrew package manager installed.
You can verify this by running the command:
The commands should return the installed brew version as shown:
Homebrew 3.5.4 Homebrew/homebrew-core (git revision d657d60b748; last commit 2022-07-17)
Once you have verified that Homebrew is installed, run the command below to install Redis:
brew install redis
The command will use the Homebrew package manager to download and install the Redis server on your system.
Once completed, proceed to the next step to learn how to manage the Redis server.
Start and Stop the Redis Server using Systemd
If you are on Linux, you can manage the Redis server using the
For example, to start the Redis server, run the command:
sudo systemctl start redis-server
To check the status of the service, run the command:
sudo systemctl status redis-server
To stop the Redis service, run the command:
sudo systemctl stop redis-server
Similarly, you can restart the service by running the command:
sudo systemctl restart redis-server
Start and Stop the Redis Server using Homebrew
On macOS, you can start the Redis server in two modes:
a. Run the server in the foreground
b. Starting the Redis server daemon in the background.
Redis server in the Foreground.
To run the Redis server in the foreground, use the command:
The command will launc the Redis server in the foreground and return the output details of the running server.
Keep in mind that this will block your terminal input until you stop the server by hitting Control + C. or
^ + C
An example output is as shown:
3153:C 17 Jul 2022 21:18:25.068 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3153:C 17 Jul 2022 21:18:25.068 # Redis version=7.0.3, bits=64, commit=00000000, modified=0, pid=3153, just started 3153:C 17 Jul 2022 21:18:25.068 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 3153:M 17 Jul 2022 21:18:25.069 * Increased maximum number of open files to 10032 (it was originally set to 2560). 3153:M 17 Jul 2022 21:18:25.069 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 7.0.3 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 3153 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 3153:M 17 Jul 2022 21:18:25.071 # WARNING: The TCP backlog setting of 511 cannot be enforced because kern.ipc.somaxconn is set to the lower value of 128. 3153:M 17 Jul 2022 21:18:25.071 # Server initialized 3153:M 17 Jul 2022 21:18:25.071 * Ready to accept connections
You can then connect to the redis server on port
Redis server in the background
If you do not want the Redis server to block your terminal session, you can start the Redis server by running the command:
brew services start redis
The command will start the Redis server in the background and return a simple message. If the server is already running, the command will give you a message indicating so.
To view the status of the service, run the command:
brew services info redis
The command will return a brief information about the status of the service. An example output is as shown:
redis (homebrew.mxcl.redis) Running: ✔ Loaded: ✔ Schedulable: ✘ User: geekbits PID: 644
In the example above, we can see that the service is loaded and running. We can also see the process ID of the Redis server and the user running that service.
To stop the Redis server, run the command:
brew services stop redis
You can also restart the service as:
brew services restart redis
Once have the Redis server up and running, you can connect to it using the command:
This will attempt to connect to localhost on port 6379. If the Redis server is running, you should see the Redis REPL interface as shown:
The command will include the address and port in which the Redis server is running.
To learn how you can test if the Redis server is up and running, check the resource below:
Congratulations to you. In this article, you learned how you can install and setup the Redis server on Linux and macOS. We also learned how to manage the Redis service using
That being said, we hope you enjoyed this tutorial. If you did, share with your friends and leave us a comment below.
Thanks for reading && Happy coding!!