Channels & Channel-Backed Flows
KOTLIN › Flow
Coroutine-to-coroutine queues, fan-out and fan-in, and the callbackFlow/channelFlow builders built on them.
Channels are the suspending queue that lets two coroutines talk, and they are also the machinery under callbackFlow and channelFlow. Interviewers probe them to see whether you understand hot versus cold, back-pressure, and when consumers should divide work rather than each see all of it. Expect questions on the four capacity modes, send versus trySend, close versus cancel, and the receiveAsFlow/consumeAsFlow distinction that silently breaks event buses when it is got wrong.
What this covers
- A Channel is a hot, suspending queue: send() suspends when full, receive() suspends when empty
- Four capacity modes: RENDEZVOUS (0), BUFFERED (64), CONFLATED (keeps latest), UNLIMITED
- produce {} is a coroutine builder that returns a ReceiveChannel, closing it on completion
- Fan-out: each element goes to exactly one receiver, giving a load-balanced work queue
- Fan-in: many senders into one channel, closed exactly once by whoever knows all producers finished
- Channels are hot (active regardless of consumers); flows are cold (only run when collected)
- close() lets buffered elements drain; cancel() discards them, and sending after either throws
- trySend never suspends and fails silently on a full channel, which breaks RENDEZVOUS callbackFlow wrappers
- callbackFlow bridges a callback that cannot suspend; channelFlow allows several coroutines to emit concurrently
- consumeAsFlow cancels the channel when collection ends; receiveAsFlow does not, which is why event buses need it
Study this topic
- Channels & Channel-Backed Flows explained: the guided lesson
- 21 practice quiz questions
- Channels & Channel-Backed Flows interview questions and answers