search
HomeWeb Front-endJS TutorialLearn more about how to use the redis cluster function in node [detailed configuration]

How to use the redis cluster function in node? The following article will give you a detailed explanation of the redis cluster configuration based on node. I hope it will be helpful to you!

Learn more about how to use the redis cluster function in node [detailed configuration]

Regarding the use of redis cluster function in nodejs, I have not found a relatively complete and easy-to-understand article. I understand the article, so I also took many detours during the development and debugging process.

This article will introduce in detail how to build a redis cluster locally, how to use the cluster on the client, and summarize and explain the problems and errors encountered during the construction process to avoid using it next time When necessary, we will take detours to improve development and work efficiency.

The background for using the cluster is: in Redis singleton mode, with the increase in the number of users and visits, the qps value rises sharply??, and a large number of io operations lead to a certain amount of occupancy at a certain moment When the cpu (100%) is full, there is a risk of downtime at any time. At the same time, batch processing of redis and other methods only treat the symptoms but not the root cause, and cannot break through the bottleneck of server performance. Therefore, it is imperative to use a cluster solution or add redis instances.

Explanation of terms - cluster

Cluster generally refers to a server cluster, which is different from a distributed system. It is a collection of many servers that perform the same service together. From the client's perspective, it looks like There is only one server. The cluster can use multiple computers for parallel computing to obtain a very high computing speed. It can also use multiple computers for backup, so that if any machine breaks down, the entire system can still function normally. run. (Before redis3.0, the sentinel mode was generally used, but the configuration of the sentinel is slightly complicated, and the performance and high availability are average)

redis cluster requirements

Since the voting fault tolerance mechanism requires more than half of the nodes to think that a node is down, the node is down, so 2 nodes cannot form a cluster, so the Redis cluster requires at least 3 nodes.

To ensure the high availability of the cluster, each node needs to have a slave node (that is, a backup node), so the Redis cluster requires at least 6 servers. (Three masters and three slaves, three deposits and three retrievals, high availability, backup)

Of course, it is impossible for us to use so many servers when debugging locally, so we can simulate running locally 6 redis instances, In fact, the Redis cluster setup in the production environment is basically the same as here.

Build a local redis cluster in mac environment

1. Download and install redis

You can choose to install it on the official website, or you can use the named line Installation

#安装
brew install redis
#启动
redis-server
#进入redis客户端
redis-cli

2. Configure the cluster environment through redis

First, find the location of the redis configuration file

  • brew list redis # Check the location of redis installation
  • cd /opt/homebrew/Cellar/redis/6.2.4 # Enter the folder where the version number is located based on the location
  • open . # Open the folder
  • Use Xcode.app to open homebrew.mxcl.redis.plist, that is You can find the location where redis.conf is located, as follows:

Learn more about how to use the redis cluster function in node [detailed configuration]

Learn more about how to use the redis cluster function in node [detailed configuration]

Create Six service configuration files

cd /opt/homebrew/etc/(the configuration file directory found in the previous step)

# 需要在 /opt/homebrew/etc/ 路径下
mkdir -p redis/cluster/7000
mkdir -p redis/cluster/7001
mkdir -p redis/cluster/7002
mkdir -p redis/cluster/7003
mkdir -p redis/cluster/7004
mkdir -p redis/cluster/7005

Modify the configuration file

/opt/homebrew/etc/redis.confThe configuration file under the path does not need to be modified, just copy it to the redis/cluster/7000 created above directory, and then modify it, the steps are as follows

  • Copy a configuration file first and modify it
cd /opt/homebrew/etc/ # 进入配置文件目录
cp redis.conf redis/cluster/7000/7000.conf
code redis/cluster/7000/7000.conf # 用编辑器打开或者用vim打开配置文件来进行修改
  • Enter 7000.confAfter that, modify the following properties
# Redis端口号(7000-7005每个配置文件都要修改)
port 7000  

# 开启集群模式运行
cluster-enabled yes   

# 集群内部配置文件配置文件路径,默认nodes-6379.conf(7000-7005每个配置文件都要修改)
cluster-config-file nodes-7000.conf 

# 节点间通信的超时时间
cluster-node-timeout 5000  

# 数据持久化
appendonly yes
  • Copy 7000.conf to the directory of each redis service
cd /opt/homebrew/etc/redis/cluster # 进入配置文件目录

cp 7000/7000.conf 7001/7001.conf
cp 7000/7000.conf 7002/7002.conf
cp 7000/7000.conf 7003/7003.conf
cp 7000/7000.conf 7004/7004.conf
cp 7000/7000.conf 7005/7005.conf
  • then modify it7001.conf-7005.confThe port and cluster-config-file attributes of each configuration file

Note:Each configuration file must be configured differently port and cluster-config-file values ​​(otherwise the cluster will not take effect), the above are distinguished by ports.

The directory of the configuration file can be found through the find /opt/homebrew -name nodes-7000.conf command


3. Start and stop cluster services

Since we have configured 6 services, it is impossible to start or stop them one by one. We need to use shell scripts to achieve this

进入/opt/homebrew/etc/redis/cluster目录,创建start.sh和stop.sh文件

