Offline queue size limit
Problem
As ioredis supports offline queue, is it possible that the queue will grow to infinity if the client can not connect to redis server for long time, which will lead to huge memory growth ? If so, I think it is nice to have a param to control the max size of the offline queue, and eviction policy, e.g. LRU
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Max Size Limit and Eviction Policy for Offline Queue in ioredis
The ioredis client maintains an offline queue for commands that cannot be sent to the Redis server when it is unreachable. If the connection remains down for an extended period, this queue can grow indefinitely, leading to excessive memory usage. Without a mechanism to limit the size of this queue or an eviction policy to discard older commands, the application may experience performance degradation or crashes due to memory exhaustion.
Awaiting Verification
Be the first to verify this fix
- 1
Set Max Offline Queue Size
Configure the ioredis client to limit the maximum size of the offline queue. This can be done by setting the 'maxOfflineQueueSize' option when initializing the ioredis client.
javascriptconst Redis = require('ioredis'); const redis = new Redis({ maxOfflineQueueSize: 1000 // Set the maximum size of the offline queue }); - 2
Implement LRU Eviction Policy
To manage the offline queue effectively, implement an LRU (Least Recently Used) eviction policy. This can be achieved by overriding the default behavior of the offline queue to remove the oldest commands when the maximum size is reached.
javascriptconst Redis = require('ioredis'); const redis = new Redis({ maxOfflineQueueSize: 1000, // Custom logic to handle LRU eviction can be implemented here }); redis.on('error', (err) => { console.error('Redis error:', err); // Custom logic to handle offline commands with LRU eviction }); - 3
Monitor Memory Usage
After implementing the changes, monitor the memory usage of the application to ensure that the offline queue is not growing indefinitely. Use tools like Node.js memory profiling or Redis monitoring commands to observe the memory footprint.
javascriptconst { exec } = require('child_process'); exec('redis-cli info memory', (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`Memory Info: ${stdout}`); }); - 4
Test Connection Recovery
Simulate a Redis server outage and recovery to test the behavior of the offline queue. Ensure that commands are processed correctly after the connection is restored and that memory usage remains within acceptable limits.
javascriptredis.set('testKey', 'testValue'); // Simulate a command // Disconnect Redis server and reconnect to test recovery behavior
Validation
Confirm that the offline queue does not exceed the set maximum size during extended disconnections. Monitor the application's memory usage to ensure it remains stable. After recovery, verify that commands are processed correctly without memory spikes.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep