Junior — Middle
How do you implement data distribution strategy when sharding?
sobes.tech AI
Answer from AI
Data sharding strategy determines how data is partitioned and distributed across different shards (parts of a database). Main approaches:
-
Horizontal sharding by key (hash-based sharding) — a hash of the key value (e.g., userId) is computed, and the shard is selected based on the result.
-
Range-based sharding — data is distributed across key ranges, for example, users with IDs from 1 to 1000 in one shard, from 1001 to 2000 in another.
-
Geographical or business logic-based sharding — data is distributed based on regions or other criteria.
Implementation usually includes:
- Defining the sharding key.
- Routing logic for requests to the appropriate shard server.
- Ensuring consistency and load balancing.
Example in Java (simplified):
int shardCount = 4;
int shardId = Math.abs(userId.hashCode()) % shardCount;
// Then the request is directed to the database with index shardId
The choice of strategy depends on the nature of the data and performance requirements.