


Learn 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!
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
1 2 3 4 5 6 |
|
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 locationopen .
# Open the folder- Use
Xcode.app
to openhomebrew.mxcl.redis.plist
, that is You can find the location whereredis.conf
is located, as follows:
Create Six service configuration files
cd /opt/homebrew/etc/
(the configuration file directory found in the previous step)
1 2 3 4 5 6 7 |
|
Modify the configuration file
/opt/homebrew/etc/redis.conf
The 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
1 2 3 |
|
- Enter
7000.conf
After that, modify the following properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- Copy 7000.conf to the directory of each redis service
1 2 3 4 5 6 7 |
|
- then modify it
7001.conf-7005.conf
The 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文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
执行./start.sh
或者./stop.sh
来启停服务
执行ps -ef |grep redis
来查看已启动的redis服务
注意: 第一次执行./start.sh需要通过sudo chmod +x start.sh
授权执行权限
4. 相关命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
客户端使用 ioredis 框架接入集群
Redis.Cluster提供了在多个Redis节点上自动分片的功能,使用前面搭建好的六个redis服务器,然后在本地启动node redis.js
,就可以测试集群的效果了。ioredis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
使用bull框架(redis 队列)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
使用bull
框架连接ioredis
集群时存在问题: 每次有数据push
到redis
队列时对应的回调函数可能会触发多次,目前无法确定是使用的问题还是框架本身的问题(如果有了解的欢迎大家留言告知)。
替代集群的方案:在不需要数据同步和数据迁移的情况下,可以在客户端使用多个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
1 2 3 4 5 6 7 8 9 10 11 |
|
结语
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

How to delete node with nvm: 1. Download "nvm-setup.zip" and install it on the C drive; 2. Configure environment variables and check the version number through the "nvm -v" command; 3. Use the "nvm install" command Install node; 4. Delete the installed node through the "nvm uninstall" command.

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

npm node gyp fails because "node-gyp.js" does not match the version of "Node.js". The solution is: 1. Clear the node cache through "npm cache clean -f"; 2. Through "npm install -g n" Install the n module; 3. Install the "node v12.21.0" version through the "n v12.21.0" command.

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

What is a single sign-on system? How to implement it using nodejs? The following article will introduce to you how to use node to implement a single sign-on system. I hope it will be helpful to you!
