Set Up a Redis Server for Caching on AlmaLinux

By Raman Kumar

Updated on Feb 17, 2025

In this tutorial, we'll learn how to set up a Redis server for caching on AlmaLinux 9 server.

Caching is one of the most effective strategies to achieve this, and Redis—a high-performance in-memory data store—is a favorite among IT professionals for its speed and reliability. In this guide, we’ll walk you through installing and configuring a Redis server on AlmaLinux 9, transforming it into a powerful caching layer that can drastically reduce load times and improve overall application performance. 

Whether you’re a seasoned developer or just stepping into the world of web technologies, this tutorial will provide clear, actionable steps to help you harness the full potential of Redis, secure your setup, and seamlessly integrate it with your web apps. Let’s dive into optimizing your system for better performance and a smoother user experience.

Prerequisites:

Setting Up a Redis Server for Caching on AlmaLinux

1. Update Your System and Install Redis

Before installing any new software, it’s essential to update your package lists. This ensures that you’re fetching the latest version of Redis available in EPEL.

Install EPEL (if not already installed):

sudo dnf install epel-release -y

Once the update completes, install Redis using:

sudo dnf install redis -y

2. Configuring Redis for Caching

The default Redis configuration file is located at /etc/redis/redis.conf. Open this file in your favorite text editor (here, we use nano):

sudo nano /etc/redis/redis.conf

Key configuration points for caching:

Memory Management:

Caching means storing data temporarily in memory. To prevent Redis from consuming all available memory, set a memory limit. For instance, to limit Redis to 256 MB, add or modify the following line:

maxmemory 256mb

Then, choose an eviction policy so Redis knows how to free up memory when this limit is reached. For caching scenarios where every key is a cache entry, the allkeys-lru (Least Recently Used) policy is often ideal:

maxmemory-policy allkeys-lru

Explanation:

  • maxmemory restricts the total amount of memory Redis can use.
  • maxmemory-policy defines the behavior when the memory limit is reached. The allkeys-lru policy evicts the least recently used keys, which is generally a good strategy for caching.

Persistence Settings:

For caching, you might not need data persistence (i.e., saving data to disk) because cached data is by nature transient. If you decide you want to disable persistence, you can comment out or adjust the save directives. For example, to disable automatic snapshots, you can add:

save ""

Note: Only disable persistence if you’re confident that losing cached data (which can be regenerated) won’t harm your application.

Binding and Security:

By default, Redis binds to 127.0.0.1 (localhost), which is ideal for security if your web application runs on the same server. If you need remote access, you might change the bind address, but be sure to secure your server either with a firewall or password authentication.

To require a password, locate and modify the following line:

# requirepass foobared

Remove the # to uncomment and replace foobared with a strong password:

requirepass YourStrongPasswordHere

After making your changes, save and exit the file (in Nano, press Ctrl+O to write out the file and Ctrl+X to exit).

3. Restart and Enable the Redis Service

After updating the configuration, restart Redis to apply the changes:

sudo systemctl restart redis-server

To ensure that Redis starts automatically on boot (important for production systems), run:

sudo systemctl enable redis-server

Why this matters:

  • Restarting the service ensures that the new configurations take effect immediately.
  • Enabling at boot provides high availability, so your caching layer is always ready when your system reboots.

4. Testing Your Redis Installation

To verify that Redis is installed and running correctly, use the Redis command-line interface:

redis-cli -a YourStrongPasswordHere ping

Once in the CLI, test the server by typing:

ping

If everything is working correctly, Redis will respond with:

PONG

Type exit to leave the Redis CLI.

Why this matters:

  • This simple test confirms that your Redis server is active and responsive, forming a solid foundation for integrating with your web application.

5. Integrating Redis with Your Web Application

Redis is commonly used to cache database queries, sessions, or frequently accessed data, thus speeding up web applications. Below is an example using Python with the redis module.

First, install the Python Redis client if you haven’t already:

pip install redis

Then, use the following sample code in your application:

import redis

# Connect to Redis (adjust host, port, and password as necessary)
cache = redis.Redis(host='localhost', port=6379, db=0, password='YourStrongPasswordHere')

# Set a key-value pair
cache.set('example_key', 'example_value')

# Retrieve and print the value
value = cache.get('example_key')
print(value)  # Should output: b'example_value'

Explanation:

  • Connection Setup: Replace the host, port, and password parameters based on your Redis configuration.
  • Caching Data: The snippet sets a key and retrieves it, demonstrating how simple it is to use Redis as a caching layer.

6. Monitoring and Fine-Tuning Redis

For ongoing performance monitoring, you can use the built-in INFO command:

redis-cli -a YourStrongPasswordHere INFO

This command outputs detailed statistics about your Redis server, including memory usage, connected clients, and key eviction events. Monitoring these metrics helps you adjust your configurations over time for optimal performance.

Why this matters:

  • Regular monitoring ensures that your caching strategy remains effective and that your Redis server is not over-consuming system resources.

7. Securing Your Redis Server

If you need to allow remote connections or if your server is exposed to untrusted networks, consider additional security measures:

Firewall Configuration:

Use tools like ufw (Uncomplicated Firewall) to restrict access to port 6379 only to trusted IP addresses.

sudo firewall-cmd --permanent --zone=public --add-rich-rule=' rule family="ipv4" source address="192.168.1.100/32" port protocol="tcp" port="6379" accept' sudo firewall-cmd --reload
  • Password Protection: As previously shown, setting a strong password with the requirepass directive adds a layer of security.
  • Binding to localhost: When possible, keep Redis bound to 127.0.0.1 so that only local applications can connect.
  • Why this matters: Redis, by default, is optimized for speed rather than security. Taking these steps helps ensure that your caching layer doesn’t become a security vulnerability.

In this tutorial, we've learnt how to set up a Redis server for caching on AlmaLinux 9 server and configured it specifically for caching purposes, and integrated it into a web application environment. This setup not only boosts performance by reducing load times but also provides a flexible, high-speed caching solution that can be fine-tuned as your application grows.

Happy caching!