<?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 Kacper Perzankowski on Medium]]></title>
        <description><![CDATA[Stories by Kacper Perzankowski on Medium]]></description>
        <link>https://medium.com/@kacperperzankowski?source=rss-f1c82ac9c80c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-VoPQNWjqRGe12F-8iYjMQ.jpeg</url>
            <title>Stories by Kacper Perzankowski on Medium</title>
            <link>https://medium.com/@kacperperzankowski?source=rss-f1c82ac9c80c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:31:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kacperperzankowski/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[Concurrent GenServer in Elixir]]></title>
            <link>https://kacperperzankowski.medium.com/concurrent-genserver-in-elixir-6eae3b4225f2?source=rss-f1c82ac9c80c------2</link>
            <guid isPermaLink="false">https://medium.com/p/6eae3b4225f2</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[genserver]]></category>
            <category><![CDATA[iex]]></category>
            <dc:creator><![CDATA[Kacper Perzankowski]]></dc:creator>
            <pubDate>Wed, 21 Apr 2021 20:55:11 GMT</pubDate>
            <atom:updated>2021-04-25T20:44:58.697Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pTwO_lmq7aHwI_qpyKOuEg.jpeg" /></figure><p>When building the GenServers, one thing always struck me. Since GenServer is really a single process, what happens when multiple processes are trying to send requests at the same time to a given GenServer? Will it handle them synchronously or maybe asynchronously? Could GenServer be an application bottleneck?</p><p>Well, let’s make an experiment.</p><p>First, I need a naive implementation of the GenServer that just does fake work.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/205ceb7af16b2a8eb080e0884cd817f3/href">https://medium.com/media/205ceb7af16b2a8eb080e0884cd817f3/href</a></iframe><p>The responsibility of the Worker is to process “expensive” jobs. For the purposes of this experiment, each job will be <strong>1 second long</strong>.</p><p>Then, let’s prepare a module that will schedule jobs to make a probe.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6f471558e967cafdcf791c425c9699d/href">https://medium.com/media/d6f471558e967cafdcf791c425c9699d/href</a></iframe><p>The Scheduler will run N jobs simultaneously, spawning supervised <a href="https://hexdocs.pm/elixir/Task.html">Tasks</a> for each of them.</p><p>Ok, let’s wrap it up and take a measurement.</p><pre>iex(1)&gt; {:ok, pid} = Worker.start_link<br>{:ok, #PID&lt;0.123.0&gt;}<br>iex(2)&gt; :timer.tc(fn -&gt; Scheduler.start_jobs(pid, 100) end)<br>{100104309, :ok}</pre><p>It took <strong>~100.14</strong> seconds to process <strong>100 jobs</strong>. It means that every job has been processed one by one, <strong>synchronously</strong>.</p><p>That actually is the answer I was looking for. <strong>Badly used GenServer can be a bottleneck.</strong></p><p>Are we able to change this state of affairs?</p><p>Well, there is one solution… It’s called <a href="https://hexdocs.pm/elixir/GenServer.html#reply/2">GenServer.reply/2</a>.</p><p>From documentation:</p><blockquote>Replies to a client.</blockquote><blockquote>This function can be used to explicitly send a reply to a client that called <a href="https://hexdocs.pm/elixir/GenServer.html#call/3">call/3</a> or <a href="https://hexdocs.pm/elixir/GenServer.html#multi_call/4">multi_call/4</a> when the reply cannot be specified in the return value of <a href="https://hexdocs.pm/elixir/GenServer.html#c:handle_call/3">handle_call/3</a>.</blockquote><p>Sounds reasonable, huh? I will now rebuild the implementation of the handle_call method so that it uses GenServer.reply/2.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfca8e12e11dc30eea7e2126955c7a6c/href">https://medium.com/media/bfca8e12e11dc30eea7e2126955c7a6c/href</a></iframe><p>After receiving the call :process_job request, I spawn a new linked process to execute the job and then call GenServer.reply(from, result) to return the reply back. Importantly, the reply is returned from this spawned process, not from the GenServer’s one.</p><p>Let’s check how it works.</p><pre>iex(1)&gt; {:ok, pid} = ConcurrentWorker.start_link           <br>{:ok, #PID&lt;0.233.0&gt;}<br>iex(2)&gt; :timer.tc(fn -&gt; Scheduler.start_jobs(pid, 100) end)<br>{1003401, :ok}</pre><p>It works! The <strong>ConcurrentWorker</strong> has processed <strong>100 jobs</strong> in <strong>~1.003 seconds asynchronously.</strong></p><p>Thus, this very simple trick is able to increase GenServer’s throughput significantly. Of course, this kind of optimization is worth only in specific cases. It won’t work for operations that mutate the state of GenServer. It seems to be a nice pattern for read operations.</p><p>Elixir: v1.11.3<br>Erlang/OTP: 23</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6eae3b4225f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>