CentOSRHEL Based

How To Install Redis on CentOS Stream 10

Install Redis on CentOS Stream 10

 

In this tutorial, we will show you how to install Redis on CentOS Stream 10. Redis, an open-source, in-memory data structure store, has become an essential tool for modern web applications. Its versatility as a database, cache, and message broker makes it a popular choice among developers. In this guide, we’ll walk you through the process of installing Redis on CentOS Stream 10, providing you with detailed instructions, best practices, and troubleshooting tips.

Prerequisites

Before we dive into the installation process, ensure that your system meets the following requirements:

  • A CentOS Stream 10 server with root or sudo privileges
  • At least 1GB of RAM (2GB or more recommended for production environments)
  • A minimum of 5GB free disk space
  • An active internet connection for downloading packages

It’s crucial to update your system packages before proceeding with the installation. Run the following command:

sudo dnf update -y

Installation Methods Overview

There are several ways to install Redis on CentOS Stream 10. We’ll cover three primary methods:

  1. EPEL repository method
  2. Remi repository method
  3. Source compilation method

Each method has its advantages, and the choice depends on your specific needs and expertise level.

Installing Redis via Package Manager

EPEL Repository Method

The EPEL (Extra Packages for Enterprise Linux) repository is a trusted source for additional packages not included in the default CentOS repositories. Here’s how to install Redis using EPEL:

  1. Install the EPEL repository:
    sudo dnf install epel-release -y
  2. Update the package cache:
    sudo dnf update -y
  3. Install Redis:
    sudo dnf install redis -y
  4. Start the Redis service:
    sudo systemctl start redis
  5. Enable Redis to start on boot:
    sudo systemctl enable redis

Remi Repository Method

The Remi repository often provides more up-to-date versions of Redis. To install Redis using this method:

  1. Install the Remi repository:
    sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-10.rpm -y
  2. Enable the Remi repository:
    sudo dnf module enable redis:remi-6.2 -y
  3. Install Redis:
    sudo dnf install redis -y
  4. Start and enable the Redis service:
    sudo systemctl start redis
    sudo systemctl enable redis

Installing Redis from Source

For those who prefer the latest version or need specific compile-time options, installing Redis from source is an excellent choice. Follow these steps:

  1. Install development tools:
    sudo dnf groupinstall "Development Tools" -y
  2. Download the latest stable Redis source code:
    wget https://download.redis.io/redis-stable.tar.gz
  3. Extract the archive:
    tar xzf redis-stable.tar.gz
  4. Navigate to the Redis directory:
    cd redis-stable
  5. Compile Redis:
    make
  6. Install Redis:
    sudo make install

After installation, you’ll need to set up Redis as a service manually. Create a systemd unit file for Redis:

sudo nano /etc/systemd/system/redis.service

Add the following content to the file:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Basic Configuration

Proper configuration is crucial for optimal Redis performance and security. Let’s explore some essential settings:

Redis Configuration File

The Redis configuration file is typically located at /etc/redis/redis.conf. Open it with your preferred text editor:

sudo nano /etc/redis/redis.conf

Network Interface Binding

By default, Redis listens on all network interfaces. For improved security, bind it to localhost:

bind 127.0.0.1

Memory Settings

Set the maximum amount of memory Redis can use. For example, to set it to 1GB:

maxmemory 1gb

Persistence Options

Redis offers two persistence options: RDB (Redis Database) and AOF (Append Only File). Enable AOF for better durability:

appendonly yes

Security Configuration

Securing your Redis installation is paramount. Here are some key security measures:

Password Authentication

Set a strong password by uncommenting and modifying the following line in the Redis configuration file:

requirepass YourStrongPasswordHere

Firewall Rules

If you’re using firewalld, allow Redis traffic on the default port:

sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload

SELinux Configuration

If SELinux is enabled, ensure Redis can bind to its port:

sudo setsebool -P redis_enable_notify 1

Performance Optimization

To get the best performance out of Redis, consider the following optimizations:

Memory Allocation

Use the maxmemory-policy directive to define how Redis should handle memory when the maximum limit is reached. For example:

maxmemory-policy allkeys-lru

CPU Affinity

For multi-core systems, you can bind Redis to specific CPU cores:

taskset -c 0,1 redis-server /etc/redis/redis.conf

Filesystem Tuning

Consider using the ext4 filesystem with the noatime mount option for improved performance.

Testing and Verification

After installation and configuration, it’s crucial to verify that Redis is working correctly:

Connection Testing

Use the Redis CLI to test the connection:

redis-cli
127.0.0.1:6379> ping
PONG

Basic Redis Commands

Try some basic Redis commands to ensure functionality:

127.0.0.1:6379> set mykey "Hello, Redis!"
OK
127.0.0.1:6379> get mykey
"Hello, Redis!"

Performance Testing

Use the built-in Redis benchmarking tool:

redis-benchmark -q -n 100000

Troubleshooting Common Issues

Even with careful installation and configuration, you may encounter issues. Here are some common problems and their solutions:

Connection Problems

If you can’t connect to Redis, check the following:

  • Ensure Redis is running: sudo systemctl status redis
  • Verify the bind address in the configuration file
  • Check firewall settings

Permission Issues

If Redis fails to start due to permission errors:

  • Check the ownership of the Redis data directory: ls -l /var/lib/redis
  • Ensure the Redis user has the necessary permissions: sudo chown redis:redis /var/lib/redis

Memory Errors

If Redis is terminating due to out-of-memory errors:

  • Adjust the maxmemory setting in the configuration file
  • Implement an appropriate maxmemory-policy
  • Consider increasing system swap space

Advanced Topics

For those looking to further enhance their Redis deployment, consider exploring these advanced topics:

Clustering Configuration

Redis Cluster provides a way to run a Redis installation where data is automatically sharded across multiple Redis nodes. To set up a basic cluster:

  1. Enable cluster support in the Redis configuration file:
    cluster-enabled yes
  2. Set a unique cluster node ID:
    cluster-config-file nodes-6379.conf
  3. Set the cluster node timeout:
    cluster-node-timeout 5000

Replication Setup

To improve data redundancy and read scalability, set up Redis replication:

  1. On the replica server, add the following to the Redis configuration file:
    slaveof master_ip master_port
  2. Restart the Redis service on the replica:
    sudo systemctl restart redis

Backup Strategies

Implement a robust backup strategy to prevent data loss:

  • Use the SAVE or BGSAVE commands for point-in-time snapshots
  • Set up automated backups using cron jobs
  • Consider using Redis Enterprise for more advanced backup features

Congratulations! You have successfully installed Redis. Thanks for using this tutorial for installing the Redis cache on your CentOS Stream 10. For additional help or useful information, we recommend you check the official Redis website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button