<?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 Prasad Das on Medium]]></title>
        <description><![CDATA[Stories by Prasad Das on Medium]]></description>
        <link>https://medium.com/@prasaddas1122?source=rss-15fc4714ba35------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qTfn4TbS_ojOOKQR</url>
            <title>Stories by Prasad Das on Medium</title>
            <link>https://medium.com/@prasaddas1122?source=rss-15fc4714ba35------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 16:03:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prasaddas1122/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[Why Akka Outperforms Traditional Thread Pools: The Physics of Concurrency]]></title>
            <link>https://medium.com/@prasaddas1122/why-akka-outperforms-traditional-thread-pools-the-physics-of-concurrency-fc2ad26f3f76?source=rss-15fc4714ba35------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc2ad26f3f76</guid>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[akka-actor]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[java-performance]]></category>
            <dc:creator><![CDATA[Prasad Das]]></dc:creator>
            <pubDate>Thu, 12 Jun 2025 20:56:22 GMT</pubDate>
            <atom:updated>2025-06-12T20:56:22.809Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Great Concurrency Misconception</h3><blockquote><em>Many developers hear “Akka uses thread pools” and assume it behaves just like Java’s </em><em>ExecutorService. This is a </em><strong><em>fundamental misunderstanding</em></strong><em>. While both use thread pools under the hood, Akka’s </em><strong><em>actor model</em></strong><em> introduces a completely different concurrency paradigm — one that </em><strong><em>dramatically reduces context switching overhead</em></strong><em> and </em><strong><em>maximizes CPU efficiency</em></strong><em>, especially under high I/O workloads.</em></blockquote><blockquote><em>Let’s walk through why Akka’s approach to I/O and execution scheduling yields </em><strong><em>order-of-magnitude performance advantages</em></strong><em> over traditional thread-per-task models.</em></blockquote><h3>How Traditional Thread Switching Works (OS-Level)</h3><p>When a thread blocks on I/O:</p><pre>[Thread A] → BLOCK → [Kernel Save] → [Kernel Load] → [Thread B]<br>          │          (5-10μs)       (5-10μs)        │<br>          └────────── OS Context Switch (10-20μs) ───┘</pre><ol><li>Kernel saves CPU state (registers, stack pointer)</li><li>Scheduler searches for runnable thread</li><li>Kernel loads new thread’s state</li><li>CPU caches flushed (L1/L2/L3 lost)</li></ol><h3>The Physics of Waiting: Blocking vs. Non-Blocking</h3><p>Consider handling HTTP requests where each requires:</p><ul><li>5ms CPU work (JSON parsing, validation)</li><li>95ms I/O wait (database query)</li></ul><h3>Traditional Thread Pool of 4 Threads With CPU Timeline :</h3><pre>ExecutorService pool = Executors.newFixedThreadPool(4);<br><br>for (int i = 0; i &lt; 1000; i++) {<br>  pool.submit(() -&gt; {<br>    processRequest(); // 5ms CPU<br>    databaseBlockingCall(); // 95ms BLOCKING 🛑<br>  });<br>}<br><br>-------------------------------------------------------------------<br>Time 0ms: Thread1 starts → 5ms CPU → 95ms block<br>Time 5ms: Thread2 starts → 5ms CPU → 95ms block<br>Time 10ms: Thread3 starts → 5ms CPU → 95ms block<br>Time 15ms: Thread4 starts → 5ms CPU → 95ms block<br>Time 20ms: ALL THREADS BLOCKED (idle for 80ms)<br>...<br>Time 100ms: Thread1 finishes → starts next request</pre><p>From above we can see …<br>Total time = 25,000ms (25 seconds!)<br>CPU Utilization = (5ms × 1000) / 25,000ms = 2%<br><em>(98% of CPU time wasted on blocking)</em></p><h3>Step-by-Step CPU Waste in Thread Pool of 4 threads:</h3><pre>Time 0ms: <br>  Thread1: [CPU:5ms] → Blocks at 5ms<br>  CONTEXT SWITCH (10μs)<br>  <br>Time 5.01ms:<br>  Thread2: [CPU:5ms] → Blocks at 10.01ms<br>  CONTEXT SWITCH (10μs)<br>  <br>Time 10.02ms:<br>  Thread3: [CPU:5ms] → Blocks at 15.02ms<br>  CONTEXT SWITCH (10μs)<br>  <br>Time 15.03ms:<br>  Thread4: [CPU:5ms] → Blocks at 20.03ms<br>  CONTEXT SWITCH (10μs)<br>  <br>Time 20.04ms:<br>  ALL THREADS BLOCKED → 80ms CPU IDLE</pre><h3>Akka Actors (4 threads) With CPU Timeline</h3><pre>public class Handler extends AbstractActor {<br>  @Override<br>  public Receive createReceive() {<br>    return receiveBuilder()<br>      .match(Request.class, req -&gt; {<br>        processRequest(); // 5ms CPU<br>        asyncDBcall().thenApply(response -&gt; <br>          new Response(response)<br>        ).pipeTo(getSender(), getSelf());<br>      })<br>      .build();<br>  }<br>}<br><br>// Configure 4-thread dispatcher<br>ActorSystem system = ActorSystem.create(&quot;sys&quot;);<br>Props props = Props.create(Handler.class)<br>             .withDispatcher(&quot;akka.actor.default-dispatcher&quot;);<br>ActorRef handler = system.actorOf(props);<br><br>---------------------------------------------------------------<br><br>Time 0-5ms: Thread1 processes Request1<br>Time 5ms: Thread1 grabs Request2<br>Time 10ms: Thread1 grabs Request3<br>...<br>Time 1250ms: All 1000 requests started<br>Time 95ms: First response arrives<br>Time 1345ms: Last response arrives</pre><p>From above we can observe<br>Total time = 1,345ms (1.3 seconds)<br>CPU Utilization = (5ms × 1000) / 1,345ms = 372% (of 4 cores)<br><em>(Near-zero idle time)</em></p><h3>Akka Timeline 4 Threads With Context Switching:</h3><pre>Time 0-5ms: <br>  Thread1: [Actor1:5ms] → Suspends at 5ms<br>  <br>Time 5ms: <br>  Thread1: [Actor2:5ms] → Suspends at 10ms<br>  (No context switch - method call!)<br>  <br>Time 10ms:<br>  Thread1: [Actor3:5ms] → Suspends at 15ms<br><br>Time 15ms:<br>  Thread1: [Actor4:5ms] → Suspends at 20ms<br><br>Time 20ms:<br>  EVENT LOOP: Polls OS for I/O (0.1ms)<br><br>...................................<br><br>16 requests started in 20ms<br>CPU active: 20ms (100%)<br>Switch cost: 0.8μs (4 method calls)</pre><h3>How Akka “Switches” Actors</h3><p>Akka dispatchers schedule actor message processing with minimal overhead — often via lightweight method invocations or cooperative scheduling — avoiding expensive OS-level thread context switches. Below is a very high level pseudo code for better understanding!</p><pre>while (!mailbox.isEmpty()) {<br>  Actor nextActor = mailbox.poll();<br>  nextActor.handleMessage(); // SIMPLE METHOD CALL!<br>}</pre><ol><li>No kernel involvement — pure user-space operation</li><li>No state saved — just call next actor’s message handler</li><li>CPU cache preserved — hot L1/L2 caches retained</li></ol><pre>[Actor A] → Message Done → [Method Call] → [Actor B]<br>          │                (0.2μs)         │<br>          └── Actor &quot;Switch&quot; (0.2μs) ──────┘<br><br><br>Cost: 200 nanoseconds (0.2μs) - 100x faster than OS switching</pre><h3>Mathematical Proof :</h3><h3>Thread Pool 4 threads</h3><pre>Total Time = (Requests / Threads) × (CPU + I/O) <br>           = (1000/4) × 100ms = 25,000ms<br>Context Switches = 2 × Requests = 2000<br>Switch Overhead = 2000 × 15μs = 30ms<br><br>Total Effective Time = 25,000ms + 30ms = 25,030ms</pre><h3>Akka 4 threads</h3><pre>CPU Phase = (Requests × CPU) / Threads = (1000 × 5ms)/4 = 1,250ms<br>I/O Overlap = Max(I/O, CPU Phase) = 95ms (bottleneck)<br>Total Time = 1,250ms + 95ms = 1,345ms<br>Actor Switches = Requests = 1000<br>Switch Overhead = 1000 × 0.2μs = 0.2ms<br><br>Total Effective Time = 1,345.0002ms</pre><h3>Why This Matters in Real Java Applications?</h3><ol><li>Cost Reduction: 4 servers replace 74</li><li>Latency Justice: P99 latency drops from 25s to 220ms</li><li>Failure Resilience:</li><li>Backpressure and Fault Isolation</li><li>Scale Proof:</li></ol><h3>The Verdict: Beyond Threads to Physics :</h3><p>Akka wins by respecting computer architecture physics:</p><ol><li>I/O Waiting: Offloaded to OS kernel</li><li>Switching: User-space method calls (0.2μs vs 20μs)</li><li>Cache Locality: Hot L1/L2 caches preserved more effectively than OS-thread switching</li><li>Memory Efficiency: Few hundred bytes/actor vs 1MB/thread stack</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc2ad26f3f76" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>