Skip to content

Instantly share code, notes, and snippets.

@banker
Created September 8, 2011 22:03
Show Gist options
  • Save banker/1204900 to your computer and use it in GitHub Desktop.
Save banker/1204900 to your computer and use it in GitHub Desktop.
MongoDB for Rails at DC Ruby

MongoDB Pointers for Rubyists

Kyle Banker

DC Ruby | September, 2011

THE RAILS

1. Is MongoDB a good choice for your Rails project?

The answer is YES if:

You're doing content management.

  • HarmonyApp
  • NYTimes
  • MTV

You store logs or analytics data.

(Especially if you can pre-aggregate).

  • CustomInk
  • Yottaa
  • Gaug.es
  • Intuit

You're consuming APIs or unstructured data.

  • Craigslist (record archive)
  • NYTimes (R & D)
  • Disney (gaming back end)
  • LexisNexis (auditing meta-data)

You're storing static data with complex structures

  • Trees
  • Graphs
  • Documents are a good place for these.

You're building a location-based app.

You need high availability:

  • Use replica sets.
  • Get automated failover.
  • Multi-data center.
  • Configurable write concern.

You have "big data."

  • MongoDB supports sharding.
  • But be careful: setting up a sharded cluster takes some planning.
  • You need to know about your access patterns so that you can choose the right shard key and the right number of shards.

You've put together a prototype, and you've seen good results.

You're hip to MongoDB.

The answer is MAYBE if:

You're doing lots of ad-hoc, relational-style queries that usually require complicated joins.

You need transactions proper.

So, you're gonna build your project on MongoDB?

2. Get yourself an object mapper:

Mongoid (biggest community)

  • Use with a larger, less experienced team.
  • Tons of plugins / gems.
  • Beware: can shield you from MongoDB too much.

MongoMapper (oldest, mature)

  • Fewer plugins, simpler than Mongoid.
  • Nunemaker wrote most of the plugins.

Are you a minimalist? Do you still believe in the dream of simplicity?

  • Mongomatic
  • MongoODM

3. Get some auth:

  • Devise (plugins for Mongoid)
  • bcrypt-ruby. Use it to roll your own in half it takes to write a Devise plugin :)

Get some hosting:

Full-service, EC2-based

  • MongoHQ
  • MongoLab

EC2 Your Own

  • Hosting advice
  • If you're gonna shard, shard big.
  • Extra-large at least.

Real hardware

  • Sometimes you need the big guns.
  • Can be a better value than EC2.

THE TIPS

(That Thou Mayest Not Shoot Thyself in the Foot)

Do NOT get crazy with massive documents.

  • Watch our for deep denormalization.
  • Documents larger than 20KB == schema smell.
  • Create separate collections instead.

Do NOT grow documents indefinitely.

  • The $push operator can be overused.
  • Normalize a bit more.

Do NOT send reads to secondary nodes by default.

  • Premature optimization...
  • Do this only if your primary can't handle the read load and your app is decidedly read-heavy.

Do use journaling.

  • Unless there's a compelling reason not to (e.g., can't get the performance you need).

Do use replication.

  • It may save your (job | life | sanity) someday.

Do use separate instances.

  • For write-heavy, orthogonal parts of the application.
  • If you're doing lots of application analytics, writing this to a separate machine will help performance.

Do get some help on schema design.

  • There are videos.
  • There are books.
  • There's a forum.

Do monitor your deployment:

  • MMS
  • Nagios
  • mongostat

THE AWESOME

Great support.

  • MongoDB User
  • Stack Overflow
  • 10gen support

Great learning resources.

MongoDB v2.0

  • Journaling faster and enabled by default
  • Indexes 25% smaller (and faster)
  • Spatial polygon search

MongoDB v2.0 (cont.)

THE ANNOUNCE

There's a book for that!

  • MongoDB in Action (Oct. 28th) (!)
  • Use 'mongokyle40' at checkout at manning.com for 40% off!

10gen's hiring!

These slides are at:

QUESTIONS && THANK YOU

RESOURCES

Production deplyoments:

Learning more:

Good articles / presentations

ODMs

OSS:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment