When sharding, the user does not know which machine holds their data. How is this resolved?
sobes.tech AI
Answer from AI
When sharding data, the user indeed does not know on which specific server their data is stored. To solve this problem, the following approaches are usually used:
-
Hashing-based sharding (Consistent Hashing) — the client or an intermediate layer computes the hash of the key and determines the required shard based on it. Thus, the client does not need to know the physical server, only the distribution algorithm.
-
Intermediate layer (Proxy, Router) — the client contacts a single point (for example, a proxy server or router), which knows where the data is located and redirects the request to the appropriate shard.
-
Metadata Service — a separate service stores metadata about data location, and the client or service contacts it to find out where to send the request.
-
Client library — the logic for determining the required shard based on the key is implemented in the client library, hiding the details from the user.
Example in Go using hashing to select a shard:
func getShard(key string, shards []string) string {
h := fnv.New32a()
h.Write([]byte(key))
idx := h.Sum32() % uint32(len(shards))
return shards[idx]
}
// shards := []string{"shard1", "shard2", "shard3"}
// shard := getShard("user123", shards)
Thus, the user does not know the physical server, but the system determines it automatically.