问题描述

在正常使用Azure Redis的服务中,突然发现Redis 的CPU达到了100%, 正常的使用中发现性能问题严重。从Redis的门户图表中,观察到CPU, Connection,Lentency,Server Load都出现高的情况

CPU

Server Load

Lentency(Redis处理请求的延迟情况)

Connection(客户端连接数情况)

问题分析

根据以上的图表发现,在Connection高的情况下导致了CPU上升并持续到100%, 引起了Server Load比较高(有时达到100%),从而导致在整个Redis的延迟升高。

在Redis创建一个新的连接时,是非常昂贵的高消耗操作。同时在Azure中,每一种定价级别的Redis都有连接数的限制,如果达到限制都会导致性能问题。而根据Reids的官方文档说明,当前连接数就是因为超过了最大上限。

如基本层的连接数限制为(https://www.azure.cn/pricing/details/cache/)

缓存名称 缓存大小 基本 网络性能 客户端连接数量
C0 250 MB ¥0.14片/节点/小时(约¥104.16 /月) 256
C1 1 GB ¥0.35片/节点/小时(约¥260.40 /月) 中等 1000
C2 2.5 GB ¥0.57片/节点/小时(约¥424.08 /月) 中等 2000
C3 6 GB ¥1.14片/节点/小时(约 ¥848.16 /月) 5000
C4 13 GB ¥1.33片/节点/小时(约 ¥989.52 /月) 中等 10000
C5 26 GB ¥2.66片/节点/小时(约 ¥1979.04 /月) 15000
C6 53 GB ¥5.31片/节点/小时(约¥3950.64 /月) 最高 20000

而在处理请求的并发和性能上,可以参考(https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-planning-faq#azure-cache-for-redis-performance):

定价层 大小 CPU 核心数 可用带宽 1 KB 值大小 1 KB 值大小
标准缓存大小     兆位/秒(Mb/秒)/兆字节/秒(MB/秒) 非 SSL 请求数/秒 (RPS) SSL 请求数/秒 (RPS)
C0 250 MB 共享 100/12.5 15,000 7,500
C1 1 GB 1 500/62.5 38,000 20,720
C2 2.5 GB 2 500/62.5 41,000 37,000
C3 6 GB 4 1000/125 100,000 90,000
C4 13 GB 2 500/62.5 60,000 55,000
C5 26 GB 4 1,000 / 125 102,000 93,000
C6 53 GB 8 2,000 / 250 126,000 120,000

解决办法

使用连接池来改善连接问题, 如JedisPool,可以参考GitHub中的说明:https://gist.github.com/JonCole/925630df72be1351b21440625ff2671f#use-jedispool, (如链接不可访问,可以点击[转] Azure Redis Best Practices - Java)参考示例代码:

  1. 9 private static JedisPool pool;
  2. 15 private static JedisPoolConfig config;
  3. 16
  4. 17 // Should be called exactly once during App Startup logic.
  5. 18 public static void initializeSettings(String host, int port, String password, int connectTimeout, int operationTimeout) {
  6. 19 Redis.host = host;
  7. 20 Redis.port = port;
  8. 21 Redis.password = password;
  9. 22 Redis.connectTimeout = connectTimeout;
  10. 23 Redis.operationTimeout = operationTimeout;
  11. 24 }
  12. 25
  13. 26 // MAKE SURE to call the initializeSettings method first
  14. 27 public static JedisPool getPoolInstance() {
  15. 28 if (pool == null) { // avoid synchronization lock if initialization has already happened
  16. 29 synchronized(staticLock) {
  17. 30 if (pool == null) { // don't re-initialize if another thread beat us to it.
  18. 31 JedisPoolConfig poolConfig = getPoolConfig();
  19. 32 boolean useSsl = port == 6380 ? true : false;
  20. 33 int db = 0;
  21. 34 String clientName = "MyClientName"; // null means use default
  22. 35 SSLSocketFactory sslSocketFactory = null; // null means use default
  23. 36 SSLParameters sslParameters = null; // null means use default
  24. 37 HostnameVerifier hostnameVerifier = new SimpleHostNameVerifier(host);
  25. 38 pool = new JedisPool(poolConfig, host, port, connectTimeout,operationTimeout,password, db,
  26. 39 clientName, useSsl, sslSocketFactory, sslParameters, hostnameVerifier);
  27. 40 }
  28. 41 }
  29. 42 }
  30. 43 return pool;
  31. 44 }
  32. 45
  33. 46 public static JedisPoolConfig getPoolConfig() {
  34. 47 if (config == null) {
  35. 48 JedisPoolConfig poolConfig = new JedisPoolConfig();
  36. 49
  37. 50 // Each thread trying to access Redis needs its own Jedis instance from the pool.
  38. 51 // Using too small a value here can lead to performance problems, too big and you have wasted resources.
  39. 52 int maxConnections = 200;
  40. 53 poolConfig.setMaxTotal(maxConnections);
  41. 54 poolConfig.setMaxIdle(maxConnections);
  42. 55
  43. 56 // Using "false" here will make it easier to debug when your maxTotal/minIdle/etc settings need adjusting.
  44. 57 // Setting it to "true" will result better behavior when unexpected load hits in production
  45. 58 poolConfig.setBlockWhenExhausted(true);
  46. 59
  47. 60 // How long to wait before throwing when pool is exhausted
  48. 61 poolConfig.setMaxWaitMillis(operationTimeout);
  49. 62
  50. 63 // This controls the number of connections that should be maintained for bursts of load.
  51. 64 // Increase this value when you see pool.getResource() taking a long time to complete under burst scenarios
  52. 65 poolConfig.setMinIdle(50);
  53. 66
  54. 67 Redis.config = poolConfig;
  55. 68 }
  56. 69
  57. 70 return config;
  58. 71 }

    114 }

如PHP, NodeJS, ASP.Net的代码,都可以在以上的GitHub中找到示例代码。

同时,也非常推荐在代码中使用Lazy模式。

Reconnecting with Lazy<T> pattern

We have seen a few rare cases where StackExchange.Redis fails to reconnect after a connection blip (for example, due to patching). Restarting the client or creating a new ConnectionMultiplexer will fix the issue. Here is some sample code that still uses the recommended Lazy<ConnectionMultiplexer>pattern while allowing apps to force a reconnection periodically. Make sure to update code calling into the ConnectionMultiplexer so that they handle any ObjectDisposedException errors that occur as a result of disposing the old one.

  1. 1 using System;
  2. 2 using System.Threading;
  3. 3 using StackExchange.Redis;
  4. 4
  5. 5 static class Redis
  6. 6 {
  7.  
  8. 21 static string connectionString = "TODO: CALL InitializeConnectionString() method with connection string";
  9. 22 static Lazy<ConnectionMultiplexer> multiplexer = CreateMultiplexer();
  10. 23
  11. 24 public static ConnectionMultiplexer Connection { get { return multiplexer.Value; } }
  12. 25
  13. 93
  14. 94 private static Lazy<ConnectionMultiplexer> CreateMultiplexer()
  15. 95 {
  16. 96 return new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString));
  17. 97 }

