Ayende @ Rahien

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

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,970 | Comments: 44,492

filter by tags archive

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.


Comments

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

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

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.

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

What happened to previous post? RavenDB exciting new features

Lee
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

Lee, Build 2192 is the one you want.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB On Linux–Status Update - 10 hours from now

There are posts all the way to Jul 31, 2015

RECENT SERIES

  1. Production postmortem (5):
    29 Jul 2015 - The evil licensing code
  2. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
  3. API Design (7):
    20 Jul 2015 - We’ll let the users sort it out
  4. What is new in RavenDB 3.5 (3):
    15 Jul 2015 - Exploring data in the dark
  5. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats