3

I would like to do something similar to this: Rails way to call external API from view?

But I don't want to call the API for every request from users because that would put a lot of unnecessary load on the API server and deplete my quota too fast.

Is there any way to cache the response from every 100th user and display the cached version to every other user or something of the sort? There's probably something already out there to do this, but I'm very new to Ruby and would appreciate some help.

Viktor
  • 2,623
  • 3
  • 19
  • 28
rubyyyyyy
  • 33
  • 4
  • what is the resource that you would like to conserve due to its quota limit? A cached request may avoid a database hit, but it's still an http request. – Les Nightingill Aug 09 '20 at 20:15

1 Answers1

1

There are numerous ways to achieve what you are looking for. I would advise against caching the response per xxx user, since there are many variables around days and times where traffic will be more strenuous than others. I would advise that you ask yourself what the behaviour of the method is. Is it to pull some complex data or would it just be a simple count? If real-time information is not important, what is an acceptable timeframe for the information to be cached?

If the answer to the above questions can be answered in time metric rather than xxx Users visiting, then you may want to use the built in Rails.cache, by defining the metric collection method in a helper and then calling from a view:

def method_to_call
   Rails.cache.fetch("some_method", expires_in: 1.hour) do
      SomeThing.to_cache
   end
end

from here you can forecast your access to the API and be certain of your usage over a defined time period, without worrying about what times of day your website may be more busy, or any unexpected spikes in application usage.

If you want to cache per xxx user visit, I would highly recommend redis. It's a fantastic piece of software that is incredibly fast and scalable. It's a key value pair store that can hold the data around unique users and page views.

Another question to ask is are you caching on individual user or individual page view? Based on the answer you can store user id or page view count and have conditional logic to refresh the cache on each xxx metric. Performance should not be too much of an issue if you have some due diligence to clear the store every week or so, depending on the data stored.

When you get to large scales of caching you might have to think about the infrastructure of hosting a redis instance. Will you need a dedicated server? Is docker a viable option for a production redis? Can you host the redis instance on the same instance of the application? All of these possible overheads favour the initial approach, but again it is dependant on your needs.

benjessop
  • 1,729
  • 1
  • 8
  • 14
  • This is awesome! Thanks so much for the help :) It doesn't matter on what user it is, it's the same result from the API nevertheless. We just have really limited quota :) – rubyyyyyy Aug 10 '20 at 09:39