# How does using a balancer differ from using Sink.forEachParallel?

**URL:** <https://discuss.akka.io/t/how-does-using-a-balancer-differ-from-using-sink-foreachparallel/10867>\
**Category:** Akka Libraries\
**Created:** [August 16, 2024, 8:08am UTC](https://discuss.akka.io/t/how-does-using-a-balancer-differ-from-using-sink-foreachparallel/10867 "2024-08-16T08:08:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [August 16, 2024, 8:08am UTC](https://discuss.akka.io/t/how-does-using-a-balancer-differ-from-using-sink-foreachparallel/10867/1 "2024-08-16T08:08:59Z")

</div>

How does using a balancer differ from using Sink.forEachParallel?

> This topic is based on a conversation between an anonymous user of Akka and our “Ask AI about Akka” bot in the documentation.

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [August 16, 2024, 8:09am UTC](https://discuss.akka.io/t/how-does-using-a-balancer-differ-from-using-sink-foreachparallel/10867/2 "2024-08-16T08:09:35Z")

</div>

A balancer and `Sink.forEachParallel` are both used in Akka Streams to handle concurrent processing, but they serve different purposes and are used in different scenarios.

A balancer, as described in the [Akka Streams Cookbook](https://doc.akka.io/docs/akka/current/stream/stream-cookbook.html), is a special fan-out operation that tries to route elements to available downstream consumers. It is used to distribute incoming jobs to a pool of workers, which can process the jobs in parallel. The balancer ensures that the workload is evenly distributed among the workers. Here is an example of how to use a balancer:

```auto
def balancer[In, Out](worker: Flow[In, Out, Any], workerCount: Int): Flow[In, Out, NotUsed] = {
  import GraphDSL.Implicits._

  Flow.fromGraph(GraphDSL.create() { implicit b =>
    val balancer = b.add(Balance[In](workerCount, waitForAllDownstreams = true))
    val merge = b.add(Merge[Out](workerCount))

    for (_ <- 1 to workerCount) {
      // for each worker, add an edge from the balancer to the worker, then wire
      // it to the merge element
      balancer ~> worker.async ~> merge
    }

    FlowShape(balancer.in, merge.out)
  })
}

val processedJobs: Source[Result, NotUsed] = myJobs.via(balancer(worker, 3))

```

On the other hand, `Sink.forEachParallel` is a sink that will consume a stream, invoking a given function for each element in the stream in parallel. It is used when you want to perform some side-effecting operation (like printing to the console or updating a database) for each element in the stream, and you want these operations to be performed in parallel. Note that `Sink.forEachParallel` does not change the order of the elements.

In summary, a balancer is used when you want to distribute work among a pool of workers, while `Sink.forEachParallel` is used when you want to perform a side-effecting operation on each element in a stream in parallel.
