Ayende @ Rahien

It's a girl

The last RavenDB feature of the year, bulk inserts

One of the most exciting new features that got into RavenDB 2.0 is the notion of bulk inserts. Unlike the “do batches in a loop” approach, we actually created an optimized approach and a hand crafted code path that reduce the time of the standard RavenDB saves (which does a lot, but come at a cost).

In particular, we made sure that we can parallelize the operation between the client and the server, so we don’t have to build the entire request in memory on the client and then wait for it all to be in memory on the server before we can start operation. Instead, we have a fully streamed operation from end to end.

Here is what the API looks like:

   1: using (var bulkInsert = store.BulkInsert())
   2: {
   3:     for (int i = 0; i < 1000*1000; i++)
   4:     {
   5:         bulkInsert.Store(new User {Name = "Users #" + i});
   6:     }
   7: }

This uses a single request to the server to do all the work. And here are the results:

image

This API has several limitations:

  • You must provide the id at the client side (in this case, generated via hilo).
  • It can't take part of DTC transactions
  • If you want updates, you need to explicitly state (other would throw).
  • Put triggers will execute, but the AfterCommit will not.
  • This bypass the indexing memory pre fetching layer.
  • Changes() will not be raised for documents inserted using bulk-insert.
  • There isn't a single transaction for the entire operation, rather, this is done in batches and each batch is transactional on its own.

This is explicitly meant to drop a very large number of records to RavenDB very fast, and it does this very well, typically an order of magnitude or more faster than the “batches in a loop”  approach.

A note about the last limitation, though. The whole idea here it to reduce, as much as possible, the costs of actually doing a bulk insert. That means that we can’t keep a transaction of millions of item open. Instead, we periodically flush the transaction buffer throughout the process. Assuming the default batch size of 512 documents, that means that an error in one of those documents will result in the entire batch of 512 being rolled back, but will not roll back previously committed batches.

This is done to reduce transaction log size and to make sure that even during a bulk insert operation, we can index the incoming documents while they are being stream in.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

configurator
12/31/2012 01:04 PM by
configurator

Why use transactional batches at all? Why not insert each document as its own transaction? The reason being dev might see things roll back when they have an error in document #100 and assume everything is always rolled back.

Ayende Rahien
12/31/2012 01:10 PM by
Ayende Rahien

Configurator, Batching things gives us several advantages: a) We can compress multiple documents, which generally result in better perf. b) We can batch several writes to the disk in one go, saving in the number of flushes we have to do.

configurator
12/31/2012 01:14 PM by
configurator

I assumed you'd gzip the entire connection anyway so a wouldn't be relevant. B makes sense though.

Ayende Rahien
12/31/2012 01:23 PM by
Ayende Rahien

Configurator, I am zipping per batch, because that allows to immediately get and process a batch, not wait for all of that to go through.

configurator
12/31/2012 04:55 PM by
configurator

gzipped streams are still streamable - you can read everything that was already flushed by the underlying writer stream (what was actually written, irrespective of Flush() calls) - and allow you to control the size of the batch on the server rather than on the client.

But it's not really important.

Felipe Fujiy Pessoto
12/31/2012 05:18 PM by
Felipe Fujiy Pessoto

What happened to previous post? RavenDB exciting new features

Lee
01/02/2013 03:59 AM by
Lee

I'd like to play with the BulkInsert stuff but can't seem to find the correct build the new feature. Can you point me in the right direction? Thx!

Ayende Rahien
01/02/2013 05:31 AM by
Ayende Rahien

Lee, Build 2192 is the one you want.

Comments have been closed on this topic.