[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.我们一般连接redis都需要用到连接池,最常用的就是jedis连接池,连接池中有两个参数的设置对高性能有较大影响:maxIdle和maxTotal
2.maxIdle的意思是连接池中空闲连接的最大数量,maxTotal是连接池中总连接的最大数量
3.之前我一般设置这两者的时候是没有设置成相等的值的,也就是比如设置maxIdle=10,然后maxTotal=30这样,但是基础架构的压测报告发现在高并发的情况下这样设置的后果竟然会产生大量的短连接,这样的结果令人非常意外,这些这么多的短链接是怎么产生的?
4.回答3的问题:还是以maxIdle=10,maxTotal=30作为例子,假设时刻1,30条连接全部使用来进行redis操作,时刻2,有20条连接释放,那么将会有10条连接放回连接池中,另外的10条连接将会被close掉,成为短连接,此时其他线程再来获取比如20个连接的时候,将会需要再额外创建10条连接。这就是短连接的主要产生场景;至于创建的短连接的数量取决于cpu的调度,简单归结原因是:
连接放回连接池的速度要比等待线程从线程池中获取连接要快,这样每次释放连接的时候都有部分连接超过maxIdle数量而被物理close掉成为短链接。至于为何释放连接的速度要比获取连接的速度快,留个悬念