Differences between bounded and unbounded channels

I would like to know what are the differences between bounded and unbounded variants of MPSC in tokio or crossbeam.
As documentation mentioned in the unbounded version, it can hold any number of messages at a time.
So why we should use the bounded version which has limitations on holding messages.

1 Like

It gives back-pressure. When the receiver has too much work stop/delay giving it more.

5 Likes

Bounded version is usually much more performant. Unbounded version must either use a growable container like Vec and lock on every send-receive operation (since the container may suddenly reallocate and invalidate concurrent operations), or use a linked list, which kills the CPU cache and adds a lot of wasteful indirection. Bounded versions, on the other hand, can be implemented as a ring-buffer or similar compact structure, and both the library author and the compiler can optimize it a lot.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.