Suppose that you are developing a Sinatra app that will be deployed to multiple server instances and must be seen as a single application (it must be balanced with nginx, for instance). You need a mechanism for sharing user sessions between all the application instances. To accomplish this task, we'll use memcached for sharing the session and Dalli as the memcached client.
First, add dalli
to your Gemfile
:
gem 'dalli'
Then, add the Rack::Session::Dalli
middleware:
configure do
use Rack::Session::Dalli, cache: Dalli::Client.new
end
Dalli::Client
defaults the memcache server url to localhost:11211
. If you
need to use another server, you must configure it yourself.
See a full example configuring the memcache server and the session namespace:
configure do
use Rack::Session::Dalli,
namespace: 'other.namespace'
cache: Dalli::Client.new('example.com:11211')
end
Refer to Rack::Session documentation to know more about rack sessions. Refer to Dalli documentation to know more about Rack::Session::Dalli.