Introduce
In today's application development environment, ensuring the availability and fault tolerance of systems is extremely important. In this context, Redis Sentinel emerges as a crucial solution for managing and protecting your Redis systems.
Redis Sentinel is an automated system management component of Redis, designed to monitor, manage, and provide fault tolerance for Redis nodes. By deploying Redis Sentinel, you can confidently build and operate Redis systems that are self-healing and highly fault-tolerant.
In this article, we will explore Redis Sentinel, including its main functions, how to deploy and configure it, and the benefits it brings to managing your Redis system. Let’s delve into how Redis Sentinel can enhance the performance and reliability of your Redis systems!
Check the logs in the containers; if they display as shown below, you have successfully run it:
- Log redis-sentinel-demo-redis-master-1:
Thus, the database setup step is complete.
Connecting the Application to Redis Sentinel
- Information on the necessary environment variables:
REDIS_MASTER_HOST_PORT_DOCKER=172.50.0.10:6379
REDIS_SLAVE_HOST_PORT_DOCKER_1=172.50.0.11:6379
REDIS_SLAVE_HOST_PORT_DOCKER_2=172.50.0.12:6379
REDIS_MASTER_MACHINE_HOST=your_machine_ip
REDIS_SLAVE_MACHINE_HOST_1=your_machine_ip
REDIS_SLAVE_MACHINE_HOST_2=your_machine_ip
REDIS_MASTER_MACHINE_PORT=6379
REDIS_SLAVE_MACHINE_PORT_1=6380
REDIS_SLAVE_MACHINE_PORT_2=6381
REDIS_SENTINEL_HOST_PORT_DOCKER_1=172.50.0.13:26379
REDIS_SENTINEL_HOST_PORT_DOCKER_2=172.50.0.14:26379
REDIS_SENTINEL_HOST_PORT_DOCKER_3=172.50.0.15:26379
REDIS_SENTINEL_HOST_1=your_machine_ip
REDIS_SENTINEL_HOST_2=your_machine_ip
REDIS_SENTINEL_HOST_3=your_machine_ip
REDIS_SENTINEL_PORT_1=26379
REDIS_SENTINEL_PORT_2=26380
REDIS_SENTINEL_PORT_3=26381
REDIS_MASTER_NAME=mymaster
REDIS_DB=0
- Write a config file to connect to Redis Sentinel:
const dbConfig = {
redisSentinels: {
natMap: {
[`${process.env.REDIS_MASTER_HOST_PORT_DOCKER}`]: {
// Docker IP and internal port of redis-primary
host: process.env.REDIS_MASTER_MACHINE_HOST,
port: parseInt(process.env.REDIS_MASTER_MACHINE_PORT), // Exposed port in docker-compose
},
[`${process.env.REDIS_SLAVE_HOST_PORT_DOCKER_1}`]: {
// Docker IP and internal port of redis-replica 1
host: process.env.REDIS_SLAVE_MACHINE_HOST_1,
port: parseInt(process.env.REDIS_SLAVE_MACHINE_PORT_1), // Exposed port in docker-compose
},
[`${process.env.REDIS_SLAVE_HOST_PORT_DOCKER_2}`]: {
// Docker IP and internal port of redis-replica 2
host: process.env.REDIS_SLAVE_MACHINE_HOST_2,
port: parseInt(process.env.REDIS_SLAVE_MACHINE_PORT_2), // Exposed port in docker-compose
},
// Docker IP of Sentinel 1
[`${process.env.REDIS_SENTINEL_HOST_PORT_DOCKER_1}`]: {
host: process.env.REDIS_SENTINEL_HOST_1,
port: parseInt(process.env.REDIS_SENTINEL_PORT_1), // Exposed port in docker-compose
},
// Docker IP of Sentinel 2
[`${process.env.REDIS_SENTINEL_HOST_PORT_DOCKER_2}`]: {
host: process.env.REDIS_SENTINEL_HOST_2,
port: parseInt(process.env.REDIS_SENTINEL_PORT_2), // Exposed port in docker-compose
},
// Docker IP of Sentinel 3
[`${process.env.REDIS_SENTINEL_HOST_PORT_DOCKER_3}`]: {
host: process.env.REDIS_SENTINEL_HOST_3,
port: parseInt(process.env.REDIS_SENTINEL_PORT_3), // Exposed port in docker-compose
},
},
sentinels: [
{ host: process.env.REDIS_SENTINEL_HOST_1, port: parseInt(process.env.REDIS_SENTINEL_PORT_1) },
{ host: process.env.REDIS_SENTINEL_HOST_2, port: parseInt(process.env.REDIS_SENTINEL_PORT_2) },
{ host: process.env.REDIS_SENTINEL_HOST_3, port: parseInt(process.env.REDIS_SENTINEL_PORT_3) },
],
name: process.env.REDIS_MASTER_NAME || 'mymaster',
redisDB: parseInt(process.env.REDIS_DB, 10) || 0,
},
};
module.exports = dbConfig;
- Create a client using
ioredisto connect to the Sentinel:
const { Redis } = require('ioredis');
const config = require('../configs');
const redisClient = new Redis(
{
sentinels: config.redisSentinels.sentinels,
name: config.redisSentinels.name,
showFriendlyErrorStack: true,
db: config.redisSentinels.redisDB,
},
{ natMap: config.redisSentinels.natMap },
);
redisClient.on('connect', () => {
console.info('Connect Redis successfully');
});
redisClient.on('error', (err) => {
console.error('Redis client error', err);
});
module.exports = { redisClient };
- Write a simple
ExpressJSserver with two routes: /write and /read to test writing and reading data to Redis via the Sentinel connection:
Results
- Run the server and check the connection:
- Check the API for reading data:
SOCIAL SHARE CARD GENERATOR