Structured Concurrency in Practice: CoroutineScope vs StructuredTaskScope [Part 2]
This post series assumes familiarity with Kotlin, Java, and Spring Boot. No AI was used during the writing of this post series. What about errors? In Part 1 we managed to reduce the load time of ou...
![Structured Concurrency in Practice: CoroutineScope vs StructuredTaskScope [Part 2]](https://media2.dev.to/dynamic/image/width=1200,height=627,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs63rwp8e3rqgxu49jau4.jpg)
Source: DEV Community
This post series assumes familiarity with Kotlin, Java, and Spring Boot. No AI was used during the writing of this post series. What about errors? In Part 1 we managed to reduce the load time of our endpoint from 2s to 1s, but we only covered the happy path. In this part, we'll see what happens when things go wrong. As a reminder, here's our example: @GetMapping("/{id}") fun fetchBook(@PathVariable id: Long): Book = runBlocking(Dispatchers.IO) { log.info("[START] $id") val book = async { findBook(id) } val reviews = async { fetchReviewsFor(id) } book.await() .copy(reviews = reviews.await()) .also { log.info("[SUCCESS] Returning ${it.id}") } } What if we can't fetch the reviews The reviews service is a 3rd party service and we have no control over it. What happens if this service becomes unavailable or starts returning bad results? We can simulate that in our code and see what happens: // application.properties ... reviews-service.fail=true ... And the calling the endpoint: ➜ ~ curl -s