Table of Contents

  1. Task Processing
  2. Introducing Cats Effect and FS2
  3. Testing Setup
  4. Approach 1: Batch Consuming
  5. Approach 2: Use Blocking Queue to Buffer Tasks
  6. Approach 3: Use Cats Effect Friendly Queue
  7. Approach 4: Use FS2 Stream Directly
  8. Approach 5: Make Producers Run in Parallel
  9. More
  10. Test Results

Compare Task Processing Approaches in Scala

Posted on 27 Aug 2023, tagged Scalaconcurrentcatscats-effectfs2queuestream

All the source code mentioned in this blog can be found in my Github repo.

Task Processing

There is a common problem in computer science and I’ve met it again recently: how to generate and process tasks efficiently? Use my recent project RSS Brain as an example: it needs to find the RSS feeds that haven’t been updated for a while in a database, and fetch the newest data from network.

The easiest way to do it is producing and consuming the tasks in a sequence, for example:

1val feeds = getPendingFeeds() // produce the tasks
2feeds.foreach(fetchFromNetwork) // consume the dtasks

However, it is unnecessarily slow. Network request doesn’t take lots of CPU and we can send multiple requests at the same time. Even if fetchFromNetwork is a CPU bound task, it can be parallelized if there are multiple CPU cores on a machine.

In this article, we will explore ways to do it more efficiently with Cats Effect and FS2 in a functional programming fashion.

You may wonder why not using AKKA stream? Other than it’s using a different programming paradigm (not functional programming), it’s also because AKKA has changed its license with a ridiculous price.

Introducing Cats Effect and FS2

To make processTask async, there is Future in Scala’s standard library. However, the side effect will happen when you create a Future instance. For example:

1def processTask(task: Task): Future[Unit] = Future(println(task))
3val runTask1 = processTask(task1) // this will start the async task

I assume the readers have a basic understanding of functional programming, so I’ll not explain why we want to avoid side effects. While Scala is not a pure functional language, a popular Scala library Cats Effect provides convenient ways to wrap side effects. With the help of its IO type, we can define an async task like this:

1def processTask(task: Task): IO[Unit] = IO(println(task))
3// this will not start the task, so no side effect
4val runTask1 = processTask(task1)
6// out of pure functional world and starts the side effect

Then there is fs2 that is a stream library that can be used with cats effect. It will be very handy when resolving our problem as we can see later.

Cat Effect has some big changes in version 3.x. In this article, we are using version 2.x. But I may upgrade the version in the future.

Testing Setup

In order to test which approach is the best under different scenarios, we need some basic setup. In TestRunner.scala, I defined some functions to generate tasks. Here are their signatures:

1// Produce a sequence of tasks represented by `Int`
2def produce(start: Int, end: Int): IO[Seq[Int]]
4// Process a task
5def consume(x: Int): IO[Unit]
7// Produce tasks as a stream
8def def produceStream(start: Int, end: Double): fs2.Stream[IO, Int]

produce simply produces tasks as int, and consume just print characters. In each of the functions, I use IO.sleep to create some delay to simulate the real world non-blocking IO. They also print characters P (produce) or C (consume) (based on the width of terminal, some of the C outputs may be skipped to fit the width) when being invoked, so that we can have an intuitive view of how quick tasks are produced and consumed.

Then there is TestConfig.scala for configuring the test:

