Ryan Dy's blog
CouchDB + Ruby: Perform Like a Pr0n Star - Matt Aimonetti
Intro
Matt Aimonetti is part of the Merb team.
Size matters
- memory usage
- amount of servers
- infrastructure
You need reliability
Fault tolerant
- downtime
Multiple Partners
- Nice public interfaces
- no discrimination
Scalability
- reaction underload
- handle concurrent connections
- numbert of requests persecond
- latency
Approaches
Every p0rn star needs a trick
Enter CouchDB
Why aren't you a porn star now? Because of RDBMS
What does RDBMS have problems?
- most/all fields become options, schema changes and it becomes hard to deal with
- joins hurts performance (add indices/tables)
- replication is difficult when using multiple masters
- auto-incremental ids
What other kinds of approaches can we use?
Hypertable
Distributed Hash Table
- fault tolerant
- used by p2p and IM
- memached is another example
Some other projects: Project Voldemort, Tokyo Cabinet, Redis
What is CouchDB?
It is a project written in Erlang by Apache. It uses spider monkey.
Can be used as a key/value store. Can also put data in a schema-less store, format your data as a JSON object and dump the whole thing into the db
CouchDB comes with a nice web interface called Futon that lets you inspect your database and its contents.
CouchDB is de-centralized. You can do replication between multiple masters.
map/reduce is built in to compute data. Maps and reduces can be written in Javascript.
Optimized for the web, more reads than writes.
- full Acid compliance
- https rest interface
- caching (couchdb uses etags) - this makes caching with existing HTTP caching technology (nginx+memcache, varnish, etc) really easy.
- built-in conflict management using MVCC (multiversion concurrency control)
- Every single record gets saved as a different revision
- document attachments are attached as document stubs, that also get replicated to different nodes and don't use a lot of memory
How does this relate to Ruby?
Using CouchRest Everything is a JSON object.
You can define properties so you get an attribute reader/writer, and do document.attr
There is no SQL in CouchDB, you do queries like Card.first, Card.all, Card.get('matt_aiomonetti')
The problem is trying to map documents into Object-Oriented languages, but it doesn't always work. If you have dependent objects (such as Card and Address), then you tell CouchDB to cast it as something.
property :questions, :cast_as > ['Question']
Callbacks:
save_callback :before, :generate_slug_from_title
Relationships can be done, but you have to decide how to do it, and there are a lot of ways.
When to use couch?
- When you need to scale you database and availibility is more important than consistency.
- When your data is decentralized (you have more than one master
- When you need to compute data
Use cases:
- Analytics - combined with traditional rdbms system to get statistics using couchdb
- Personal finance - Bank accounts in different countries - download all accounts, and can process in one place - attach PDF files, desktop app, etc.
- Medical Record System - Many patients with visits, history, records, etc.
- distributed e-commerce sites (1 main website working with multiple partners, the data can be replicated easily) In this case, CouchDB is much faster than RDBMS, because of drugs, compounds, and complex structures.
Questions
Q: Can you use couchdb with objects that have attributes that change often?
A: Yes you can use it.
Q: There are many ways to do relationships. Can you give an example?
A: Blog has an article with many comments. You can make one doc that is an article, and you can add comments. Problem is you need to send the whole document that increases risk of conflicts. So you create a new object that is the comment object that just refers to the article. Then, in the view you define the Article as aggregating all comments. Alternately, you can make a query to retrieve all comments for the article.
We're looking for more information about Taste or other similar kinds of suggestion engines. What kind of experiences have people had integrating these tools into Rails applications. Are there other useful engines worth looking at?
Direct uploads to Amazon S3 service can be serviced by EC2 servers; however, there might be more direct ways of doings this with S3.
Newer versions of RubyMine have been reported having problems running the gem version of rspec but a solutions has been found. The problem comes from the way gems are specified in RubyMine. You need to add the rspec gem you are using to the Project Structure >> Ruby SDK and Gems.
Using Desert and exception notifier can cause some slow startup times. If you are seeing this problem try moving exception notifier's sender_address inside your environment's post-load block.
There is a firebug that works with Firefox 3, it can be found at GetFirebug.com. The current beta version 1.1 appears to fix a problem that Firebug was having with inspecting anchor elements.
Also, there is a google code project ie7-js. It is a Javascript library that attempts to make IE7 behave more like a standards-compliant browser. It includes a limited PNG transparency fix. It also gives you Pseudo-element like :before and :after.
Combining this with yui and IE7 gets a lot closer to behaving as you might expect!









