A Comprehensive Guide to Redis: Installation, Management, and Essential Commands
Redis is an in-memory data store used as a database, cache, and message broker. Its lightning-fast performance makes it a popular choice for caching web applications to reduce database load and improve speed. This guide provides a comprehensive overview of how to work with Redis, from installation to essential commands.
How to Install Redis
The installation process varies depending on your operating system and environment. For AlmaLinux, a popular choice for web hosting, the recommended method is to use the DNF package manager.
Step 1: Install EPEL Repository
Many Redis packages, especially the latest versions, are not in the default AlmaLinux repositories. You’ll need to install the Extra Packages for Enterprise Linux (EPEL) repository first.
sudo dnf install epel-release -y
Step 2: Install Redis
Once EPEL is installed, you can install Redis directly using DNF.
sudo dnf install redis -y
Step 3: Enable and Start the Redis Service
After the installation is complete, start the Redis service and enable it to run automatically on server boot.
sudo systemctl start redis
sudo systemctl enable redis
You can verify that Redis is running by checking its status.
sudo systemctl status redis
How to Uninstall Redis
To completely remove Redis and its configuration files from your system, you should use the remove or purge command depending on your package manager.
For AlmaLinux/CentOS (using DNF/YUM)
Use the remove command to uninstall the Redis package.
sudo dnf remove redis
To also remove any dependencies that were installed with Redis and are no longer needed, you can add the autoremove command.
sudo dnf autoremove
For Ubuntu/Debian (using APT)
On Debian-based systems, the purge option is used to remove the package and all of its configuration files.
sudo apt-get purge --auto-remove redis-server
Important Redis Commands
After installing Redis, you can interact with it using the command-line interface, redis-cli. This tool is essential for managing your Redis instance and its data.
How to Connect to Redis
To connect to a Redis server, use redis-cli. By default, it connects to the local server on port 6379.
redis-cli
If your Redis instance is for a cPanel user, it’s likely using a UNIX socket instead of a port for security and performance. You must specify the socket path to connect.
redis-cli -s /home/your_username/.redis/redis.sock
Once connected, you can run commands. You’ll know it’s working if you get a PONG response after typing PING.
127.0.0.1:6379> PING
PONG
Flushing the Redis Cache
Clearing the Redis cache is a common task for developers and administrators. There are two primary commands for this:
FLUSHALL
This command is the most common for clearing a cache. It deletes all keys from all databases on the connected Redis instance. Use this with caution, as it is a permanent and destructive action.
redis-cli FLUSHALL
FLUSHDB
This command is used to clear all keys from the currently selected database only. This is useful if you have multiple applications using different databases within a single Redis instance and you don’t want to affect other applications’ caches.
redis-cli FLUSHDB
To run a flush command without entering the interactive CLI, you can pass it as an argument.
redis-cli FLUSHALL
# or for a per-user cPanel instance
redis-cli -s /home/your_username/.redis/redis.sock FLUSHALL
Other Essential Redis Commands
SET <key> <value>: Stores a string value.SET mykey "Hello, World!"GET <key>: Retrieves the value of a key.GET mykeyDEL <key>: Deletes a key.DEL mykeyEXPIRE <key> <seconds>: Sets an expiration time on a key.EXPIRE mykey 60will deletemykeyafter 60 seconds.INFO: Provides detailed information about the Redis server, including memory usage and client connections.KEYS *: Returns all keys in the current database. This is a very slow command and should never be used in a production environment as it can block the server.SAVE: Saves the current database to disk.SHUTDOWN: Stops the Redis server.