Sharding vs. Having Multiple Databases
Join the DZone community and get the full member experience.Join For Free
i was recently at a customer site, and he made a mention of sharding their database in a specific way, putting all of the user generated content on a separate server. i jumped in and told him that this isn’t sharding. and then i had to explain, both to the customer and to myself, why splitting things up in that way bothered me.
the basic idea is the same, we want to split the information over several databases to reduce the load on each individual server, and get higher capacity. we’ll use the flickr model for this post. what the client suggested was:
and what i suggested was:
on the face of it, there isn’t much difference, right? but i really don’t like calling the first example sharding. from my perspective, this is merely splitting the data into multiple databases, it isn’t sharding. i’ll accept arguments about this still being sharding, but it doesn’t feel right from my perspective.
the main problem with the first option is that in order to actually do something interesting, you have to go to three different servers. on the other hand, by selecting a good sharding function, you can usually serve a single request completely from a single database.
that is important , because databases, whatever they are relational or ravendb all include multiple ways for you to gather several pieces of information in an efficient manner. the common part about all of those efficient ways to gather multiple pieces of information? they all break down when you are trying to gather different pieces of information from different servers. that is leaving aside things like connection pooling and persistent connections, which are also quite important.
for example, loading an image with its user’s information and its comments would be the following three queries in the first example:
var image = imagessession.load<image>("images/1"); var user = userssession.load<user>(image.userid) var comments = commentssession.load<imagecomments>(images.commentsid);
whereas with the second example, we will have:
session .include<image>(x=>x.userid) .include(x=>x.commentsid) .load("images/1");
other things, like transactions across the data set, are also drastically simpler when you are storing all of the related data on the same server.
in short, sharding means splitting the dataset, but a good sharding function would make sure that related information is located on the same server (strong locality), since that drastically simply things. a bad sharding function (splitting by type) would create very weak locality, which will drastically impact the system performance down the road.
Opinions expressed by DZone contributors are their own.