When we write applications in an RDBMS database, we can’t create whatever relationship we want between database objects. Since we’re using a relational database, this seems like a contradiction. Let’s skip the semantics and see what happens in the Rails world.
Rails is presented as database agnostic, but that really means Relational Database Agnostic — you still have to use an RDBMS database to persist your objects.
SQL doesn’t play well with large clusters. Note that neither Google not Amazon use relational databases. With several NOSQL options available, some may be tempted to say, “Rremove the RDBMS and use this awesome NOSQL.” Wrong decision. Relational Databases have good features like ACID transactions or tabular data organization. There are a lot of tools and almost every developer knows how to do a query.
NOSQL and RDBMS together?
Before deciding that, though, we have to consider that in Rail, we have ActiveModel as well as beautiful and pure Ruby objects that can use different adapters to persist themselves into different kinds of databases based on our needs.
Let’s think about a shopping cart app.
We can distinguish several objects: Session, Cart, Item, User, Order, Product and Payment. Usually we use ActiveRecord to store all of them. But this time let’s think about it differently.
For sessions, we don’t need durable data at all — Redis can be a good option, and of course will be faster than any RDBMS. For Cart and Item,we will need high availability across different locations. Riak can fit well for this use case. For User Order Product and Payment, a relational database can fit well, focusing on Transactions and Reporting about our application.
Which adapters do we have in Ruby/Rails?
- RDBMS (MySql, Postgresql, etc): Do you need an explanation about RDBMS? With Mysql and Postgresql we have the necessary gems available to Rails and can use ActiveRecord directly.
- Riak: A truly fault-tolerant system, Riak has no single point of failure. No machine is special or central in Riak, so developers and operations professionals can decide exactly how fault-tolerant they want and need their applications to be. To use Riak on our Rails Application, we have to use Ripple. Ripple allows you to have ActiveModel abstraction of Riak documents.
- Redis: Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. For Redis and ActiveModel we can use redis-persistence.
Having the right persistence for most uses in Web development and distributed applications isn’t about being SQL or NOSQL, it’s about choosing the right tool for the right purpose. With Ruby on Rails we have the adapters to work with all of them and our app should be polyglot to survive in the cloud.