# start.sh 文件
#!/bin/sh
redis-server /opt/homebrew/etc/redis/cluster/7000/7000.conf &
redis-server /opt/homebrew/etc/redis/cluster/7001/7001.conf &
redis-server /opt/homebrew/etc/redis/cluster/7002/7002.conf &
redis-server /opt/homebrew/etc/redis/cluster/7003/7003.conf &
redis-server /opt/homebrew/etc/redis/cluster/7004/7004.conf &
redis-server /opt/homebrew/etc/redis/cluster/7005/7005.conf &

# stop.sh 文件
#!/bin/sh
redis-cli -p 7000 shutdown &
redis-cli -p 7001 shutdown &
redis-cli -p 7002 shutdown &
redis-cli -p 7003 shutdown &
redis-cli -p 7004 shutdown &
redis-cli -p 7005 shutdown &

执行./start.sh或者./stop.sh来启停服务

执行ps -ef |grep redis来查看已启动的redis服务

注意: 第一次执行./start.sh需要通过sudo chmod +x start.sh授权执行权限

4. 相关命令

redis-cli -p 7000 # 单个客户端启动
redis-server 7000/7000.conf  # 启动单个服务端
redis-cli -p 7000 shutdown # 关闭服务端
sudo chmod +x start.sh # 开启脚本执行权限

# 设置redis主从关系(三主三从)
redis-cli --cluster create  --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

cluster nodes #查看集群节点情况(进入某个客户端执行)
cluster info #查看集群信息(进入某个客户端执行)

查看所有key值:keys *

删除指定索引的值:del key

清空整个 Redis 服务器的数据:flushall 

清空当前库中的所有 key:flushdb

客户端使用 ioredis 框架接入集群

Redis.Cluster提供了在多个Redis节点上自动分片的功能,使用前面搭建好的六个redis服务器,然后在本地启动node redis.js,就可以测试集群的效果了。ioredis

// redis.js
const Redis = require("ioredis");

const cluster = new Redis.Cluster([
  {
    port: 7000,
    host: "127.0.0.1",
  },
  {
    port: 7001,
    host: "127.0.0.1",
  },
]);

cluster.set("foo", "bar");
cluster.get("foo", (err, res) => {
  // res === 'bar'
});

使用bull框架(redis 队列)

import Queue from 'bull'
// 创建redis队列实例
const instance = new Queue('custom', {
  prefix : '{myprefix}',
  createClient(type) {
    // cluster 集群实例同上
    return cluster
  }
})

// 添加数据到redis队列(生产者)
instance.add(
  'request', 
  { 
    ...params
  },
  {
    removeOnComplete: false
  }
).catch(e => {
  console.error(e)
})

// 消费者回调
instance.process('request', 5, async (job, done) => {
  console.log('获取当前消费的数据:', job.data)
  // 执行异步操作
  await new Promise((resolve)=>resolve())
  done()
})

使用bull框架连接ioredis集群时存在问题: 每次有数据pushredis队列时对应的回调函数可能会触发多次,目前无法确定是使用的问题还是框架本身的问题(如果有了解的欢迎大家留言告知)。

替代集群的方案:在不需要数据同步和数据迁移的情况下,可以在客户端使用多个redis实例,结合Math.random()使数据平分到其中的一个redis,从而解决了单个实例硬件(cpu等)瓶颈的问题。

问题处理

1、Mac系统下连接redis报错?

控制台错误提示:Could not connect to Redis at 127.0.0.1:6379: Connection refused

原因:服务端没有开启或启动失败

解决办法:需要先启动redis服务端redis-server 

参考链接

https://blog.csdn.net/qq_23347459/article/details/104257529

2、客户端启动、读写报错?

错误提示:ClusterAllFailedError: Failed to refresh slots cache.

原因:每个服务下的配置文件中的cluster-config-file属性一致。

处理:修改成唯一的属性值 

  • 参考链接1 

    https://stackoverflow.com/questions/57350961/ioredis-clusterallfailederror-failed-to-refresh-slots-cache

  • 参考2

    https://github.com/luin/ioredis/issues/711

3、执行创建主从redis语句失败?

执行语句:redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

提示错误:[ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

原因:执行创建语句时,没有清空数据和重置集群

处理:清空数据和重置集群, 清除rdb和aof文件

参考清除redis数据

https://stackoverflow.com/questions/37206993/redis-server-cluster-not-working

# 以7000端口的服务为例,7001-7005重复以下操作
$redis-cli -p 7000
127.0.0.1:7000> flushall
127.0.0.1:7000> cluster reset
127.0.0.1:7000> exit

# 使用find找到rdb和aof文件(也在rdb目录下)
find /opt/homebrew -name dump.rdb

# 重新执行创建语句成功
redis-cli --cluster create  --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

结语

redis集群在客户端的使用是非常简单的,相比之下服务端的配置会比较繁琐。

客户端的具体使用只做了一下简单的说明,使用过程中要注意redis数据的同步和迁移等问题。

使用集群虽然能提升服务能力、支持主从复制、哨兵模式、读写分离、平分服务器的压力等特点。但不具备自动容错和恢复功能,如果出现宕机会使部分读写请求失败,降低了系统的可用性。在使用时根据业务情况分析、选择不同的方案。

本文转载自:https://juejin.cn/post/7079928963107127327

作者:tager

更多node相关知识,请访问:nodejs 教程

The above is the detailed content of Learn more about how to use the redis cluster function in node [detailed configuration]. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.