1

I'm writing a gem that uses an API from a 3rd party app. The content of the request just changes once per day. So I thought, some kind of caching would be useful. The returned value is just a short JSON.

Storing this data in a table feels un-handy, because you would have to write migration... Using redis or memcache introduces another dependency, that I would like to avoid.

So I was thinking about using class variables to store the cached values. As far as I know

  • They are the same for every request in a single thread
  • Every thread has its own class variables (am I right? I'm unsure)
  • Every thread (e.g. 5 Passenger instances) has to do its own calls and cache them inside the class variable

I know using class variables accross states can be very tricky and sneaky. But do you think it's ok in my case? Or are there any pitfalls I haven't thought of? Or is it better to use something like redis or memcache?

3
  • Bad idea; what if you scale horizontally? Aug 21, 2015 at 7:58
  • Then every machine and every thread has to do its own request to get the data, once per day. I think that would be ok
    – 23tux
    Aug 21, 2015 at 8:01
  • 2
    However, every thread should share class definitions with other threads, so: - They are the same for every request in a single process - Every process has its own class variables - You must handle concurrency Aug 21, 2015 at 8:01

1 Answer 1

1

It will work, a per-process cache in a variable is very fast to read (potentially the fastest overall option), and simple to implement. In essence most file-based configuration systems are long-term caches implemented in this way, so that getting a config variable is not a file I/O each time.

There are some caveats:

  • It is hard to monitor and communicate with the cache outside of the running process, unless you write additional code to log cache hits/misses and view cache contents.

  • You will only have mechanisms to invalidate the cache that you explicitly write and allow for in advance. Or you could re-start your server just to clear a bad cache. Cache invalidation logic is hard to get right.

  • The cache store is generally per-process (threads will share the same cache data). That will require more calls to the external service than a separate cache service such as database or memcached.

  • The per-process store may also mean you can have a situation where some of your server processes have different versions of the data, which could lead to inconsistent responses from your server per user request (visitor sees one version of data then another, randomly, until the discrepancy is resolved by cache invalidation)

If any of these caveats are important to you, or would cost development time to resolve, then you may be better off using a purpose-written cache service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.