Back to Practice

Practice · Scaling · Card 3

What's the failure mode of this cache under high traffic?

The code is textbook. It works in development. Under load, it has a specific failure mode you have to name.

The code

A homepage controller serving 5,000 requests per minute.

def index
  @posts = Rails.cache.fetch("homepage_feed", expires_in: 1.minute) do
    Post.includes(:user, :tags)
        .order(score: :desc)
        .limit(100)
        .to_a
  end
end

The question

Under sustained traffic, this code starts behaving badly every minute. Name the failure mode and how you'd fix it.

Take a moment. Every 60 seconds, the cache entry expires. What does a flood of in-flight requests do at that instant?