Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Kotlin Design Patterns and Best Practices

You're reading from   Kotlin Design Patterns and Best Practices Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Arrow left icon
Product type Paperback
Published in Apr 2024
Publisher Packt
ISBN-13 9781805127765
Length 474 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Alexey Soshin Alexey Soshin
Author Profile Icon Alexey Soshin
Alexey Soshin
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Section 1: Classical Patterns FREE CHAPTER
2. Getting Started with Kotlin 3. Working with Creational Patterns 4. Understanding Structural Patterns 5. Getting Familiar with Behavioral Patterns 6. Section 2: Reactive and Concurrent Patterns
7. Introducing Functional Programming 8. Threads and Coroutines 9. Controlling the Data Flow 10. Designing for Concurrency 11. Section 3: Practical Application of Design Patterns
12. Idioms and Anti-Patterns 13. Practical Functional Programming with Arrow 14. Concurrent Microservices with Ktor 15. Reactive Microservices with Vert.x 16. Assessments
17. Other Book You May Enjoy
18. Index

Fan-Out

The purpose of the Fan-Out design pattern is to divide the workload among multiple concurrent processors, or workers, efficiently. To grasp this concept better, let’s revisit the previous section but consider a specific problem: what if there’s a significant disparity in the amount of work at different stages in our pipeline?

For instance, fetching HTML content might take much longer than parsing it. In such cases, it makes sense to distribute the heavy lifting across multiple coroutines. In the previous example, each channel had only one coroutine reading from it. However, it’s possible for multiple coroutines to consume from a single channel, effectively sharing the workload.

To simplify the problem we’re about to discuss, let’s assume we have only one coroutine producing some results:

fun CoroutineScope.generateWork() = produce {
    for (i in 1..10_000) {
        send("page$i")
    }
    close()
}

And we’ll create a function that generates a new coroutine responsible for reading those results:

fun CoroutineScope.doWork(
    id: Int,
    channel: ReceiveChannel<String>
) = launch(Dispatchers.Default) {
    for (p in channel) {
        println("Worker $id processed $p")
    }
}

This function generates a coroutine that runs on the default dispatcher. Each coroutine listens to a channel and prints every message it receives to the console.

Now, let’s kick off our producer. Keep in mind that all the following code pieces should be wrapped in the runBlocking or a suspend main function, but for simplicity, we’ve omitted that part:

val workChannel = generateWork()

Next, we can create multiple workers that collaborate to distribute the work among themselves by reading from the same channel:

val workers = List(10) { id ->
    doWork(id, workChannel)
}

Now, let’s examine a portion of the program’s output:

... 
> Worker 4 processed page9994 
> Worker 8 processed page9993 
> Worker 3 processed page9992 
> Worker 6 processed page9987

Note that no two workers receive the same message, and the messages are not printed in the order they were sent.

Load balancing is a critical aspect of the Fan-Out design pattern. It ensures that the workload is evenly and efficiently distributed across the available resources, preventing situations where some workers are overloaded while others remain underutilized.

Kotlin channels inherently provide a level of fairness in load balancing. When multiple consumer coroutines are waiting to receive data from a channel, the channel distributes data fairly among them. Each consumer gets an opportunity to receive data in a round-robin fashion. This ensures that no single consumer is starved while others receive all the data.

Channels also offer mechanisms for backpressure handling. When data is produced faster than it can be consumed, channels can suspend producers until consumers are ready. This helps prevent overloading the system.

The Fan-Out design pattern enables efficient distribution of work across a number of coroutines, threads, and CPUs.

Next, we’ll take a look at associated design pattern that often complements Fan-Out.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime