Morphia and Mongo Revisited

Back in May, we took at look at a promising Mongo DAO called Morphia. At the time, we concluded that it may not be ready for prime-time, but as with all things (hopefully) things improve.

We had sometime this week to revisit the results, hook up the newest versions and try again.

Software Upgrade

Our first task was to ensure that we were running with the latest version of the software (aiming for stable, not bleeding edge). We ended up with the following:

Software Original New
Java JRE 1.5 (OS X) 64-bit 1.6 (OS X) 64-bit
Mongo DB 1.5.3-pre 1.6.3
Mongo Java Driver 2.0-rc0 2.2
Morphia 0.9.2 0.9.6

Initial Run

When running the scripts we were originally doing the following (in a single execution):

  • Clear the data
  • Load the Mongo Record Set (via simple Mongo Record)
  • Load the Morphia Record Set (via Morphia POJO Class)

On upgrading we started immediately saw the following stats:

  • 1340 ms / 10,000 for the BasicDBObject
  • 950 ms / 10,000 records for Morphia

Amazing! Morphia has overtaken vanilla Mongo inserts. Of course this started the alarm bells ringing, so we swapped the execution of the inserts to :

  • Clear the data
  • Load the Morphia Record Set (via Morphia POJO Class)
  • Load the Mongo Record Set (via simple Mongo Record)

All of a sudden the performance stats became:

  • 1800 ms / 10,000 records for Morphia
  • 650 ms / 10,000 for the BasicDBObject

So obviously some caching was going on somewhere (even though we were inserting different record ranges).


So to help with the caching issue, we only ran a single insert function per run. This would at least eliminate any caching issue between Mongo's and Morphia's driver.

We were still getting quite high results ( > 900 ms for 10,000 inserts). So we tried a loop of 10 batches of 10,000 inserts in a single run. This helped massively. The vanilla Mongo inserts (after the initialisation and first insert run) dropped to approximately 450 ms / 10,000 inserts.

So the code was changed again, to perform 100 batches of 10,000 inserts within a single run. This would hopefully flatten out the initialisation issue with the drivers, and give us a much more accurate average.

First, let's remind ourselves of the previous results. The Speed is milliseconds, and I've added a new column to show the approximate inserts / second.

Test Records Speed Inserts / Sec
BasicDBObject (Insert) 10,000 877.76 11,402
Morphia (Class with Get/Set) 10,000 2113.11 4,761

So what were the new numbers? These are averaged over 100 runs, each Test (Mongo or Morphia) running separately.

Test Records Speed Inserts / Sec
BasicDBObject (Insert) 10,000 494.44 19532.81
Morphia (Class with Get/Set) 10,000 497.80 19357.45

What an amazing change for the better. With a few code changes for optimisation, Morphia has now made it to within 99.5% of the performance of the base Mongo install. With the additional benefits Morphia brings for code abstraction and custom classes, it's definitely (at least for us) now a serious contender. We'll be looking in the coming weeks how we can use and deploy it to help us proving solutions quicker, with less code, more consistency and faster operation.

So there you have it. It's worth mentioning that one of our great readers left a comment regarding the previous tests. This was related to the fact that Write = Strict was probably turned on within Mongo/Morphia. That's probably true of this release as well, but to be honest we wouldn't be running without that flag at this stage, so it's still a worthwhile test.

Hope you liked the update, and as always, comments are appreciated below, or you can contact us at