Introducing Friendly: NoSQL With MySQL in Ruby


Dec 17, 2009

I've been a big proponent of NoSQL for a while. I have played with just about all of the new generation of data stores. We almost got cassandra running in production once, and we've been running mongodb in production for about six months now.

But, here's the thing: as awesome as these new dbs are, they're still young. Our app generates a ton of data and gets pretty serious traffic. So, we started hitting walls quickly.

To make a long story short, we decided to fall back to MySQL. It's battle hardened. We know its production characteristics and limitations. Backups are a science. We know we can count on it.

But, we have a lot of data, and adding fields and indexes was starting to get painful. Flexible schemas are one of the things that attracted me to NoSQL in the first place. Then, I remembered this article about How FriendFeed uses MySQL to store schema-less data. So, I decided to implement the system they describe in the article.

Since we put Friendly in to production, we've seen a significant increase in site performance. Friendly's built-in caching has achieved a hit rate of 99.8%. We're pretty happy with things so far.

Introducing Friendly

Friendly makes MySQL look like a document store. When you save an object, it seralizes all of its attributes to JSON and stores them in a single field. To query your data, Friendly creates and maintains indexes in separate tables. It even has write-through and read-through caching built right in.

Let's build a model:

class Post
  include Friendly::Document

  attribute :author, String
  attribute :title,  String
  attribute :body,   String
end

We can create objects using a familiar syntax:

Post.create :author => "Stewie Griffin",
            :title  => "World Domination Plot",
            :body   => "All the gory details here..."

At this point, we can only query our models by id, which isn't great. So, let's add an index:

class Post
  # ... snip ...
  
  indexes :author, :created_at
end

Note that all friendly tables have automatically managed created_at and updated_at fields. Now we can query our model by author:

Post.all(:author => "Stewie Griffin", :order! => :created_at.desc)

With Friendly's query syntax, all parameters are conditions, except modifiers, which end in !.

Since everybody obviously wants to read all about Stewie's world domination plot, his site is getting hammered. Let's add some caching to make this outrageously fast:

class Post
  # ... snip ...

  caches_by :id
end

Now, the Post.all query we performed above will only hit the database once, and the rest of the data will come from a single memcached multiget. When we modify the row, Friendly will automatically update the cache. If we try to access the row in cache and it's missing, Friendly will query the database for it, and stick the result in cache.

Currently, only caching by id is supported, but arbitrary caches are on the way.

Now what?

  • Install Friendly and start playing:
    sudo gem install friendly
    
  • Check out the site (designed by the awesome John Baku) for a more detailed tutorial.
  • Follow the github project.
  • Subscribe to my blog for further updates.
  • Join #friendlyorm on freenode.