Simon Rentzke

Migrating from Activerecord session store to Redis session store

February 23, 2021

It’s not a great idea to have your sessions stored in the database, as it can cause all sorts of unnecessary load problems that can be offset to another service.

The easiest and most performant option is to use a cookie store, but if you’re keen on migrating existing sessions and still having control of your sessions, I’d recommend moving to Redis.

It’s quite straightforward to make the move. All you need to do is install the Redis Actionpack gem, gem 'redis-actionpack'.

Then set your session store config, to point to your Redis service.

  Rails.application.config.session_store :redis_store,
                                         servers: [REDIS_SESSIONS_URL],
                                         key: '_session_key',
                                         httponly: true,
                                         secure: true

Then migrate all of your sessions over from ActiveRecord onto Redis, so that your users are not logged out:


old_sessions = ActiveRecord::SessionStore::Session

old_sessions.find_each do |session|
  # create each session into Redis, dumping the object appropriately with Marshal