Maintaining foreign identifier mapping in RavenDB

time to read 2 min | 333 words

A fairly common question that gets ask is how to maintain identifiers between RavenDB and a secondary system. For this example, we will assume that we have a table in SQL Server that have a list of users. In SQL Server, we use a identity to identify the users. But in RavenDB we want to use different ids (maybe we want to have ids that are created only in RavenDB).

That means that we have something like:

image

As you can see, users 1 – 2 map to the same id in the RDBMS system, but users/3 was created in the RavenDB system, and the other users are on different ids.

The question now becomes, how do you keep track of that, especially with regards to having updates later on. The easiest way to go about it would be to just keep the RDMBS id in the document, and query on that. That means that you need to query, and that subject to RavenDB BASE queries. So we want to try to do something better.

We can just keep a document reference, so we could do something like Load(“mapping/3”) –> “users/4”. This means that we keep a document with the RDBMS  id that just points to the actual user’s document. That works, but that isn’t very optimal. Mostly because you will need to do a lot of requests (or load a lot of documents) to get that working nicely in bulk scenarios.

But that isn’t the only option. Instead, you are going to use a document per 1000 ids. That would be called something like: “mappings/1-1000”, “mappings/1001-2000”, etc. The idea here is that we can load a single document, and get a thousand mapping all at once. Since we will usually be processing those values in order, this gives us a cheap way to preload stuff.