Breaking performance barriers with MongoDB

A.K.A How to convince Drupal to love MongoDB

Janez Urevc / @slashrsm

Photo by Dan McCreary - http://www.flickr.com/photos/25086260

MongoDB?

  • document-oriented storage
  • free software - GNU AGPL v3.0 (DBMS), Apache (drivers)
  • can store complex data structures
  • stores data as JSON documents
  • schemaless
  • MongoDB company (professional support, training, ...)

> show dbs;
local	0.078125GB
> use drupal;
> show collections;
> db.awesomestuff.insert({"a":1, "b":2});
> db.awesomestuff.find();
{"_id":ObjectId("526965cd93259605e9afbc41"), "a":1, "b":2}
> show collections;
system.indexes
awesomestuff
> show dbs;
drupal 0.203125GB
local 0.078125GB
            

> db.awesomestuff.insert({
  "a": [1, 2, 3], "b":"some string"
});
> db.awesomestuff.insert({
  "a": [[1,2,4], [4,5,6], [7,8,9]]
});
> db.awesomestuff.insert({
  "a": {
    "name" : "Dries",
    "interests" : ["Drupal", "MongoDB"]
  }
);
            

What else?

  • no joins
  • no transactions (there are atomic update operations)
  • allows you to index on anything
  • seamless replication and scaling (sharding)
  • capped collections
  • geospatial indexes
  • aggregation and MapReduce frameworks
MySQL MongoDB
database database
table collection
row document
column field
index index
joins embedded documents and linking

persistent, easy to set-up, fast ("fire and forget"), automatic garbage collection

Photo by http://www.webdesignerdepot.com/2009/03/40-comics-strips-for-web-designers/

easy to set-up, fast ("fire and forget"), very good for multi-server envs, uses capped collections

Photo by Which? Campaigns - http://www.flickr.com/photos/71528417@N02/6709386951

easy to set-up, per-queue-collection

Photo by Darren Tunnicliff - http://www.flickr.com/photos/25572591@N07/3717976312

a bit funky :), stores blocks configuration, exposes UI

Photo by capitaine_commerce - http://www.flickr.com/photos/8488292@N04/1807465667

stores entites /w fields as Mongo docs, still needs base entity table, lazy destruction of stale data, new fields added very easily

Photo by Aleksej Leonov - http://www.flickr.com/photos/31849470@N02/7652495954

{
  "_id" : 60549286,
  "_type" : "node",
  "_bundle" : "story",
  "_revision_id" : 150412786,
  "nid" : 60549286,
  "vid" : 150412786,
  "type" : "story",
  "language" : "und",
  "title" : "Hello world!",
  "uid" : 2044271,
  "status" : 1,
  "created" : 1364855903,
  "changed" : 1364855940,
  "comment" : 2,
  "promote" : 1,
  "sticky" : 0,
  "field_image" : {
    "fid" : 47928616,
    "width" : 594,
    "height" : 396,
    "alt" : "",
    "title" : ""
  },
  "body" : {
    "value" : "

MongoDB is great!

" }, "field_tags" : [ { "tid" : 14244596 }, { "tid" : 14244596 } ] }

Other stuff

  • locking backend
  • sessions storage
  • Entity (field) query

Conclusion

  • you cannot completely run away from MySQL (Yet! - Drupal 8!!)
  • you can use mongo to achieve performance improvements
  • Views can be used with EFQ query plugin
  • you need to know why you're going to use MongoDB

Want more?

Google Code-in

We need tasks and mentors. Please help!

www.google-melange.com/gci/homepage/google/gci2013 | @slurpee on #drupal-gci

Thank you!

Questions?

Janez Urevc / @slashrsm