当前位置:网站首页>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
边栏推荐
- Basic Grammar Introduction of Carbon Tutorial (Tutorial)
- focus on!Haitai Fangyuan joins the "Personal Information Protection Self-discipline Convention"
- ThreadLocal
- A few permanent free network transmission, convenient and simple (Intranet through tutorials)
- BOW/DOM (top)
- The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
- Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
- -xms -xmx(information value)
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- Three. Introduction to js
猜你喜欢
[PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
[Intensive reading of the paper] iNeRF
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
MATLAB program design and application 2.4 Common internal functions of MATLAB
高效并发:Synchornized的锁优化详解
顺序表的实现
GateWay implements load balancing
Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
手把手教你学会部署Nestjs项目
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
随机推荐
Teach you how to deploy Nestjs projects
Introduction to Audio Types and Encoding Formats in Unity
cas and spin locks (is lightweight locks spin locks)
GAC Honda Safety Experience Camp: "Danger" is the best teacher
idea中搜索具体的字符内容的快捷方式
老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
Bika LIMS open source LIMS set - use of SENAITE (detection process)
Basic configuration of OSPFv3
【Yugong Series】July 2022 Go Teaching Course 023-List of Go Containers
求n以内的素数
How to identify fake reptiles?
sqlite3 simple operation
【愚公系列】2022年07月 Go教学课程 025-递归函数
GateWay implements load balancing
Apache EventMesh distributed event-driven multi-runtime
The principle of ReentrantLock (to be continued)
Tkinter 入门之旅
全网一触即发,自媒体人的内容分发全能助手——融媒宝
Architect 04 - Application Service Encryption Design and Practice
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解