The Problem: Uneven Distribution Kills Parallelism
Scaling data crawls involves distributing work across multiple workers. The common strategy is to split the keyspace into N ranges, assign each range to a worker, and let them run in parallel. This approach, however, crumbles when data isn't uniformly distributed. If prefixes are dense, one worker might get a disproportionately large chunk of the work, while others finish quickly and sit idle. This imbalance means the entire crawl speed is dictated by the slowest worker, effectively negating the benefits of parallelization. In one real-world scenario, a single shard was assigned 71% of the total work, turning a planned 56-way fan-out into a crawl barely faster than a 3-way one.
The core issue isn't about dividing the alphabet into equal widths. It's about ensuring each worker handles an equal item count. Achieving this balance requires knowing precisely which key sits at a specific global position within the entire sorted dataset.

The Solution: Leverage Existing Ranking
The breakthrough comes from recognizing that many public registries, the target of the crawl, already provide a mechanism to rank their keys. This means the problem of finding the key at position P can be solved by querying the registry itself. The author developed a concise, ten-line binary search algorithm that leverages this existing ranking capability.
The algorithm works by first determining the total number of items. Let's say this is 4 million. The goal is to divide this into N equal chunks, where N is the number of workers. For instance, with 56 workers, each should ideally handle 4,000,000 / 56 = 71,428 items. The binary search doesn't operate on the keys themselves, but on the rank of those keys. It starts by picking a random key and checking its rank. If the rank is too high, it means the chosen key is too far down the sorted list, and the search space is narrowed to keys with lower ranks. Conversely, if the rank is too low, the search space is adjusted to keys with higher ranks. This process iteratively refines the search until it pinpoints the key that corresponds to the desired rank threshold for each shard.
Consider the example of finding the key that marks the boundary for the first shard (rank 71,428). The binary search would repeatedly guess a key, check its global rank, and adjust its guess. If the guessed key has a rank of 100,000, it's too high, so the algorithm searches for keys with ranks less than 100,000. If the next guess has a rank of 50,000, it's too low, so the search shifts to ranks between 50,000 and 100,000. This continues until the key with the exact rank of 71,428 is found. This key then defines the upper bound for the first shard and the lower bound for the second.
The beauty of this ten-line solution lies in its simplicity and efficiency. It avoids the need for complex pre-processing or external indexing. By utilizing the registry's inherent ranking, it directly addresses the uneven distribution problem. The result is a balanced workload across all workers, maximizing the efficiency of the parallel crawl. The author successfully used this technique to crawl approximately 4 million items within a single day, a feat that would have been significantly slower or impossible with a naive sharding approach.
Implementation Details and Benefits
The implementation is remarkably compact. The core logic relies on a standard binary search pattern, but instead of searching for a value in an array, it searches for a rank. The essential components are:
- A way to get the total count of items.
- A function to get the rank of a given key.
- The binary search loop itself, which narrows down the key space based on rank comparisons.
The benefits are substantial:
- Maximized Parallelism: Ensures all workers are utilized efficiently, drastically reducing crawl time.
- Simplicity: The ten-line code is easy to understand, implement, and maintain.
- No External Dependencies: Relies only on the public registry's API for ranking information.
- Scalability: Effectively handles large datasets with non-uniform distributions.
This technique is not limited to public registries. Any system that provides a way to determine the rank of an item or the item at a specific rank can benefit from this approach. It's a powerful reminder that sometimes the most elegant solutions are the simplest, hiding in plain sight by leveraging existing system capabilities.
What remains interesting is the potential for this pattern to be applied to other distributed task scheduling problems where data skew is a known issue. If a system can provide a global ordering or ranking, even implicitly, a similar binary search on rank could equalize workloads. The author's concise implementation provides a clear blueprint for developers facing similar challenges in large-scale data processing and crawling operations.
