It's a girl

In my previous post, I introduced RavenDB Sharding and discussed how we can use Blind Sharding to a good effect. I also mentioned that this approach is somewhat lacking, because we don’t have enough information at hand to be able to really understand what is going on. Let me show you how we can define a proper sharding function that shards your documents based on their actual data.

What we have done is given RavenDB the information about how our entities are structured and how we should shard them. We should shard the companies based on their regions, and the invoices based on their company id.

Let us see how the code behaves now, shall we? As before, we will analyze the output of the HTTP logs from execute this code. Here is the first server output:

As before, we can see the first four request are there to handle the hilo generation, and they are only there for the first server.

The 5th request is saving two documents. Note that this is the Asia server, and unlike the previous example, we don’t get companies/1 and invoices/1 in the first shard.

Instead, we have companies/1 and invoice/2. Why is that? Well, RavenDB detected that invoices/2 belongs to a company that is associated with this shard, so it placed it in the same shard. This ensures that we have good locality and that we can utilize features such as Includes or Live Projections even when using sharding.

Another interesting aspect is that we don’t see a request for companies in the America region. Because this is what we shard on, RavenDB was able to figure out that there is no way that we will have a company in the America region in the Asisa shard, so we can skip this call.

Conversely, when we need to find an invoice for an asian company, we can see that this request gets routed to the proper shard.

Exciting, isn’t it?

Let us see what we have in the other two shards.

In the second shard, we can see that we have just two requests, one to save two documents (again, a company and its associated invoice) and the second to load a particular company by id.

We were able to optimize all the other queries away, because we actually understand the data that you save.

And here is the final shard results:

Again, we got a save for the two documents, and then we can see that we routed the appropriate query to this shard, because this is the only place that can answer this question.

Data Driven Sharding For The Win!

But so far we have seen how RavenDB can optimize the queries made to the shards when it has enough information to do so. But what happens when it can’t?

For example, let us say that I want to get the 2 highest value invoices. Since I didn’t specify a region, what would RavenDB do? Let us look at the code:

So we were actually able to get just the two highest invoices. But what actually happened?

Shard 1 (Asia):

Shard 2 (Middle-East):

Shard 3 (America):

As you can see, we have actually made 3 queries, asking the same question from each of the shards. Each shard returned its own results. On the client side, we merged those results, and gave you back exactly the information that requested, across the entire cluster.

Morcs,
It does not, that is all the beauty in it :-)
What actually happens is that we use conventions to figure out that this is likely to be a reference, and then we figure out what the relevant shard is for the company.

Ayende, I may have missed something, but yesterday the name of the shard didn't seem important. Does it only become important once you specify the strategy? And then how does it now the name relates to Company.Region not Invoice.CompanyId?

What happens if we query for, say, the third page with pageSize = 10?
Clearly the client can't ask each shard for just the third page, as the first item in shard B can be on page three when merging with shard A - naively, we'd have to query for 30 items from each shard and do the paging locally.

configurator, I was asking the same question yesterday because I'm quite curious how far does RavenDB client go to hide the sharding from the user
BTW, is anyone doing sharding with RavenDB in a real application?

Simon,
In this case, I am using shard names with a meaning, yes.
You can also decide that you want to shard by region, but the shard names are meaningless, therefor you use a transformation between the region to the shard name as well.

Configurator & Rafal,
Right now, we support the first page natively, including sorting and merging the results.
But we don't support deeper than that, because of this issue. Consider the case when we have 25 results per page, and we want to go to page 3, we would have to query for 225 results, to show just 25, and it gets worse quite fast.
We decided that paged sharded queries are something that we are going to leave out of it, because we don't have a way to provide enough information there to decide, and it is better if the user run into those issues ASAP, rather than run into them at a large data set.

We don't consider this much of a problem, because RavenDB's sharding encourage a strong locality of reference, so most of the time, sharded queries will hit only a single server.

"We decided that paged sharded queries are something that we are going to leave out of it"

So what would you do if you wanted to provide a list of EVERY Company? Need to call all 3 document stores manually and deal with paging manually that after you reach the end of 1 store, to goto the next store and not support any type of sorting?

dotnetchris,
In that case, we have an extension point for you to plug things in, but in a sharded env, that is decidedly non trivial.
What is more likely is that we will show you a page with lists from each shard, which is much easier and more effective.

Consider the case when you have 10,000 companies, and you want to go to page 5 in the overall list, what is the total cost?
That is why we don't try, because that would be hiding something very important.

In the "Europe" case, doesn't the fact that it throws violate the "safe by default" premise? This means that I can't just run what would be an otherwise normal data creation and save without knowing the sharding strategy. Shouldn't it just pick a shard and succeed? I fully recognize that this is a terribly difficult problem if you want to be able the handle changes after the fact, but at face value it seems to go against one of your core beliefs.

Jack,
In this example, I intentionally picked something that you can relate to. Most of the time, you would do data driven sharding to a set of shards that aren't really pre-selected.
In those cases, yes, you won't have the "shard does not exists" issue, but for our purposes in this blog post, having a European customer means that we need a European data center, so we fail.