Reputation: 21593
If I am using ActiveRecord, I have to use this middleware:
use ActiveRecord::ConnectionAdapters::ConnectionManagement
This will close connection after each request.
I am wondering if there's something similar I have to use if I want to use the official Redis gem for Ruby?
Or does it close the connection/manage it itself?
Thanks
Upvotes: 0
Views: 139
Reputation: 33656
You don't have to close the connections after each request. In fact, the common approach is to have a global Redis connection per application server (eg. per unicorn worker) that all the requests will use and which stays open but idle. Redis handles this so you don't have to.
This connection remains open as long as your app server is running but is put on in non-blocking state since Redis uses non-blocking IO, so it doesn't affect negatively anything.
I can't think of a good reason for opening & closing the connection on each request. In fact, this would only add unecessary overhead (sockets would have to be created and teared down every time).
There is a relevant discussion here: https://groups.google.com/forum/#!topic/redis-db/xcz5MXykXdk
Upvotes: 0