范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

springredis上百万的QPS压力太大连接失败,我TM人傻了

  大家好,我们最近业务量暴涨,导致我最近一直 TM 人傻了。前几天晚上,发现由于业务压力激增,某个核心微服务新扩容起来的几个实例,在不同程度上,出现了  Redis 连接失败 的异常: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to redis.production.com 	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1553) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1461) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.doGetAsyncDedicatedConnection(LettuceConnection.java:1027) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.getOrCreateDedicatedConnection(LettuceConnection.java:1013) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.openPipeline(LettuceConnection.java:527) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.DefaultStringRedisConnection.openPipeline(DefaultStringRedisConnection.java:3245) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at jdk.internal.reflect.GeneratedMethodAccessor319.invoke(Unknown Source) ~[?:?] 	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] 	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] 	at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:61) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at com.sun.proxy.$Proxy355.openPipeline(Unknown Source) ~[?:?] 	at org.springframework.data.redis.core.RedisTemplate.lambda$executePipelined$1(RedisTemplate.java:318) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:222) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:189) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:176) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.executePipelined(RedisTemplate.java:317) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.executePipelined(RedisTemplate.java:307) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate$FastClassBySpringCGLIB$81812bd6.invoke() ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	//省略一些堆栈 Caused by: org.springframework.dao.QueryTimeoutException: Redis command timed out 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.closePipeline(LettuceConnection.java:592) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	... 142 more
  同时,也有业务调用  Redis 命令超时 的异常: org.springframework.data.redis.connection.RedisPipelineException: Pipeline contained one or more invalid commands; nested exception is org.springframework.data.redis.connection.RedisPipelineException: Pipeline contained one or more invalid commands; nested exception is org.springframework.dao.QueryTimeoutException: Redis command timed out 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.closePipeline(LettuceConnection.java:594) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.connection.DefaultStringRedisConnection.closePipeline(DefaultStringRedisConnection.java:3224) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at jdk.internal.reflect.GeneratedMethodAccessor198.invoke(Unknown Source) ~[?:?] 	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] 	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] 	at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:61) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at com.sun.proxy.$Proxy355.closePipeline(Unknown Source) ~[?:?] 	at org.springframework.data.redis.core.RedisTemplate.lambda$executePipelined$1(RedisTemplate.java:326) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:222) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:189) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:176) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.executePipelined(RedisTemplate.java:317) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate.executePipelined(RedisTemplate.java:307) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.data.redis.core.RedisTemplate$FastClassBySpringCGLIB$81812bd6.invoke() ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.7.jar!/:5.3.7] 	at org.springframework.data.redis.core.StringRedisTemplate$EnhancerBySpringCGLIB$c9b8cc15.executePipelined() ~[spring-data-redis-2.4.9.jar!/:2.4.9] //省略一部分堆栈 Caused by: org.springframework.data.redis.connection.RedisPipelineException: Pipeline contained one or more invalid commands; nested exception is org.springframework.dao.QueryTimeoutException: Redis command timed out 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.closePipeline(LettuceConnection.java:592) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	... 142 more Caused by: org.springframework.dao.QueryTimeoutException: Redis command timed out 	at org.springframework.data.redis.connection.lettuce.LettuceConnection.closePipeline(LettuceConnection.java:592) ~[spring-data-redis-2.4.9.jar!/:2.4.9] 	... 142 more
  我们的 spring-data-redis 的配置是: spring:   redis:     host: redis.production.com     port: 6379     # 命令超时     timeout: 3000     lettuce:       pool:         max-active: 128         max-idle: 128         max-wait: 3000
  这些请求虽然在第一次请求发送到的实例失败了,但是我们有重试的机制,请求最后还是成功了。但是比正常的请求多了 3s,这部分请求占了所有请求的 3% 左右。
  从异常堆栈上面可以看出, 异常的根源都是 redis 命令超时 ,但是为何建立 Redis 连接的时候,也会有 Redis 命令执行呢? lettuce 建立连接的流程
  我们的 Redis 访问,使用的是 spring-data-redis + Lettuce 连接池。默认情况下,Lettuce 中的 Redis 连接建立的流程是: 建立 TCP 连接 进行必要的握手: 针对 Redis 2.x ~ 5.x 的版本:
  1. 如果需要用户名密码,则发送用户名密码信息
  2. 如果开启了连接使用前心跳,则发送 PING 针对 Redis 6.x 的版本:6.x 之后引入了新命令 HELLO,使用这个命令来统一初始化 Redis 连接:REDIS HELLO,这个命令参数中可以带用户名与密码,完成验证。
  针对 Redis 2.x ~ 5.x 的版本,我们可以配置是否在启用连接前发送 PING 心跳,默认为 是 :
  ClientOptions public static final boolean DEFAULT_PING_BEFORE_ACTIVATE_CONNECTION = true;
  我们使用的 Redis 版本是最新的 6.x,所以在建立连接,握手的阶段,一定需要发送一个 HELLO 命令,并等待响应成功才算连接创建成功。
  那么为何这个简单的命令也会超时呢? 通过 JFR 查看 Redis 命令压力
  我们的项目中 redis 操作是通过 spring-data-redis + Lettuce 连接池,启用并且增加了关于 Lettuce 命令的 JFR 监控,可以参考我的这篇文章:这个 Redis 连接池的新监控方式针不戳~我再加一点佐料,截至目前我的 pull request 已经合并,这个特性会在 6.2.x 版本发布。我们看下出问题时间附近的 Redis 命令采集,如下图所示:
  可以看出,这时候 Redis 压力还是比较大的(图中的 firstResponsePercentiles 的单位是微秒)。我们这个时候,有 7 个实例,这个实例时刚启动的,压力相对于其他实例还比较小,就已经出现了连接命令超时。而且我们这里只截取了 HGET 命令,还有 GET 命令执行的次数和 HGET 是同一量级的,然后剩下其他的命令加起来相当于 HGET 的一半。这时候从客户端看,发往 Redis 的命令的 QPS 已经超过了百万。
  从 Redis 的监控来看,压力确实有一些,可能会造成某些命令等待过长时间导致超时异常。 优化思路思考
  我们先明确一点,针对 spring-data-redis + lettuce,如果我们没有使用需要独占连接的命令(包括 Redis 事务以及 Redis Pipeline),那么 我们不需要连接池 ,因为 lettuce 是异步响应式的,对于可以使用共享连接的请求,都会使用同一个实际的 redis 连接进行请求,不需要连接池。但是这个微服务中,使用了大量的 pipeline 命令来提高查询效率。如果我们不使用连接池,那么会导致频繁的连接关闭与创建(每秒几十万个),这样会严重降低效率。 虽然官网说,lettuce 不需要连接池,但是这是在你没有使用事务以及 Pipeline 的情况下 。
  首先, Redis 扩容 :我们的 Redis 部署在公有云上,如果扩容也就是提高机器配置,下一个更高的配置指标相对于当前多了一倍,成本也是高了差不多一倍。目前只有在瞬时压力的时候,会出现少于 3% 的请求失败并重试下一实例,最后还是成功,针对这个对 Redis 进行扩容, 从成本考虑并不值得 。
  然后,对于压力过大的应用,我们是有动态扩容机制存在的。对于失败的请求,我们也是有重试的。但是这个问题给我们带来的影响是: 由于瞬时压力到来,新启动的实例可能一开始就会有大量请求到来,导致接口请求和建立连接之后的心跳请求混合。并且由于这些请求并没有公平队列排序,某些心跳请求响应过慢从而导致失败,重新建立连接依然可能失败。 有些实例可能建立的连接比较少,不能满足并发度需求。导致很多请求其实阻塞在等待连接的过程,从而使 CPU 压力没有一下子变很大,所以没有继续触发扩容。这样对于扩容带来了更大的滞后性。
  其实,如果我们有办法尽量减少或者避免连接创建失败,那么就能很大程度优化这个问题。即在微服务实例开始提供服务前,就将连接池中所有的连接创建好。 如何实现 Redis 连接池连接预创建
  我们首先看看,是否可以借助于官方配置,实现这个连接池。
  我们查看官方文档,发现了这样两个配置:
  min-idle 即连接池中最少的连接数。 time-between-eviction-runs 是定时任务,检查连接池中的连接是否满足至少有 min-idle 的个数,同时,不超过 max-idle 那么多个数。官方文档中说,min-idle 只有配合 time-between-eviction-runs 都配置,才会生效。究其原因是:lettuce 的链接池是基于 commons-pool 实现的。连接池可以配置 min-idle,但是需要手动调用 preparePool,才会创建至少 min-idle 个数的对象:
  GenericObjectPool public void preparePool() throws Exception {     //如果配置了有效的 min-idle,则调用 ensureMinIdle 保证创建至少 min-idle 个数的对象     if (this.getMinIdle() >= 1) {         this.ensureMinIdle();     } }
  那么这个是在什么时候调用呢? commons-pool 有定时任务,初始延迟和定时间隔都是 time-between-eviction-runs,配置的,其内容是: public void run() {     final ClassLoader savedClassLoader =             Thread.currentThread().getContextClassLoader();     try {         if (factoryClassLoader != null) {             // Set the class loader for the factory             final ClassLoader cl = factoryClassLoader.get();             if (cl == null) {                 // The pool has been dereferenced and the class loader                 // GC"d. Cancel this timer so the pool can be GC"d as                 // well.                 cancel();                 return;             }             Thread.currentThread().setContextClassLoader(cl);         }          // Evict from the pool         try {             evict();         } catch(final Exception e) {             swallowException(e);         } catch(final OutOfMemoryError oome) {             // Log problem but give evictor thread a chance to continue             // in case error is recoverable             oome.printStackTrace(System.err);         }         // Re-create idle instances.         try {             ensureMinIdle();         } catch (final Exception e) {             swallowException(e);         }     } finally {         // Restore the previous CCL         Thread.currentThread().setContextClassLoader(savedClassLoader);     } }
  可以看出,这个定时任务执行主要保证当前池内空闲对象个数不超过 max-idle,同时至少有 min-idle 个链接。这些都是 common-pools 自己带的机制。但是没有我们需要的,在连接池一创建就去初始化所有链接。
  这就需要我们自己实现了,我们首先配置 min-idle = max-idle = max-active,这样无论何时连接池中都有同样最大个数的链接。之后,我们在连接池创建出来的地方,修改源码,强制调用 preparePool 去初始化所有链接,即:
  ConnectionPoolSupport // lettuce 初始化创建连接池的时候,会调用这个方法 public static > GenericObjectPool createGenericObjectPool(             Supplier connectionSupplier, GenericObjectPoolConfig config, boolean wrapConnections) {     //省略其他代码      GenericObjectPool pool = new GenericObjectPool(new RedisPooledObjectFactory(connectionSupplier), config) {          @Override         public T borrowObject() throws Exception {             return wrapConnections ? ConnectionWrapping.wrapConnection(super.borrowObject(), poolRef.get())                     : super.borrowObject();         }          @Override         public void returnObject(T obj) {              if (wrapConnections && obj instanceof HasTargetConnection) {                 super.returnObject((T) ((HasTargetConnection) obj).getTargetConnection());                 return;             }             super.returnObject(obj);         }      };     //创建好后,调用 preparePool     try {         pool.preparePool();     } catch (Exception e) {         throw new RedisConnectionException("prepare connection pool failed",e);     }     //省略其他代码 }
  这样,我们就可以实现初始化 Redis 的时候,在微服务真正提供服务之前,初始化所有 Redis 链接。由于这里涉及源码修改,大家目前可以通过在项目中添加同名同路径的类,进行依赖库源码替换。针对这个优化,我也向 lettuce 提了 issue 以及对应的 pull request:

助听器日常使用要注意什么?佩戴助听器时间每天佩戴时间由短到长佩戴环境由安静环境到嘈杂环境,慢慢适应之后,就可以全天佩戴了。防潮气潮气是电子设备的大敌,助听器也不例外。潮气的侵袭会损坏定制机内的芯片麦克风和受和邻居的听力一样,可以将他的助听器直接拿过来戴上吗?你好,你和邻居的听力一样,这只代表了你们几个频率点的平均值一样,也许测得的你们每个频率点的具体数值是不一样的。助听器是根据每个频率点的听损进行补偿的,补偿得越精确,佩戴的效果会更好阿里巴巴股价大跌的原因到底是什么?随着时间的推移,阿里巴巴的真实面貌慢慢清晰,电商平台只是一个改变营销模式,却为平台资本赚回了数以万亿的财富资本工具,疯狂掠夺实体经济体和数亿国人的社会价值,而阿里巴巴本身没有社会价手机手持云台各式各样,到底选择哪种?大疆的是不是比较好?很荣幸被邀请回答。手机拍摄视频,很有必要配一个手持云台。手持云台画面稳定,画面稳定的视频看上去才舒服我去年夏天买的是智云智云smooth4,当时的价格是799元这款云台买的现在感觉曾让苹果三星感到威胁的华为,突然牛不起来了,华为还撑得住吗?华为近两年营收利润一直在增长,而且是在大量研发投入的情况下。人家只是手机,消费类电子业务受影响。后期发展起来的各行业企业整体物联解决方案和之前电信业务依然很猛,而且领先。再假以时日为什么有的人一参加饭局,就把手机放在桌子上?手机,手机!朋友喝茶同学聚会战友相逢同事饭局,团团围坐,欢声笑语,热闹非凡。总有帅哥美女,喜欢把自己的手机放在桌上。(前几年一种带有烂苹果标志白边某国手机,受到一部分人的喜欢)接听手机套餐中的话术陷阱不该成为用户之痛如果你想更换手机流量套餐,花了十几元办了个免流卡,却发现每月流量费用不降反升如果你被运营商电话告知可以优惠升级手机套餐,却发现手机账单里突然多了一笔新增费用近年来,随着移动通信业的联想陷入了舆论风波,该如何收场?柳传志和杨元庆确实应该回答怎么样以空手套白狼的手段掘取国有资产的,然后国家有关部门介入调查,该抓的抓该查的查,他们并不代表民族企业,更不是民族英雄,国家应有壮士断腕的决心,不姑息不联想真的不香了吗?今儿刷道满屏的关于联想的负面报道,作为一名终端经销商,心里有种说不出的酸酸的滋味。一路走来,联想的客户可以说在内地还是占在前面的。在电脑不紧气的情况下,联想一直坚持走到现在。那为啥暴风眼中的联想出局ampampamp重生作者儒商研习社创始人梁张义曾几何时让人倍觉荣光的联想和备受尊重的老掌舵人柳传志成了众矢之的,被扣上了不止一顶耻辱的帽子,不由得让人浮想联翩,唏嘘不已。热议事件主要发起人司马南,也是马化腾为什么你们都放弃了QQ等级?众所周知,在微信火之前,QQ才是人民普遍用的互联网社交工具。虽然QQ现在已经衰落,但是对于大多数人来说,QQ依然乘载了我们的青春。QQ当年也很火爆,其中的每一项功能都堪称经典,尤其