SpringBoot的redis連接池lettuce參數配置優化建議


Redis客戶端配置優化建議

配置項 默認值 描述 中文描述
spring.redis.lettuce.cluster.refresh.adaptive false Whether adaptive topology refreshing using all available refresh triggers should be used. redis集群拓撲自動刷新
spring.redis.lettuce.cluster.refresh.period Cluster topology refresh period. 集群拓撲刷新周期。
spring.redis.lettuce.pool.max-active 8.0 Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. 連接池可以分配的最大連接數。使用負值表示無限制。
spring.redis.lettuce.pool.max-idle 8.0 Maximum number of “idle” connections in the pool. Use a negative value to indicate an unlimited number of idle connections. 連接池可以分配的最大連接數。使用負值表示無限制。
spring.redis.lettuce.pool.max-wait -1ms Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely. 連接池資源耗盡時,連接嘗試分配阻塞時間,超時即拋出異常。使用負值無限期阻塞。
spring.redis.lettuce.pool.min-idle 0.0 Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if both it and time between eviction runs are positive. 連接池最小空閑連接數.僅在它和time-between-eviction-runs都為正時有效
spring.redis.lettuce.pool.time-between-eviction-runs Time between runs of the idle object evictor thread. When positive, the idle object evictor thread starts, otherwise no idle object eviction is performed. 空閑對象逐出器線程的運行間隔時間。當為正值時,空閑對象逐出器線程啟動,否則不執行空閑對象逐出。
spring.redis.lettuce.shutdown-timeout 100ms Shutdown timeout. 關閉超時

配置詳解

spring.redis.lettuce.pool.max-active 連接池最大的連接數.過少會導致競爭\阻塞.過多會浪費資源.

配置數量過少,往往在開發環境時配置會比較低,在壓測時會導致競爭激烈,多數線程被阻塞,導致TPS上不去.
可以通過打印redis查詢接口耗時發現,接口耗時不穩定.有些快的在1ms完成,慢的在40ms以上,甚至超時.[建議查詢耗時在1-3ms之間]

配置數量過大,配置數量過大不僅浪費資源,甚至可能搶占不到與redis的連接,這與redis服務器可連接的最大數有關.處理redis連接數過多

解決方法: 加大配置值.
建議默認值: CPU*2

一次redis連接池連接數配置過少引起的性能問題

spring.redis.lettuce.pool.max-idle
連接池最大的空閑數.過少會導致頻繁釋放\建立鏈接,十分耗時(建立連接是耗時操作).過多會浪費資源.

配置數量過少,導致並發高時,需要新建與redis的連接.

通過監控查看redis每秒新建連接數與當前連接數,逐步提高配置數量,以致達到預期.

一次redis調優——連接池優化

spring.redis.lettuce.pool.max-wait
連接嘗試分配阻塞時間.過短會頻繁拋出異常,在有旁路設計的系統中,壓力就會宣泄到數據庫中.過長或者無限制會導致接口響應時間過長.

spring.redis.lettuce.pool.min-idle
連接池最小空閑連接數.

spring.redis.lettuce.pool.time-between-eviction-runs
空閑對象逐出器線程的運行間隔時間.空閑連接線程釋放周期時間.

配置推薦

開發環境

spring.redis.lettuce.pool.max-active = 2
spring.redis.lettuce.pool.max-idle = 2

生產環境

spring.redis.lettuce.pool.max-active = 大於cpu*2
spring.redis.lettuce.pool.max-idle = cpu*2
spring.redis.lettuce.pool.max-wait = 5s
spring.redis.lettuce.pool.min-idle = 0
spring.redis.lettuce.pool.time-between-eviction-runs = 1s


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM