当前位置:网站首页>In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
2022-07-31 21:32:00 【m0_67401228】
Foreword
As for how to call the cluster mode of go-redis, I won't say much.You can refer to my other article: Go language study notes - redis cluster cluster mode | Web framework Gin (thirteen) _'s blog-CSDN blog
When I tested the redis call in cluster mode, it was not as efficient as single node mode.
Let's first reproduce how the problem occurs.
Recurrence
First initialize and build a cluster cluster connection.
package connectimport ("context""fmt""github.com/go-redis/redis/v8""time")var Cluster *redis.ClusterClientfunc init() {Cluster = redis.NewClusterClient(&redis.ClusterOptions{Addrs: []string{"xxxxx:7379","xxxxx:7380",},Password: "123456",//DialTimeout: 100 * time.Microsecond,ReadTimeout: 100 * time. Microsecond,//WriteTimeout: 100 * time.Microsecond,DialTimeout: 5 * time.Second, //Connection establishment timeout, the default is 5 seconds.//ReadTimeout: 3 * time.Second, //Read timeout, the default is 3 seconds, -1 means cancel read timeoutWriteTimeout: 3 * time.Second, //write timeout, default equal to read timeoutOnConnect: func(ctx context.Context, conn *redis.Conn) error {fmt.Printf("Create a new connection: %v", conn)return nil},})ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)defer cancel()Cluster.Ping(ctx).Result()}Write a simple demo for testing the use of redis.
package demoimport ("RedisCluster/connect""context""fmt""time")const Key = "test-111"func SampleDemo() {// Write data for the first time and set a 10-minute cacheconnect.Cluster.Set(context.TODO(), Key, "666", 10*time.Minute)cmd := connect.Cluster.Get(context.TODO(), Key)result, _ := cmd.Result()fmt.Println("result:", result)// Write data for the second time and set a 10-minute cachestart := time.Now().UnixMilli()connect.Cluster.Set(context.TODO(), Key, "777", 10*time.Minute)end := time.Now().UnixMilli()fmt.Println("set ->", (end - start))cmd = connect.Cluster.Get(context.TODO(), Key)end1 := time.Now().UnixMilli()fmt.Println("get ->", (end1 - end))result, _ = cmd.Result()fmt.Println("result:", result)}Code Description
1. The Cluster will be initialized when it is started. We first access it once, and then access it for the second time.Mainly look at the time-consuming situation of the second time.
2. A hook function is built when the Cluster is created. If a new connection is created, a sentence will be printed.
Execution results

You can see that in the second execution, multiple connections were created, which directly led to the time to set the key to 150ms.
Problem resolution
I searched the Internet for a long time, but I couldn't find any similar questions, so I started to try to read the reference.
Finally, after adjusting the ReadTimeout to 3 seconds, it is normal.
The code is adjusted as follows:
//DialTimeout: 100 * time.Microsecond,ReadTimeout: 3000 * time.Microsecond,//WriteTimeout: 100 * time.Microsecond,DialTimeout: 5 * time.Second, //Connection establishment timeout, the default is 5 seconds.//ReadTimeout: 3 * time.Second, //Read timeout, the default is 3 seconds, -1 means cancel read timeoutWriteTimeout: 3 * time.Second, //write timeout, default equal to read timeoutExecution results

Summary
In the future, I will have time to look at the specific reasons.

Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
- 高效并发:Synchornized的锁优化详解
- multithreaded lock
- leetcode 665. Non-decreasing Array
- "The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
- Architecture Battalion Module 8 Homework
- Introduction to Audio Types and Encoding Formats in Unity
- leetcode 665. Non-decreasing Array
- spark reports an error OutOfMemory "recommended collection"
- Several methods of mysql backup table
猜你喜欢

Arduino框架下STM32全系列开发固件安装指南

Realization of character makeup
![[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs](/img/91/63d4f7869e0a55d19701c5ca5c9ed8.png)
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs

Realize serial port receiving data based on STM32 ring queue

嵌入式开发没有激情了,正常吗?

Three.js入门

使用 Flutter 和 Firebase 制作!计数器应用程序
![[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array](/img/4d/038e6cd6ecad19934122cff89f4d76.png)
[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array

广汽本田安全体验营:“危险”是最好的老师

STM32 full series development firmware installation guide under Arduino framework
随机推荐
求n以内的素数
Getting Started with Tkinter
OSPFv3的基本配置
MATLAB program design and application 2.4 Common internal functions of MATLAB
How programmers learn open source projects, this article tells you
Socket Review and I/0 Model
Commonly used security penetration testing tools (penetration testing tools)
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
利用反射实现一个管理对象信息的简单框架
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
linux view redis version command (linux view mysql version number)
Three. Introduction to js
How to identify fake reptiles?
20. Support vector machine - knowledge of mathematical principles
leetcode 665. Non-decreasing Array 非递减数列(中等)
Linux environment redis cluster to build "recommended collection"
-xms -xmx(information value)
npm 更改为淘宝镜像的方法[通俗易懂]
c语言解析json字符串(json对象转化为字符串)
Pytorch lstm time series prediction problem stepping on the pit "recommended collection"