可以看到Redis对各种命令进行了压测。
[root@192 redis-6.0.8]# redis-benchmark -h 192.168.5.101 ====== PING_INLINE ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 0.01% <= 0.1 milliseconds 48.89% <= 0.2 milliseconds 86.78% <= 0.3 milliseconds 94.35% <= 0.4 milliseconds 97.30% <= 0.5 milliseconds 98.63% <= 0.6 milliseconds 99.26% <= 0.7 milliseconds 99.58% <= 0.8 milliseconds 99.75% <= 0.9 milliseconds 99.86% <= 1.0 milliseconds 99.94% <= 1.1 milliseconds 99.98% <= 1.2 milliseconds 99.99% <= 1.3 milliseconds 100.00% <= 1.4 milliseconds 100.00% <= 1.4 milliseconds 117647.05 requests per second ====== PING_BULK ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 0.02% <= 0.1 milliseconds 47.76% <= 0.2 milliseconds 84.99% <= 0.3 milliseconds 93.23% <= 0.4 milliseconds 96.76% <= 0.5 milliseconds 98.54% <= 0.6 milliseconds 99.12% <= 0.7 milliseconds 99.47% <= 0.8 milliseconds 99.69% <= 0.9 milliseconds 99.84% <= 1.0 milliseconds 99.93% <= 1.1 milliseconds 99.97% <= 1.2 milliseconds 99.99% <= 1.3 milliseconds 99.99% <= 1.4 milliseconds 100.00% <= 1.5 milliseconds 100.00% <= 1.6 milliseconds 117508.81 requests per second ====== SET ====== 100000 requests completed in 0.84 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 0.00% <= 0.1 milliseconds 53.79% <= 0.2 milliseconds 87.07% <= 0.3 milliseconds 94.75% <= 0.4 milliseconds 97.48% <= 0.5 milliseconds 98.93% <= 0.6 milliseconds 99.44% <= 0.7 milliseconds 99.68% <= 0.8 milliseconds 99.81% <= 0.9 milliseconds 99.88% <= 1.0 milliseconds 99.91% <= 1.1 milliseconds 99.93% <= 1.2 milliseconds 99.96% <= 1.3 milliseconds 99.97% <= 1.4 milliseconds 99.98% <= 1.5 milliseconds 99.99% <= 1.6 milliseconds 99.99% <= 1.7 milliseconds 100.00% <= 1.8 milliseconds 100.00% <= 1.9 milliseconds 118764.84 requests per second ====== GET ====== 100000 requests completed in 0.90 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 0.00% <= 0.1 milliseconds 45.58% <= 0.2 milliseconds 78.84% <= 0.3 milliseconds 88.70% <= 0.4 milliseconds 93.91% <= 0.5 milliseconds 97.42% <= 0.6 milliseconds 98.88% <= 0.7 milliseconds 99.44% <= 0.8 milliseconds 99.68% <= 0.9 milliseconds 99.81% <= 1.0 milliseconds 99.86% <= 1.1 milliseconds 99.90% <= 1.2 milliseconds 99.90% <= 1.5 milliseconds 99.91% <= 1.6 milliseconds 99.93% <= 1.7 milliseconds 99.93% <= 1.8 milliseconds 99.95% <= 1.9 milliseconds 99.96% <= 2 milliseconds 100.00% <= 2 milliseconds 110741.97 requests per second ====== INCR ====== 100000 requests completed in 0.82 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.96% <= 1 milliseconds 100.00% <= 1 milliseconds 121802.68 requests per second ====== LPUSH ====== 100000 requests completed in 0.83 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.95% <= 1 milliseconds 100.00% <= 1 milliseconds 120048.02 requests per second ====== RPUSH ====== 100000 requests completed in 0.86 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.86% <= 1 milliseconds 100.00% <= 1 milliseconds 116414.43 requests per second ====== LPOP ====== 100000 requests completed in 0.86 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.86% <= 1 milliseconds 100.00% <= 1 milliseconds 116279.07 requests per second ====== RPOP ====== 100000 requests completed in 0.83 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.96% <= 1 milliseconds 100.00% <= 1 milliseconds 120772.95 requests per second ====== SADD ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.85% <= 1 milliseconds 100.00% <= 1 milliseconds 117370.89 requests per second ====== HSET ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.92% <= 1 milliseconds 100.00% <= 1 milliseconds 117785.63 requests per second ====== SPOP ====== 100000 requests completed in 0.84 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.88% <= 1 milliseconds 100.00% <= 1 milliseconds 118483.41 requests per second ====== ZADD ====== 100000 requests completed in 0.83 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.92% <= 1 milliseconds 100.00% <= 1 milliseconds 120048.02 requests per second ====== ZPOPMIN ====== 100000 requests completed in 0.86 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.84% <= 1 milliseconds 100.00% <= 1 milliseconds 116959.06 requests per second ====== LPUSH (needed to benchmark LRANGE) ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.94% <= 1 milliseconds 100.00% <= 1 milliseconds 117096.02 requests per second ====== LRANGE_100 (first 100 elements) ====== 100000 requests completed in 0.86 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.78% <= 1 milliseconds 100.00% <= 1 milliseconds 116414.43 requests per second ====== LRANGE_300 (first 300 elements) ====== 100000 requests completed in 0.84 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.82% <= 1 milliseconds 100.00% <= 1 milliseconds 118623.96 requests per second ====== LRANGE_500 (first 450 elements) ====== 100000 requests completed in 0.82 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.96% <= 1 milliseconds 100.00% <= 1 milliseconds 121802.68 requests per second ====== LRANGE_600 (first 600 elements) ====== 100000 requests completed in 0.84 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.90% <= 1 milliseconds 100.00% <= 1 milliseconds 118483.41 requests per second ====== MSET (10 keys) ====== 100000 requests completed in 0.85 seconds 50 parallel clients 3 bytes payload keep alive: 1 host configuration "save": 10 2 host configuration "appendonly": yes multi-thread: no 99.90% <= 1 milliseconds 100.00% <= 1 milliseconds 117924.53 requests per second [root@192 redis-6.0.8]#
可以看到性能还是很高的。多数命令都在2ms内执行完成了。第二个命令就是monitor:
第三个命令就是慢查询,可以通过这种方式进行查询较慢的统计:
今天聊到的所有问题的通用解决方案从长期来看分为以下几个手段,结合各个问题和性能指标配合食用:
- 让数据获取不再那么频繁,更多的页面静态化处理。
- 给缓存加个缓存,构建多级缓存架构,Nginx缓存+Redis缓存+ehcache缓存
- 让数据库更快,对数据库进行严重耗时的性能排查和优化,对数据库的瓶颈进行排查
- 灾难预警机制,监控Redis服务器性能指标:CPU占用率、CPU使用率、内存容量、查询平均响应时间
- 限流、降级,业务高峰期对非核心数据的访问进行限流,降低访问压力,牺牲一部分用户体验
这个解决方案其实适用于今天所有的缓存相关的问题,从入口到最终落地点整体进行把控,再结合各个具体问题的实践处理手段,达到一个稳定的防御机制。还有一个概念是缓存热备,缓存热备即当一台缓存服务器不可用时能实时切换到备用缓存服务器,不影响缓存使用。集群模式下,每个主节点都会有一个或多个从节点来当备用,一旦主节点挂点,从节点立即充当主节点使用,所以天然支持,这里不做讨论了