<?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 Michael David Koch on Medium]]></title>
        <description><![CDATA[Stories by Michael David Koch on Medium]]></description>
        <link>https://medium.com/@the.sovereign.craftsman?source=rss-6a69fd311113------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mxDSZs2OjYDJr5VGeaAfrQ.png</url>
            <title>Stories by Michael David Koch on Medium</title>
            <link>https://medium.com/@the.sovereign.craftsman?source=rss-6a69fd311113------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:46:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@the.sovereign.craftsman/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[Elixir Concurrency — Visualized]]></title>
            <link>https://medium.com/@the.sovereign.craftsman/elixir-concurrency-visualized-7cac0b57096c?source=rss-6a69fd311113------2</link>
            <guid isPermaLink="false">https://medium.com/p/7cac0b57096c</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Michael David Koch]]></dc:creator>
            <pubDate>Mon, 26 Oct 2026 21:22:37 GMT</pubDate>
            <atom:updated>2026-10-26T21:22:37.883Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using the classic Boids flocking algorithm to show how the BEAM handles crashes, failures and CPU overload</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FaQ75ZD381FQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DaQ75ZD381FQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FaQ75ZD381FQ%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ff6064cabe77b2c48bf72450314c509/href">https://medium.com/media/3ff6064cabe77b2c48bf72450314c509/href</a></iframe><p>Elixir is a fascinating, albeit esoteric, technology. It is well known for its functional programming paradigm, concurrency model, and high fault tolerance. I recently had the opportunity to work professionally on an Elixir monolith after having spent the better part of my career working on traditional OOP stacks like .NET. I was excited and eager to explore what this niche technology had to offer.</p><p>When I began researching Elixir and Phoenix, I wanted to understand what made it different. Elixir runs on the BEAM, Erlang’s virtual machine, and uses OTP, Erlang’s framework for building concurrent, fault-tolerant systems. There is a wealth of documentation and case studies available that articulate the “what” and the “how” that sets Elixir apart, but not a lot of hands-on, visual applications that provide a tangible representation of its true advantages.</p><p>The questions I found myself unable to answer were: “Why would I choose to build my product with Elixir? What would make Elixir the correct choice vs another established ecosystem?”</p><p>I created this Elixir Boids application to try and provide an answer to those questions. I set out to create a visual representation of the BEAM concurrency model at work, using the classic Boids flocking algorithm originally created by computer graphics researcher Craig Reynolds in 1986.</p><p>In this demonstration, every Boid is its own Erlang process. Up to 5,000 of them concurrently, ticking 30 times a second. Each tick, each individual process investigates its neighboring Boids, and updates its own trajectory accordingly, based on the flocking rules enabled. We can then start breaking things to observe how effective the BEAM is at recovering and returning to a healthy state.</p><h3>What Is A Boid?</h3><p>In the 1980’s, animators had to script the path of individuals in a flock by hand. In 1986, Craig W. Reynolds, a computer graphics researcher, developed an algorithm that treated each individual in a flock as an independent agent that only perceives its nearby neighbors and follows 3 rules:</p><ol><li><strong>Separation: </strong>steer away from neighbors that are too close.</li><li><strong>Cohesion: </strong>steer towards their center.</li><li><strong>Alignment: </strong>match their heading.</li></ol><p>You can read more about Boids from Craig’s own site here: <a href="https://www.red3d.com/cwr/boids/">https://www.red3d.com/cwr/boids/</a></p><p>The concept of individual agents working simultaneously, in isolation, to perform a unit of work (determine their heading based on their perceived neighbors) is a perfect way to visually demonstrate the concurrency model of Elixir and the BEAM VM. The flocking behavior of the whole is an emergent property of the calculations each Boid is doing per tick, which mimics real world production concurrency challenges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*bIaMJ69NA-RJQ7IaC3B68A.gif" /><figcaption>A flock of 500 Boids, each an individual Erlang process, with flocking rules applied</figcaption></figure><h3>How It Works</h3><h4>Setup</h4><p>The world that the Boids exist in is a 1000x1000 unit arena. Each Boid is a GenServer process ticking at ~30 Hz. When spawned, a Boid is given a random position and velocity.</p><pre>@type t :: %Boids.Boid{<br>          position: {float(), float()},<br>          velocity: {float(), float()},<br>          spawned_at_ms: integer() | nil<br>        }</pre><p>On each tick, a Boid performs 4 steps:</p><h4><strong>1. Find Neighbors Efficiently (Spatial Partitioning)</strong></h4><p>Every Boid is given a perception radius of 50 units. All the decisions that a Boid makes in regards to its position and velocity are local. The arena is divided into a 20×20 grid of 50-unit cells, the same size as the perception radius. On every tick, a Boid works out which cell it’s in, and searches only that cell and the 8 around it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U9CLRvYTVMindmxy63FjiQ.png" /></figure><p>The position of every Boid is kept in an in-memory <a href="https://elixir.hexdocs.pm/main/ets.html"><strong>ETS (Erlang Term Storage) Table</strong></a><strong>. </strong>The keys of this table are in the following format:</p><blockquote><strong>{cell_x, cell_y, process_id}</strong></blockquote><p>This key structure provides the indexing in the ETS table when the table is created as an ordered_set. The tuple sorts element by element first (cell_x, then cell_y, then the pid). This means for a given key:</p><blockquote><strong>{8, 11, #PID&lt;0.333.0&gt;}</strong></blockquote><p>every Boid in cell {8, 11} ends up stored side by side. To read a cell, ETS jumps to the first {8, 11, …} row and reads forward until the key moves on to the next cell.</p><p>Along with determining all of the neighboring cells for a Boid, a secondary filter is applied to only return Boids within the given Boid’s perception radius. This secondary filter is provided in the read of the ETS table as a match spec, which ensures that only the Boid neighbors within the current Boid’s perception radius are returned from the ETS table on each tick.</p><p>This spatial partitioning distance filter is necessary for the performance of the simulation. At 5,000 Boids, each Boid copies out about 45 neighbors per tick. Without the grid, every Boid would check its distance to every other Boid. That would require ~25 million checks per tick across the flock.</p><h4>2. <strong>Apply The Rules</strong></h4><p><strong>Separation</strong></p><p>Neighboring Boids closer than 20 units push away with a steering force inversely proportional to the distance of each neighbor. A neighbor 2 units away pushes 5 times harder than a neighbor 10 units away. The separation force of every neighbor is summed and applied to a given Boid per tick.</p><p><strong>Cohesion</strong></p><p>The position of all the neighboring Boids is summed and divided by the number of neighbors. This identifies a central x, y position within a group of Boids. The current Boids position is subtracted from the neighboring Boids average position to create a vector towards the group’s center. That vector is applied as a steering force each tick.</p><p><strong>Alignment</strong></p><p>The velocity of all the neighboring Boids is summed and divided by the number of neighbors. This identifies an average velocity vector for a given group of Boids. The current Boids velocity is subtracted from the group’s average velocity and the resulting vector is applied as a steering force to the current Boid each tick.</p><p>Each of these steering vectors is weighted and summed into a single steering vector applied to each Boid on each tick.</p><h4>3. <strong>Move</strong></h4><p>The combined steering vector is added to the Boid’s velocity, and its speed is clamped to between 2 and 4 units per tick. The Boid then moves by that velocity. If the move would take it past the edge of the arena, it’s held at the wall and bounced back the other way.</p><h4>4. <strong>Publish And Sleep</strong></h4><p>Once the final new position for the Boid is determined, the ETS table is updated with the new position. If the Boid has moved into a new cell, the old row is deleted. The Boid then schedules its next tick with a timer and sleeps until it fires.</p><h3>Let’s Break It</h3><p>The lightweight process concurrency model isn’t the only advantage of Elixir and the BEAM VM. The other major advantage provided by having many independent lightweight processes, is that failures or exceptions encountered within a given process are entirely self-contained. An exception encountered in one process doesn’t escape or bleed into another process. When a given process dies or encounters an unexpected exception, it is quickly restarted by its supervisor. All other processes continue working and the application remains stable and healthy. The Elixir Boids demo provides levers that simulate various breaking scenarios so that you can visually see how quickly the BEAM VM responds and restores full throughput. In the examples provided, newly spawned Boids will show as red for ~1.5 seconds to help visualize how quickly the flock is restored. You will see in the telemetry dashboard that the Boid count holds at 5k and the tick rate is barely affected.</p><h4>1. Kill 4000 Processes</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*TYL-srqxYcSCsyuTuQ9tOQ.gif" /></figure><p>When you manually kill off a given number of Boid processes, the process Supervisor replaces every single one almost instantly.</p><h4>2. Crash 1,000 Boids With Bad Data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*BOyp_9oNzQXMdxYbuvCyYw.gif" /></figure><p>You can also simulate Boid processes encountering bad data and encountering an exception rather than having the process killed manually. The Boid code has no error handling at all. No try/rescue, no defensive checks. When the bad data makes it crash, the damage stays inside that one process. The Supervisor quickly respawns the bad processes, the simulation never drops below 5k Boids, and the tick rate remains steady.</p><h4>3. CPU Hog On Every Core</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*Jd1gpDQk2sL9RWENiOLLwg.gif" /></figure><p>Another massive benefit of Elixir and the BEAM VM is the load balancing provided by the CPU Scheduler. In this scenario, we start a processes that runs on an infinite loop, one per core, eating up the CPU. The scheduler gives each process a small budget of work, then switches to the next one, so no process can hold a core for long, however busy it is. This is called preemptive scheduling, and you can see in this demonstration that the simulation continues to flow and the tick rate per Boid barely moves even when every core is bombarded by a runaway process.</p><h3>Why Only 5,000?</h3><p>An idle process costs ~2.6 KB of memory, and the BEAM runs millions concurrently. In most production scenarios, those millions of processes are sitting idle waiting for instructions.</p><p>In this demo, every process is busy all the time. The limit comes from the amount of work each process does per tick, not the number of processes the BEAM can handle. 5,000 Boids × 30 ticks a second is 150,000 wake-ups a second, each one doing non-trivial computation.</p><h3>Try It Yourself</h3><p><a href="https://github.com/the-sovereign-craftsman/elixir-boids">GitHub - the-sovereign-craftsman/elixir-boids: A boids flocking simulation in Elixir/Phoenix where every boid is its own BEAM process, with live telemetry and failure demos.</a></p><p>The README provides instructions for pulling down the application and running it yourself locally. If you don’t have Elixir and Erlang installed on your machine, you can run the application image via Docker.</p><pre>docker run --rm -p 4000:4000 ghcr.io/the-sovereign-craftsman/elixir-boids</pre><h3>In Conclusion</h3><p>Hopefully this simulation provides strong visual examples of the power of Elixir and the BEAM VM. Replace the concept of Boids with more practical production concepts such as Connections, Jobs, Sessions, or any non-trivial unit of work and you can start to see why this technology is chosen for high throughput, fault tolerance and concurrency.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7cac0b57096c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>