<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishek Gautam on Medium]]></title>
        <description><![CDATA[Stories by Abhishek Gautam on Medium]]></description>
        <link>https://medium.com/@abugautam?source=rss-d495aaee31a3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IUE6yB3ptVL3YLGci3NmiQ.jpeg</url>
            <title>Stories by Abhishek Gautam on Medium</title>
            <link>https://medium.com/@abugautam?source=rss-d495aaee31a3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 12:46:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abugautam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Project Reactor — flatMap vs concatMap vs flatMapSequential]]></title>
            <link>https://medium.com/@abugautam/project-reactor-flatmap-vs-concatmap-vs-flatmapsequential-dc74ca58829e?source=rss-d495aaee31a3------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc74ca58829e</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[reactor-project]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[spring-webflux]]></category>
            <dc:creator><![CDATA[Abhishek Gautam]]></dc:creator>
            <pubDate>Fri, 24 Feb 2023 11:26:18 GMT</pubDate>
            <atom:updated>2023-02-24T11:26:18.805Z</atom:updated>
            <content:encoded><![CDATA[<h3>Project Reactor — flatMap vs concatMap vs flatMapSequential</h3><blockquote>While working with project reactor or web-flux, there are many occasions where we come across a choice to use an operator between flatMap or concatMap or flatMapSequential inside of our reactive pipelines in order to do an API call or a database call etc.</blockquote><blockquote>The obvious confusion which arises is between the working of these operators like which one will work in parallel and which one would be sequential. It gets very confusing sometimes when we are dealing with huge data sets and we often get confuse which operator to use for our specific use case.</blockquote><blockquote>Today I will be explaining these 3 operators with the help of some sample code piece which is like a mock of an actual production like pipeline.</blockquote><blockquote>To understand these concepts better one should be very familiar with the keywords <strong>publisher, subscriber, subscription and flux/mono</strong>.</blockquote><h3><strong>flatMap</strong></h3><p>The definition of flatMap states it subscribes to its inners eagerly which means it will do parallel calls to the downstream/inners the moment it gets the data from the upstream publishers (flux/mono).</p><ul><li><strong>Generation of inners and subscription</strong>: this operator is eagerly subscribing to its inners.</li><li><strong>Ordering of the flattened values</strong>: this operator does not necessarily preserve original ordering, as inner element are flattened as they arrive.</li><li><strong>Interleaving</strong>: this operator lets values from different inners interleave (similar to merging the inner sequences).</li></ul><p>In the diagram we can see that it subscribes to both the green and yellow eagerly which means in parallel and in the output it can provide data by interleaving the results of both the colours in any order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XhaaMbKphtkm-5NzuyDxQ.png" /></figure><p>To demonstrate this, let say we have a code piece which gets items from a flux and calls a downstream database/API which is mocked as a separate method for the sake of this demo. <br>The downstream returns a Mono which delays each element by 300ms and then returns the squares of the elements in order to showcase some database operation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EZyYhAqQJpO8OVTt9xjw4A.png" /></figure><p>Output</p><p>Here we can see that all the calls are made parallel to the downstream even before a single element result is returned and the elements are returned in any order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*nrlWFVqws4twRa_qm9q2Jg.png" /></figure><p><strong>Note:</strong></p><p>There is a concurrency option in flatMap which can be passed as a separate argument to flatMap in order to control how many parallel calls we want at a single time. The default value is 256.</p><h3><strong>concatMap</strong></h3><p>Its definition states it waits for one of the inner to complete before generating the next one and subscribing to it.</p><ul><li><strong>Generation of inners and subscription:</strong> this operator waits for one inner to complete before generating the next one and subscribing to it.</li><li><strong>Ordering of the flattened values</strong>: this operator naturally preserves the same order as the source elements, concatenating the inners from each source element sequentially.</li><li><strong>Interleaving</strong>: this operator does not let values from different inners interleave (concatenation).</li></ul><p>In the diagram we can see that it subscribes to the green first and then waits for it to give the result back before subscribing to yellow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEUyHefklTm4T1RP3aOr-w.png" /></figure><p>In the same code if we change flatMap to concatMap we will see a difference in the output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CrbdnQN1yKBdONbDiLfD-w.png" /></figure><p>Here in the output, we can see that all the elements will call downstream one by one and will wait for one element to get returned before calling the next one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*pSlAfpVgme5UAwvqFs_N7w.png" /></figure><p><strong>Note:</strong></p><p>concatMap would break its sequential execution in case some exception is thrown from downstream and it will not call the next element in the flow.</p><h3><strong>flatMapSequential</strong></h3><p>Eagerly subscribes to inners like flatMap but maintains the order of the result in which elements were subscribed.</p><ul><li><strong>Generation of inners and subscription</strong>: this operator is eagerly subscribing to its inners (like flatMap).</li><li><strong>Ordering of the flattened values</strong>: this operator queues elements from late inners until all elements from earlier inners have been emitted, thus emitting inner sequences as a whole, in an order that matches their source’s order.</li><li><strong>Interleaving</strong>: this operator does not let values from different inners interleave (similar looking result to concatMap, but due to queueing of values that would have been interleaved otherwise).</li></ul><p>In the diagram we can see that it subscribes to all the green, orange and pink eagerly which means in parallel but in the output it will provide result in the same order in which the colours were subscribed, which means even if we have the result of pink ready before the green it will wait for green to finish too since it is first in the order.</p><p>This operator maintains a queue to achieve this out of order computation to get strict order results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kN3JbsD7UWpQAijgp32nhQ.png" /></figure><p>In the same code, if we change flatMap to flatMapSequential, we will see a difference in output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_28ti46bIQ3XYVB0PwDZSw.png" /></figure><p>Here, if we see all the calls were made parallel to downstream and it modifies the items in any order but when the data is returned back flatMapSequential ensures it gets back in the same order in which it was called.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*xtHdMRfGuNaIdecss1xEFA.png" /></figure><blockquote><strong>Conclusion</strong>:</blockquote><blockquote>use <strong>flatMap </strong>for fast parallel calls (max 256 limit — default concurrency), when ordering of elements is not required.</blockquote><blockquote>use <strong>concatMap </strong>for sequential calls when strict ordering of elements is required.</blockquote><blockquote>use <strong>flatMapSequential </strong>for parallel calls with strict ordering of elements.</blockquote><p><strong>Sources:</strong></p><p><a href="https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#flatMap-java.util.function.Function-">https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#flatMap-java.util.function.Function-</a><br><a href="https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#concatMap-java.util.function.Function-">https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#concatMap-java.util.function.Function-</a><a href="https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#flatMapSequential-java.util.function.Function-">https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#flatMapSequential-java.util.function.Function-</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc74ca58829e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>