参考资料

Redis Best Practiceshttps://gist.github.com/JonCole/925630df72be1351b21440625ff2671f#reuse-connections (如链接不可访问,可以点击[转]Azure Redis Best Practices - PHP)

Azure Redis 缓存性能: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-planning-faq#azure-cache-for-redis-performance

Reuse Connections: https://gist.github.com/JonCole/925630df72be1351b21440625ff2671f#php

Reuse Connections

The most common problem we have seen with PHP clients is that they either don't support persistent connections or the ability to reuse connections is disabled by default. When you don't reuse connections, it means that you have to pay the cost of establishing a new connection, including the SSL/TLS handshake, each time you want to send a request. This can add a lot of latency to your request time and will manifest itself as a performance problem in your application. Additionally, if you have a high request rate, this can cause significant CPU churn on both the Redis client-side and server-side, which can result in other issues.

As an example, the Predis Redis client has a "persistent" connection property that is false by default. Setting the "persistent" property to true will should improve behavior drastically.

【Azure Redis 缓存 Azure Cache For Redis】Redis性能问题,发现Server Load非常的高,导致正常连接/操作不成功的更多相关文章

  1. 【Azure Redis 缓存 Azure Cache For Redis】如何设置让Azure Redis中的RDB文件暂留更久(如7天)

    问题描述 Azure Redis和所有的Redis服务一样,可以让你保留存储在Redis中的数据.以防万一在Redis服务器出现故障的时候能尽可能小的减少数据的损失.在Azure Redis服务中,默 ...

  2. 【Azure Redis 缓存 Azure Cache For Redis】Azure Redis由低级别(C)升级到高级别(P)的步骤和注意事项, 及对用户现有应用的潜在影响,是否需要停机时间窗口,以及这个时间窗口需要多少的预估问题

    问题描述 由于Azure Redis的性能在不同级别表现不同,当需要升级/缩放Redis的时候,从使用者的角度,需要知道有那些步骤? 注意事项? 潜在影响?停机事件窗口? 升级预估时间? 解决方案 从 ...

  3. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  4. 【Azure Redis 缓存 Azure Cache For Redis】在创建高级层Redis(P1)集成虚拟网络(VNET)后,如何测试VNET中资源如何成功访问及配置白名单的效果

    当使用Azure Redis高级版时候,为了能更好的保护Redis的安全,启用了虚拟网路,把Redis集成在Azure中的虚拟网络,只能通过虚拟网络VENT中的资源进行访问,而公网是不可以访问的.但是 ...

  5. 【Azure Redis 缓存 Azure Cache For Redis】使用Redis自带redis-benchmark.exe命令测试Azure Redis的性能

    问题描述 关于Azure Redis的性能问题,在官方文档中,可以查看到不同层级Redis的最大连接数,每秒处理请求的性能. 基本缓存和标准缓存 C0 (250 MB) 缓存 - 最多支持 256 个 ...

  6. 【Azure Redis 缓存 Azure Cache For Redis】Redis出现 java.net.SocketTimeoutException: Read timed out 异常

    问题描述 在使用Azure Redis时,遇见Read Timed out异常, Redis的客户端使用的时jedis.问题发生时,执行redis部分指令出错,大部分get指令,set指令能正常执行. ...

  7. 【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection lost

    问题情形 当在执行Redis一直指令时,有可能会遇见如下几种错误: 1) redis.clients.jedis.exceptions.JedisConnectionException: Could ...

  8. 【Azure Redis 缓存】Windows版创建 Redis Cluster 实验 (精简版)

    简介 学习Redis Cluster的第一步,即本地搭建Redis Cluster.但是在Redis的官方文档中,是介绍在Linux系统中搭建Redis Cluster.本文主要介绍在Windows系 ...

  9. Redis缓存篇(一)Redis是如何工作的

    Redis提供了高性能的数据存取功能,所以广泛应用在缓存场景中,既能有效地提升业务应用的响应速度,还可以避免把高并发压力发送到数据库层. 因为Redis用作缓存的普遍性以及它在业务应用中的重要作用,所 ...

