Understanding why my Rails 4 App is randomly timing out

Viewed 362

For the past few months, my site (running on Rails 4 with Heroku) has been having no issues. Traffic has been steady and response times below 500 ms.

Randomly today, response times shot up to 20,000 ms+.

Heroku response times

When looking at my logs, I noticed ActiveRecord taking unusually long to run a SELECT DISTINCT COUNT. This, in fact, seemed to be causing the slow loads.

(5029.1ms)  SELECT DISTINCT COUNT(DISTINCT "impressions"."session_hash") FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2  [["impressionable_id", 826], ["impressionable_type", "Article"]]

I want to say this is being caused by the impressionist gem and my counter cache that I am using to display article count, but I am not sure.

I am now also seeing this error in my logs for the first time:

PG::DuplicatePstatement: ERROR:  prepared statement "a9" already exists

Any help understanding what is going on would be awesome. Can't seem to find documentation on the issue. Very strange that my app has solidly been running for months now, and just today it decides to go downhill.

I am also using the Rack Timeout gem with Heroku. I heard this gem can directly or indirectly cause the above error, but I just can wrap my head around why any of this is happening. Let me know if there is any other info or metrics I can provide.

One-Day-Later Update:

Response times went back to normal on their own (< 500 ms). Can't figure out why such a random spike.

And just for reference, the above SELECT DISTINCT COUNT query was being executed from this code in my article show page which is simply counting the impressions unique to the session:

<%= @article.impressionist_count(filter: :session_hash) %>
1 Answers
Related