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,130 | Comments: 45,558

filter by tags archive

RavenDBReplicating to a relational database

time to read 2 min | 369 words

I just finished implementing a very cool feature for RavenDB, the Index Replication bundle allows you to replicate an index to a relational database.

What does this mean? Well, consider the following document:

var q = new Question
     Title = "How to replicate to SQL Server?",
     Votes = new[]
         new Vote{ Up = true, Comment = "Good!"}, 
         new Vote{ Up = false, Comment = "Nah!"}, 
         new Vote{ Up = true, Comment = "Nice..."}, 

And this index:

from q in docs.Questions
select new 
         Title = q.Title, 
         VoteCount = q.Votes.Count

With the aid of the Index Replication bundle, that index will be replicated to a relational database, giving us:


You can find full documentation for this feature here and the bundle itself is part of RavenDB’s unstable as of build 159.

More posts in "RavenDB" series:

  1. (25 May 2016) Got anything to declare, ya smuggler?
  2. (23 May 2016) I'm no longer conflicted about this
  3. (19 May 2016) What did you subscribe to again?
  4. (17 May 2016) See here, I got a contract, I say!
  5. (13 May 2016) Deeper insights to indexing
  6. (11 May 2016) Digging deep into the internals
  7. (09 May 2016) I'll have the 3+1 goodies to go, please
  8. (04 May 2016) I’ll find who is taking my I/O bandwidth and they SHALL pay
  9. (02 May 2016) You want all the data, you can’t handle all the data
  10. (29 Apr 2016) A large cluster goes into a bar and order N^2 drinks
  11. (27 Apr 2016) I’m the admin, and I got the POWER
  12. (25 Apr 2016) Can you spare me a server?
  13. (21 Apr 2016) Configuring once is best done after testing twice
  14. (19 Apr 2016) Is this a cluster in your pocket AND you are happy to see me?



Very cool feature!

You have a type in the documentation:

"You can directly replicate between a document and a RDBMS table, because the format of a document simply cannot be made to work on a relational database."

Should probably be "You can't ..."

Jeff Handley

Nice! I'm getting closer and closer to wanting to use RavenDB. :-)

Khalida Abuhakmeh

When does the replication happen?

  • is it triggered with every new document that is added to that index ?

  • is it on demand (press this button here) ?

  • is it a timed job ?

When I first heard about this i thought "Why would I want my index definition stored in a database?" Now i understand you meant the index result set is replicated into the database.

This is a great bundle for teams who have members more comfortable doing reporting with SQL.



This is very very useful as it makes it very easy to do mixed data storage with sql server still in use. This will make migration a lot easier too. I think you should advertise this feature a bit to see if it might be a killer one.

You could also try to replicate json trees as xml for which sql server has great support.

Ayende Rahien

Configurator, thanks, I fixed that.

Ayende Rahien


Xml fields in SQL Server are... workable, but they don't really work nice with reporting tools.


Another idea would be to replicate from sql server to ravendb as a means of having stacked materialized views (which sql server does not support yet). You can implement this in a number of different (complex) ways: CDC, replication infrastructure, triggers, change notification/sqldependency class.

I was proposing xml fields as a means of making sql server behave more like a document storage. I am sure there are cases where the performance benefit justifies the complexity.

Ayende Rahien


I would be pretty trivial to do the reverse using triggers, but I am not sure what it would give you.

What do you mean, stacked materialized views?


Materialized views can only reference tables, not other materialized views. So you can only get one level deep. It could be very convenient to have nested materialized views to build up more complex structures. I have encountered the need multiple times (I wanted to do two level aggregation and the other time I wanted to express exists queries through joins and groupings over multiple levels).

nested materialized views = stacked materialized views.


Also I would have liked to pre-materialize a xml/json document that describes every piece of data needed for a particular url. This is not possible using sql server indexed views but would be quit a killer. Image you could stitch all information required for a whole forum thread (thread-info, posts, users, their reputation and badges, ...) up into one json document by creating a tree of raven indexes that ultimately result in a table with pk (threadid) and a json document containing all required data. this would allow you to write normalized data in sql server (convenient and immune to data anomalies) and read in a document-oriented way from raven (also convenient and very fast). not sure if this is possible today as I believe raven indexes cannot join or build tree structures but I hope you see the use case. Low dev costs and blinding performance. certainly a complex feature set to implement.

Ayende Rahien


You can certainly do it today using Raven.

Comment preview

Comments have been closed on this topic.


  1. How to waste CPU and kill your disk by scaling 100 million inefficiently - 9 hours from now
  2. RavenDB Conference 2016–Slides - about one day from now

There are posts all the way to Jun 01, 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