Ruby APIs for NoSQL - Polyglot Persistence - GoGaRuCo 2010
by Sarah Mei on Sep 17, 2010
- 2,036 views
Presentation from GoGaRuCo 2010 on Polyglot Persistence and Ruby APIs for NoSQL databases.
Presentation from GoGaRuCo 2010 on Polyglot Persistence and Ruby APIs for NoSQL databases.
Statistics
- Favorites
- 0
- Downloads
- 19
- Comments
- 0
- Embed Views
- Views on SlideShare
- 2,029
- Total Views
- 2,036

Who’s written a Ruby app that uses a relational database AND some kind of alternative datastore?
Who’s written a Ruby app that uses ONLY non-relational datastores?
I was showing this diagram to a friend, and he said, “you know, if you have a diagram like this, you are legally required to have a little drawing of a cloud to represent the internet.” So...
So, at this point, you have five different datastores, and you haven’t even done anything particularly crazy. In fact I would say that this type of setup is a lot more common in real applications than my original undecorated diagram of what a Rails app looks like. This is really what a basic Rails app looks like these days.
These models have relationships, which are hard to model in SQL - those joins get nasty pretty quick. They have blocks of text that need to be searched semantically. They have frequently-read, rarely-written objects that need to be retrieved very very quickly. And they have associated assets that go along with them.
And I would like to point out, before I go on, that until recently, we didn’t even think of all of these things as alternative datastores. Cassandra and redis, certainly, but s3? solr? memcached? Are all of these things datastores?
Those guys were doing NoSQL...before it was coool.
There’s another question, too...
Digg, for example, just moved over to Cassandra! For everything! Of course this is not always the right answer, because shortly after Digg rolled out their new architecture, they fired their VP of engineering, ... so...
But if you want to do it, and sometimes you do, historically, this has been pretty difficult to do in Rails.
Then they use non-SQL persistence for other things, where it fits. I mean, you can keep cassandra for your voluminous sparse data...if you have that, and a surprising number of apps do!
It’s not a new idea - Ben Scofield has been talking about it for more than a year. But a lot of Ruby developers think of “the data storage layer” and they think it’s a choice between MySQL and Postgres, when in reality, it’s going to end up with an assortment of different technologies. Whether or not they want that, and whether or not they explicitly plan for it.
So let’s take a look at what that’s going to look like.
So I add a “follow” method that uses the astonishingly-descriptively-named “redis” gem to insert stuff into a redis keyspace.
Current datastore count: 4
Now at this point, the Squid class is started to look a little...
You know what I would like? Some kind of consistent interface.
Unfortunately, relational algebra is totally useless with a non-relational datastore where you don’t structure your data into rows, and columns, and tables. It just doesn’t map to something like a key-value store like redis, or column-oriented document database like mongo.
A lot of the blog posts and stuff about ActiveModel have talked about how this makes it easier to take validations and serialization and so on from ActiveModel and use them in plain old Ruby objects outside of Rails.
But as someone who does mostly Rails development, I actually don’t really care about that. What I’m excited about is that it’s now much easier to extract ActiveRecord from your models in Rails. It makes it much easier to write an adapter for a non-relational datastore and be able to use it in your models in a Rails app. All you have to do is present the same API to ActiveModel that ActiveRecord does, and that’s actually pretty easy. And in fact,
Now that it’s getting easier to use a nonrelational store, I think we’re going to start seeing a lot more applications take advantage of them.
Sadly, ActiveModel doesn’t solve all of our problems. We still have to work at it to use more than one datastore within the same model.
For instance,
But the second one
AND, if you’re going to do this, upgrade to Rails 3!