Senior
What is database sharding?
sobes.tech AI
Answer from AI
Database sharding is a method of horizontal scaling where data is partitioned into parts (shards), each stored on a separate server or cluster. This allows distributing the load and increasing performance.
For example, if you have a users table, you can split it by ID ranges or by geographic location, so that requests to different shards are processed in parallel.
An example of simple sharding by ID:
// Example function to select shard by userID
func getShard(userID int) int {
shardCount := 4
return userID % shardCount
}
Thus, requests to the database are directed to the appropriate shard, reducing the load on individual servers and increasing system scalability.