Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,131 | Comments: 45,568

filter by tags archive

The last RavenDB feature of the year, bulk inserts

time to read 5 min | 812 words

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:


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.



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

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.


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

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.


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

What happened to previous post? RavenDB exciting new features


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

Lee, Build 2192 is the one you want.

Comment preview

Comments have been closed on this topic.


  1. RavenDB Conference 2016–Slides - 7 hours from now
  2. Proposed solution to the low level interview question - about one day from now

There are posts all the way to Jun 02, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats