

Ask HN: Caching in Ruby? - antonb

I know that there are a lot of experts in Ruby on Rails around here, so I was hoping someone could answer a quick question. As an interpreted language, Ruby clearly must be relatively slow. As a result, I would expect that for production systems, there should be some sort of bytecode caching. However, after searching Google, I could not find a conclusive article describing this process for Ruby. Coming from PHP background, I am used to installing APC on production systems. Is there something similar to be installed for systems running Ruby on Rails? Thanks for your answers!
======
regularfry
The most-deployed interpreter, MRI 1.8, doesn't compile to bytecode, so it's
not possible for there to be a general system which does this.

HOWEVER, I would suggest that your fundamental assumptions are wrong here.
Without a cache, PHP has to re-interpret the source files for every single
request. That's not how Rails works: the code is parsed _once_ at server
startup (or the first time it's used, depending on which part of the code
you're looking at) and reused in the same process for subsequent requests, so
there isn't a per-request parsing cost.

As ever, benchmark and profile before making optimisation assumptions.

------
knowtheory
Regularfry's points are absolutely correct.

However, in the event that you do need caching, it can be achieved through a
variety of means, including ORM level object caching, controller caching
and/or view/fragment caching.

Lastly, have you read this guide on Rails caching? Much of this is readily
available in the standard distribution of rails:
<http://guides.rubyonrails.org/caching_with_rails.html>

(final thought — this is a question might have been better suited for
StackOverflow, just given the manner in which SO and HN are geared.)

------
antonb
Thank you everyone for your answers!

@clyfe and @regularfry: Thank you! The fact that my assumptions where wrong,
and that a Rails applications is not re-interpreted every time an http request
is made, is exactly what I wanted to know!

@knowtheory: I tried reading the caching guide but it's too darn long for just
skimming it. The answers I got here were exactly what I was looking for -
brief and to the point, however trying to ask on SO first may be a good idea
next time.

------
clyfe
As the ruby application loads in memory at startup and then is executed from
there (in production) there is no need of bytecode caching, in contrast with
PHP where the code is read, parsed and interpreted on each request, and in
this (second) case, caching the bytecode to save parsing execution makes
sense.

That is the case of _bytecode_ caching. In regards to other forms of caching
(etag, memoization etc) frameworks implement different strategies.

------
amikazmi
jruby bytecode is heavily cached (on top of the caching done by the JVM)

[http://blog.headius.com/2010/05/kicking-jruby-performance-
up...](http://blog.headius.com/2010/05/kicking-jruby-performance-up-
notch.html)

------
spooneybarger
There are a variety of Ruby vms out there now. Each with their own performance
characteristics. Instead of looking for bytecode or ast caching solutions, I
would just look for articles on 'scaling rails' and 'ruby performance' and
similar.

