fuyi
fuyi

Reputation: 2639

Rails share cache between web and worker on Heroku

I have a Rails application which has both web process and background worker (running Sidekiq).

I have one feature which write value to cache from web process, then the worker read this value from cache later.

In web:

Rails.cache.write 'key', 'xxx'

In worker:

Rails.cache.read 'key'

This works find on my local environment, but when I deploy this code to Heroku, the worker can NOT read corresponding key from cache, it always returns nil.

I am wondering if there is any special config I ignored for production mode?

Upvotes: 3

Views: 1407

Answers (1)

jordelver
jordelver

Reputation: 8432

The default cache store in Rails is filestore which means the cache is stored on disk.

From the Rails guide on caching

This is the default cache store implementation.

Your worker and web process do not share the same filesystem, the dynos are spawned in totally different environments, which do not share resources.

If you want to share the cache, you will need to store it centrally in a place that they can both access.

This means probably means a datastore such as Memcache or Redis.

There are many Memcache and Redis addons available on Heroku.

Upvotes: 4

Related Questions