https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

João Rodrigues

09/04/2021, 11:31 PM
Hi! I've been having some trouble wrapping my head around how coroutines work with non-suspending code. For example, I was trying to do some load testing, by emitting a ton of Kafka events and since I was learning about coroutines, I wanted to give it a try. I used the "normal"
KafkaProducer
, and it's
send
method (non-suspending, native java function). If I understood correctly, the coroutines for this purpose should be in the
<http://Dispatchers.IO|Dispatchers.IO>
context, correct? Can the number of emitting coroutines exceed the number of threads of my machine, given that the send function is non-suspending?
j

Jacob

09/05/2021, 2:40 PM
I thought kafkaProducer's send call was already asynchronous using threads. Doesn't really matter which dispatcher you use, the internals of the send will happen on the kafkaProducer thread
You may need to be careful with memory. KafkaProducer's send method blocks when the internal buffer is full. This a safety measure to prevent the applicationCode from overwhelming the producer. If you bring in coroutines, you might be defeating that safety measure and end up leaving so many events waiting to be emitted that you run out of memory
j

João Rodrigues

09/05/2021, 2:58 PM
Doesn't really matter which dispatcher you use, the internals of the send will happen on the kafkaProducer thread
Didn't know about that, makes sense. Thank you! Yea I noticed that in the beginning the Producer was able to keep up but then some throttling came in. Fiddling around with the producer configs helped. I might need several instances of the app running in parallel if I want to achieve higher load
3 Views