DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Data Driven Sharding with RavenDB

Data Driven Sharding with RavenDB

Oren Eini user avatar by
Oren Eini
·
Mar. 23, 12 · Interview
Like (0)
Save
Tweet
Share
3.82K Views

Join the DZone community and get the full member experience.

Join For Free

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.

we are still going to run the exact same code as we have done before:

string asianid, middleeasternid, americanid;

using (var session = documentstore.opensession())
{
    var asian = new company { name = "company 1", region = "asia" };
    session.store(asian);
    var middleeastern = new company { name = "company 2", region = "middle-east" };
    session.store(middleeastern);
    var american = new company { name = "company 3", region = "america" };
    session.store(american);

    asianid = asian.id;
    americanid = american.id;
    middleeasternid = middleeastern.id;

    session.store(new invoice { companyid = american.id, amount = 3 });
    session.store(new invoice { companyid = asian.id, amount = 5 });
    session.store(new invoice { companyid = middleeastern.id, amount = 12 });
    session.savechanges();

}

using (var session = documentstore.opensession())
{
    session.query<company>()
        .where(x => x.region == "america")
        .tolist();

    session.load<company>(middleeasternid);

    session.query<invoice>()
        .where(x => x.companyid == asianid)
        .tolist();
}

what is different now is how we initialize the document store:

image

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:

image

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.

image

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:

image

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:

var topinvoices = session.query<invoice>()
    .orderbydescending(x => x.amount)
    .take(2)
    .tolist();

foreach (var invoice in topinvoices)
{
    console.writeline("{0}\t{1}", invoice.amount, invoice.companyid);
}

this code outputs:

image

so we were actually able to get just the two highest invoices. but what actually happened ?

shard 1 (asia):

image

shard 2 (middle-east):

image

shard 3 (america):

image

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.

Shard (database architecture) Data (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • HTTP vs Messaging for Microservices Communications
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: