当前位置:网站首页>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
边栏推荐
- Tkinter 入门之旅
- matplotlib ax bar color Set the color, transparency, label legend of the ax bar
- BM3 flips the nodes in the linked list in groups of k
- renderjs usage in uni-app
- 高效并发:Synchornized的锁优化详解
- Three.js入门
- The principle of ReentrantLock (to be continued)
- Basic Grammar Introduction of Carbon Tutorial (Tutorial)
- A few permanent free network transmission, convenient and simple (Intranet through tutorials)
- 财务盈利、偿债能力指标
猜你喜欢
Three. Introduction to js
Teach you how to deploy Nestjs projects
利用反射实现一个管理对象信息的简单框架
Shell script quick start to actual combat -02
useragent online lookup
ReentrantLock原理(未完待续)
Getting Started with Tkinter
NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores
顺序表的实现
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
随机推荐
Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query
"The core concept of" image classification and target detection in the positive and negative samples and understanding architecture
Architect 04 - Application Service Encryption Design and Practice
Several methods of mysql backup table
A solution to the server encountered an internal error that prevented it from fulfilling this request [easy to understand]
grep命令 笔试题
<artifactId>ojdbc8</artifactId>「建议收藏」
rj45对接头千兆(百兆以太网接口定义)
给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
npm 更改为淘宝镜像的方法[通俗易懂]
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
【愚公系列】2022年07月 Go教学课程 025-递归函数
Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
第六章
grep command written test questions
spark reports an error OutOfMemory "recommended collection"
Three.js入门
Efficient Concurrency: A Detailed Explanation of Synchornized's Lock Optimization