# stream.mapAsync(1) vs single threaded execution context with stream.mapAsync(16) to maintain ordering?

**URL:** <https://discuss.akka.io/t/stream-mapasync-1-vs-single-threaded-execution-context-with-stream-mapasync-16-to-maintain-ordering/886>\
**Category:** Akka Streams & Alpakka\
**Created:** [May 2, 2018, 2:54am UTC](https://discuss.akka.io/t/stream-mapasync-1-vs-single-threaded-execution-context-with-stream-mapasync-16-to-maintain-ordering/886 "2018-05-02T02:54:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kpritam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/kpritam/32/1132_2.png) [@kpritam](https://discuss.akka.io/u/kpritam)\
**Post date:** [May 2, 2018, 2:54am UTC](https://discuss.akka.io/t/stream-mapasync-1-vs-single-threaded-execution-context-with-stream-mapasync-16-to-maintain-ordering/886/1 "2018-05-02T02:54:59Z")

</div>

Use case is that we want to publish events to redis and requirement is that events published from single publisher should be in order.

So to achieve this, we have a code like this for single publisher:  
**1.**

```auto
source
.mapAsync(1)(publish)
.to(Sink.ignore)
.run()

```

**2.** But when i ran a benchmark with this code on single machine (8GB RAA, 8 vCPUS, MacOs) with 40 Publishers and 40 Subscribers, I got only 25k/sec throughput.

After little investigation, I thought of creating single threaded execution context per Publisher to maintain ordering and increased mapAsync to 8 and ran the same benchmark, in this case, I am getting around 45k/sec throughput and code looks like this:

```auto
implicit val synchronousEc = ExecutionContext.fromExecutor((task: Runnable) => task.run())
source
.mapAsync(8)(publish)
.to(Sink.ignore)
.run()

```

So the question is which one is the recommended way? What are pros and cons for both the approaches or is there any other way I can guarantee ordering?

---

<div class="post-metadata">

**Author:** ![kpritam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/kpritam/32/1132_2.png) [@kpritam](https://discuss.akka.io/u/kpritam)\
**Post date:** [May 16, 2018, 5:10pm UTC](https://discuss.akka.io/t/stream-mapasync-1-vs-single-threaded-execution-context-with-stream-mapasync-16-to-maintain-ordering/886/2 "2018-05-16T17:10:04Z")

</div>

Any suggestions on this please?

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [May 17, 2018, 8:34am UTC](https://discuss.akka.io/t/stream-mapasync-1-vs-single-threaded-execution-context-with-stream-mapasync-16-to-maintain-ordering/886/3 "2018-05-17T08:34:04Z")

</div>

The guarantee of ordering for `mapAsync` is only about the order the results are emitted downstream, so `mapAsync(n)` could lead to unexpected ordering of the things happening in publish (as it will happen concurrently for up to 8 invocations).

One thing to try out could be batching, for example through `groupWithin` and push batches of elements to the database per `mapAsync` operation (could help if crossing the async boundary is in fact dominating)
