Grokking ZIO

What is ZIO? (In layman’s terms)

ZIO represents a philosophical approach to developing reliable, fault-tolerant, concurrent applications. It is meant to apply all the power of Functional Programming (and Object-oriented Programming!) that the Scala language has to offer. The ZIO approach is meant to lead to scalable, reliable, performant application development.

Modeling Effects in FP

ZIO is not unique in the Scala landscape when it comes to working with effects. Programmers have been working with Cats Effect and Monix for years. Cats Effect had its first commits in April 2017, and Monix dates back to 2014, and there’s even this seminal book by Sam Halliday, Functional Programming for Mortals, that showed how to use the tagless-final pattern to solve these problems with Scalaz.

Core Capabilities and Evolution of ZIO

What I think is the most interesting and compelling about ZIO (apart from the vibrant community) the story of its evolution. In the preface of the in-progress book Zionomicon, John De Goes talks about the history and journey of ZIO, as it went through various interesting stages…

  • Starting out as an effect type, but embracing an emphasis on concurrency problems (late 2017)
  • Committing to statically-typed errors by adding an error type, much like the Either[L,R] does, but also embracing subtyping and covariance (2018 with contribution by Weim Zine Elabidine)
  • Developing an asynchronous queue (contributions by Artem Pyanykh and Pierre Ricadat) and adding an entire ZManaged subsystem for reliably handling resource allocation and cleanup, plus introducing a ZIO Stream library (contributions by Itamar Ravid for both in late 2018)
  • Introduction of a third “environment” type, thus completing the ZIO[R, E, A] signature that we know today (late 2018, unveiled by De Goes with his “Death of Tagless Final” talk with contributions by Weim Zine Elabidine)
  • Addition of STM (Software Transactional Memory) for handling concurrent changes across two or more concurrent structures (2019 with contributions by Weim Zine Elabidine and Dejan Mijic)
  • ZIO Test testing framework, both enabling proper testing of effects and concurrency, but also solving very tough problems that other testing frameworks struggled at (2018, project led by Adam Frasier)
  • Adding a sort of “dependency injection” system for the environment types (the R in ZIO[R,E,A]) that handles the interdependency of services with ZLayer (2019)
  • ZIO 1.0 released in August 3, 2020 after a lot of work solving and completing the structured concurrency problem.
  • Lots of performance enhancement in the ZIO Runtime System (see John De Goes presentation in April 2021)
  • Improvement and abstraction of ZQueue with the addition of ZHub for a pub/sub model (see this great ZIO World presentation by Adam Fraser)
  • An elegant internal reworking of ZStreams, building a unified model for constructing, transducing (pipelining), and consuming (sinking) a stream.
  • Extreme simplification of the ZLayers system, moving away from the confusing Module Pattern to the sleeker Service Pattern 2.0 (see this excellent presentation by Kit Langton from ZIO World 2021)
  • Even better simplification of the new ZLayer Service Pattern by completely eliminating the need for the Has keyword (December 2021, unveiled in ZIO 2.0-RC1)
  • Complete removal of the entire ZManaged resource management system, greatly simplifying the overall API and reducing the size and complexity of the core codebase! (March 2022, unveiling in ZIO 2.0-RC3)
  1. ZIO started with being a fairly simple effect type, and has evolved over several years and many people's’ great ideas and contributions to become a comprehensive system for effects, concurrency, queues topics and streams, testing, resource management and dependency injection, scheduling, etc.
  2. This evolution has included an aggressive and almost merciless refactoring of things, resulting in a ZIO 2.0 that both simpler, faster and more powerful.

Impressive Multiplatform Support

It’s worth mentioning that ZIO supports Scala on the JVM, ScalaJS, and Scala Native, and it works in both Scala 2 and 3. There’s been also a lot of “interop” work so you can use it with Cats Effect or a variety of other libraries like http4s, fs2, doobie, etc.

The Inner ZIO Ecosystem

When I was first mulling over writing this article, I was considering comparing ZIO with Enterprise Javabeans or J2EE. I’ve worked extensively with J2EE in the past, and I remember thinking it was the coolest thing in the world because of all the libraries and integrated functionality and plug-and-play modularity that it had. And like Java SE, it just came with a comprehensive library that allowed you to do a whole lot without worrying about managing a bunch of 3rd-party libraries.

The Outer ZIO Ecosystem

Outside of these core types and modules, there’s an incredible list of external ZIO projects. Some are developed and maintained by the core ZIO developers, like zio-json, zio-sqs, zio-kafka, zio-config, and zio-nio—but there are also a lot of mature projects by outside organizations, two of the most exciting ones being zio-http by Dream11 (Read this compelling article from March 2021) and Quill.

Should I use ZIO in my current/upcoming project?

Lately, when I have a new project I want to knock off—whether its pulling and transforming and assembling data from a network API (I’m doing some work based on 2020 Census data) or simply looking through Wordle results to see what viable words are left—I have been starting with a clean ZIO project and using it as an excuse to tackle the resource management and streaming and related tasks.

Is ZIO better than X?

I’ve always tried to be fair and platform agnostic. I’ve been through way too many platform wars (and OS wars) to go down that path. I mentioned my appreciation of ZIO’s attempt at supporting a lot of “interop” capabilities, most notably with Cats Effect.

Topical Starting-points for Beginners

If you are very new to Scala, I think you’ll get lost trying to learn ZIO without a little bit of practice dealing with type signatures and understanding things like Either[L,R] and flatMap and for-comprehensions.

  1. How to really use Option (and Try and Either), teaching idiomatic use, understanding flatMap and for-comprehensions and ultimately monads. Video link here.
  2. Scala and the three implicits, teaching implicit classes and parameters, typeclasses and the “pimp my library” pattern. Video link here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Murray Todd Williams

Murray Todd Williams

Life-long learner, foodie and wine enthusiast, living in Austin, Texas.