Dealing with the scaling concerns we are supposedly lucky to have

Session Storage

StimulusReflex configures :cache_store to be the Rails session storage mechanism. In a production environment, you'll want to move beyond the Rails default :memory_store cache in favor of a more robust solution.

The recommended solution is to use Redis as your cache store, and :cache_store as your session store. Memcache is also an excellent cache store; we prefer Redis because it offers a far broader range of data structures and querying mechanisms. If you're not using Redis' advanced features, both tools are equally well-suited to key:value string caching.

Make sure that your Redis instance is configured to use the lru-volatile expiration strategy with expiring session keys.

Many Rails projects are already using Redis for ActiveJob queues and Russian doll caching, making the decision to use it for session storage easy and incremental. Add the redis and hiredis gems to your Gemfile:

gem "redis", ">= 4.0", :require => ["redis", "redis/connection/hiredis"]
gem "hiredis"

Then configure your environments to suit your caching strategy and pool size:

config.cache_store = :redis_cache_store, {url: ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" }}
config.session_store :cache_store,
key: "_session",
compress: true,
pool_size: 5,
expire_after: 1.year

Another powerful option for session storage is to use the activerecord-session_store gem and keep your sessions in the database. This technique requires some additional setup in the form of a migration that will create a sessions table in your database.

Database-backed session storage offers a single source of truth in a production environment that might be preferable to a sharded Redis cluster for high-volume deployments. However, it's also important to weigh this against the additional strain this will put on your database server, especially in high-traffic scenarios.

Regardless of which option you choose, keep an eye on your connection pools and memory usage.


"But does it scale?"


We're excited to announce that StimulusReflex now works with AnyCable, a library which allows you to use any WebSocket server (written in any language) as a replacement for your Ruby WebSocket server. You can read more about the dramatic scalability possible with AnyCable in this post.

Getting to this point required significant effort and cooperation between members of both projects. You can try out a preview of the upcoming AnyCable v1.0.0 release today.

First, add gem "anycable-rails", "1.0.0.preview1" to your Gemfile.

Next, install anycable-go v1.0.0.preview (binaries available here, Docker images are also available).

Finally, if you use session in your Reflex classes, add persistent_session_enabled: true to anycable.yml.

There is also a brand-new installation wizard which you can access via rails g anycable:setup after the gem has been installed.

If you notice any issues with AnyCable support, please tell us about it here.