Routing Requests to Shards
What you'll learn: How distributed systems direct client queries to the correct shard holding the requested data.
The Core Problem
Once you've partitioned data across multiple shards (as covered in earlier lessons), you face a new challenge: when a client sends a request, how does the system know which shard contains the data? The system must route each query to the appropriate node without scanning every shard.
Three Primary Routing Patterns
Client-Side Routing
The client itself knows the partitioning scheme and calculates which shard to contact directly. Think of it like having a detailed map: the client uses the partition key (hash-based, range-based, etc.) to determine the destination and connects straight to that shard.
Pros: Lowest latency (no intermediary hops), simple infrastructure.
Cons: Clients must stay updated about cluster topology changes; logic duplication across clients.
Proxy Routing
All requests flow through a dedicated routing layer (proxy/load balancer) that knows the partitioning scheme. The proxy acts like a knowledgeable receptionist—it examines each request and forwards it to the correct shard.
Pros: Clients remain simple and ignorant of topology; centralized routing logic.
Cons: Added network hop; the proxy becomes a potential bottleneck or single point of failure.
Coordinator Pattern (Gossip-Based)
Clients contact any node in the cluster. If that node doesn't own the data, it either forwards the request or redirects the client to the correct shard. Nodes share topology information through gossip protocols, so every node maintains an eventually-consistent view of shard locations.
Pros: High availability (no single routing point); clients can contact any node.
Cons: Possible extra hops; complexity in maintaining distributed routing metadata.
Key Takeaway: Routing requests to shards requires choosing between client intelligence (client-side), centralized routing infrastructure (proxy), or distributed coordination (gossip-based), each trading off complexity, latency, and availability differently.