Search code examples
ruby-on-railsrails-activestorage

High response time with active storage


I am using active_storage to store and retrieve three plots on a web page on Heroku. The code to retrieve the plot is

image_tag plot.variant(resize: '500x500'), class: 'r_plot'

Each plot is fairly small, around 500 KByte. I frequently see the following error from the Logentries heroku add-on:

High Response Time: Heroku/Syslog drain
    2020-05-30 21:39:37.733
    671 <158>1 2020-05-30T21:39:35.184905+00:00 heroku router - - at=info method=GET path="/rails/active_storage/representations/long_token/-tmp-plotsPlotname" host=cvd-tracker.herokuapp.com request_id=some_id fwd="ip_address" dyno=web.1 connect=0ms service=5015ms status=302 bytes=1679 protocol=https

Why is it taking so long to retrieve these plots?


Solution

  • Could be a few things:

    1. The variants are created on demand by the server, so the first time it is called it can take some time and a decent amount of resources to complete.
    2. Heroku has an ephemeral filesystem, meaning if you're storing these variants locally then after every new deployment or other event causing a dyno change, the previously created variant will be destroyed so the next time it is called you will have to go through the previous step again.
    3. If you aren't storing the variants locally then perhaps your dyno is too small, meaning the server is running out of memory and having to use swap space to complete the variant processing?

    Does it sound like any of these apply to your situation?

    EDIT - Background Processing:

    It appears that the actual processing is triggered when processed is called so you could do something like this in your rake task:

    upload = Upload.find(upload_id)
    variant = upload.image.variant(options)
    was_processed = variant.processed
    Rails.logger.info "Processed variant for #{upload.id}" if was_processed
    

    I found the above along with some other tips here.