How to merge multiple asynchronous sequences without left-side bias?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you merge asynchronous sequences, a naive implementation often favors whichever source is checked first. That left-side bias can increase latency for later sources and make the merged output unfair under load. A better design waits for whichever source becomes ready first and rotates fairly when several sources are simultaneously available.
Why Left-Side Bias Happens
Bias usually appears when code polls sources in a fixed order. If source A is always checked before source B, then A can dominate the merged stream whenever both have data available.
A toy Python example shows the problem conceptually.
This is not truly asynchronous, but it shows the bias clearly: the left side wins until it is empty.
In real async code, the equivalent mistake is awaiting sources in a fixed sequence instead of responding to whichever one becomes ready.
Merge By Waiting For Ready Producers
A fairer pattern is to have each producer publish items into a shared queue as soon as they are ready. The consumer then reads from that queue in arrival order instead of polling producers left to right.
This design is not biased toward the leftmost producer. Items are processed when they become available.
Adding Fairness When Several Sources Are Ready
Arrival order removes fixed left-side polling bias, but if multiple sources can enqueue aggressively, you may still want explicit fairness. A common strategy is round-robin draining: once you receive one item from a source, give other ready sources a chance before taking another from the same source.
The simplest architecture is to keep one queue per source and rotate across queues.
This is a small example, but the principle scales: separate readiness from fairness policy.
Design Guidance
If your sources represent genuine event streams, preserving actual arrival order is often the right behavior. If your problem is more like work scheduling, a round-robin or weighted policy may be better.
The important point is to avoid serially awaiting source A, then source B, then source C in a loop. That structure bakes bias into the merge algorithm.
Common Pitfalls
A common mistake is awaiting each async iterator in declaration order. That makes later sources wait even when they already have items ready.
Another mistake is trying to solve fairness only after values have already been merged through a biased poller. At that point, the damage is already done.
Developers also sometimes confuse throughput with fairness. A highly active source may still produce more items overall without the algorithm being biased. Fairness means every ready source gets a chance, not that all sources emit identical counts.
Finally, if you use queues, make sure completion signaling is explicit. Without an end-of-stream marker or task tracking, the consumer may wait forever.
Summary
- Left-side bias comes from checking or awaiting sources in a fixed order.
- A shared queue removes fixed polling bias by consuming items as they arrive.
- Per-source queues plus round-robin logic add stronger fairness when several sources are ready.
- Keep arrival policy and fairness policy separate in the design.
- Explicit completion signaling is necessary in queue-based merges.
- Test the merge under uneven producer speeds to confirm that no source is starved.
Related reading
- How to Multi-thread an Operation Within a Loop in Python
- How to multithread C code
- How to notify user when async task ends in PHP/ Windows
- How to obtain a Thread id in Python?
- How to obtain a Thread id in Python?
- How to obtain JNI interface pointer JNIEnv for asynchronous calls
- How to parallelize a training loop ever samples of a batch when CPU is only available in pytorch?
- How to parallelize stdpartition using TBB
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.