How To Perform Operations on ZIO Fibers

Reading Time: 3 minutes

In this blog post, we will discuss ZIO fibers and how we can use them. We will also get a quick overview of operations on ZIO fibers.

Concurrency in ZIO

ZIO is a highly concurrent framework powered by fibers, which are lightweight virtual threads that enable huge scalability when compared to threads, reinforced with resource-safe cancellation, which supports several features in ZIO.

This robust concurrency architecture enables us to accomplish more with less, resulting in high scalability, and ultra-low-latency systems that are globally efficient and resource-safe.

ZIO Fibers

Fibers are lightweight equivalents of OS threads that represent an ongoing computation. A single fiber performs the instructions sequentially. A single JVM thread can run several fibers. So fibers have many to one relationships with threads.

ZIO fibers, just like operating system-level threads, have a well-defined lifespan that is dictated by the effect they are executing. The success or failure of the effect that each fiber is executing determines whether it succeeds or fails.

ZIO Fiber Operations:

1. Forking effects

The simplest method of producing a fiber is to fork an existing effect. Conceptually, forking an effect starts the effect’s execution on a new fiber, by providing us a reference to the freshly established fiber.

2. Joining Fibers

Joining is a method of waiting for a fiber to compute its value. We wait for fiber to complete its execution and receive its results.

3. Awaiting Fibers

We can use await on that fiber to see if our fiber succeeded or failed. This call will wait for that fiber to terminate before returning the fiber’s value as an Exit. Failure or success could be the exit value.

4. Interrupting Fibers

A fiber whose result is no longer required may be interrupted, which terminates the fiber immediately. It safely releases all the resources by running the finalizers. The interrupt operation is not resumed until the fiber has been completed or interrupted and all of its finalizers have been run, thus not leaking any resources.

5. Composing Fibers

ZIO helps us to compose fibers with zip or zipWith methods. They combine the results of two fibers into a single fiber. If either fiber fails, the composite fiber will also fail.

6. Racing Effects

ZIO lets us race multiple effects concurrently, which means they will be executed in parallel. The final value will be the value of the first effect that completes successfully. For first success or failure, we can use left.either.race(right.either), for any two effects left and right.

7. Timeout

ZIO allows us to timeout effects with the timeout method, which returns a new effect with an Option value if it succeeds. Timeout is helpful for effects like querying a database or calling a cloud API, as well as running a streaming pipeline or fully handling a web request. A value of None indicates that the timeout expired before the effect was finished.


In this blog, we discussed ZIO fibers and some operations on them. We learned about methods like fork, and join when we work with fibers. To read more blogs like this, do check out here

For exploring more regarding fibers in ZIO please refer to the following link

Written by 

Pragati is currently working as a Software Engineer at Knoldus Inc. She is having more than 1.5 year of experience working in IT industry and working in the Scala domain.