Node.js~ioredis处理耗时请求时连接数瀑增
生活随笔
收集整理的這篇文章主要介紹了
Node.js~ioredis处理耗时请求时连接数瀑增
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于redis連接數過高的解釋
對于node.js開發環境里,使用傳統的redis或者使用ioredis都是不錯的選擇,而在處理大數據請求程中,偶爾出現了連接池( redis服務端的最大可用連接數,默認為1萬)不夠用的情況,一般的提示如下:
It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail在redis-cli上輸入info命令也可以進行查看
?redis-server.conf里配置了它默認的最大連接數
maxclients 10000產生它的原因有幾個:
大叔建議的作法:
減少單次請求的響應時間,建議把redis從一個大請求中拿出來,因為純redis還是很快的
正確使用redis組件,用完就關了
正確理解多線程與socket連接,要知道socket連接直接影響你的服務器CPU性能
ioredis代碼實例
ioredis是個好東西,它完全支持了redis的cluster,sentinal等新技術
new Redis() // Connect to 127.0.0.1:6379 new Redis(6380) // 127.0.0.1:6380 new Redis(6379, '192.168.1.1') // 192.168.1.1:6379 new Redis('/tmp/redis.sock') new Redis({port: 6379, // Redis porthost: '127.0.0.1', // Redis hostfamily: 4, // 4 (IPv4) or 6 (IPv6)password: 'auth',db: 0 })同時支持標準的字符連接串
// Connect to 127.0.0.1:6380, db 4, using password "authpassword": new Redis('redis://:authpassword@127.0.0.1:6380/4')支持發布與訂閱,它會存儲在進程里,它不會被持久化,所有會有消息丟失的情況
var Redis = require('ioredis'); var redis = new Redis(); var pub = new Redis(); redis.subscribe('news', 'music', function (err, count) {// Now we are subscribed to both the 'news' and 'music' channels.// `count` represents the number of channels we are currently subscribed to. pub.publish('news', 'Hello world!');pub.publish('music', 'Hello again!'); });好了,下次我們有時間去講講ioredis的具體操作!
感謝各位的閱讀!
本文轉自博客園張占嶺(倉儲大叔)的博客,原文鏈接:Node.js~ioredis處理耗時請求時連接數瀑增,如需轉載請自行聯系原博主。
總結
以上是生活随笔為你收集整理的Node.js~ioredis处理耗时请求时连接数瀑增的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【开发者的精进】 数据驱动下的程序设计
- 下一篇: java-信息安全(一)-BASE64,