Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 1.22 KB

share_session_with_memcached.md

File metadata and controls

42 lines (32 loc) · 1.22 KB

How to share a session with Memcached?

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

More Info

Refer to Rack::Session documentation to know more about rack sessions. Refer to Dalli documentation to know more about Rack::Session::Dalli.