Kotlin Academy · Lesson

Channel Types: Rendezvous, Buffered, Conflated, Unlimited

Choose the right channel type for your communication pattern.

Lesson 2 of 413 steps

Channel Types: Rendezvous, Buffered, Conflated, Unlimited is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

Channel Factory

Create channels with Channel<T>(capacity). The capacity determines buffering and suspension behavior.

import kotlinx.coroutines.channels.Channel
val rendezvous = Channel<Int>()              // capacity 0
val buffered   = Channel<Int>(64)            // capacity 64
val unlimited  = Channel<Int>(Channel.UNLIMITED)
val conflated  = Channel<Int>(Channel.CONFLATED)
fun main() { println("Channel types created") }

Rendezvous Channel (capacity=0)

Default channel. send suspends until a receiver is ready, and receive suspends until a sender sends. They rendezvous — meet at the same point.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>() // rendezvous
    launch {
        println("Sending...")
        ch.send(1)  // suspends until receiver is ready
        println("Sent")
    }
    delay(100)
    println("Receiving...")
    println(ch.receive())
}

Buffered Channel

A buffered channel allows the sender to continue without waiting until the buffer is full. When full, send suspends.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>(3) // buffer of 3
    launch {
        repeat(3) {
            println("Sending $it")
            ch.send(it) // does not suspend until buffer full
        }
        ch.close()
    }
    delay(500)
    for (v in ch) println("Got $v")
}

Channel.BUFFERED

Channel.BUFFERED uses the default capacity (64). This is equivalent to Channel(64).

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.*
fun main() = runBlocking {
    val ch = Channel<String>(Channel.BUFFERED)
    println("Default buffer capacity is implementation-defined (~64)")
    ch.close()
}

Unlimited Channel

Channel.UNLIMITED never suspends the sender — it buffers all values. Use with caution: unlimited memory growth possible.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>(Channel.UNLIMITED)
    launch {
        repeat(1000) { ch.send(it) } // never suspends
        ch.close()
    }
    var sum = 0
    for (v in ch) sum += v
    println("Sum: $sum")
}

Conflated Channel

Channel.CONFLATED keeps only the most recent value. If a new value arrives before the previous is received, the old one is dropped.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>(Channel.CONFLATED)
    launch {
        repeat(5) { ch.send(it) } // 0..4 sent rapidly
        ch.close()
    }
    delay(50) // let all sends happen
    println("Received: ${ch.tryReceive().getOrNull()}") // likely 4 (last)
}

RENDEZVOUS vs BUFFERED Timing

Rendezvous synchronizes producer and consumer. Buffered lets the producer run ahead, decoupling timing.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>(Channel.BUFFERED)
    val t = System.currentTimeMillis()
    launch {
        repeat(5) { ch.send(it) } // fast — doesn't wait
        ch.close()
    }
    for (v in ch) { delay(100); println("$v at ${System.currentTimeMillis()-t}ms") }
}

onBufferOverflow Strategy

With Channel(capacity, onBufferOverflow), choose what happens when the buffer is full: SUSPEND (default), DROP_OLDEST, or DROP_LATEST.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ch = Channel<Int>(2, BufferOverflow.DROP_OLDEST)
    launch {
        repeat(5) { ch.trySend(it) } // 0,1,2,3,4 — buffer keeps newest 2
        ch.close()
    }
    for (v in ch) println(v) // likely 3 4
}

Choosing Channel Type

Decision guide: Rendezvous for tight synchronization; Buffered for throughput; Conflated for latest-value-only (like UI state); Unlimited for event logging where loss is unacceptable.

import kotlinx.coroutines.channels.Channel
// Use cases:
// RENDEZVOUS  — handshake between producer and consumer
// BUFFERED    — decouple I/O-heavy producer from slow consumer
// CONFLATED   — UI updates (only latest matters)
// UNLIMITED   — audit log (never drop)
fun main() { println("Choose based on your loss tolerance and timing needs") }

Channel in select

select allows waiting on multiple channels simultaneously, taking the first one ready.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
fun main() = runBlocking {
    val a = produce { delay(100); send("from a") }
    val b = produce { delay(50);  send("from b") }
    val result = select<String> {
        a.onReceive { it }
        b.onReceive { it }
    }
    println(result) // "from b"
    coroutineContext.cancelChildren()
}

Ticker Channel

ticker(delay) creates a channel that emits Unit every delay milliseconds — useful for repeated polling.

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
fun main() = runBlocking {
    val ticker = ticker(delayMillis = 100, initialDelayMillis = 0)
    var count = 0
    for (tick in ticker) {
        println("Tick ${++count}")
        if (count == 3) { ticker.cancel(); break }
    }
}

Quick Check

Which channel type keeps only the most recent value?

Recap

Rendezvous: synchronized, no buffer. Buffered: decouple timing. Conflated: latest value only, drops old. Unlimited: never suspend sender. Choose based on your loss tolerance and throughput needs.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Channel Types: Rendezvous, Buffered, Conflated, Unlimited” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Channel Types: Rendezvous, Buffered, Conflated, Unlimited”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Channel Types: Rendezvous, Buffered, Conflated, Unlimited”?

Choose the right channel type for your communication pattern. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Channel Types: Rendezvous, Buffered, Conflated, Unlimited” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Channel Basics: send, receive, and close
  2. Channel Types: Rendezvous, Buffered, Conflated, Unlimited
  3. Mutex and Semaphore for Shared State
  4. Actors and Structured State Management
← Back to Kotlin Academy