Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:14:25+00:00 2026-06-17T11:14:25+00:00

I am building a Rails backend to an iPhone app. After profiling my application,

  • 0

I am building a Rails backend to an iPhone app.

After profiling my application, I have found the following call to be especially expensive in terms of performance:

@messages.as_json

This call returns about 30 message objects, each including many child records. As you can see, a single message json response may make many DB calls to be composed:

  def as_json(options={})

     super(:only => [...],
      :include => {
        :user => {...},
        :checkin => {...}
                     }},
        :likes => {:only => [...],
                      :include => { :user => {...] }}},
        :comments => {:only => [...],
                                    :include => { :user => {:only => [...] }}}
                   },
      :methods => :top_highlight)
  end

On average the @messages.as_jsoncall (all 30 objects) takes almost 1100ms.

Wanting to optimize I’ve employed memcached. With the solution below, when all my message objects are in cache, average response is now 200-300ms. I’m happy with this, but the issue I have is that this has made cache miss scenarios even slower. In cases where nothing is in cache, it now takes over 2000ms to compute.

   # Note: @messages has the 30 message objects in it, but none of the child records have been grabbed

    @messages.each_with_index do |m, i|
      @messages[i] = Rails.cache.fetch("message/#{m.id}/#{m.updated_at.to_i}") do
        m.as_json
      end
    end

I understand that there will have to be some overhead to check the cache for each object. But I’m guessing there is a more efficient way to do it than the way I am now, which is basically serially, one-by-one. Any pointers on making this more efficient?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:14:26+00:00Added an answer on June 17, 2026 at 11:14 am

    I believe Rails.cache uses the ActiveSupport::Cache::Store interface, which has a read_multi method for this exact purpose. [1]

    I think swapping out fetch for read_multi will improve your performance because ActiveSupport::Cache::MemCacheStore has an optimized implementation of read_multi. [2]

    Code

    Here’s the updated implementation:

    keys = @messages.collect { |m| "message/#{m.id}/#{m.updated_at.to_i}" }
    hits = Rails.cache.read_multi(*keys)
    keys.each_with_index do |key, i|
      if hits.include?(key)
        @messages[i] = hits[key]
      else
        Rails.cache.write(key, @messages[i] = @messages[i].as_json)
      end
    end
    

    The cache writes are still performed synchronously with one round trip to the cache for each miss. If you want to cut down on that overhead, look into running background code asynchronously with something like workling.

    Be careful that the overhead of starting the asynchronous job is actually less than the overhead of Rails.cache.write before you start expanding your architecture.

    Memcached Multi-Set

    It looks like the Memcached team has at least considered providing Multi-Set (batch write) commands, but there aren’t any ActiveSupport interfaces for it yet and it’s unclear what level of support is provided by implementations. [3]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an iPhone application that talks to a Ruby on Rails backend. The
I am building a Rails application that contains Developer s who have Application s.
I'm very new with rails and I've been building a CMS application backend. All
I'm building Rails application and I want to have user registration/login functionality. I also
Hi Im new to Ruby On Rails, but have experience building iPhone apps. So
I am currently working on building an Android app for my Rails backend. I
I have a Rails application that right now is pretty standard: Heroku/PostgreSQL backend, users
I'm building a (Rails-based) web service with a mobile app (iPhone) as frontend. In
Im building a Rails 3 app, and have an Observer... in the observer I
I'm building a Rails app and have copied the files from the Bootstrap Github

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.