Language: EN

instalar-redis-en-raspberry-pi

How to Install Redis on Raspberry Pi

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:

ConfigurationDescriptionDefault Value
portPort on which Redis listens for connections.6379
bindIP address to which Redis is bound.127.0.0.1
requirepassPassword required to authenticate clients.(empty)
maxmemoryMaximum amount of memory that Redis can use.(no limit)
maxmemory-policyPolicy to follow when the memory limit is reached.noeviction
saveFrequency with which Redis performs a backup (RDB).900 1, 300 10, 60 10000
appendonlyEnables AOF (Append Only File) persistence mode.no
appendfilenameName of the AOF file.appendonly.aof
appendfsyncFrequency of AOF file synchronization with disk.everysec
loglevelLevel of detail for log messages.notice
logfileFile where logs are stored.(empty)
databasesNumber of databases in Redis.16
client-output-buffer-limitOutput buffer size limit for clients.(default)
timeoutInactivity timeout in seconds after which a client is disconnected.0
tcp-keepaliveInterval to send TCP KEEPALIVE packets.300
protected-modeProtection mode to prevent unwanted access to Redis.yes
slaveofConfigure a node as a slave of another Redis node (for replication).(empty)
masterauthPassword for authentication when setting up replication.(empty)
requirepassPassword 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.