High response time with active storage

Viewed 414

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?

2 Answers

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.

Extending a little on @Simon L. Brazell's answer, some applications may redirect_to a view that displays the images after they're uploaded, meaning the application may first need to create variants (which takes time).

A few things that may help:

  • Consider whether the view needs to display the images at all (e.g. could it display links to the images that the user can click on, thereby producing the variants incrementally (or not at all if you link to the original, full-size images).

  • Some variant methods may create variants for a collection, consider whether that's necessary (if you're only displaying a subset of the collection, you may not need to create variants for the whole collection).

Another reason

Don't pass attachments through `.update!() like this:

article.update!(
  content: article_params[:content],
  photos: article_params[:photos],
  videos: article_params[:videos]
  )

instead, do this:

article.update!(content: article_params[:content])
article.photos.attach(article_params[:photos]) 
article.videos.attach(article_params[:videos]) 
  • See here for more on that.

Additional notes:

  • I saw this in my heroku logs: Skipping image analysis because the ruby-vips gem isn't installed, I already had the ruby-vips gem installed, but hadn't installed vips on heroku I hoped for a speed improvement after doing so, but I couldn't detect any.
Related