随机推荐

  1. xmake v2.3.7 发布, 新增 tinyc 和 emscripten 工具链支持

    xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...

  2. 教你一招Linux下文本比对方法

    我们在写代码的过程中,免不了会对代码进行一些修修改改.但经常会出现改着改着,就不知道改完后与源文件的差异是怎样的.这里,我们就需要一个文本比对工具来进行文本比对. 有经验的程序员都知道,Windows ...

  3. netty全局分析1

    这个系列都是别人的分析文 https://www.jianshu.com/p/ac7fb5c2640f 一丶 Netty基础入门 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.U ...

  4. FTP服务器稳定性测试

    FTP服务器稳定性探讨,如何部署FTP服务在server2003上,可能广大网友们有其他的选择,我选择的是Filezilla server.毕竟他是开源又免费 在架构师的悉心指导下,对FTP有了个更深 ...

  5. windows服务器中创建账号及管理相关的net命令

    本文测试环境:windows server 2012 R2 Datacenter 实例要求: 1.创建账号,加入到远程桌面组,能实现远程桌面登录 2.指定Full name .及Description ...

  6. Java Web学习(十一)Java过滤器

    一.引言 上一篇文章学习了java三大器的拦截器,拦截器主要是针对于action请求进行拦截处理的,那么对于requst的一些信息如果在调用前,想先进行过滤和处理,那么就要使用到第二个神器,也就是本文 ...

  7. Python安装与环境变量配置 入门详解 - 精简归纳

    Python安装与环境变量配置 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 24 转载请注明出处!️ 目录 Python安装与环境变量配置 入门详解 - 精简归纳 一.下载Py ...

  8. 每日一个知识点系列:volatile的可见性原理

    每日一个知识点系列的目的是针对某一个知识点进行概括性总结,可在一分钟内完成知识点的阅读理解,此处不涉及详细的原理性解读. img 看图说话 关键点1: 总线嗅探器(MESI 缓存一致性原理 ) 关键点 ...

  9. java整合Elasticsearch,实现crud以及高级查询的分页,范围,排序功能,泰文分词器的使用,分组,最大,最小,平均值,以及自动补全功能

    //为index创建mapping,index相当于mysql的数据库,数据库里的表也要给各个字段创建类型,所以index也要给字段事先设置好类型: 使用postMan或者其他工具创建:(此处我使用p ...

  10. PyCharm-缩进 格式化代码

    格式化代码 Ctrl + Alt + l 缩进代码 Tab    向右缩进4格 Shift + Tab 向左缩进4格