当前位置:网站首页>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 timeout
Execution 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
边栏推荐
- STM32 full series development firmware installation guide under Arduino framework
- NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
- 返回一个零长度的数组或者空的集合,不要返回null
- 架构实战营模块八作业
- 【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
- 【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
ojdbc8 "Recommended Collection"- iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
- Batch (batch size, full batch, mini batch, online learning), iterations and epochs in deep learning
- ResNet的基础:残差块的原理
猜你喜欢
Daily practice——Randomly generate an integer between 1-100 and see how many times you can guess.Requirements: The number of guesses cannot exceed 7 times, and after each guess, it will prompt "bigger"
ThreadLocal
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
ReentrantLock原理(未完待续)
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
21. Support Vector Machine - Introduction to Kernel Functions
Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
Embedded development has no passion, is it normal?
STM32 full series development firmware installation guide under Arduino framework
高效并发:Synchornized的锁优化详解
随机推荐
Returns a zero-length array or empty collection, do not return null
关注!海泰方圆加入《个人信息保护自律公约》
统计UTF-8字符串中的字符函数
leetcode 665. Non-decreasing Array
grep命令 笔试题
linux view redis version command (linux view mysql version number)
第六章
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
Talking about the algorithm security of network security
STM32 full series development firmware installation guide under Arduino framework
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
Count characters in UTF-8 string function
1161. 最大层内元素和 : 层序遍历运用题
角色妆容的实现
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
请问我的这段sql中sql语法哪里出了错
Three.js入门
ThreadLocal
BM3 将链表中的节点每k个一组翻转