Spread objects evenly over multiple collections
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spreading objects evenly across multiple collections sounds simple until you decide what "evenly" means. If every object has equal weight and the collections are equivalent, round-robin is usually enough. If objects have different weights or the collections already contain different loads, you need a load-aware strategy instead.
Start by Defining the Objective
There are at least three common versions of the problem:
- equal number of objects in each collection
- equal total weight across collections
- stable distribution that changes minimally when collections are added or removed
The algorithm depends on which of these you actually want.
Equal Count: Round Robin
If all objects are interchangeable and the goal is just balanced counts, round-robin is the simplest correct answer.
This guarantees that bucket sizes differ by at most one.
That is hard to beat when the requirement is purely count-based balance.
Unequal Weights: Greedy Load Balancing
If objects have different costs or sizes, equal counts may still produce an uneven workload. Then a better heuristic is to sort heavier objects first and always place the next object into the currently lightest collection.
This is a greedy approximation to a partitioning problem. It is not always globally optimal, but it is practical and often very good.
Stable Distribution for Dynamic Systems
If the number of collections changes over time, such as adding or removing servers or shards, minimizing reshuffling can matter more than perfect balance. That is where consistent hashing becomes relevant.
Consistent hashing is not primarily about exact evenness for a small static set. It is about controlled redistribution when the set of target collections changes.
So a practical selection rule is:
- round robin for equal-count spread
- greedy min-load assignment for weighted spread
- consistent hashing for dynamic membership and low reshuffle cost
A Useful Implementation Detail
If you are assigning objects repeatedly over time, keep track of current collection sizes or loads instead of recalculating them from scratch each time. A min-heap or priority queue is ideal when you repeatedly need the least-loaded target.
That makes load-aware distribution efficient even for large batches.
What Not to Overcomplicate
A common mistake is using consistent hashing when the real problem is a one-time even split across a fixed number of collections. Another is using round robin when object sizes differ dramatically.
The simplest correct algorithm is usually the best one.
Common Pitfalls
Confusing equal count with equal workload is the biggest design mistake. Those are different objectives.
Using a weighted algorithm without sorting heavy objects first often produces worse balance than necessary.
Ignoring dynamic collection membership leads to expensive reshuffling later if the system grows.
Finally, if assignment stability matters, do not choose an algorithm that recomputes completely different placements after every topology change.
Summary
- choose the distribution algorithm based on what "even" actually means in your system
- use round robin when all objects are equivalent and equal counts are enough
- use greedy least-loaded assignment when objects have different weights
- use consistent hashing when collections may be added or removed and reassignment cost matters
- the right solution is usually the simplest algorithm that matches the real balancing objective

