Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/8871856 to your computer and use it in GitHub Desktop.
Save tylerhunt/8871856 to your computer and use it in GitHub Desktop.
Demonstrate an issue with reaching the maxclients limit in Redis, even when using a connection pool.

Start a Redis server with a limited number of clients:

$> /usr/local/opt/redis/bin/redis-server --port 6380 --maxclients 5

Then, run the script:

$> ruby redis_connection_pool.rb

The script should fail with the following error during the second iteration:

ERR max number of clients reached (Redis::CommandError)

Run the script again with SHUTDOWN=true:

$> SHUTDOWN=true ruby redis_connection_pool.rb

This will allow the second iteration to complete successfully, since the connections from the first iteration will have been properly closed.

require 'redis'
require 'connection_pool'
2.times do
pool = ConnectionPool.new(size: 5) { Redis.connect(port: 6380) }
pool.with { |connection| connection.set :count, 0 }
5.times.collect {
Thread.new do
pool.with { |connection| connection.incr :count }
end
}.each(&:join)
puts pool.with { |connection| connection.get :count }
pool.shutdown { |connection| connection.quit } if ENV['SHUTDOWN']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment