In this tutorial we shall look at what is data sharding, why it is important and complexities involved in data sharding.
Before going to data sharding, let us understand Single storage and why we moved to data sharding.
Before introduction of distributed systems, we had a single master storage. where in all the data will be dumped in to single DB and all the application traffic used to refer to that database.
But as the application traffic increased, and usually we have more reads than writes, it made more sense to add more database to accommodate more reads. Hence we do read replication. In this model, we take two to three copy of the original database and redirect the traffic to those database. Usually in this model, the write will go the master (original database) and then the latest data will be copied to other database.
But in the above model, we have solved read delay but we introduced one potential issue. The issue is consistancy, how to make use the read is the latest write? So how do we solve this?
We solve the above consistency issue by Data Sharding.
Data sharding is a way to break a large database into smaller DB’s, in such a way that we maintain consistency in the database.
How do we do this?
Data sharding can be done in a very simple way. Consider we have 3 servers. We break the key into 3 different parts and assign each set of keys to different database.
For example we have keys from A to Z. we divide the keys into 3 parts, and assign to the serevers below:
Server Key
Server Key
Server 1 A to F
Server 2 G to N
Server 3 O to z
Hence we shard the data into multiple server, there by increasing the consistancy. And each one of the dataases will have their own replicated cluster.
Above method of Data Sharding is called as Index based partitioning .
We can take this to the next level with the help of Consistent Hashing.