1trait TestConfig {
2  val testName: String
3  val produceDelay: FiniteDuration
4  val minConsumeDelayMillis: Long
5  val maxConsumeDelayMillis: Long
6  val batchSize = 100  // consume batch size
7  val totalSize = 1000 // how many tasks to generate

By setting up produce and consume delays, we can test scenarios when producer is slower, consumer is slower, or producer and consumer speed is almost the same. Here are the configurations we are going to use in Main.scala

 1val configs = Seq(
 2  new TestConfig {
 3    override val testName: String = "slow-producer"
 4    override val produceDelay: FiniteDuration = 1000.millis
 5    override val minConsumeDelayMillis: Long = 10
 6    override val maxConsumeDelayMillis: Long = 100
 7  },
 8  new TestConfig {
 9    override val testName: String = "balanced"
10    override val produceDelay: FiniteDuration = 1005.millis
11    override val minConsumeDelayMillis: Long = 10
12    override val maxConsumeDelayMillis: Long = 2000
13  },
14  new TestConfig {
15    override val testName: String = "slow-consumer"
16    override val produceDelay: FiniteDuration = 10.millis
17    override val minConsumeDelayMillis: Long = 10
18    override val maxConsumeDelayMillis: Long = 1000
19  }

Approach 1: Batch Consuming

The first approach is to make the consuming side parallel. We can consume a batch of tasks concurrently, like in BatchIOApp.scala.

 1def loop(start: Int): IO[Unit] = {
 2  if (start >= config.totalSize) {
 3    IO.unit
 4  } else {
 5    produce(start, start + config.batchSize)
 6      .flatMap{}
 7      .flatMap(_ => loop(start + config.batchSize))
 8  }

However, this only makes a batch of tasks run in parallel. It needs to wait the whole batch to be finished in order to start next batch. This is very obvious when we run this approach and see the output of characters (download Github repo and run sbt "run -n BatchIOApp"). See how it paused after each batch even when consumer is slower than producer:

Approach 2: Use Blocking Queue to Buffer Tasks

We need a way to let producers not waiting for consumers, and also let consumers not wait for a batch to finish in order to start next batch. A very common solution is to use a queue between producers and consumers. Producers put tasks into the queue, and consumers get tasks for the queue. If the queue is thread safe, then both producers and consumers can work on their own without care about each other. In order to not let producer put unlimited tasks into the queue to blowup the memory, we need the queue to have a capacity. When the queue is full, the producer should be blocked. And when the queue is empty, the consumers should be blocked as well.

In Java, BlockingQueue meets our requirements. We can use an implementation LinkedBlockingQueue. However, BlockingQueue will block the whole thread instead of a single IO. Let’s not worry about it for now and see how to use a queue to implement producing and consuming in parallel. The implementation is in BlockingQueueApp.scala:

 2val queue = new LinkedBlockingQueue[Option[Int]](config.batchSize * 2)
 4override def work(): IO[Unit] = {
 5  Seq(
 6    (produceStream(0).map(Some(_)) ++ fs2.Stream.emit(None))
 7                        .evalMap(x => IO(queue.put(x))).compile.drain,
 8    dequeueStream().unNoneTerminate.parEvalMap(config.batchSize)(consume).compile.drain,
 9  ) => ())
12private def dequeueStream(): fs2.Stream[IO, Option[Int]] = {
13  fs2.Stream.eval(IO(queue.take())) ++ dequeueStream()

Here we have two IOs run in parallel with parSequence: the first one creates a task stream by produceStream, and append None at the end so that the consumer knows it should end processing. Another stream dequeueStream gets the tasks from the queue then consumes it in parallel with parEvalmap(config.batchSize)(consume).

When run it with sbt "run -n BlockingQueueApp", we can see it’s much faster when the consumer is faster or has the same speed as the producer. Especially when the consumer is slow, it prints multiple P at first, which means the producers doesn’t wait all the consumers to finish in order to produce tasks.

Back to the blocking the whole thread problem: it doesn’t seem to be a problem in this case, right? It’s only because we are lucky! In this setup, we are using two fixed threads as the thread pool of running IO in Main.scala:

 1private val executor = Executors.newFixedThreadPool(2, (r: Runnable) => {
 2  val back = new Thread(r)
 3  back.setDaemon(true)
 4  back
 7implicit override def executionContext: ExecutionContext = ExecutionContext.fromExecutor(executor)
 9implicit override def timer: Timer[IO] = IO.timer(executionContext)
11implicit override def contextShift: ContextShift[IO] = IO.contextShift(executionContext)

If 2 consumers with empty queue happens to be scheduled on these 2 threads separately, it will block. If we change our BlockingQueueApp to the code in RealBlockingQueueApp:

1override def work(): IO[Unit] = {
2  Seq(
3    dequeueStream().unNoneTerminate.parEvalMap(config.batchSize)(consume).compile.drain,
4    dequeueStream().unNoneTerminate.parEvalMap(config.batchSize)(consume).compile.drain,
5    (produceStream(0).map(Some(_)) ++ fs2.Stream.emit(None)).evalMap(x => IO(queue.put(x))).compile.drain,
6  ) => ())

Here we started two dequeue stream at first. Now the whole program will block when run it with sbt "run -b" .

The lesson learned here is that there is a big risk if any operation blocks the whole thread in cats effect. Even it doesn’t block the whole program, it may make a whole thread unavailable.

Actually in Cats Effect’s thread model, there is another thread pool for blocking tasks if we mark it explicitly. In AsyncConsole.scala, I use this exact block mode to run console output so that it won’t effect other non blocking IO operations:

1def asyncPrintln(s: String)(
2    implicit cs: ContextShift[IO], blocker: Blocker): IO[Unit] = blocker.blockOn(IO(println(s)))

However, if a thread is blocked in this pool, it will start another thread for the next operation. Based on the document, there is no limit on how many threads will be created. So if the producer is much slower than consumer, there will be more and more consume operations blocked on dequeue, so it will generate a large amount of threads, which is not ideal and eventually even will blow up the memory.

Approach 3: Use Cats Effect Friendly Queue

What if we have a queue that only block the dequeue IO when empty instead of blocking the whole thread? Luckily, FS2 provides such a queue. (Cats Effect 3.x also provides such a queue). The implementation is basically the same as above (code in StreamQueueApp.scala):

 1import fs2.concurrent.Queue
 3def work(): IO[Unit] = {
 4  for {
 5    queue <- Queue.bounded[IO, Option[Int]](config.batchSize * 2)
 6    _ <- Seq(
 7      (produceStream(0).map(Some(_)) ++ fs2.Stream.emit(None)).through(queue.enqueue).compile.drain,
 8      queue.dequeue.unNoneTerminate.parEvalMap(config.batchSize)(consume).compile.drain,
 9    ).parSequence
10  } yield ()

Run sbt "run -n StreamAppQueue" to see how it performs.

Approach 4: Use FS2 Stream Directly

FS2 actually provides some advanced stream operations that makes it possible to combine the producing stream and consume stream, like the code in StreamApp.scala:


Here we map consume in parallel on produce stream. However, if you try to run sbt "run -n StreamApp" vs sbt "run -n StreamQueueApp", you will find this is slower than before. This is because produceStream will give the next batch when the downstream asks. If we can prepare at least one batch before the downstream is free, we can save more time. Luckily, it’s very easy to do in fs2. As we can see in PrefetchStreamApp.scala, we can add prefetch after the produceStream:


It will prefetch a chunk of elements. Use prefetchN if you want to prefetch N chunks.

Then run this with sbt "run -n PrefetchStreamApp", you will find the performance is similar as the queued approach.

Actually if you check the source code of prefetch, you will find the implementation is almost the same as ours:

1def prefetch[F2[x] >: F[x]: Concurrent]: Stream[F2, O] = prefetchN[F2](1)
3def prefetchN[F2[x] >: F[x]: Concurrent](n: Int): Stream[F2, O] =
4  Stream.eval(Queue.bounded[F2, Option[Chunk[O]]](n)).flatMap { queue =>
5    queue.dequeue.unNoneTerminate
6      .flatMap(Stream.chunk(_))
7      .concurrently(chunks.noneTerminate.covary[F2].through(queue.enqueue))
8  }

Approach 5: Make Producers Run in Parallel

We’ve made it runs in parallel between consumers, also between consumers and producers. But we haven’t made producers run in parallel yet. With the queue, its very easy to do, just start multiple IOs for produceStream.through(queue.enqueue). ConcurrentProduceQueueApp.scala is an example:

 1private val counter = new AtomicInteger(0)
 3override def work(): IO[Unit] = {
 4  for {
 5    queue <- Queue.bounded[IO, Int](config.batchSize * 2)
 6    _ <- Seq(
 7      produceStream(0, config.totalSize / 2).through(queue.enqueue).compile.drain,
 8      produceStream(config.totalSize / 2, config.totalSize).through(queue.enqueue).compile.drain,
 9      queue.dequeue.parEvalMap(config.batchSize) { x =>
10        consume(x).map { _ =>
11          if (counter.incrementAndGet() >= config.totalSize) {
12            None
13          } else {
14            Some()
15          }
16        }
17      }.unNoneTerminate.compile.drain,
18    ).parSequence
19  } yield ()

It has 2 concurrent producers but in theory you can create as many as you want, just be careful with the parameters of produceStream.

If you run this with sbt "run -n ConcurrentProduceQueueApp", you can find the performance is much better with slower producer. However, with the help of fs2 library, we can make the code cleaner without depends on any queue explicitly. Here is what I did in ConcurrentProducerApp.scala:

 1def work(): IO[Unit] = {
 2  fs2.Stream.emits(Range(0, produceParallelism))
 3    .map(batch => produceStream(
 4      batch * config.totalSize / produceParallelism,
 5      (batch + 1) * config.totalSize / produceParallelism.toDouble))
 6    .parJoin(produceParallelism)
 7    .prefetch
 8    .parEvalMap(config.batchSize)(consume).compile.drain

Here we use parJoin to join multiple producer stream at the same time.


All the approaches above other than the first one uses a queue either implicitly or explicitly. However, under high parallelism and load, every job operating on a single queue may makes this queue a bottleneck. In this case, there is a work stealing algorithm that each consumers can has its own queue, and whenever a consumer’s queue is empty, it steal some tasks from another one. But it’s a little bit complex and unnecessary if the load is not so high, so I will not cover it in this article.

Test Results

Now let’s run all the approaches and compare the performance with sbt "run -n". Here are the results:

  slow producer balanced slow consumer
BatchIO 11086.078637 ms 29912.377578 ms 10015.51878 ms
BlockingQueue 10190.038753 ms 14195.228189 ms 6495.333179 ms
StreamQueue 10138.016643 ms 14458.443122 ms 6418.078377 ms
Stream 10356.178562 ms 15655.697826 ms 6560.111697 ms
PrefetchStream 10141.110634 ms 14578.362136 ms 6376.628036 ms
ConcurrentProduceresQueue 5187.442452 ms 14395.321922 ms 6576.538821 ms
ConcurrentProducer 5198.723825 ms 14544.247312 ms 6418.078377 ms

We can see approaches that parallelize all the parts win the performance game.

comments powered by Disqus