当前位置:网站首页>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
边栏推荐
- 利用反射实现一个管理对象信息的简单框架
- Embedded development has no passion, is it normal?
- 性能优化:记一次树的搜索接口优化思路
- c语言解析json字符串(json对象转化为字符串)
- How to get useragent
- 嵌入式开发没有激情了,正常吗?
- 手把手教你学会部署Nestjs项目
- [Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
- Implementing a Simple Framework for Managing Object Information Using Reflection
- leetcode: 6135. The longest ring in the graph [inward base ring tree + longest ring board + timestamp]
猜你喜欢

How programmers learn open source projects, this article tells you

Implementing a Simple Framework for Managing Object Information Using Reflection

Realize serial port receiving data based on STM32 ring queue

The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao

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"

21. Support Vector Machine - Introduction to Kernel Functions

使用 Flutter 和 Firebase 制作!计数器应用程序

AI 自动写代码插件 Copilot(副驾驶员)

MATLAB program design and application 2.4 Common internal functions of MATLAB

Shell script quick start to actual combat -02
随机推荐
给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
MATLAB program design and application 2.4 Common internal functions of MATLAB
Bika LIMS open source LIMS set - use of SENAITE (detection process)
uni-app中的renderjs使用
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
Douyin fetches video list based on keywords API
深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
idea中搜索具体的字符内容的快捷方式
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
【AcWing】The 62nd Weekly Match 【2022.07.30】
Thymeleaf是什么?该如何使用。
One thing to say, is outsourcing company worth it?
Realize serial port receiving data based on STM32 ring queue
Getting Started with Tkinter
multithreaded lock
请问我的这段sql中sql语法哪里出了错
Write a database document management tool based on WPF repeating the wheel (1)
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
Made with Flutter and Firebase!counter application