Redis is an in-memory, key-value data structure database that is widely used for caching, session storage, and other applications that require high speed and performance.
Unlike traditional disk-based databases, Redis stores all information in RAM, providing ultra-fast access to data.
Redis is known for its low latency and high performance, making it ideal for home automation projects, web application development, and distributed systems on the Raspberry Pi.
Key features of Redis:
- Persistence: Although it is an in-memory database, Redis can persist data on disk using snapshots or transaction logs.
- Replication: Supports master-slave replication and clustering.
- Pub/Sub: Allows communication between processes using a publish-subscribe model.
- Atomicity: Offers atomic operations and transactions to ensure data integrity.
Installing Redis on Raspberry Pi
Before starting the installation, we ensure that the system is up to date. We run the following commands to update our system:
sudo apt update
sudo apt upgrade
Now we can install Redis from the official repositories. Redis is available in the Debian repositories, so the installation is quite straightforward. Simply run the following command to install Redis:
sudo apt install redis-server
Starting and Enabling the Redis Service
Redis starts automatically after installation. To ensure that the service is running, use the following command:
sudo systemctl status redis-server
To start or restart Redis, you can use the following commands:
sudo systemctl start redis-server
sudo systemctl restart redis-server
Additionally, enable the service to start automatically on boot:
sudo systemctl enable redis-server
Basic Redis Configuration
Redis is primarily configured through its configuration file, located at /etc/redis/redis.conf
. You can modify this file to adjust Redis configuration to your needs.
By default, Redis runs on port 6379 and listens only on the local interface (localhost). You can change network, persistence, and security settings by modifying the configuration file.
To edit the configuration file, use the nano
editor:
sudo nano /etc/redis/redis.conf
After making changes to the configuration file, save and close the file.
Some common configurations you might adjust include:
Configuration | Description | Default Value |
---|---|---|
port | Port on which Redis listens for connections. | 6379 |
bind | IP address to which Redis is bound. | 127.0.0.1 |
requirepass | Password required to authenticate clients. | (empty) |
maxmemory | Maximum amount of memory that Redis can use. | (no limit) |
maxmemory-policy | Policy to follow when the memory limit is reached. | noeviction |
save | Frequency with which Redis performs a backup (RDB). | 900 1, 300 10, 60 10000 |
appendonly | Enables AOF (Append Only File) persistence mode. | no |
appendfilename | Name of the AOF file. | appendonly.aof |
appendfsync | Frequency of AOF file synchronization with disk. | everysec |
loglevel | Level of detail for log messages. | notice |
logfile | File where logs are stored. | (empty) |
databases | Number of databases in Redis. | 16 |
client-output-buffer-limit | Output buffer size limit for clients. | (default) |
timeout | Inactivity timeout in seconds after which a client is disconnected. | 0 |
tcp-keepalive | Interval to send TCP KEEPALIVE packets. | 300 |
protected-mode | Protection mode to prevent unwanted access to Redis. | yes |
slaveof | Configure a node as a slave of another Redis node (for replication). | (empty) |
masterauth | Password for authentication when setting up replication. | (empty) |
requirepass | Password needed to access Redis. | (empty) |
These are just some of the more common configurations. The redis.conf
file contains many more options that you can adjust according to your specific environment and use cases.
Testing the Redis Installation
To check that Redis is working correctly, you can use the command-line tool redis-cli
to connect to the Redis server:
redis-cli
Inside the Redis CLI, you can test basic commands like PING
to check connectivity:
127.0.0.1:6379> PING
PONG
You can also perform basic operations, such as storing and retrieving values:
127.0.0.1:6379> SET mykey "luisllamas_es!"
OK
127.0.0.1:6379> GET mykey
"luisllamas_es!"
Redis also provides many other useful features, such as lists, sets, hashes, etc. These features can be used to store and retrieve data efficiently.