Redis Core Characteristics
Redis is an open-source, BSD-licensed, in-memory data structure store. It functions as a high-performance key-value database and supports multiple data structures. Its distinct features include:
- Exceptional Performance: Capable of processing approximately 110,000 read operations and 81,000 write operations per second due to its in-memory storage architecture.
- Data Persistence: While primarily memory-based, Redis supports persisting data to disk, allowing data to be preserved across service restarts.
- Rich Data Types: Beyond simple key-value pairs, Redis supports complex data structures including Lists, Sets, Sorted Sets (ZSets), Hashes, BitMaps, and GEO locations.
- Replication: Supports master-slave replication to ensure data availability and redundancy.
- Advanced Features: Includes Pub/Sub messaging, Lua scripting, transactions, and pipelining capabilities.
- Atomicity: All operations are atomic, ensuring commands execute completely or not at all. Transactions are supported via MULTI and EXEC commands.
Compared to other key-value stores, Redis operates primarily in memory but can commit to disk in a compact, append-only format. This design allows for high-speed read/write operations while managing the trade-offs between memory capacity and dataset size. The data structures are exposed transparently to the developer without requiring complex abstraction layers.
Source Code Installation on CentOS
The following guide demonstrates installing Redis version 6.0.6 on CentOS 7 using source code. Since Redis 6.x requires a newer GCC compiler, the script includes steps to update the toolchain.
#!/bin/bash
# Define version and paths
REDIS_VERSION="6.0.6"
REDIS_PKG="redis-${REDIS_VERSION}.tar.gz"
DOWNLOAD_URL="http://download.redis.io/releases/${REDIS_PKG}"
INSTALL_BASE="/usr/local"
REDIS_HOME="${INSTALL_BASE}/redis"
CONFIG_DIR="/etc/redis"
# 1. Install Dependencies
yum install -y centos-release-scl
yum install -y devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
# Enable the new GCC version for the current session
source scl_source enable devtoolset-9
# 2. Download and Extract
cd /tmp
wget "${DOWNLOAD_URL}"
tar xzf "${REDIS_PKG}" -C "${INSTALL_BASE}"
mv "${INSTALL_BASE}/redis-${REDIS_VERSION}" "${REDIS_HOME}"
# 3. Compile
cd "${REDIS_HOME}"
make
# 4. Configure System
mkdir -p "${CONFIG_DIR}"
cp "${REDIS_HOME}/redis.conf" "${CONFIG_DIR}/6379.conf"
# Update configuration: bind to all interfaces, disable protected mode, run as daemon
sed -i 's/^bind 127.0.0.1/bind 0.0.0.0/' "${CONFIG_DIR}/6379.conf"
sed -i 's/^protected-mode yes/protected-mode no/' "${CONFIG_DIR}/6379.conf"
sed -i 's/^daemonize no/daemonize yes/' "${CONFIG_DIR}/6379.conf"
# 5. Create Systemd Service Unit
cat <<EOF > /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
Type=forking
ExecStart=${REDIS_HOME}/src/redis-server ${CONFIG_DIR}/6379.conf
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and start service
systemctl daemon-reload
systemctl start redis
Key Executables
After compilation, the src directory contains several essential tools:
| Tool Name | Description |
|---|---|
| redis-server | The main server executable to start the Redis service. |
| redis-cli | Command line interface for interacting with Redis. |
| redis-benchmark | Utility for testing performance and throughput. |
| redis-check-aof | Tool for repairing Append Only Files (AOF). |
| redis-check-rdb | Tool for checking RDB snapshot files. |
| redis-sentinel | Management tool for Redis high availability (HA). |
Startup Methods
Redis offers three primary ways to start the server.
1. Direct Execution (Foreground)
Executing the binary directly starts the server with default settings. This is useful for quick testing but blocks the terminal session.
cd /usr/local/redis/src
./redis-server
2. Runtime Parameters
You can override default configurations by passing arguments via the command line. For example, starting Redis on a specific port:
./redis-server --port 6380
3. Configuration File (Recommended)
In production environments, using a configuration file is the standard approach. This method ensures stability and persistence of configuration across restarts.
# Start Redis using the configuration file created earlier
/usr/local/redis/src/redis-server /etc/redis/6379.conf
If the daemonize directive is set to yes in the configuration file, the process will run in the background.
Service Management
To stop a running Redis instance gracefully, it is recommended to use the CLI tool rather than forcibly killing the process to prevent data corruption.
# Using redis-cli
redis-cli -p 6379 shutdown
# Or using systemd if configured
systemctl stop redis