<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Jaehyeon Kim</title><link>https://jaehyeon.me/</link><description>Data Engineer | Data Streaming | Powering ML &amp; AI in Real Time</description><language>en</language><atom:link href="https://jaehyeon.me/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Fri, 30 Oct 2026 00:00:00 +0000</lastBuildDate><item><title>desflow from the Command Line: One Model, Every Command</title><link>https://jaehyeon.me/blog/2026-10-30-desflow-command-line/</link><guid>https://jaehyeon.me/blog/2026-10-30-desflow-command-line/</guid><pubDate>Fri, 30 Oct 2026 00:00:00 +0000</pubDate><category>Discrete Event Simulation</category><category>desflow</category><category>SimPy</category><category>YAML</category><category>Queueing Theory</category><category>Python</category><description>&lt;p>A simulation study answers a question about a system by building a model of it and running the model on a computer. Here the system is a shop that packs orders, and the question is whether it needs a third packer. A study goes through the same steps each time: get a model, check that it makes sense, look at what it produces, run it many times, compare the options, and base its inputs on real measurements.&lt;/p>
&lt;p>desflow has a command for each step. The &lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/">previous post&lt;/a> said what changed in desflow 0.25.0. This post runs every command on one model and shows what each prints. Everything runs on your own machine, in one folder, without Docker.&lt;/p></description><content:encoded>&lt;p>A simulation study answers a question about a system by building a model of it and running the model on a computer. Here the system is a shop that packs orders, and the question is whether it needs a third packer. A study goes through the same steps each time: get a model, check that it makes sense, look at what it produces, run it many times, compare the options, and base its inputs on real measurements.&lt;/p>
&lt;p>desflow has a command for each step. The &lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/">previous post&lt;/a> said what changed in desflow 0.25.0. This post runs every command on one model and shows what each prints. Everything runs on your own machine, in one folder, without Docker.&lt;/p>
&lt;p>&lt;picture>&lt;img class="img-fluid " alt="desflow examples get, desflow explain, desflow run &amp;amp;ndash;sample and desflow run &amp;amp;ndash;replications on the shop, in one terminal" src="https://jaehyeon.me/blog/2026-10-30-desflow-command-line/command-line.gif" loading="lazy" width="1200" height="720" />
&lt;/picture>

&lt;/p>

&lt;h2 id="install" data-numberify>Install&lt;a class="anchor ms-1" href="#install" aria-label="Link to this section: Install">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">pip install &lt;span class="s2">&amp;#34;desflow[fit]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">desflow --version
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">0.25.0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>fit&lt;/code> extra adds scipy and matplotlib, which only &lt;code>desflow fit&lt;/code> needs. &lt;code>desflow&lt;/code> on its own lists the commands, and &lt;code>--help&lt;/code> after any command shows its options with examples.&lt;/p>

&lt;h2 id="get-the-model" data-numberify>Get the Model&lt;a class="anchor ms-1" href="#get-the-model" aria-label="Link to this section: Get the Model">&lt;/a>&lt;/h2>
&lt;p>desflow comes with example models. &lt;code>desflow examples list&lt;/code> lists them, with any service, such as Kafka, that an example needs:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow examples list
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">local prints events and telemetry, with no container (no service)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">kafka takes updates from Kafka and publishes events and telemetry to Kafka (odctl up kafka-lite)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">parquet a week generated unpaced, written to Parquet (no service)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">experiment two designs of the shop compared with paired replications, a check, observed waits and a report (no service)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">fitting the packing time fitted from 40 measured times, then loaded into the shop (no service)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">related-records orders and their line items, in two PostgreSQL tables (odctl up postgres)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>desflow examples get&lt;/code> downloads one into a folder named after it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow examples get experiment --api yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">wrote experiment/yaml/experiment.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">wrote experiment/yaml/experiment_shop.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">wrote experiment/observed_waits.csv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">Find the bottleneck, with no run: cd experiment/yaml &amp;amp;&amp;amp; desflow explain experiment_shop.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">Run the yaml version: cd experiment/yaml &amp;amp;&amp;amp; desflow experiment experiment.yaml
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>experiment_shop.yaml&lt;/code> is the model of the shop:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">simulation&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">sim_id&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Shop&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">factor&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">0&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># run as fast as possible, not in real time&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">random_seed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">42&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># the same random draws on every run&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">resources&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">current_cap: 2, max_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">4&lt;/span>}&lt;span class="w"> &lt;/span>&lt;span class="c"># 2 packers, at most 4&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">services&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packing&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: gamma, shape: 4, scale&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">1&lt;/span>}&lt;span class="w"> &lt;/span>&lt;span class="c"># 4 seconds on average, varying&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">arrivals&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">orders&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: exponential, rate: 0.4, spawn&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">pack} &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># an order every 2.5 seconds on average&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">tasks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">pack&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">service&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packing&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">resource&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">payload&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">event_type&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">order_packed}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Orders arrive at random moments, every 2.5 seconds on average. Each one waits for a free packer, and packing takes 4 seconds on average, sometimes more and sometimes less. &lt;code>experiment.yaml&lt;/code> is the study that compares two packers with three, and &lt;code>observed_waits.csv&lt;/code> holds the wait measured in the real shop. The commands below run from &lt;code>experiment/yaml&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> experiment/yaml
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="check-it-makes-sense" data-numberify>Check It Makes Sense&lt;a class="anchor ms-1" href="#check-it-makes-sense" aria-label="Link to this section: Check It Makes Sense">&lt;/a>&lt;/h2>
&lt;p>Before running anything, it helps to know whether the packers can keep up. Queueing theory, the mathematics of waiting lines, describes a line such as the shop&amp;rsquo;s with a few numbers, each written as a letter:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>λ (lambda), the arrival rate:&lt;/strong> how many orders arrive a second. An order every 2.5 seconds is 0.4 a second.&lt;/li>
&lt;li>&lt;strong>μ (mu), the service rate:&lt;/strong> how many orders one packer finishes a second. Packing takes 4 seconds, so one packer finishes 0.25 a second.&lt;/li>
&lt;li>&lt;strong>c, the capacity:&lt;/strong> the number of packers, 2.&lt;/li>
&lt;li>&lt;strong>ρ (rho), the utilisation:&lt;/strong> the share of time the packers are busy. Two packers finish c × μ = 0.5 orders a second while 0.4 arrive, so ρ = λ ÷ (c × μ) = 0.4 ÷ 0.5 = 0.8. They are busy 80% of the time.&lt;/li>
&lt;/ul>
&lt;p>When ρ is 1 or more, orders arrive faster than they are packed, and the queue grows for as long as the shop stays open.&lt;/p>
&lt;p>&lt;code>desflow explain&lt;/code> works these numbers out from the model, for every resource, and prints them:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow explain experiment_shop.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">Shop
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">`-- orders (arrival, rate 0.4/s)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> `-- spawn -&amp;gt; pack (task, rate 0.4/s)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> +-- uses packers (resource)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> `-- next (share 1) -&amp;gt; exit
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">Resources
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">+---------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">| resource | λ | μ | c | ρ | Wq | L | status |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">|----------+-----+------+---+-----+----+---+--------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">| packers | 0.4 | 0.25 | 2 | 0.8 | - | - | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">+---------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">packers by units
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">+-----------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">| c | ρ | status |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">|---+--------+----------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">| 1 | 1.6 | UNSTABLE |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">| 2 | 0.8 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">| 3 | 0.5333 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">| 4 | 0.4 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">+-----------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">Bottleneck: packers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">Not estimated:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl"> wait at packers: no closed formula for c &amp;gt; 1 with non-exponential times; run --verify to measure it
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl">Hints for utilisation 0.85:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl"> packers with 2 units: utilisation 0.8, at most 0.85
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl"> packers with 2 units: utilisation 0.85 at a request every 2.35 seconds (25.5 a minute, 0.425 a second)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The output reads from the top:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Graph:&lt;/strong> each order starts a &lt;code>pack&lt;/code> task, which uses a packer and then leaves. &lt;code>rate 0.4/s&lt;/code> is λ.&lt;/li>
&lt;li>&lt;strong>Resources:&lt;/strong> λ, μ, c and ρ for the packers, as worked out above. Wq is the average time an order waits in the queue, and L is the average number of orders in the shop, waiting or being packed. &lt;code>status&lt;/code> is &lt;code>OK&lt;/code> while ρ is below 0.85, &lt;code>CONGESTED&lt;/code> from 0.85, where the queue gets long quickly, and &lt;code>UNSTABLE&lt;/code> from 1.&lt;/li>
&lt;li>&lt;strong>packers by units:&lt;/strong> ρ for one to four packers, which shows how many packers the shop needs. One packer gives ρ = 0.4 ÷ 0.25 = 1.6, so it is &lt;code>UNSTABLE&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Bottleneck:&lt;/strong> the resource with the highest ρ.&lt;/li>
&lt;li>&lt;strong>Not estimated:&lt;/strong> Wq and L are blank. A formula gives them only for simpler shops, such as one with a single packer, so &lt;code>explain&lt;/code> says so rather than guess.&lt;/li>
&lt;li>&lt;strong>Hints:&lt;/strong> 0.85 is where &lt;code>CONGESTED&lt;/code> starts. The first line says two packers are below it. The second gives the arrival rate at which they reach it, only a little faster than the order every 2.5 seconds the shop has now.&lt;/li>
&lt;/ul>
&lt;p>&lt;code>--verify&lt;/code> runs the model to measure what no formula gave, and to check the arithmetic. Here it runs one hour of the shop ten times:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow explain experiment_shop.yaml --verify --replications 10 --until 1h
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">Verify: 10 runs to 3600 s, seed 42
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">+------------------------------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">| node | measure | analysis | measured | ± | result |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">|---------+--------------+----------+----------+----------+--------------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">| packers | utilization | 0.8 | 0.7953 | 0.01561 | agrees |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">| packers | queue_length | - | 1.823 | 0.2856 | not compared |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">| pack | wait | - | 4.505 | 0.6483 | not compared |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">| orders | throughput | 0.4 | 0.4002 | 0.007201 | agrees |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">+------------------------------------------------------------------------+
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>...&lt;/code> stands for the same output as before. Each row of the table is one measure of one part of the model:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>node:&lt;/strong> the part measured: the &lt;code>packers&lt;/code>, the &lt;code>pack&lt;/code> task or the &lt;code>orders&lt;/code> arrival.&lt;/li>
&lt;li>&lt;strong>measure:&lt;/strong> what was measured. &lt;code>throughput&lt;/code> is how many orders leave the shop a second.&lt;/li>
&lt;li>&lt;strong>analysis:&lt;/strong> the value the arithmetic gives, or &lt;code>-&lt;/code> where no formula applies.&lt;/li>
&lt;li>&lt;strong>measured:&lt;/strong> the average over the ten runs.&lt;/li>
&lt;li>&lt;strong>±:&lt;/strong> give or take: the true average very likely lies within this distance of the measured one (a 95% confidence interval).&lt;/li>
&lt;li>&lt;strong>result:&lt;/strong> &lt;code>agrees&lt;/code> when the analysis lies within that range, &lt;code>differs&lt;/code> when it does not, and &lt;code>not compared&lt;/code> when there is no analysis to compare.
The packers are as busy as the arithmetic says, and orders leave as fast as they arrive, so the model does what its settings describe. The runs also measure the wait, which the arithmetic could not give.&lt;/li>
&lt;/ul>

&lt;h2 id="look-at-what-it-produces" data-numberify>Look at What It Produces&lt;a class="anchor ms-1" href="#look-at-what-it-produces" aria-label="Link to this section: Look at What It Produces">&lt;/a>&lt;/h2>
&lt;p>A model produces events: one record each time something happens to an order. &lt;code>desflow run --sample&lt;/code> prints the first few, without sending anything to the model&amp;rsquo;s destinations, such as Kafka or a database:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow run experiment_shop.yaml --sample 5
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">{&amp;#34;stream_type&amp;#34;:&amp;#34;event&amp;#34;,&amp;#34;sim_ts&amp;#34;:3.19,&amp;#34;timestamp&amp;#34;:&amp;#34;2026-10-27T01:47:34.769+00:00&amp;#34;,&amp;#34;key&amp;#34;:&amp;#34;orders-1&amp;#34;,&amp;#34;task&amp;#34;:&amp;#34;pack&amp;#34;,&amp;#34;instance&amp;#34;:0,&amp;#34;value&amp;#34;:{&amp;#34;path_id&amp;#34;:&amp;#34;Shop.service.packing&amp;#34;,&amp;#34;status&amp;#34;:&amp;#34;queued&amp;#34;}}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">{&amp;#34;stream_type&amp;#34;:&amp;#34;event&amp;#34;,&amp;#34;sim_ts&amp;#34;:3.19,&amp;#34;timestamp&amp;#34;:&amp;#34;2026-10-27T01:47:34.769+00:00&amp;#34;,&amp;#34;key&amp;#34;:&amp;#34;orders-1&amp;#34;,&amp;#34;task&amp;#34;:&amp;#34;pack&amp;#34;,&amp;#34;instance&amp;#34;:0,&amp;#34;value&amp;#34;:{&amp;#34;path_id&amp;#34;:&amp;#34;Shop.service.packing&amp;#34;,&amp;#34;status&amp;#34;:&amp;#34;started&amp;#34;}}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">{&amp;#34;stream_type&amp;#34;:&amp;#34;event&amp;#34;,&amp;#34;sim_ts&amp;#34;:3.37,&amp;#34;timestamp&amp;#34;:&amp;#34;2026-10-27T01:47:34.949+00:00&amp;#34;,&amp;#34;key&amp;#34;:&amp;#34;orders-2&amp;#34;,&amp;#34;task&amp;#34;:&amp;#34;pack&amp;#34;,&amp;#34;instance&amp;#34;:1,&amp;#34;value&amp;#34;:{&amp;#34;path_id&amp;#34;:&amp;#34;Shop.service.packing&amp;#34;,&amp;#34;status&amp;#34;:&amp;#34;queued&amp;#34;}}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">{&amp;#34;stream_type&amp;#34;:&amp;#34;event&amp;#34;,&amp;#34;sim_ts&amp;#34;:3.37,&amp;#34;timestamp&amp;#34;:&amp;#34;2026-10-27T01:47:34.949+00:00&amp;#34;,&amp;#34;key&amp;#34;:&amp;#34;orders-2&amp;#34;,&amp;#34;task&amp;#34;:&amp;#34;pack&amp;#34;,&amp;#34;instance&amp;#34;:1,&amp;#34;value&amp;#34;:{&amp;#34;path_id&amp;#34;:&amp;#34;Shop.service.packing&amp;#34;,&amp;#34;status&amp;#34;:&amp;#34;started&amp;#34;}}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">{&amp;#34;stream_type&amp;#34;:&amp;#34;event&amp;#34;,&amp;#34;sim_ts&amp;#34;:4.672,&amp;#34;timestamp&amp;#34;:&amp;#34;2026-10-27T01:47:36.250+00:00&amp;#34;,&amp;#34;key&amp;#34;:&amp;#34;orders-3&amp;#34;,&amp;#34;task&amp;#34;:&amp;#34;pack&amp;#34;,&amp;#34;instance&amp;#34;:2,&amp;#34;value&amp;#34;:{&amp;#34;path_id&amp;#34;:&amp;#34;Shop.service.packing&amp;#34;,&amp;#34;status&amp;#34;:&amp;#34;queued&amp;#34;}}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each order is &lt;code>queued&lt;/code> when it joins the line and &lt;code>started&lt;/code> when a packer takes it. The first two orders find a packer free and start at once. &lt;code>sim_ts&lt;/code> is the time in the simulation, in seconds from the start.&lt;/p>
&lt;p>&lt;code>--stdout&lt;/code> prints every record of a full run instead of sending it on, so it can go to a file. This line runs the shop for 20 seconds, then lets the orders still inside finish:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">desflow run experiment_shop.yaml --seed &lt;span class="m">42&lt;/span> --until &lt;span class="m">20&lt;/span> --drain --stdout &amp;gt; records.jsonl
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="run-it-many-times" data-numberify>Run It Many Times&lt;a class="anchor ms-1" href="#run-it-many-times" aria-label="Link to this section: Run It Many Times">&lt;/a>&lt;/h2>
&lt;p>Arrival and packing times are random, so each run gives a slightly different answer. One run is like one day in the shop: a quiet day or a busy one by chance. &lt;code>--replications&lt;/code> runs the model several times, each with different random draws, and reports the average of each measure with a range around it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow run experiment_shop.yaml --replications 10 --seed 42 --until 1h
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">measure n mean half-width (95%)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">resources.packers.capacity 10 2 0
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">resources.packers.in_use 10 1.8 0.3016
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">resources.packers.queue_length 10 4 3.422
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">resources.packers.utilization 10 90 15.08
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">resources.packers.utilization_avg 10 79.53 1.561
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">resources.packers.queue_length_avg 10 1.823 0.2856
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">tasks.pack.in_progress 10 5.8 3.566
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">tasks.pack.finished 10 1441 25.92
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">tasks.pack.wait_mean 10 4.505 0.6483
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">tasks.pack.wait_max 10 28.84 4.347
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">tasks.pack.service_mean 10 3.972 0.04102
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">tasks.pack.service_max 10 14.64 0.8328
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">flows.orders.finished 10 1441 25.92
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">flows.orders.started 10 1446 26.78
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">flows.orders.in_system 10 5.8 3.566
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">Seed 42; replication seeds: 2684470948, 4091952314, 233227757, 3276785861, 3644269654, 1206282609, 3543069911, 3479688010, 877132087, 1244265337
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">Repeat one replication with: desflow run experiment_shop.yaml --seed &amp;lt;its seed&amp;gt; --until 1h
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Read &lt;code>tasks.pack.wait_mean&lt;/code> as: orders wait 4.5 seconds on average, give or take 0.65. The true average is very likely within that range (a 95% confidence interval). More runs make the range narrower.&lt;/p>
&lt;p>A measure ending in &lt;code>_avg&lt;/code> is averaged over the whole hour. One without it, such as &lt;code>utilization&lt;/code>, is a single reading at the moment the run ends. That is why its range is so much wider. The last line gives the command that repeats any one of the ten runs exactly.&lt;/p>

&lt;h2 id="compare-two-options" data-numberify>Compare Two Options&lt;a class="anchor ms-1" href="#compare-two-options" aria-label="Link to this section: Compare Two Options">&lt;/a>&lt;/h2>
&lt;p>Would a third packer keep the average wait under 2 seconds? &lt;code>desflow experiment&lt;/code> answers that from a file. It runs one hour of the shop 20 times with two packers and 20 times with three:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">experiment&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">question&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Does a third packer keep the mean wait under 2 seconds?&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">model&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">experiment_shop.yaml&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">until&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">1&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">h&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">warmup&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">min &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># leave out the first 3 minutes, when the shop is still empty&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">replications&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">20&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">seed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">42&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">pairing&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">common &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># both versions see the same orders&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">measures&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="l">tasks.pack.wait_mean, resources.packers.utilization_avg]&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">variants&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">two&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">three&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">Shop.resources.packers.current_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">compare&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">baseline: two, variant&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">three}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">checks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">measure: tasks.pack.wait_mean, variant: three, max&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">2&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">observed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">file&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">../observed_waits.csv &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># the mean wait measured in the real shop&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">variant&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">two &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># the real shop has two packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">output&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">results/packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow experiment experiment.yaml --output results/packers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">Wrote the artifacts and report.md to results/packers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">passed: three: tasks.pack.wait_mean max 2, checked 0.5919
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Both versions see the same orders at the same times, so a difference between them comes from the third packer, not from luck. The check passes: with three packers, even the top of the range of the average wait is under 2 seconds. The report, &lt;code>report.md&lt;/code>, puts the two side by side:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Packers&lt;/th>
&lt;th>Average wait (s)&lt;/th>
&lt;th>Range&lt;/th>
&lt;th>Packers busy (%)&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>two&lt;/td>
&lt;td>4.872&lt;/td>
&lt;td>4.323 to 5.422&lt;/td>
&lt;td>80.56&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>three&lt;/td>
&lt;td>0.5389&lt;/td>
&lt;td>0.4859 to 0.5919&lt;/td>
&lt;td>53.74&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The experiment also compares two packers with the real shop. The wait measured there falls inside the range the model gives for two packers, which is evidence that the model describes the shop well. The command fails when a check fails, so it can run in a CI pipeline. &lt;code>desflow report results/packers&lt;/code> builds the report again from the saved results, without running anything.&lt;/p>

&lt;h2 id="base-the-inputs-on-measurements" data-numberify>Base the Inputs on Measurements&lt;a class="anchor ms-1" href="#base-the-inputs-on-measurements" aria-label="Link to this section: Base the Inputs on Measurements">&lt;/a>&lt;/h2>
&lt;p>The packing time in a model should come from measured times, not a guess. Measured times vary, and the model needs the shape of that variation: a distribution. The &lt;code>fitting&lt;/code> example holds 40 packing times timed at a bench, and &lt;code>desflow fit&lt;/code> tries each kind of distribution on them:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow examples get fitting --api yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">$ desflow fit fitting/packing_times.csv --column packing_seconds --lower 0
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">40 rows: 40 used, 0 missing, 0 invalid
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">family status AIC KS stat KS p (approx.) parameters or reason
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">exponential rejected its std is 1.5, the data&amp;#39;s is 0.4832: more than 25% apart
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">normal fitted 59.33 0.157 0.2504 mean=1.5, std=0.483218 (can draw below the lower bound 0)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">lognormal fitted 57.33 0.09406 0.8385 mean=1.50251, std=0.511854
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">truncated_normal fitted 59.25 0.1558 0.2579 mean=1.49836, std=0.48579, low=0
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">constant skipped the values are not all equal
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">uniform fitted 59.45 0.225 0.02923 low=0.7, high=2.7
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">triangular * fitted 56.68 0.1258 0.511 low=0.546332, mode=1.2, high=2.83766
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">gamma fitted 56.79 0.1157 0.6165 shape=9.55777, scale=0.15694
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">weibull fitted 58.99 0.1502 0.2972 shape=3.33522, scale=1.67203
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">empirical fitted values: 40 numbers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">* best: triangular
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each row is one kind of distribution. AIC scores how well it fits, and lower is better. The KS p column tests whether the fit is believable at all: a value near 0, as for &lt;code>uniform&lt;/code>, means it is not. The triangular fits best, just ahead of the gamma. &lt;code>--lower 0&lt;/code> says a time cannot be negative, which is why the normal is flagged. &lt;code>--out&lt;/code> writes the best fit as a setting the model can load.&lt;/p>
&lt;p>&lt;code>desflow dist&lt;/code> converts in the other direction. Given an average and a spread, it gives the settings a distribution needs:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow dist gamma --mean 1.5 --std 0.4832
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">parameters: shape 9.63671, scale 0.155655
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">mean 1.5, std 0.4832
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">variability: low (std / mean 0.322)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">with no random generator, every draw is 1.5
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">YAML: {dist: gamma, shape: 9.63671, scale: 0.155655}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">Python: dist=&amp;#34;gamma&amp;#34;, shape=9.63671, scale=0.155655
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The last two lines are ready to paste into a model. &lt;code>desflow dist&lt;/code> on its own lists every kind of distribution.&lt;/p>

&lt;h2 id="set-up-an-editor" data-numberify>Set Up an Editor&lt;a class="anchor ms-1" href="#set-up-an-editor" aria-label="Link to this section: Set Up an Editor">&lt;/a>&lt;/h2>
&lt;p>&lt;code>desflow schema&lt;/code> prints a description of every setting a model can have, as a JSON Schema:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">desflow schema &amp;gt; blueprint.schema.json
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>With the Red Hat YAML extension in VS Code, this line at the top of a model makes the editor suggest settings and mark a misspelt one as you type:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c"># yaml-language-server: $schema=./blueprint.schema.json&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;a href="https://jaehyeon.me/desflow/latest/guides/set-up-your-editor/">Set Up Your Editor&lt;/a> shows other ways to point the editor at it.&lt;/p>

&lt;h2 id="every-command-in-one-place" data-numberify>Every Command in One Place&lt;a class="anchor ms-1" href="#every-command-in-one-place" aria-label="Link to this section: Every Command in One Place">&lt;/a>&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Command&lt;/th>
&lt;th>What it does&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>desflow examples&lt;/code>&lt;/td>
&lt;td>lists the example models and downloads one&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow explain&lt;/code>&lt;/td>
&lt;td>checks that a model makes sense, without running it, and with &lt;code>--verify&lt;/code> by running it&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow run&lt;/code>&lt;/td>
&lt;td>runs a model, prints its first events, or runs it many times&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow experiment&lt;/code>&lt;/td>
&lt;td>compares versions of a model and writes a report&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow report&lt;/code>&lt;/td>
&lt;td>builds an experiment&amp;rsquo;s report again from its saved results&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow fit&lt;/code>&lt;/td>
&lt;td>finds the distribution that matches measured times&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow dist&lt;/code>&lt;/td>
&lt;td>lists the distributions and converts their settings&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>desflow schema&lt;/code>&lt;/td>
&lt;td>prints the description of every setting, for an editor&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Each command calls the same library function a Python script would, so both give the same result. &lt;a href="https://jaehyeon.me/desflow/latest/user-guide/command-line/">Command Line&lt;/a> has every option on the same shop, and &lt;a href="https://jaehyeon.me/desflow/latest/reference/cli/">CLI&lt;/a> gives the help of every command.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related Posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related Posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/">From dynamic-des to desflow: What Changed in Five Releases&lt;/a> - what each command was added for&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">Defining Data-Streaming Simulations in YAML, Without Writing Python&lt;/a> - the YAML format the shop is written in&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">pip install &lt;span class="s2">&amp;#34;desflow[fit]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">desflow examples get experiment --api yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> experiment/yaml &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> desflow explain experiment_shop.yaml
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">jaehyeon-kim/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/desflow/" target="_blank" rel="noopener noreferrer">pypi.org/project/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>Documentation:&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/">jaehyeon.me/desflow&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>From dynamic-des to desflow: What Changed in Five Releases</title><link>https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/</link><guid>https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/</guid><pubDate>Wed, 28 Oct 2026 00:00:00 +0000</pubDate><category>Discrete Event Simulation</category><category>desflow</category><category>SimPy</category><category>YAML</category><category>Queueing Theory</category><category>Python</category><description>&lt;p>A simulation is a small model of a real system that a computer plays forward in time. Take a shop that packs orders. Orders arrive at random moments, packers take one at a time, and packing takes longer for some orders than for others. When orders arrive faster than the packers can finish them, a queue forms. A simulation shows how long that queue gets and how long orders wait, without running the real shop.&lt;/p>
&lt;p>The &lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">last post&lt;/a> in this series showed such a model written as a YAML file and run with one command, with dynamic-des 0.16.0 to 0.19.0. The package is now called desflow. It still sends its events to Kafka, Redis, PostgreSQL, Parquet and Iceberg, and it can now also answer the question a simulation is usually built for, such as &amp;ldquo;would a third packer keep the wait under 2 seconds?&amp;rdquo;. Everything below runs on your own machine, without Docker.&lt;/p></description><content:encoded>&lt;p>A simulation is a small model of a real system that a computer plays forward in time. Take a shop that packs orders. Orders arrive at random moments, packers take one at a time, and packing takes longer for some orders than for others. When orders arrive faster than the packers can finish them, a queue forms. A simulation shows how long that queue gets and how long orders wait, without running the real shop.&lt;/p>
&lt;p>The &lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">last post&lt;/a> in this series showed such a model written as a YAML file and run with one command, with dynamic-des 0.16.0 to 0.19.0. The package is now called desflow. It still sends its events to Kafka, Redis, PostgreSQL, Parquet and Iceberg, and it can now also answer the question a simulation is usually built for, such as &amp;ldquo;would a third packer keep the wait under 2 seconds?&amp;rdquo;. Everything below runs on your own machine, without Docker.&lt;/p>
&lt;p>This post covers what is new since dynamic-des 0.19.0:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/#a-new-name">A new name&lt;/a>:&lt;/strong> why dynamic-des is now desflow, and what to change.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/#modelling-more-of-a-real-system">More of a real system&lt;/a>:&lt;/strong> customers who give up waiting, machines that break, and every feature of SimPy in YAML.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/#trusting-the-numbers">Numbers you can trust&lt;/a>:&lt;/strong> repeated runs, experiments that compare two versions of a model, and inputs fitted to measured times.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/#checking-a-model-before-running-it">A check before running&lt;/a>:&lt;/strong> 0.25.0 shows whether a model makes sense before it runs.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/#help-with-writing-a-model">Help with writing a model&lt;/a>:&lt;/strong> an editor suggests each setting as you type.&lt;/li>
&lt;/ul>

&lt;h2 id="a-new-name" data-numberify>A New Name&lt;a class="anchor ms-1" href="#a-new-name" aria-label="Link to this section: A New Name">&lt;/a>&lt;/h2>
&lt;p>dynamic-des became &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> in 0.20.0. The new name reflects a wider scope: from sending simulated events to whole simulation studies, with a server to run them next. &lt;a href="https://jaehyeon.me/desflow/latest/about/why-desflow/">Why desflow&lt;/a> explains the name. Only the names changed in that release:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">pip install desflow &lt;span class="c1"># was: pip install dynamic-des&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">python -c &lt;span class="s2">&amp;#34;import desflow&amp;#34;&lt;/span> &lt;span class="c1"># was: import dynamic_des&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">desflow --version &lt;span class="c1"># was: ddes --version&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The old names still work, with a warning, until 0.30.0. The documentation moved to &lt;a href="https://jaehyeon.me/desflow/">jaehyeon.me/desflow&lt;/a>. Later releases changed some behaviour as well. If you upgrade a model from dynamic-des, the Upgrading section of each release in the &lt;a href="https://jaehyeon.me/desflow/latest/about/changelog/">Changelog&lt;/a> lists what to change.&lt;/p>

&lt;h2 id="modelling-more-of-a-real-system" data-numberify>Modelling More of a Real System&lt;a class="anchor ms-1" href="#modelling-more-of-a-real-system" aria-label="Link to this section: Modelling More of a Real System">&lt;/a>&lt;/h2>
&lt;p>A real shop has more going on than orders, packers and a packing time: customers give up when the queue is long, machines break, and some work needs two people at once. desflow runs on &lt;a href="https://simpy.readthedocs.io/" target="_blank" rel="noopener noreferrer">SimPy&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, the standard Python library for this kind of simulation, so a model written in Python could always build these by hand. Since 0.22.0, a model declares each of them in a few lines of YAML or builder code, with no SimPy code to write.&lt;/p>
&lt;p>Customers who give up take two settings. In this excerpt of the &lt;code>patience&lt;/code> example, each order waits at most a set number of seconds before it leaves, and after 100 seconds the shop takes no new orders:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">arrivals&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">orders&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: exponential, rate: 0.5, spawn: pack, attributes&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">patience&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">20&lt;/span>}}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">walk_ins&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: exponential, rate: 0.1, spawn: pack, attributes&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">patience&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">10&lt;/span>}}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">tasks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">pack&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">service&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packing&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">resource&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">patience&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">attr.patience &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># leave after waiting this long&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">balk&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">t &amp;gt;= 100 &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># turn new orders away from 100 seconds&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>desflow records each order that gives up and each one turned away, as events and as counts.&lt;/p>
&lt;p>A machine that breaks takes one more block. In the &lt;code>breakdowns&lt;/code> example, a packing station fails about once a minute and takes 5 to 15 seconds to repair. The order it was packing waits and then carries on:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">resources&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">station&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">current_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">2&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">max_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">breakdown&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">time_to_failure&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: exponential, rate&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">0.0167&lt;/span>}&lt;span class="w"> &lt;/span>&lt;span class="c"># about once a minute&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">repair&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: uniform, low: 5 s, high&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">15&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">s}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">work&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">interrupt&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">tasks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">pack&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">service&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packing&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">resource&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">station&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">on_interrupt&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">resume&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each failure and each repair is recorded as an event. That record is useful beyond the simulation: it is the known answer that a fault detection model can be trained and tested against.&lt;/p>
&lt;p>Releases 0.21.0 to 0.23.0 also add:&lt;/p>
&lt;ul>
&lt;li>more ways to describe a time that varies, and durations such as &lt;code>5 min&lt;/code> anywhere a time is set;&lt;/li>
&lt;li>stock that tasks take from and add to, such as parcels on a shelf;&lt;/li>
&lt;li>orders that go through several steps, where the next step depends on the order;&lt;/li>
&lt;li>work that interrupts other work, such as an urgent order;&lt;/li>
&lt;li>ways to end a run, such as closing the shop and finishing the orders already inside;&lt;/li>
&lt;li>replaying the arrival times of a real day from a file.&lt;/li>
&lt;/ul>
&lt;p>Since 0.23.0, a model can use every feature of SimPy 4.1.2 in YAML as well as in Python. The &lt;a href="https://jaehyeon.me/desflow/latest/reference/simpy-compatibility/">SimPy Compatibility&lt;/a> page lists each feature with the test that checks it against SimPy.&lt;/p>

&lt;h2 id="trusting-the-numbers" data-numberify>Trusting the Numbers&lt;a class="anchor ms-1" href="#trusting-the-numbers" aria-label="Link to this section: Trusting the Numbers">&lt;/a>&lt;/h2>
&lt;p>A simulation draws its arrival and packing times at random, so two runs give different answers. One run might show a short wait by luck. To trust a result, run the model many times and look at the range the answers fall in. Releases 0.21.0 and 0.22.0 add the tools for this:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Statistics:&lt;/strong> each run measures waits, queue lengths and how busy each resource was. The first minutes can be left out, because the shop starts empty and would make the wait look shorter.&lt;/li>
&lt;li>&lt;strong>Repeated runs:&lt;/strong> &lt;code>desflow run --replications 10&lt;/code> runs a model ten times and gives the average of each measure, with a range that the true average very likely falls in (a 95% confidence interval).&lt;/li>
&lt;li>&lt;strong>Experiments:&lt;/strong> &lt;code>desflow experiment&lt;/code> compares versions of a model and writes a report.&lt;/li>
&lt;li>&lt;strong>Fitting inputs:&lt;/strong> &lt;code>desflow fit&lt;/code> turns measured times into a setting for the model.&lt;/li>
&lt;/ul>
&lt;p>The shop of the &lt;code>experiment&lt;/code> example has two packers. An order arrives every 2.5 seconds on average, and packing takes 4 seconds on average. The example asks whether a third packer keeps the average wait under 2 seconds. The example holds the model, the wait measured in the real shop, and the study file &lt;code>experiment.yaml&lt;/code>. The study runs one hour of the shop 20 times with two packers and 20 times with three:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">experiment&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">question&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Does a third packer keep the mean wait under 2 seconds?&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">model&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">experiment_shop.yaml&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">until&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">1&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">h&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">warmup&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">min &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># leave out the first 3 minutes&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">replications&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">20&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">seed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">42&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">pairing&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">common &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># both versions see the same orders&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">measures&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="l">tasks.pack.wait_mean, resources.packers.utilization_avg]&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">variants&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">two&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">three&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">Shop.resources.packers.current_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">compare&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">baseline: two, variant&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">three}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">checks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">measure: tasks.pack.wait_mean, variant: three, max&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">2&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">observed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">file&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">../observed_waits.csv &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># the mean wait measured in the real shop&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">variant&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">two &lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="c"># the real shop has two packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">$ desflow examples get experiment --api yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">wrote experiment/yaml/experiment.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">wrote experiment/yaml/experiment_shop.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">wrote experiment/observed_waits.csv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">$ cd experiment/yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">$ desflow experiment experiment.yaml --output results/packers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">Wrote the artifacts and report.md to results/packers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">9&lt;/span>&lt;span class="cl">passed: three: tasks.pack.wait_mean max 2, checked 0.5919
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Both versions see the same orders at the same times, so any difference comes from the third packer and not from luck. With three packers, even the top of the range of the average wait is well under 2 seconds, so the check passes. The experiment also compares two packers with the real shop: the wait measured there falls inside the range the model gives, which is evidence that the model describes the shop well.&lt;/p>
&lt;p>The report, &lt;code>report.md&lt;/code>, records the question, the model, the inputs and where they came from, every result and the limits of the study. The command fails when a check fails, so it can run in a CI pipeline.&lt;/p>

&lt;h2 id="checking-a-model-before-running-it" data-numberify>Checking a Model Before Running It&lt;a class="anchor ms-1" href="#checking-a-model-before-running-it" aria-label="Link to this section: Checking a Model Before Running It">&lt;/a>&lt;/h2>
&lt;p>A model with a wrong setting still runs and gives numbers that look reasonable, so nothing in the output shows the mistake. Release 0.25.0 adds a check that compares a model with simple arithmetic before it runs.&lt;/p>
&lt;p>The recording shows this on the shop in three steps. First, desflow checks the model without running it. Then it runs the model ten times to confirm the check. Last, it runs the experiment from the previous section.&lt;/p>
&lt;p>&lt;picture>&lt;img class="img-fluid " alt="desflow explain on the shop, then explain &amp;amp;ndash;verify with ten one-hour runs that agree with the analysis, then desflow experiment, whose check that three packers keep the mean wait under 2 seconds passes" src="https://jaehyeon.me/blog/2026-10-28-desflow-0-25-0/check-and-compare.gif" loading="lazy" width="1300" height="1000" />
&lt;/picture>

&lt;/p>
&lt;p>Queueing theory, the mathematics of waiting lines, describes a line such as the shop&amp;rsquo;s with a few numbers, each written as a letter. &lt;code>desflow explain&lt;/code> works them out from the model and prints them:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>λ (lambda), the arrival rate:&lt;/strong> how many orders arrive a second. An order every 2.5 seconds is 0.4 a second.&lt;/li>
&lt;li>&lt;strong>μ (mu), the service rate:&lt;/strong> how many orders one packer finishes a second. Packing takes 4 seconds, so one packer finishes 0.25 a second.&lt;/li>
&lt;li>&lt;strong>c, the capacity:&lt;/strong> the number of packers, 2.&lt;/li>
&lt;li>&lt;strong>ρ (rho), the utilisation:&lt;/strong> the share of time the packers are busy. Two packers finish c × μ = 0.5 orders a second while 0.4 arrive, so ρ = λ ÷ (c × μ) = 0.4 ÷ 0.5 = 0.8. They are busy 80% of the time.&lt;/li>
&lt;/ul>
&lt;p>When ρ is 1 or more, orders arrive faster than they are packed, and the queue grows for as long as the shop stays open. One packer would give ρ = 0.4 ÷ 0.25 = 1.6.&lt;/p>
&lt;p>&lt;code>desflow explain&lt;/code> works this out for every resource in a model, without running it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow explain experiment_shop.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">Resources
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">+---------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">| resource | λ | μ | c | ρ | Wq | L | status |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">|----------+-----+------+---+-----+----+---+--------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">| packers | 0.4 | 0.25 | 2 | 0.8 | - | - | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">+---------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">packers by units
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">+-----------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">| c | ρ | status |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">|---+--------+----------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">| 1 | 1.6 | UNSTABLE |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">| 2 | 0.8 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">| 3 | 0.5333 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">| 4 | 0.4 | OK |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">+-----------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">Bottleneck: packers
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first table holds those four numbers for the packers, and three more columns:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Wq:&lt;/strong> the average time an order waits in the queue.&lt;/li>
&lt;li>&lt;strong>L:&lt;/strong> the average number of orders in the shop, waiting or being packed.&lt;/li>
&lt;li>&lt;strong>status:&lt;/strong> &lt;code>OK&lt;/code> while ρ is below 0.85, &lt;code>CONGESTED&lt;/code> from 0.85, where the queue gets long quickly, and &lt;code>UNSTABLE&lt;/code> from 1, where it never stops growing.&lt;/li>
&lt;/ul>
&lt;p>Wq and L are blank here. A formula gives them only for simpler shops, such as one with a single packer, and &lt;code>explain&lt;/code> leaves them out rather than guess.&lt;/p>
&lt;p>The second table, &lt;code>packers by units&lt;/code>, works out ρ for one to four packers, which shows how many packers the shop needs. &lt;code>Bottleneck&lt;/code> names the resource with the highest ρ.&lt;/p>
&lt;p>The second step, &lt;code>--verify&lt;/code>, runs the model and checks it against the arithmetic. Here it runs one hour of the shop ten times:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ desflow explain experiment_shop.yaml --verify --replications 10 --until 1h
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">+------------------------------------------------------------------------+
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">| node | measure | analysis | measured | ± | result |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">|---------+--------------+----------+----------+----------+--------------|
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">| packers | utilization | 0.8 | 0.7953 | 0.01561 | agrees |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">| packers | queue_length | - | 1.823 | 0.2856 | not compared |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">| pack | wait | - | 4.505 | 0.6483 | not compared |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">| orders | throughput | 0.4 | 0.4002 | 0.007201 | agrees |
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">+------------------------------------------------------------------------+
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each row is one measure of one part of the model:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>node:&lt;/strong> the part measured: the &lt;code>packers&lt;/code>, the &lt;code>pack&lt;/code> task or the &lt;code>orders&lt;/code> arrival.&lt;/li>
&lt;li>&lt;strong>measure:&lt;/strong> what was measured. &lt;code>throughput&lt;/code> is how many orders leave the shop a second.&lt;/li>
&lt;li>&lt;strong>analysis:&lt;/strong> the value the arithmetic gives, or &lt;code>-&lt;/code> where no formula applies.&lt;/li>
&lt;li>&lt;strong>measured:&lt;/strong> the average over the ten runs.&lt;/li>
&lt;li>&lt;strong>±:&lt;/strong> give or take: the true average very likely lies within this distance of the measured one (a 95% confidence interval).&lt;/li>
&lt;li>&lt;strong>result:&lt;/strong> &lt;code>agrees&lt;/code> when the analysis lies within that range, &lt;code>differs&lt;/code> when it does not, and &lt;code>not compared&lt;/code> when there is no analysis to compare.&lt;/li>
&lt;/ul>
&lt;p>The runs keep the packers as busy as the arithmetic says, and finish orders as fast as they arrive, so the model does what its settings describe. The runs also measure the wait that no formula gave. Whether the model matches the real shop is a different question, which the experiment answers with the wait measured there.&lt;/p>
&lt;p>Two smaller commands help before a run as well. &lt;code>desflow run --sample 5&lt;/code> prints the first five events, without sending anything to the model&amp;rsquo;s destinations. &lt;code>desflow dist&lt;/code> turns a measured average and spread of packing times into the settings a model needs.&lt;/p>

&lt;h2 id="help-with-writing-a-model" data-numberify>Help With Writing a Model&lt;a class="anchor ms-1" href="#help-with-writing-a-model" aria-label="Link to this section: Help With Writing a Model">&lt;/a>&lt;/h2>
&lt;p>A blueprint is easier to write when the editor knows its settings. Release 0.24.0 ships a description of every setting as a JSON Schema. With the Red Hat YAML extension in VS Code, one line at the top of a blueprint makes the editor suggest settings and mark a misspelt one as you type:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c"># yaml-language-server: $schema=https://jaehyeon.me/desflow/latest/schema/blueprint.schema.json&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The same release adds a skill for AI coding assistants such as Claude Code. It teaches an assistant to write a blueprint, check it with &lt;code>desflow explain&lt;/code>, and run it enough times to trust the result. With the GitHub CLI 2.90.0 or later, one command installs it for Claude Code:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">gh skill install jaehyeon-kim/desflow desflow --agent claude-code --scope user
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;a href="https://jaehyeon.me/desflow/latest/guides/use-an-ai-assistant/">Use an AI Assistant&lt;/a> also shows how to install it by hand.&lt;/p>

&lt;h2 id="documentation-to-learn-from" data-numberify>Documentation to Learn From&lt;a class="anchor ms-1" href="#documentation-to-learn-from" aria-label="Link to this section: Documentation to Learn From">&lt;/a>&lt;/h2>
&lt;p>The documentation now teaches simulation itself, not only the library. The &lt;a href="https://jaehyeon.me/desflow/latest/learn/">Learn&lt;/a> section explains waiting lines, why one run is not enough, how to check a model, and the steps of a simulation study, with code you can run. The tutorials build up the shop one idea at a time, from a first model to fitting measured times.&lt;/p>
&lt;p>The new &lt;a href="https://jaehyeon.me/desflow/latest/tutorials/complete-model/">Complete Model&lt;/a> tutorial goes through a whole study in one page, from measured data to a recommendation. &lt;a href="https://jaehyeon.me/desflow/latest/user-guide/command-line/">Command Line&lt;/a> runs every command on one shop.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related Posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related Posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">Defining Data-Streaming Simulations in YAML, Without Writing Python&lt;/a> - the YAML blueprint this post builds on&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - a simulated shop feeding a change data capture pipeline&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/">Why Digital Twins Are Rewiring Industry 4.0&lt;/a> - where a simulation like this fits in a digital twin&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># desflow, the library and its command&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">pip install desflow
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># desflow fit, which needs scipy and matplotlib&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">pip install &lt;span class="s2">&amp;#34;desflow[fit]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="c1"># Download the shop of this post and check it&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">desflow examples get experiment --api yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">9&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> experiment/yaml &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> desflow explain experiment_shop.yaml
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">jaehyeon-kim/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/desflow/" target="_blank" rel="noopener noreferrer">pypi.org/project/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>Documentation:&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/">jaehyeon.me/desflow&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>odctl 1.0: Kafka, Flink, Spark, Iceberg, Trino and MLflow on Your Laptop with One Command</title><link>https://jaehyeon.me/blog/2026-10-12-odctl-1-0/</link><guid>https://jaehyeon.me/blog/2026-10-12-odctl-1-0/</guid><pubDate>Mon, 12 Oct 2026 00:00:00 +0000</pubDate><category>Open Data Stack</category><category>odctl</category><category>Apache Kafka</category><category>Apache Flink</category><category>Apache Spark</category><category>Apache Iceberg</category><category>MLflow</category><category>Python</category><description>&lt;p>Trying a modern data tool on your own machine is rarely about the tool. Writing Iceberg tables from Flink needs a catalog, the catalog needs a database and object storage, Avro on Kafka needs a schema registry, and every piece needs the right connector jars, ports and network settings. Most of an afternoon goes into making them talk to each other before you write a line of your own code.&lt;/p>
&lt;p>&lt;code>odctl&lt;/code> removes that work. It is a command-line tool that runs a set of open-source data and machine learning services on your laptop with Docker, already connected to each other. You say what you want, for example Kafka and Flink, and it starts them together with everything they depend on. It has now reached 1.0, with a documentation site that has a guide for every service.&lt;/p></description><content:encoded>&lt;p>Trying a modern data tool on your own machine is rarely about the tool. Writing Iceberg tables from Flink needs a catalog, the catalog needs a database and object storage, Avro on Kafka needs a schema registry, and every piece needs the right connector jars, ports and network settings. Most of an afternoon goes into making them talk to each other before you write a line of your own code.&lt;/p>
&lt;p>&lt;code>odctl&lt;/code> removes that work. It is a command-line tool that runs a set of open-source data and machine learning services on your laptop with Docker, already connected to each other. You say what you want, for example Kafka and Flink, and it starts them together with everything they depend on. It has now reached 1.0, with a documentation site that has a guide for every service.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>&lt;picture>&lt;img class="img-fluid " alt="Installing odctl, previewing what flink-lite starts, starting kafka-lite, listing the containers and stopping them" src="https://jaehyeon.me/blog/2026-10-12-odctl-1-0/odctl-1-0.gif" loading="lazy" width="1300" height="760" />
&lt;/picture>

&lt;/p>

&lt;h2 id="what-it-runs" data-numberify>What It Runs&lt;a class="anchor ms-1" href="#what-it-runs" aria-label="Link to this section: What It Runs">&lt;/a>&lt;/h2>
&lt;p>odctl groups the services into profiles, and you start the profiles you need. Each profile brings its dependencies with it, so &lt;code>odctl up flink-lite&lt;/code> also starts the Iceberg REST catalog, with PostgreSQL and SeaweedFS (S3 storage) behind it, ready for Flink to write Iceberg tables.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Area&lt;/th>
&lt;th>Technologies&lt;/th>
&lt;th>Profiles&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Messaging&lt;/td>
&lt;td>Kafka, Karapace schema registry, Kafka Connect, Kafka UI&lt;/td>
&lt;td>&lt;code>kafka-lite&lt;/code>, &lt;code>kafka-full&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Stream and batch processing&lt;/td>
&lt;td>Apache Flink, Apache Spark&lt;/td>
&lt;td>&lt;code>flink-lite&lt;/code>, &lt;code>flink-full&lt;/code>, &lt;code>spark-lite&lt;/code>, &lt;code>spark-full&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Analytics&lt;/td>
&lt;td>ClickHouse, Trino, Metabase&lt;/td>
&lt;td>&lt;code>ch-lite&lt;/code>, &lt;code>ch-full&lt;/code>, &lt;code>trino&lt;/code>, &lt;code>metabase&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Orchestration&lt;/td>
&lt;td>Apache Airflow, Temporal&lt;/td>
&lt;td>&lt;code>airflow&lt;/code>, &lt;code>temporal&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>MLOps&lt;/td>
&lt;td>MLflow, Feast, Evidently&lt;/td>
&lt;td>&lt;code>mlflow&lt;/code>, &lt;code>feast&lt;/code>, &lt;code>evidently&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Metadata and lineage&lt;/td>
&lt;td>OpenMetadata, Marquez&lt;/td>
&lt;td>&lt;code>metadata&lt;/code>, &lt;code>lineage&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Observability&lt;/td>
&lt;td>OpenTelemetry Collector, Prometheus, Tempo, Loki, Grafana&lt;/td>
&lt;td>&lt;code>telemetry&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Storage and catalog&lt;/td>
&lt;td>PostgreSQL 18 with pgvector, pg_textsearch and PostGIS, SeaweedFS, Iceberg REST catalog, Valkey, Apache Fluss&lt;/td>
&lt;td>&lt;code>postgres&lt;/code>, &lt;code>storage&lt;/code>, &lt;code>catalog&lt;/code>, &lt;code>valkey&lt;/code>, &lt;code>fluss&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The &lt;code>-lite&lt;/code> profiles run one broker or worker and fit on a laptop. The &lt;code>-full&lt;/code> profiles run a small cluster, three Kafka brokers or three Flink TaskManagers, when you want to see how a tool behaves with more than one node.&lt;/p>
&lt;p>The services are already wired together. Kafka Connect ships with the Iceberg sink, the Debezium PostgreSQL source and the JDBC and S3 sinks. Flink, Spark and Trino use the same Iceberg catalog, so a table one engine writes is a table the others read. Every service reports metrics, and Grafana has a dashboard for each one.&lt;/p>

&lt;h2 id="what-you-can-use-it-for" data-numberify>What You Can Use It For&lt;a class="anchor ms-1" href="#what-you-can-use-it-for" aria-label="Link to this section: What You Can Use It For">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Learning a tool with a realistic setup.&lt;/strong> Write Flink SQL against a real Kafka topic with Avro schemas, or query Iceberg tables from Spark and Trino, without assembling the setup yourself.&lt;/li>
&lt;li>&lt;strong>Building a pipeline end to end.&lt;/strong> Change data capture from PostgreSQL with Debezium, Kafka to Iceberg with Flink, then ClickHouse or Trino on top, all on one machine. The &lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Debezium&lt;/a> and &lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/">Flink SQL leaderboard&lt;/a> posts are complete examples.&lt;/li>
&lt;li>&lt;strong>Machine learning workflows.&lt;/strong> Features in Feast, training and model serving in MLflow, scheduling in Airflow and drift reports in Evidently. The &lt;a href="https://jaehyeon.me/blog/2026-09-28-mlops-with-a-feature-store/">air quality forecast&lt;/a> series uses Feast, MLflow and Airflow together. For online learning, the &lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">product recommender&lt;/a> updates a model in Flink as clicks arrive on Kafka and serves it from Valkey.&lt;/li>
&lt;li>&lt;strong>AI applications.&lt;/strong> Hybrid search with pgvector and BM25 in PostgreSQL, vector search in Valkey, and Temporal for workflows that wait for a person to approve an agent&amp;rsquo;s action. The &lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">agentic analytics system&lt;/a> runs its lakehouse on odctl.&lt;/li>
&lt;li>&lt;strong>Demos and tests that run anywhere.&lt;/strong> A project can say which profiles it needs, and anyone can run it from a fresh clone. The &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">Benchtop&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> projects and the integration tests of &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> work this way.&lt;/li>
&lt;/ul>

&lt;h2 id="getting-started" data-numberify>Getting Started&lt;a class="anchor ms-1" href="#getting-started" aria-label="Link to this section: Getting Started">&lt;/a>&lt;/h2>
&lt;p>You need Docker with 8 to 16 GB of memory, and Python 3.10 or later.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">uv tool install odctl &lt;span class="c1"># or: pipx install odctl&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">odctl list &lt;span class="c1"># every profile and what it runs&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">odctl up flink-lite --dry-run &lt;span class="c1"># what would start, in which order&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">odctl up kafka-lite flink-lite &lt;span class="c1"># start Kafka and Flink with their dependencies&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">odctl ps --all &lt;span class="c1"># the running containers and their ports&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">odctl down --all &lt;span class="c1"># stop everything&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each service is on &lt;code>127.0.0.1&lt;/code> at a fixed port, for example Kafka UI at &lt;code>http://127.0.0.1:8086&lt;/code> and the Flink UI at &lt;code>http://127.0.0.1:8082&lt;/code>. &lt;code>odctl explain &amp;lt;profile&amp;gt;&lt;/code> prints the addresses for a profile.&lt;/p>
&lt;p>To change a memory limit, a port or the Python packages a container installs, run &lt;code>odctl init&lt;/code>. It copies the configuration into a &lt;code>.odctl&lt;/code> folder in your project, and odctl uses that copy from then on.&lt;/p>

&lt;h2 id="a-guide-for-every-service" data-numberify>A Guide for Every Service&lt;a class="anchor ms-1" href="#a-guide-for-every-service" aria-label="Link to this section: A Guide for Every Service">&lt;/a>&lt;/h2>
&lt;p>The documentation at &lt;a href="https://jaehyeon.me/odctl/">jaehyeon.me/odctl&lt;/a> has 18 guides, from producing to Kafka and running Flink SQL to serving a model from MLflow and cataloguing a database in OpenMetadata. Each guide uses the commands odctl&amp;rsquo;s own tests run before every release, and shows what you should see.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI listing the topics of a running kafka-lite profile" src="https://jaehyeon.me/blog/2026-10-12-odctl-1-0/kafka-ui.png" loading="lazy" title="Kafka UI from the Kafka guide" width="1440" height="545" />
&lt;/picture>&lt;figcaption class="figure-caption">Kafka UI from the Kafka guide&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>The site also explains how a profile starts and where its data lives, and has a page for each area with every profile&amp;rsquo;s images, ports and memory limits.&lt;/p>

&lt;h2 id="new-in-10" data-numberify>New in 1.0&lt;a class="anchor ms-1" href="#new-in-10" aria-label="Link to this section: New in 1.0">&lt;/a>&lt;/h2>
&lt;p>Since the &lt;a href="https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/">first post&lt;/a> in July, odctl has gained Feast, Evidently and Temporal, MLflow model serving, a dashboard for every service, and a PostgreSQL with keyword and geospatial search. Version 1.0 adds the documentation site, and fixes the commands, profile names and ports for the whole of 1.x, so a project that depends on &lt;code>odctl&amp;gt;=1.0,&amp;lt;2&lt;/code> keeps working as odctl is updated.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related Posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related Posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/">Data Streaming and Machine Learning Projects That Run on Your Laptop&lt;/a> - the Benchtop projects, each run on odctl from a fresh clone&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">Defining Data-Streaming Simulations in YAML, Without Writing Python&lt;/a> - generating the test data these services process&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">Productionizing an Online Product Recommender using Event Driven Architecture&lt;/a> - a recommender that learns from clicks, with Kafka, Flink and Valkey started by odctl&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - an agent answering questions over Iceberg tables, with Trino, the catalog, object storage and Valkey started by odctl&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Documentation:&lt;/strong> &lt;a href="https://jaehyeon.me/odctl/">jaehyeon.me/odctl&lt;/a>&lt;/li>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">jaehyeon-kim/odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/odctl/" target="_blank" rel="noopener noreferrer">pypi.org/project/odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>It is licensed under Apache 2.0, and contributions are welcome.&lt;/p></content:encoded></item><item><title>Defining Data-Streaming Simulations in YAML, Without Writing Python</title><link>https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/</link><guid>https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/</guid><pubDate>Tue, 06 Oct 2026 00:00:00 +0000</pubDate><category>Discrete Event Simulation</category><category>desflow</category><category>SimPy</category><category>YAML</category><category>Apache Kafka</category><category>Apache Iceberg</category><category>PostgreSQL</category><category>Python</category><description>&lt;p>A pipeline is easier to test when its input behaves like production: orders arrive at random, queues grow, a machine slows down. A simulation can produce that data, but when the simulation is a Python program, every change is a code change. Someone who only wants to double an arrival rate has to read and edit code, and a reviewer has to check that nothing else moved. A file that holds only settings is easier to read, to compare in a pull request and to run in CI.&lt;/p>
&lt;p>&lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> 0.16.0 (then named dynamic-des) adds that file. A YAML blueprint describes the whole simulation, and the &lt;code>desflow&lt;/code> command runs it. Each of the seven examples in the repository now has a YAML version in plain YAML. The documentation was rebuilt around the three ways to write a simulation.&lt;/p></description><content:encoded>&lt;p>A pipeline is easier to test when its input behaves like production: orders arrive at random, queues grow, a machine slows down. A simulation can produce that data, but when the simulation is a Python program, every change is a code change. Someone who only wants to double an arrival rate has to read and edit code, and a reviewer has to check that nothing else moved. A file that holds only settings is easier to read, to compare in a pull request and to run in CI.&lt;/p>
&lt;p>&lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> 0.16.0 (then named dynamic-des) adds that file. A YAML blueprint describes the whole simulation, and the &lt;code>desflow&lt;/code> command runs it. Each of the seven examples in the repository now has a YAML version in plain YAML. The documentation was rebuilt around the three ways to write a simulation.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-13&lt;/strong>] dynamic-des 0.17.0, 0.18.0 and 0.19.0 change some of what this post describes:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Examples download with &lt;code>desflow&lt;/code>.&lt;/strong> &lt;code>desflow examples get local --api yaml&lt;/code> downloads an example at the release you have installed, in place of the &lt;code>curl&lt;/code> line below. Every example now exists as YAML, builder and low-level code.&lt;/li>
&lt;li>&lt;strong>Reorganised documentation.&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/latest/tutorials/first-simulation/">First Simulation&lt;/a> replaces the three tutorials, with the YAML, builder and low-level code in tabs at each step, and a new section, &lt;a href="https://jaehyeon.me/desflow/latest/des/how-it-works/">How Simulation Works&lt;/a>, explains discrete-event simulation, the building blocks of a model, and queues and capacity. The links to the old tutorials below lead to First Simulation.&lt;/li>
&lt;li>&lt;strong>Parameters are checked.&lt;/strong> A distribution parameter out of range is refused when the blueprint loads, with its line, and when it changes during a run. An unknown &lt;code>dist&lt;/code> name is refused instead of running with a default. Two distributions are new: &lt;code>truncated_normal&lt;/code> and &lt;code>constant&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Capacity drops are exact.&lt;/strong> Lowering a resource&amp;rsquo;s capacity while it is busy no longer lets one more task start.&lt;/li>
&lt;li>&lt;strong>Two smaller fixes.&lt;/strong> A NaN or infinity inside a Pydantic model is now written as &lt;code>null&lt;/code>, as it already was in a plain payload. The Kafka connectors no longer leave a client open each time they fail to reach the broker.&lt;/li>
&lt;li>&lt;strong>API names (0.18.0).&lt;/strong> The declarative API is now called the Builder API, and the example folders were renamed to match: &lt;code>examples/declarative/&lt;/code> is now &lt;code>examples/builder/&lt;/code>, &lt;code>examples/imperative/&lt;/code> is now &lt;code>examples/low-level/&lt;/code>, and &lt;code>desflow examples get&lt;/code> takes &lt;code>--api yaml&lt;/code>, &lt;code>--api builder&lt;/code> or &lt;code>--api low-level&lt;/code>. Nothing changes in the Python API itself.&lt;/li>
&lt;li>&lt;strong>An online shop (0.19.0).&lt;/strong> Every example now models an online shop: orders arrive, packers are the resource and packing is the service, with &lt;code>sim_id: Shop&lt;/code>. The blueprints below are the 0.19.0 files, and the three versions of each example publish the same records.&lt;/li>
&lt;/ul>
&lt;p>The project started as a way to change a running SimPy model from Kafka (&lt;a href="https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/">Building an Event-Driven Hybrid Digital Twin with desflow&lt;/a>). It then learned to write Parquet for model training (&lt;a href="https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/">One Simulation, Two Pipelines&lt;/a>) and gained a declarative Python API with PostgreSQL and Redis connectors (&lt;a href="https://jaehyeon.me/blog/2026-07-17-dynamic-des-declarative-connectors/">A Declarative API with Postgres and Redis Connectors&lt;/a>). This release removes the need for Python in most simulations.&lt;/p>

&lt;h2 id="a-complete-blueprint" data-numberify>A Complete Blueprint&lt;a class="anchor ms-1" href="#a-complete-blueprint" aria-label="Link to this section: A Complete Blueprint">&lt;/a>&lt;/h2>
&lt;p>This file is &lt;code>examples/yaml/local.yaml&lt;/code>. It needs no containers and prints events and telemetry to the terminal:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">simulation&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">sim_id&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Shop&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">factor&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">1.0&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">random_seed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">42&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">egress&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">type&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Console&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">resources&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">current_cap: 1, max_cap&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">5&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">services&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packing&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: normal, mean: 3.0, std&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">0.5&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">arrivals&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Each order spawns one pack_order task.&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">orders&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">dist: exponential, rate: 1.0, spawn&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">pack_order}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">tasks&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">pack_order&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">service&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packing&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">resource&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># The value of the task&amp;#39;s finished event. id_field adds the task id as order_id.&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">payload&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">event_type: order_packed, shipping&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">standard}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">id_field&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">order_id&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">telemetry&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Samples the packers every 2 simulation seconds.&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">interval&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">2.0&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">publish&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers.capacity&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers.capacity&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers.in_use&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers.in_use&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">33&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers.queue_length&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers.queue_length&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">34&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">packers.utilization&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">packers.utilization&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">35&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">36&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">scenario&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">37&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Three packers from 10 seconds, then two from 20 seconds.&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">38&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">at: 10, path: Shop.resources.packers.current_cap, value&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">3&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">39&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- {&lt;span class="nt">at: 20, path: Shop.resources.packers.current_cap, value&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">2&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">40&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">41&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">run&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">42&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">until&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">30&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Install the package from &lt;a href="https://pypi.org/project/desflow/" target="_blank" rel="noopener noreferrer">PyPI&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, download the file and run it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">pip install desflow
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">curl -O https://raw.githubusercontent.com/jaehyeon-kim/desflow/main/examples/yaml/local.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">desflow run local.yaml
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="c1"># Override run.until, in seconds or as a duration such as &amp;#34;2 min&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">desflow run local.yaml --until &lt;span class="m">10&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;picture>&lt;img class="img-fluid " alt="Downloading local.yaml, printing it and running it with desflow, with events and telemetry streaming until the run is stopped" src="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/local-yaml-run.gif" loading="lazy" width="1100" height="680" />
&lt;/picture>

&lt;/p>
&lt;p>The &lt;a href="https://jaehyeon.me/desflow/">documentation&lt;/a> has the full reference. The &lt;code>desflow&lt;/code> command comes with the core package. Typer and PyYAML are now core dependencies, so no extra is needed to run a blueprint. From Python, &lt;code>SimulationContext.from_yaml(&amp;quot;local.yaml&amp;quot;)&lt;/code> returns the same simulation as a builder you can extend.&lt;/p>
&lt;p>The file is checked before the run starts. An unknown key, a task that names a missing service, or a scenario path that does not exist is reported with the file name and the line number.&lt;/p>

&lt;h2 id="what-a-blueprint-can-express" data-numberify>What a Blueprint Can Express&lt;a class="anchor ms-1" href="#what-a-blueprint-can-express" aria-label="Link to this section: What a Blueprint Can Express">&lt;/a>&lt;/h2>
&lt;p>Each section of a blueprint maps to one call of the builder API. Beyond the sections above, a blueprint supports:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Environment variables.&lt;/strong> &lt;code>${VAR}&lt;/code> and &lt;code>${VAR:-default}&lt;/code> are replaced in string values, with a default when a variable is not set. The same file then runs on your machine and in a test pipeline, with no edits.&lt;/li>
&lt;li>&lt;strong>Relative times.&lt;/strong> &lt;code>logical_start_time&lt;/code> and &lt;code>go_live_at&lt;/code> take &lt;code>now&lt;/code> or a signed duration such as &lt;code>-10m&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Backfill then live in one run.&lt;/strong> Each egress entry takes &lt;code>when: history&lt;/code> or &lt;code>when: live&lt;/code>. Records stamped before &lt;code>go_live_at&lt;/code> go to one sink, and later records go to the other.&lt;/li>
&lt;li>&lt;strong>Scenarios on simulation time.&lt;/strong> A &lt;code>scenario&lt;/code> list sets parameters at given simulation times, such as a capacity change at 30 seconds. The steps wait on the simulation clock, so they repeat exactly and also work at &lt;code>factor: 0&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Tasks without a service or a resource.&lt;/strong> Such a task emits its payload as soon as it is spawned, which suits order or click events that do not queue.&lt;/li>
&lt;li>&lt;strong>Connectors that prepare their target.&lt;/strong> Kafka creates its event and telemetry topics at start. PostgreSQL creates the tables listed under &lt;code>tables&lt;/code>. Parquet and JSONL create the destination folder and take a &lt;code>filesystem&lt;/code> mapping for local disk or S3. Iceberg takes its catalog properties and table schemas as plain mappings.&lt;/li>
&lt;/ul>
&lt;p>This excerpt from &lt;code>examples/yaml/backfill_live.yaml&lt;/code> uses three of them. It writes ten minutes of history to Parquet as fast as the machine allows, then sends the same events to Kafka in real time:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nt">simulation&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">sim_id&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Shop&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">factor&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">0.0&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">random_seed&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">42&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">logical_start_time&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>-&lt;span class="l">10m&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">go_live_at&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">now&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="nt">egress&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">type&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Parquet&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">config&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">default_path&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">data/backfill/events.parquet&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">when&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">history&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">type&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">Kafka&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">config&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">event_topic&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">sim-events&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">telemetry_topic&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">sim-telemetry&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">bootstrap_servers&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">${KAFKA_BOOTSTRAP_SERVERS:-localhost:9092}&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">when&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="l">live&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="python-for-logic-yaml-cannot-express" data-numberify>Python for Logic YAML Cannot Express&lt;a class="anchor ms-1" href="#python-for-logic-yaml-cannot-express" aria-label="Link to this section: Python for Logic YAML Cannot Express">&lt;/a>&lt;/h2>
&lt;p>Some logic is not configuration, for example an order with a random number of line items. A blueprint references such code with the &lt;code>!python&lt;/code> tag, and the module sits beside the file. The repository has one example of this, &lt;code>examples/yaml/advanced/postgres_orders.yaml&lt;/code>. Everything else in that file is still plain YAML:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-yaml" data-lang="yaml">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="nt">processes&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="c"># Called as order_generator(context, arrival=&amp;#34;orders&amp;#34;, max_items=5).&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>- &lt;span class="nt">function&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>!&lt;span class="l">python postgres_orders_logic.order_generator&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="nt">kwargs&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>{&lt;span class="nt">arrival: orders, max_items&lt;/span>&lt;span class="p">:&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="m">5&lt;/span>}&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>!python&lt;/code> also works for a task payload, a telemetry function, an egress &lt;code>when&lt;/code>, a connector class and any value under &lt;code>config&lt;/code>, &lt;code>simulation&lt;/code> or &lt;code>run.until&lt;/code>. A file without it is read with PyYAML&amp;rsquo;s safe loader and imports only the connector modules it names.&lt;/p>

&lt;h2 id="three-ways-to-write-the-same-simulation" data-numberify>Three Ways to Write the Same Simulation&lt;a class="anchor ms-1" href="#three-ways-to-write-the-same-simulation" aria-label="Link to this section: Three Ways to Write the Same Simulation">&lt;/a>&lt;/h2>
&lt;p>A simulation can now be written in three ways, and all three build the same parameters and run on the same environment:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Low-level API:&lt;/strong> &lt;code>DynamicRealtimeEnvironment&lt;/code> used directly, with SimPy processes you write yourself.&lt;/li>
&lt;li>&lt;strong>Builder API:&lt;/strong> the &lt;code>SimulationContext&lt;/code> builder and its decorators.&lt;/li>
&lt;li>&lt;strong>YAML blueprint:&lt;/strong> the file above, run with &lt;code>desflow&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>The registry paths, the records and the connectors are the same whichever you choose, so a team can start in YAML and move one part to Python when it needs to.&lt;/p>

&lt;h2 id="documentation-rebuilt" data-numberify>Documentation Rebuilt&lt;a class="anchor ms-1" href="#documentation-rebuilt" aria-label="Link to this section: Documentation Rebuilt">&lt;/a>&lt;/h2>
&lt;p>The &lt;a href="https://jaehyeon.me/desflow/">documentation&lt;/a> was reorganised around those three ways:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Tutorials in three parts.&lt;/strong> One small factory is built three times: &lt;a href="https://jaehyeon.me/desflow/latest/tutorials/low-level/">with the low-level API&lt;/a>, &lt;a href="https://jaehyeon.me/desflow/latest/tutorials/declarative/">with the builder API&lt;/a> and &lt;a href="https://jaehyeon.me/desflow/latest/tutorials/yaml/">as a YAML blueprint&lt;/a>.&lt;/li>
&lt;li>&lt;strong>Core Architecture in two groups.&lt;/strong> Writing a simulation has a page per way, starting from the &lt;a href="https://jaehyeon.me/desflow/latest/architecture/overview/">overview&lt;/a>. The runtime pages cover the environment, the &lt;a href="https://jaehyeon.me/desflow/latest/architecture/registry/">registry and live parameters&lt;/a>, time, resources, connectors, records and batching.&lt;/li>
&lt;li>&lt;strong>Examples with a tab per way.&lt;/strong> Each example page shows its versions side by side in tabs, low-level, builder and YAML where all three exist, starting with the &lt;a href="https://jaehyeon.me/desflow/latest/examples/local/">local example&lt;/a>.&lt;/li>
&lt;li>&lt;strong>Guides grouped by topic.&lt;/strong> Connectors, features such as &lt;a href="https://jaehyeon.me/desflow/latest/guides/backfill-then-live/">backfill then live&lt;/a>, and YAML, from &lt;a href="https://jaehyeon.me/desflow/latest/guides/yaml-blueprints/">a first blueprint to connectors&lt;/a> to &lt;a href="https://jaehyeon.me/desflow/latest/guides/yaml-advanced/">custom logic with &lt;code>!python&lt;/code>&lt;/a>. Modelling patterns such as preemptive breakdowns moved into an Advanced section.&lt;/li>
&lt;/ul>

&lt;h2 id="other-changes-in-this-release" data-numberify>Other Changes in This Release&lt;a class="anchor ms-1" href="#other-changes-in-this-release" aria-label="Link to this section: Other Changes in This Release">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Fractional container capacity.&lt;/strong> A capacity change on a &lt;code>DynamicContainer&lt;/code> was rounded down to a whole number. A tank set to 62.5 now holds 62.5, also when it started at a whole number.&lt;/li>
&lt;li>&lt;strong>&lt;code>DynamicContainer&lt;/code> and &lt;code>DynamicStore&lt;/code> exported.&lt;/strong> Both now import from &lt;code>desflow&lt;/code>, like &lt;code>DynamicResource&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Live &lt;code>max_cap&lt;/code> changes.&lt;/strong> A change to &lt;code>max_cap&lt;/code> used to have no effect on a running resource. It now applies at once, and a lower limit shrinks the capacity.&lt;/li>
&lt;li>&lt;strong>Flat rows for files and tables.&lt;/strong> Parquet, JSONL and Iceberg sinks with no router now write each event&amp;rsquo;s value as columns and leave telemetry out. A router keeps the earlier behaviour.&lt;/li>
&lt;li>&lt;strong>Time strings in time columns.&lt;/strong> Iceberg and PostgreSQL convert ISO time strings for timestamp and date columns, using the column types of the table.&lt;/li>
&lt;li>&lt;strong>Stricter blueprint checks.&lt;/strong> A zero interval, batch size or &lt;code>until&lt;/code> is rejected, because an interval of zero made a run hang. A &lt;code>go_live_at&lt;/code> and a start time where only one has a time zone are rejected with the line. A connector that rejects its settings is reported with the line instead of a Python error.&lt;/li>
&lt;li>&lt;strong>&lt;code>add_arrival&lt;/code> takes &lt;code>std&lt;/code>,&lt;/strong> so normal and lognormal arrivals can set a standard deviation.&lt;/li>
&lt;li>&lt;strong>CI checks.&lt;/strong> The docs are built in strict mode on every pull request, and the core package alone, with no extra, must run a YAML blueprint.&lt;/li>
&lt;/ul>

&lt;h2 id="related-posts" data-numberify>Related Posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related Posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - a simulated shop feeding a change data capture pipeline&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/">Keeping Game Leaderboards Up to Date in Real Time with Kafka and Flink SQL&lt;/a> - a simulated mobile game feeding Flink SQL leaderboards&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/">Why Digital Twins Are Rewiring Industry 4.0&lt;/a> - where a simulation like this fits in a digital twin&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - desflow filling an Iceberg lakehouse with the data an agent queries&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># Core library and the desflow command&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">pip install desflow
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># Every connector: Kafka, Redis, PostgreSQL, Avro, Parquet and Iceberg&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">pip install &lt;span class="s2">&amp;#34;desflow[all]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">jaehyeon-kim/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, with the YAML examples in &lt;a href="https://github.com/jaehyeon-kim/desflow/tree/main/examples/yaml" target="_blank" rel="noopener noreferrer">&lt;code>examples/yaml/&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/desflow/" target="_blank" rel="noopener noreferrer">pypi.org/project/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>Documentation:&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/">jaehyeon.me/desflow&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>Keeping Game Leaderboards Up to Date in Real Time with Kafka and Flink SQL</title><link>https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/</link><guid>https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/</guid><pubDate>Fri, 02 Oct 2026 00:00:00 +0000</pubDate><category>Apache Kafka</category><category>Apache Flink</category><category>Flink SQL</category><category>PostgreSQL</category><category>NiceGUI</category><category>odctl</category><category>desflow</category><category>Benchtop</category><description>&lt;p>A mobile game produces a score every time a player finishes a round, and players expect the leaderboards to move as they play. In this post, a simulation plays the game and sends each score to Kafka. Four Flink SQL jobs keep four top 10 leaderboards up to date in PostgreSQL, and a web dashboard shows them as they change. Everything runs on your own machine.&lt;/p></description><content:encoded>&lt;p>A mobile game produces a score every time a player finishes a round, and players expect the leaderboards to move as they play. In this post, a simulation plays the game and sends each score to Kafka. Four Flink SQL jobs keep four top 10 leaderboards up to date in PostgreSQL, and a web dashboard shows them as they change. Everything runs on your own machine.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>The source code is in &lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/game-leaderboard" target="_blank" rel="noopener noreferrer">benchtop/game-leaderboard&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. It is one of the &lt;a href="https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/">Benchtop&lt;/a> projects, which run locally from a fresh clone.&lt;/p>

&lt;h2 id="what-you-will-build" data-numberify>What You Will Build&lt;a class="anchor ms-1" href="#what-you-will-build" aria-label="Link to this section: What You Will Build">&lt;/a>&lt;/h2>
&lt;ol>
&lt;li>Run the simulation, which plays the game and sends each score to Kafka.&lt;/li>
&lt;li>Submit the four Flink jobs, which keep the leaderboards up to date in PostgreSQL.&lt;/li>
&lt;li>Open the dashboard, which shows the four leaderboards as they change.&lt;/li>
&lt;li>Change the game while it runs, and watch robots take over the top players and the team MVPs.&lt;/li>
&lt;li>Clean up, to start again.&lt;/li>
&lt;/ol>
&lt;p>To start again at any point, &lt;code>python -m leaderboard.stores.cleanup&lt;/code> removes everything this project has created and keeps the services running. &lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/#clean-up">Clean Up&lt;/a> describes what it removes.&lt;/p>
&lt;p>The four leaderboards:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Leaderboard&lt;/th>
&lt;th>What it shows&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Top teams&lt;/td>
&lt;td>the 10 teams with the highest total score&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Top players&lt;/td>
&lt;td>the 10 players with the highest total score&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Hot streaks&lt;/td>
&lt;td>the 10 players scoring furthest above their usual rate: their average score over the last 10 seconds divided by their average over the last 60 seconds&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Team MVPs&lt;/td>
&lt;td>each team&amp;rsquo;s top scorer and their share of the team&amp;rsquo;s total, then the 10 players with the largest shares&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h2 id="architecture" data-numberify>Architecture&lt;a class="anchor ms-1" href="#architecture" aria-label="Link to this section: Architecture">&lt;/a>&lt;/h2>
&lt;p>&lt;picture>&lt;img class="img-fluid " alt="Architecture: a simulated game sends scores to Kafka, Flink SQL jobs keep leaderboards in PostgreSQL, and a dashboard shows them" src="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/architecture.png" loading="lazy" width="1234" height="1097" />
&lt;/picture>

&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Events.&lt;/strong> A simulation built with &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/latest/getting-started/">documentation&lt;/a>) plays the game in real time. desflow is a Python library for simulations that stream their output. Each score is published to the Kafka topic &lt;code>game-scores&lt;/code> in Avro, a compact binary format whose schema is kept in a schema registry.&lt;/li>
&lt;li>&lt;strong>Leaderboards.&lt;/strong> Four Flink SQL jobs, one per leaderboard, read the topic and compute the leaderboards. Flink is a stream processor. It keeps each query&amp;rsquo;s result up to date as events arrive, rather than running the query once.&lt;/li>
&lt;li>&lt;strong>Storage.&lt;/strong> Each job writes its leaderboard to a PostgreSQL table with one row per rank. When the team or player at a rank changes, Flink updates that row.&lt;/li>
&lt;li>&lt;strong>Dashboard.&lt;/strong> A &lt;a href="https://nicegui.io/" target="_blank" rel="noopener noreferrer">NiceGUI&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> web page reads the four tables and redraws them every 2 seconds. NiceGUI is a Python library for web pages. The page shows the current leaderboards as soon as it opens, because the tables always hold the latest state.&lt;/li>
&lt;/ol>
&lt;p>The tools:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Tool&lt;/th>
&lt;th>Role here&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>desflow&lt;/td>
&lt;td>simulates the game, publishes the events to Kafka, and reads parameter changes from Kafka while it runs&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Apache Kafka, with Karapace&lt;/td>
&lt;td>carries the events; Karapace is the schema registry that holds their Avro schema&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Apache Flink&lt;/td>
&lt;td>runs the SQL jobs that compute the leaderboards&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>PostgreSQL&lt;/td>
&lt;td>stores the leaderboards&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>NiceGUI&lt;/td>
&lt;td>the dashboard&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/td>
&lt;td>starts all the services above with Docker Compose&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h2 id="setup" data-numberify>Setup&lt;a class="anchor ms-1" href="#setup" aria-label="Link to this section: Setup">&lt;/a>&lt;/h2>
&lt;p>You need Docker (Docker Desktop, OrbStack or Docker Engine), &lt;a href="https://docs.astral.sh/uv/" target="_blank" rel="noopener noreferrer">uv&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and Python 3.13. Clone the repository and run every command from the project folder:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">git clone https://github.com/jaehyeon-kim/benchtop.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> benchtop/game-leaderboard
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">uv venv &lt;span class="c1"># create .venv&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="nb">source&lt;/span> .venv/bin/activate &lt;span class="c1"># activate it, in each new shell&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">uv pip install -r requirements.txt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">odctl up kafka-lite flink-lite postgres
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>odctl is a command line tool that starts a local data stack with Docker Compose. &lt;code>kafka-lite&lt;/code> starts one Kafka broker, Karapace and Kafka UI. &lt;code>flink-lite&lt;/code> starts a Flink cluster with one TaskManager, the process that runs a job&amp;rsquo;s work. &lt;code>postgres&lt;/code> starts PostgreSQL.&lt;/p>
&lt;p>The web UIs:&lt;/p>
&lt;ul>
&lt;li>Kafka UI, for the topic and its messages: http://127.0.0.1:8086&lt;/li>
&lt;li>Flink UI, for the running jobs: http://127.0.0.1:8082&lt;/li>
&lt;/ul>

&lt;h2 id="simulation-model" data-numberify>Simulation Model&lt;a class="anchor ms-1" href="#simulation-model" aria-label="Link to this section: Simulation Model">&lt;/a>&lt;/h2>
&lt;p>A discrete-event simulation (DES) moves a clock from one event to the next, such as a player&amp;rsquo;s next round. Nothing happens between events, so the model only has to say what each event does and how long it is until the next one. Here the clock runs at the speed of real time.&lt;/p>
&lt;p>The model is built from desflow&amp;rsquo;s parts. The teams themselves are plain Python, in &lt;code>leaderboard/simulation/game.py&lt;/code>:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Part of the game&lt;/th>
&lt;th>desflow feature&lt;/th>
&lt;th>Default&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Players arrive at random&lt;/td>
&lt;td>an arrival, &lt;code>player&lt;/code>: exponential gaps&lt;/td>
&lt;td>a player every 2 seconds&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>A player&amp;rsquo;s session&lt;/td>
&lt;td>a process, one per player, started for each arrival&lt;/td>
&lt;td>lasts a lognormal time, 5 minutes on average&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Time between rounds&lt;/td>
&lt;td>a service, &lt;code>round&lt;/code> for people and &lt;code>robot_round&lt;/code> for robots&lt;/td>
&lt;td>6 seconds for people, 1.5 for robots&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>A device goes offline&lt;/td>
&lt;td>a service, &lt;code>offline&lt;/code>: the score is held, then sent with the time it was earned&lt;/td>
&lt;td>7 minutes on average&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Robots and late scores&lt;/td>
&lt;td>registry variables, &lt;code>robot_share&lt;/code> and &lt;code>late_share&lt;/code>&lt;/td>
&lt;td>5% of players, 1% of scores&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Sending the scores&lt;/td>
&lt;td>Kafka egress, with a serializer that sends only the score&lt;/td>
&lt;td>about 27 scores a second&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Changing parameters&lt;/td>
&lt;td>Kafka ingress on &lt;code>game-control&lt;/code>&lt;/td>
&lt;td>none&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The parameters and the arrival process are set in &lt;code>build&lt;/code>, in &lt;code>leaderboard/simulation/run.py&lt;/code>. When a player arrives, the arrival process decides whether they are a robot, then starts their session as a process of its own:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">build&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">app&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="kc">None&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="s2"> Adds the game&amp;#39;s parameters and processes to a simulation.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="s2"> Args:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="s2"> app (SimulationContext): The simulation, before it runs.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_arrival&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;player&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;exponential&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">rate&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">0.5&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># a player every 2 s&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_service&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;session&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;lognormal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">mean&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">300&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">std&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">120&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># 5 minutes&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_service&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;round&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;lognormal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">mean&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">6&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">std&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">2&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_service&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;robot_round&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;normal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">mean&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.5&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">std&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">0.3&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_service&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;offline&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;normal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">mean&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">420&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">std&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">120&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># 7 minutes&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_variable&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;robot_share&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">0.05&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_variable&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;late_share&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">0.01&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="nd">@app.arrival_loop&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;player&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="k">def&lt;/span> &lt;span class="nf">players&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">Iterator&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">Any&lt;/span>&lt;span class="p">]:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="n">game&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">Game&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">sampler&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rng&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># type: ignore[union-attr]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="n">count&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="kc">True&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">wait_for_arrival&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;player&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl"> &lt;span class="n">count&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl"> &lt;span class="n">robot&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">game&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rng&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">random&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="n">_share&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;robot_share&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl"> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">spawn&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl"> &lt;span class="n">_session&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">game&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="s1">&amp;#39;RBT&amp;#39;&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">robot&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="s1">&amp;#39;USR&amp;#39;&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">-&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">count&lt;/span>&lt;span class="si">:&lt;/span>&lt;span class="s2">06&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">robot&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl"> &lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>A session puts the player in a team with room, or forms a new team. A team holds up to 15 players, and one arrival in ten forms a new team anyway. The player then plays rounds until the session time is up, and each round earns a score from 0 to 20. A late score is held by a process of its own and sent minutes later, with the time it was earned. When the player leaves, they leave their team, and a team with no players left dissolves:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">_session&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="n">ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">game&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">Game&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">player&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">robot&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">bool&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">Iterator&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">Any&lt;/span>&lt;span class="p">]:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;Plays one session: joins a team, plays rounds until the session ends, and leaves.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="n">team&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">game&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">join&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">player&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="n">ends&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="n">_draw&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;session&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="kc">True&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">timeout&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">_draw&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;robot_round&amp;#34;&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">robot&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="s2">&amp;#34;round&amp;#34;&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span> &lt;span class="o">&amp;gt;&lt;/span> &lt;span class="n">ends&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="k">break&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">earned&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">start_datetime&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="n">timedelta&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">seconds&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="n">late&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">game&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rng&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">random&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="n">_share&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;late_share&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="n">event&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ScoreEvent&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="n">user_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">player&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="n">team_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">team&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="n">team_name&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">team&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">name&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nb">int&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">game&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">rng&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">integers&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">21&lt;/span>&lt;span class="p">)),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="n">event_time_millis&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="nb">int&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">earned&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">timestamp&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="mi">1000&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="n">event_type&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;late&amp;#34;&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">late&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="s2">&amp;#34;normal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">late&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl"> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">spawn&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">_send_late&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">event&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl"> &lt;span class="k">else&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl"> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">publish_event&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">player&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">event&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl"> &lt;span class="n">game&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">leave&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">team&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">player&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Every parameter is a path in desflow&amp;rsquo;s registry, such as &lt;code>game.variables.robot_share&lt;/code> or &lt;code>game.service.round.mean&lt;/code>. The registry is a store of named parameters. The processes read the live value each time they draw one, so a change sent to &lt;code>game-control&lt;/code> applies from the next arrival or round.&lt;/p>

&lt;h2 id="step-1-run-the-simulation" data-numberify>Step 1: Run the Simulation&lt;a class="anchor ms-1" href="#step-1-run-the-simulation" aria-label="Link to this section: Step 1: Run the Simulation">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m leaderboard.simulation.run &lt;span class="c1"># Ctrl + C to stop&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It creates two Kafka topics if they are missing: &lt;code>game-scores&lt;/code> for the scores, and &lt;code>game-control&lt;/code> for parameter changes. It then plays the game in real time and sends about 27 scores a second until you stop it. The first score registers the Avro schema in Karapace, under the subject &lt;code>game-scores-value&lt;/code>. Its log shows the simulation starting, and its Kafka writer and reader connecting:&lt;/p>
&lt;pre tabindex="0">&lt;code>2026-09-30 22:41:07,798 desflow.core.context Simulation engine started.
2026-09-30 22:41:07,801 desflow.connectors.egress.kafka Kafka Egress producer connected successfully.
2026-09-30 22:41:07,802 desflow.connectors.ingress.kafka Connected to Kafka Ingress topic: game-control
&lt;/code>&lt;/pre>&lt;p>In Kafka UI, the topic &lt;code>game-scores&lt;/code> shows the scores. Each message is keyed by the player:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;USR-000002&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;user_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;USR-000002&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;team_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;0000000001&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;team_name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Ruby-Wombat&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;score&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">8&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;event_time_millis&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790771752614&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;event_type&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;normal&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI showing the newest game-scores messages, decoded with their Avro schema" src="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/kafka-ui-game-scores.png" loading="lazy" title="Score events on the game-scores topic" width="1400" height="813" />
&lt;/picture>&lt;figcaption class="figure-caption">Score events on the game-scores topic&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="step-2-submit-the-flink-jobs" data-numberify>Step 2: Submit the Flink Jobs&lt;a class="anchor ms-1" href="#step-2-submit-the-flink-jobs" aria-label="Link to this section: Step 2: Submit the Flink Jobs">&lt;/a>&lt;/h2>
&lt;p>In a second terminal, with the environment activated:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m leaderboard.jobs.submit
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It creates the PostgreSQL schema &lt;code>game&lt;/code> with the four leaderboard tables in &lt;code>leaderboard/jobs/tables.sql&lt;/code>, then submits the four jobs:&lt;/p>
&lt;pre tabindex="0">&lt;code>2026-09-30 22:41:37,300 leaderboard.stores.flink Submitted 01-top-teams
2026-09-30 22:41:41,745 leaderboard.stores.flink Submitted 02-top-players
2026-09-30 22:41:46,280 leaderboard.stores.flink Submitted 03-hot-streaks
2026-09-30 22:41:50,970 leaderboard.stores.flink Submitted 04-team-mvps
&lt;/code>&lt;/pre>&lt;p>Before submitting, it also copies the OpenLineage client library into Flink&amp;rsquo;s &lt;code>lib&lt;/code> folder. Flink&amp;rsquo;s JDBC connector, which writes to PostgreSQL, needs it, and odctl&amp;rsquo;s Flink image has a copy but not in that folder.&lt;/p>
&lt;p>The jobs keep running in the Flink UI, as &lt;code>game-top-teams&lt;/code>, &lt;code>game-top-players&lt;/code>, &lt;code>game-hot-streaks&lt;/code> and &lt;code>game-team-mvps&lt;/code>. Each reads the topic from its first event, so the leaderboards include everything simulated so far.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Flink UI listing the four leaderboard jobs, all running" src="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/flink-ui-jobs.png" loading="lazy" title="The four Flink SQL jobs" width="1400" height="900" />
&lt;/picture>&lt;figcaption class="figure-caption">The four Flink SQL jobs&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h3 id="shared-tables" data-numberify>Shared Tables&lt;a class="anchor ms-1" href="#shared-tables" aria-label="Link to this section: Shared Tables">&lt;/a>&lt;/h3>
&lt;p>Flink SQL reads and writes tables that are defined over external systems. &lt;code>leaderboard/jobs/00-ddl.sql&lt;/code> defines the source table &lt;code>scores&lt;/code>, which reads the Kafka topic, and four sink tables, each writing to its PostgreSQL table. odctl&amp;rsquo;s Flink keeps table definitions only for one SQL client session, so each job loads this file first, with &lt;code>sql-client.sh -i&lt;/code>. The source table:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="c1">-- The score events. Event time comes from the event itself, and the watermark lets
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="c1">-- scores arrive up to 5 seconds out of order. Late events, minutes behind, still count
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1">-- towards the totals, but the hot streaks&amp;#39; time windows leave them out.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TABLE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">scores&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">user_id&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">score&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">INT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">event_time_millis&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">BIGINT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">event_type&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">event_time&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">TO_TIMESTAMP_LTZ&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">event_time_millis&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">3&lt;/span>&lt;span class="p">),&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">WATERMARK&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">FOR&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">event_time&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">event_time&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">INTERVAL&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;5&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">SECOND&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">WITH&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;connector&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;kafka&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;topic&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;game-scores&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;properties.bootstrap.servers&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;broker-1:19092&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;scan.startup.mode&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;earliest-offset&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;format&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;avro-confluent&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;avro-confluent.url&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;http://karapace:8081&amp;#39;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">);&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each event carries the time it happened, and Flink uses that time rather than the time the event arrives. This is called event time. A watermark tells Flink how long to wait for events that arrive out of order, here 5 seconds.&lt;/p>
&lt;p>A sink table has the rank as its primary key. The key makes the sink an upsert: Flink replaces the row at a rank when it changes.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">CREATE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">TABLE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">top_teams&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="n">rnk&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">BIGINT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">STRING&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">total_score&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">BIGINT&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">PRIMARY&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">KEY&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">rnk&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">NOT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">ENFORCED&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">WITH&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;connector&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;jdbc&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;url&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;jdbc:postgresql://postgres:5432/odctl&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table-name&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;game.top_teams&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;username&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;user&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;password&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;password&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;sink.buffer-flush.interval&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1s&amp;#39;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">);&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="one-job-per-leaderboard" data-numberify>One Job per Leaderboard&lt;a class="anchor ms-1" href="#one-job-per-leaderboard" aria-label="Link to this section: One Job per Leaderboard">&lt;/a>&lt;/h3>
&lt;p>Each leaderboard has its own job file, from &lt;code>01-top-teams.sql&lt;/code> to &lt;code>04-team-mvps.sql&lt;/code>. Each file sets its own job name and settings, then runs one &lt;code>INSERT&lt;/code> query. The top teams job, &lt;code>leaderboard/jobs/01-top-teams.sql&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="c1">-- One leaderboard job. Submit it with the shared table definitions:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="c1">-- ./bin/sql-client.sh -i /tmp/00-ddl.sql -f /tmp/01-top-teams.sql
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;pipeline.name&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;game-top-teams&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;parallelism.default&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Checkpoint every 10 seconds. The JDBC sink flushes its buffered rows at each
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="c1">-- checkpoint, and after a restart the job resumes from the last checkpoint instead of
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="c1">-- rebuilding its state from the start of the topic. odctl sets the checkpoint folder
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="c1">-- and RocksDB state, but no interval, so the job sets it.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;execution.checkpointing.interval&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;10s&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Keep a running total for an hour after its last score. A session lasts about 5
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="c1">-- minutes and a late score arrives about 7 minutes after it was earned, so no total is
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="c1">-- dropped while it can still change.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.state.ttl&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;60 min&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Collect updates for up to a second before writing, so a busy leaderboard is written
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="c1">-- once a second rather than once per event.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.enabled&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;true&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.allow-latency&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1s&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.size&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;2000&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Top teams: the 10 teams with the highest total score.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">INSERT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">INTO&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">top_teams&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">rnk&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">total_score&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">*&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">ROW_NUMBER&lt;/span>&lt;span class="p">()&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">OVER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="k">ORDER&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">BY&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">total_score&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">DESC&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">rnk&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">SELECT&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">MAX&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_name&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">CAST&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="k">SUM&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">score&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="nb">BIGINT&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">AS&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">total_score&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">FROM&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">scores&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="k">GROUP&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="k">BY&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">team_id&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl">&lt;span class="w"> &lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">WHERE&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">rnk&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">&amp;lt;=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The settings each job makes:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Checkpoints.&lt;/strong> A checkpoint is a saved copy of a job&amp;rsquo;s state. Each job checkpoints every 10 seconds. The JDBC sink writes its buffered rows at each checkpoint, and after a restart a job resumes from its last checkpoint.&lt;/li>
&lt;li>&lt;strong>State time to live.&lt;/strong> Flink keeps a running total for each team or player in its state. &lt;code>table.exec.state.ttl&lt;/code> drops a total that has not changed for the time set. The totals are kept for an hour, which is longer than a session and a late score together.&lt;/li>
&lt;li>&lt;strong>Mini-batches.&lt;/strong> Each job collects updates for up to a second before writing (&lt;code>table.exec.mini-batch.*&lt;/code>), so a busy leaderboard is written once a second rather than once per event.&lt;/li>
&lt;li>&lt;strong>Ranking.&lt;/strong> Each query ranks with &lt;code>ROW_NUMBER() OVER (ORDER BY ...)&lt;/code> and keeps ranks 1 to 10.&lt;/li>
&lt;/ul>
&lt;p>The hot streaks job differs in its settings. Its windows look back only 60 seconds, so it keeps a player&amp;rsquo;s rows for 5 minutes. Its mini-batches are half the size. These are the settings in &lt;code>leaderboard/jobs/03-hot-streaks.sql&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sql" data-lang="sql">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;pipeline.name&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;game-hot-streaks&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;parallelism.default&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Checkpoint every 10 seconds. The JDBC sink flushes its buffered rows at each
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="c1">-- checkpoint, and after a restart the job resumes from the last checkpoint instead of
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="c1">-- rebuilding its state from the start of the topic. odctl sets the checkpoint folder
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="c1">-- and RocksDB state, but no interval, so the job sets it.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;execution.checkpointing.interval&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;10s&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- The windows look back 60 seconds, so a player&amp;#39;s rows are dropped 5 minutes after
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="c1">-- their last score.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.state.ttl&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;5 min&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="c1">-- Collect updates for up to a second before writing, so a busy leaderboard is written
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="c1">-- once a second rather than once per event.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.enabled&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;true&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.allow-latency&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1s&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="w">&lt;/span>&lt;span class="k">SET&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;table.exec.mini-batch.size&amp;#39;&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="s1">&amp;#39;1000&amp;#39;&lt;/span>&lt;span class="p">;&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Its query averages each player&amp;rsquo;s scores over two windows, the last 10 seconds and the last 60 seconds, measured in event time. So late events, minutes behind, count towards the totals in the other three leaderboards, but the hot streaks leave them out.&lt;/p>
&lt;p>To see a leaderboard directly in PostgreSQL:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">docker &lt;span class="nb">exec&lt;/span> postgres psql -U user -d odctl -c &lt;span class="s2">&amp;#34;SELECT * FROM game.top_teams ORDER BY rnk&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="step-3-open-the-dashboard" data-numberify>Step 3: Open the Dashboard&lt;a class="anchor ms-1" href="#step-3-open-the-dashboard" aria-label="Link to this section: Step 3: Open the Dashboard">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m leaderboard.app.ui
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;pre tabindex="0">&lt;code>NiceGUI ready to go on http://127.0.0.1:8091
&lt;/code>&lt;/pre>&lt;p>Open http://127.0.0.1:8091. The four charts update every 2 seconds while the simulation runs.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="NiceGUI dashboard with bar charts of the top teams, top players, hot streaks and team MVPs" src="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/dashboard.png" loading="lazy" title="The leaderboard dashboard" width="1400" height="739" />
&lt;/picture>&lt;figcaption class="figure-caption">The leaderboard dashboard&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="step-4-change-the-game-while-it-runs" data-numberify>Step 4: Change the Game While It Runs&lt;a class="anchor ms-1" href="#step-4-change-the-game-while-it-runs" aria-label="Link to this section: Step 4: Change the Game While It Runs">&lt;/a>&lt;/h2>
&lt;p>The simulation reads parameter changes from the topic &lt;code>game-control&lt;/code> while it runs, through desflow&amp;rsquo;s Kafka ingress. For example, make every new player a robot:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m leaderboard.simulation.control game.variables.robot_share 1.0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;pre tabindex="0">&lt;code>Sent game.variables.robot_share = 1.0 to game-control
&lt;/code>&lt;/pre>&lt;p>Robots play a round about every 1.5 seconds, four times as often as people. So within a few minutes, as the new players&amp;rsquo; sessions go on, robots (&lt;code>RBT-...&lt;/code>) take over the top players. Before the change, the top players were mostly people:&lt;/p>
&lt;pre tabindex="0">&lt;code> rnk | user_id | team_name | total_score 
-----+------------+------------------+-------------
 1 | RBT-000005 | Ruby-Wombat | 603
 2 | RBT-000016 | Coral-Kookaburra | 342
 3 | RBT-000013 | Coral-Kookaburra | 321
 4 | USR-000002 | Ruby-Wombat | 151
 5 | USR-000011 | Ruby-Wombat | 136
 6 | USR-000007 | Ruby-Wombat | 135
 7 | USR-000001 | Ruby-Wombat | 133
 8 | USR-000008 | Ruby-Wombat | 120
 9 | USR-000015 | Ruby-Wombat | 119
 10 | USR-000014 | Coral-Kookaburra | 116
(10 rows)
&lt;/code>&lt;/pre>&lt;p>After the change, every one of the top players was a robot:&lt;/p>
&lt;pre tabindex="0">&lt;code> rnk | user_id | team_name | total_score 
-----+------------+------------------+-------------
 1 | RBT-000013 | Coral-Kookaburra | 1802
 2 | RBT-000016 | Coral-Kookaburra | 1645
 3 | RBT-000035 | Coral-Kookaburra | 1414
 4 | RBT-000039 | Coral-Kookaburra | 1386
 5 | RBT-000037 | Olive-Platypus | 1351
 6 | RBT-000041 | Olive-Platypus | 1271
 7 | RBT-000047 | Jade-Platypus | 1263
 8 | RBT-000005 | Ruby-Wombat | 1260
 9 | RBT-000044 | Olive-Platypus | 1250
 10 | RBT-000046 | Jade-Platypus | 1245
(10 rows)
&lt;/code>&lt;/pre>&lt;p>The team MVPs changed the same way. All 10 of the top MVPs were robots:&lt;/p>
&lt;pre tabindex="0">&lt;code> rnk | user_id | team_name | player_total | team_total | contrib_ratio 
-----+------------+------------------+--------------+------------+---------------------
 1 | RBT-000108 | Jade-Numbat | 235 | 475 | 0.49473684210526314
 2 | RBT-000106 | Teal-Quokka | 297 | 613 | 0.48450244698205547
 3 | RBT-000083 | Indigo-Dingo | 671 | 3367 | 0.1992871992871993
 4 | RBT-000086 | Olive-Numbat | 674 | 4302 | 0.15667131566713158
 5 | RBT-000005 | Ruby-Wombat | 1260 | 9392 | 0.13415672913117546
 6 | RBT-000013 | Coral-Kookaburra | 1820 | 13839 | 0.13151239251390998
 7 | RBT-000037 | Olive-Platypus | 1351 | 12947 | 0.10434849772147987
 8 | RBT-000047 | Jade-Platypus | 1279 | 12470 | 0.10256615878107458
 9 | RBT-000060 | Olive-Emu | 1058 | 10621 | 0.09961397231899068
 10 | RBT-000068 | Jade-Kookaburra | 900 | 9474 | 0.09499683343888538
(10 rows)
&lt;/code>&lt;/pre>&lt;p>Set the share back with &lt;code>python -m leaderboard.simulation.control game.variables.robot_share 0.05&lt;/code>. Other parameters work the same way. For example, &lt;code>game.arrival.player.rate 2.0&lt;/code> brings four times as many players, and so four times as many scores. &lt;code>python -m leaderboard.simulation.control --help&lt;/code> lists them all with their defaults.&lt;/p>

&lt;h2 id="clean-up" data-numberify>Clean Up&lt;a class="anchor ms-1" href="#clean-up" aria-label="Link to this section: Clean Up">&lt;/a>&lt;/h2>
&lt;p>Stop the simulation and the dashboard first, then:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m leaderboard.stores.cleanup
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It cancels the four Flink jobs, deletes the two Kafka topics and the schema subject, and drops the PostgreSQL schema &lt;code>game&lt;/code> with its tables. It leaves other projects&amp;rsquo; jobs, topics and tables alone, and the services keep running. It is safe to run again. To start over, run the steps again from Step 1.&lt;/p>
&lt;p>To stop the services and delete their data:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">odctl down --all --volumes &lt;span class="c1"># answer y; --volumes also deletes the data&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">deactivate
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">rm -rf .venv
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-04-15-sql-cookbook/">Run Flink SQL Cookbook in Docker&lt;/a> - more Flink SQL queries, from simple selects to windows and joins, run on a local cluster&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-06-17-kotlin-getting-started-flink-table/">Flink Table API - Declarative Analytics for Supplier Stats in Real Time&lt;/a> - a windowed aggregation over Kafka records with the Flink Table API, the code form of these SQL queries&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - another Benchtop project on the same simulation library, streaming database changes from PostgreSQL to Kafka&lt;/li>
&lt;/ul></content:encoded></item><item><title>Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect</title><link>https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/</link><guid>https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/</guid><pubDate>Thu, 01 Oct 2026 00:00:00 +0000</pubDate><category>Change Data Capture (CDC)</category><category>Debezium</category><category>Kafka Connect</category><category>Apache Kafka</category><category>PostgreSQL</category><category>SeaweedFS</category><category>odctl</category><category>desflow</category><category>Benchtop</category><description>&lt;p>Most databases change all day: users sign up, orders are placed, and each order moves from one status to the next. Change data capture (CDC) turns those changes into a stream of events that other systems can read as they happen. In this post, a simulated online shop writes to PostgreSQL in real time, Debezium streams every change to Kafka, and a sink connector saves the changes as files in object storage. Everything runs on your own machine.&lt;/p></description><content:encoded>&lt;p>Most databases change all day: users sign up, orders are placed, and each order moves from one status to the next. Change data capture (CDC) turns those changes into a stream of events that other systems can read as they happen. In this post, a simulated online shop writes to PostgreSQL in real time, Debezium streams every change to Kafka, and a sink connector saves the changes as files in object storage. Everything runs on your own machine.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>The shop&amp;rsquo;s data model follows the &lt;a href="https://console.cloud.google.com/marketplace/product/bigquery-public-data/thelook-ecommerce" target="_blank" rel="noopener noreferrer">theLook eCommerce&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> dataset. The source code is in &lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/ecommerce-cdc" target="_blank" rel="noopener noreferrer">benchtop/ecommerce-cdc&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. It is one of the &lt;a href="https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/">Benchtop&lt;/a> projects, which run locally from a fresh clone.&lt;/p>

&lt;h2 id="what-you-will-build" data-numberify>What You Will Build&lt;a class="anchor ms-1" href="#what-you-will-build" aria-label="Link to this section: What You Will Build">&lt;/a>&lt;/h2>
&lt;ol>
&lt;li>Run the simulation, which fills the tables and keeps changing them.&lt;/li>
&lt;li>Deploy the connectors: Debezium streams every insert and update to Kafka, and the S3 sink saves them as files.&lt;/li>
&lt;li>Look at the changes in Kafka and in SeaweedFS.&lt;/li>
&lt;li>Change the simulation while it runs, and see the change in the stream.&lt;/li>
&lt;/ol>
&lt;p>To start again at any point, &lt;code>python -m ecommerce.stores.cleanup&lt;/code> removes everything this project has created and keeps the services running. &lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/#clean-up">Clean Up&lt;/a> describes what it removes.&lt;/p>

&lt;h2 id="architecture" data-numberify>Architecture&lt;a class="anchor ms-1" href="#architecture" aria-label="Link to this section: Architecture">&lt;/a>&lt;/h2>
&lt;p>&lt;picture>&lt;img class="img-fluid " alt="Architecture: a simulated shop writes to PostgreSQL, Debezium streams the changes to Kafka, and an S3 sink saves them as files" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/architecture.png" loading="lazy" width="1238" height="1562" />
&lt;/picture>

&lt;/p>
&lt;p>The data moves in one direction: a simulation writes to PostgreSQL, Debezium reads the changes into Kafka topics, and the S3 sink writes the topics to files in SeaweedFS.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Change data capture&lt;/strong> means reading a database&amp;rsquo;s changes as a stream of events, instead of querying its tables again and again. PostgreSQL records every change in its write-ahead log (WAL) before it applies it, and CDC reads that log.&lt;/li>
&lt;li>&lt;strong>Kafka&lt;/strong> stores streams of events in topics. A topic keeps its events in order, and any number of readers can read it.&lt;/li>
&lt;li>&lt;strong>Kafka Connect&lt;/strong> runs connectors. A source connector copies data into Kafka, and a sink connector copies data out of it. You deploy a connector by sending its settings, as JSON, to Connect&amp;rsquo;s REST API.&lt;/li>
&lt;li>&lt;strong>Debezium&lt;/strong> is a source connector for CDC. It first copies every existing row, which is called a snapshot. It then streams each new change from the WAL, and writes one topic per table.&lt;/li>
&lt;li>&lt;strong>A replication slot&lt;/strong> is PostgreSQL&amp;rsquo;s record of how far a reader has got in the WAL. PostgreSQL keeps the part of the WAL the slot has not read yet, so Debezium can stop and carry on without losing a change.&lt;/li>
&lt;li>&lt;strong>A publication&lt;/strong> names the tables whose changes PostgreSQL sends. odctl&amp;rsquo;s PostgreSQL has a publication, &lt;code>cdc_pub&lt;/code>, that covers every table in the &lt;code>cdc&lt;/code> schema.&lt;/li>
&lt;li>&lt;strong>Aiven&amp;rsquo;s S3 sink&lt;/strong> is a sink connector. It reads the topics and writes their events to files in SeaweedFS, an object store with the same API as Amazon S3.&lt;/li>
&lt;li>&lt;strong>desflow&lt;/strong> runs the simulation, a discrete-event model of the shop. &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/latest/architecture/connectors/">documentation&lt;/a>) is a Python library for simulations that stream their output.&lt;/li>
&lt;/ul>
&lt;p>The simulation writes six tables in PostgreSQL&amp;rsquo;s &lt;code>cdc&lt;/code> schema:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Table&lt;/th>
&lt;th>Rows&lt;/th>
&lt;th>Changes&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>dist_centers&lt;/code>&lt;/td>
&lt;td>10 distribution centres&lt;/td>
&lt;td>inserted once&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>products&lt;/code>&lt;/td>
&lt;td>260 products&lt;/td>
&lt;td>inserted once&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>users&lt;/code>&lt;/td>
&lt;td>registered users&lt;/td>
&lt;td>inserted, and updated when a user moves address&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>orders&lt;/code>&lt;/td>
&lt;td>orders&lt;/td>
&lt;td>inserted as Processing, then updated to Shipped, Delivered, Cancelled or Returned&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>order_items&lt;/code>&lt;/td>
&lt;td>the products in each order&lt;/td>
&lt;td>inserted, and updated with their order&amp;rsquo;s status&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>events&lt;/code>&lt;/td>
&lt;td>page views in web sessions, including anonymous visitors&lt;/td>
&lt;td>inserted only&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h2 id="setup" data-numberify>Setup&lt;a class="anchor ms-1" href="#setup" aria-label="Link to this section: Setup">&lt;/a>&lt;/h2>
&lt;p>You need Docker (Docker Desktop, OrbStack or Docker Engine), &lt;a href="https://docs.astral.sh/uv/" target="_blank" rel="noopener noreferrer">uv&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and Python 3.13. Clone the repository and run every command from the project folder:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">git clone https://github.com/jaehyeon-kim/benchtop.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> benchtop/ecommerce-cdc
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">uv venv &lt;span class="c1"># create .venv&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="nb">source&lt;/span> .venv/bin/activate &lt;span class="c1"># activate it, in each new shell&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">uv pip install -r requirements.txt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">odctl up postgres kafka-lite storage
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> is a command line tool that starts a local data stack with Docker Compose. Its profiles start these services:&lt;/p>
&lt;ul>
&lt;li>&lt;code>postgres&lt;/code>: PostgreSQL, already set up for CDC. It runs with &lt;code>wal_level=logical&lt;/code>, which makes the WAL hold enough detail for CDC. It also has the schema &lt;code>cdc&lt;/code> with the publication &lt;code>cdc_pub&lt;/code>.&lt;/li>
&lt;li>&lt;code>kafka-lite&lt;/code>: one Kafka broker, Kafka Connect with the Debezium and Aiven S3 connectors, Karapace and Kafka UI. Karapace is a schema registry: it stores the schema of each topic&amp;rsquo;s messages, so a message carries only a short schema id.&lt;/li>
&lt;li>&lt;code>storage&lt;/code>: SeaweedFS.&lt;/li>
&lt;/ul>
&lt;p>The web UIs:&lt;/p>
&lt;ul>
&lt;li>Kafka UI: http://127.0.0.1:8086, for the topics, their messages and the connectors&lt;/li>
&lt;li>SeaweedFS file browser: http://127.0.0.1:8889&lt;/li>
&lt;/ul>

&lt;h2 id="simulation-model" data-numberify>Simulation Model&lt;a class="anchor ms-1" href="#simulation-model" aria-label="Link to this section: Simulation Model">&lt;/a>&lt;/h2>
&lt;p>A discrete-event simulation (DES) moves a clock from one event to the next, such as a visitor arriving or an order being packed. Nothing happens between events, so the model only has to say what each event does and how long it is until the next one. Here the clock runs at the speed of real time.&lt;/p>
&lt;p>The shop has three kinds of process. A process is a function that runs through simulated time and waits between its steps:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Visitor:&lt;/strong> visitors arrive at random, about one a second. Each views the home page and one to four products, and spends a few seconds on each page. Six in ten are registered users. Three in ten buy: they view the cart, sign up first if they are new, and place an order of one to four products.&lt;/li>
&lt;li>&lt;strong>Order:&lt;/strong> each order waits for one of the warehouse&amp;rsquo;s pickers. Three pickers pack orders, one at a time each. When orders arrive faster than they are packed, they queue. An order that waits longer than its customer&amp;rsquo;s patience, about two minutes, is cancelled. A packed order is shipped, delivered about a minute later, and one in ten is returned.&lt;/li>
&lt;li>&lt;strong>Move:&lt;/strong> now and then a registered user moves to a new address.&lt;/li>
&lt;/ul>
&lt;p>desflow provides each part of this model:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Part&lt;/th>
&lt;th>desflow feature&lt;/th>
&lt;th>Here&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>random arrivals&lt;/td>
&lt;td>&lt;code>add_arrival&lt;/code>, &lt;code>arrival_loop&lt;/code>&lt;/td>
&lt;td>visitors, and address moves&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>time spent on a step&lt;/td>
&lt;td>&lt;code>add_service&lt;/code>&lt;/td>
&lt;td>a page view, packing, transit, patience and the time until a return&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>limited capacity&lt;/td>
&lt;td>&lt;code>add_resource&lt;/code>&lt;/td>
&lt;td>the pickers, which make orders queue&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>chances&lt;/td>
&lt;td>&lt;code>add_variable&lt;/code>&lt;/td>
&lt;td>a visitor is registered, a visitor buys, an order is returned&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>a process of its own&lt;/td>
&lt;td>&lt;code>spawn&lt;/code>&lt;/td>
&lt;td>each visit and each order&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>live changes&lt;/td>
&lt;td>the registry and &lt;code>KafkaIngress&lt;/code>&lt;/td>
&lt;td>any of the above, while it runs&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>writes&lt;/td>
&lt;td>&lt;code>PostgresEgress&lt;/code>&lt;/td>
&lt;td>one per table, upserting on &lt;code>id&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Every parameter is in &lt;code>ecommerce/core/config.py&lt;/code>. Each one is added to desflow&amp;rsquo;s registry, a store of named parameters that the model reads each time it draws a value:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="n">SERVICES&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="c1"># mean and standard deviation, in seconds, lognormal&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;page_view&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mf">4.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">2.0&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="c1"># time on a page before the next one&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="c1"># how long an order waits for a picker before it is cancelled&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;patience&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mf">120.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">60.0&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;pick&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mf">5.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">2.0&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="c1"># a picker packs an order&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;transit&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mf">60.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">20.0&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="c1"># a shipped order reaches the customer&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;return_after&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mf">60.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">30.0&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="c1"># a returned order comes back after delivery&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="n">CHANCES&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="c1"># shares of visitors or orders, from 0 to 1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;returning&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">0.6&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="c1"># a visitor is a registered user&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;buy&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">0.3&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="c1"># a visitor buys at the end of the visit&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;return&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">0.1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="c1"># a delivered order is returned&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="n">PICKERS&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">MAX_PICKERS&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">10&lt;/span> &lt;span class="c1"># warehouse pickers packing orders at once&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The order process is in &lt;code>ecommerce/simulation/run.py&lt;/code>. It asks for a picker and a patience timeout at once, and &lt;code>picker | _wait(ctx, &amp;quot;patience&amp;quot;)&lt;/code> resumes at whichever comes first. If the timeout comes first, the order is cancelled. Each status change is published with the same &lt;code>id&lt;/code> as before:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">fulfil&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="n">ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">Shop&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">order&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">Order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">list&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="n">OrderItem&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="n">Process&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;One order: packed by a picker or cancelled, then shipped, delivered and maybe returned.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="k">with&lt;/span> &lt;span class="n">ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get_resource&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;pickers&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">request&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="n">picker&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="n">got&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="k">yield&lt;/span> &lt;span class="n">picker&lt;/span> &lt;span class="o">|&lt;/span> &lt;span class="n">_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;patience&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">picker&lt;/span> &lt;span class="ow">not&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">got&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="n">_publish&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="o">*&lt;/span>&lt;span class="n">shop&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">advance&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Cancelled&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">_now&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">)))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;pick&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">shop&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">advance&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Shipped&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">_now&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="n">_publish&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;transit&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">shop&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">advance&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Delivered&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">_now&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="n">_publish&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">_chance&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;return&amp;#34;&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">_wait&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;return_after&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="n">_publish&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="o">*&lt;/span>&lt;span class="n">shop&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">advance&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">order&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">items&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Returned&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">_now&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">state&lt;/span>&lt;span class="p">)))&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>run&lt;/code> function adds one PostgreSQL writer per table, each upserting on &lt;code>id&lt;/code>. An upsert inserts a row, or updates it when a row with the same key exists. So a changed order reaches PostgreSQL as an update, and Debezium sees it as one. The same function adds the Kafka reader for live changes:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">run&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">minutes&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">float&lt;/span> &lt;span class="o">|&lt;/span> &lt;span class="kc">None&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kc">None&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">seed&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">int&lt;/span> &lt;span class="o">|&lt;/span> &lt;span class="kc">None&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kc">None&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="kc">None&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="s2"> Runs the shop in real time, writing every row to its table and reading live changes.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="s2"> Args:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="s2"> minutes (float, optional): How long to run. None runs until interrupted.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="s2"> seed (int, optional): The seed for every random choice.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="n">postgres&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">create_tables&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">KafkaAdminConnector&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">KAFKA&lt;/span>&lt;span class="p">)&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">create_topics&lt;/span>&lt;span class="p">([{&lt;/span>&lt;span class="s2">&amp;#34;name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">CONTROL_TOPIC&lt;/span>&lt;span class="p">}])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">build&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">datetime&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">now&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">UTC&lt;/span>&lt;span class="p">),&lt;/span> &lt;span class="n">seed&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_ingress&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">KafkaIngress&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">topic&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">CONTROL_TOPIC&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">bootstrap_servers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">KAFKA&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">with_batching&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">batch_size&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">4&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">flush_interval&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.0&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1"># a few rows a second per table&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="k">for&lt;/span> &lt;span class="n">table&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">TABLES&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">values&lt;/span>&lt;span class="p">():&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">add_egress&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="n">PostgresEgress&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">DSN&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">table_name&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">table&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">upsert_keys&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">KEY&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="n">when&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">lambda&lt;/span> &lt;span class="n">r&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">t&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">table&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">r&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s2">&amp;#34;stream_type&amp;#34;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;event&amp;#34;&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">r&lt;/span>&lt;span class="p">[&lt;/span>&lt;span class="s2">&amp;#34;key&amp;#34;&lt;/span>&lt;span class="p">]&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="n">t&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">run&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">until&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">minutes&lt;/span> &lt;span class="o">*&lt;/span> &lt;span class="mi">60&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">minutes&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="kc">None&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Times are stored as ISO 8601 text in UTC. desflow publishes each row as JSON-friendly values, so a time reaches PostgreSQL as a string, and a timestamp column does not accept a string.&lt;/p>

&lt;h2 id="step-1-run-the-simulation" data-numberify>Step 1: Run the Simulation&lt;a class="anchor ms-1" href="#step-1-run-the-simulation" aria-label="Link to this section: Step 1: Run the Simulation">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m ecommerce.simulation.run --minutes &lt;span class="m">16&lt;/span> &lt;span class="c1"># or leave out --minutes, and stop with Ctrl + C&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It creates the six tables if they are missing, and writes the centres, the products and 100 users. It then runs in real time. With the default parameters it places about 20 orders a minute, and each order changes three or four times over the next few minutes. Its log shows the Kafka reader and one PostgreSQL writer per table:&lt;/p>
&lt;pre tabindex="0">&lt;code>2026-09-30 22:39:25,564 desflow.core.context Building SimulationContext for &amp;#39;ecommerce&amp;#39;...
2026-09-30 22:39:25,565 desflow.core.context Simulation engine started.
2026-09-30 22:39:25,599 desflow.connectors.ingress.kafka Connected to Kafka Ingress topic: ecommerce-control
2026-09-30 22:39:25,966 desflow.connectors.egress.postgres PostgresEgress connected to dist_centers
2026-09-30 22:39:25,966 desflow.connectors.egress.postgres PostgresEgress connected to products
2026-09-30 22:39:25,966 desflow.connectors.egress.postgres PostgresEgress connected to events
2026-09-30 22:39:25,966 desflow.connectors.egress.postgres PostgresEgress connected to order_items
2026-09-30 22:39:25,966 desflow.connectors.egress.postgres PostgresEgress connected to orders
2026-09-30 22:39:25,967 desflow.connectors.egress.postgres PostgresEgress connected to users
&lt;/code>&lt;/pre>&lt;p>Leave it running, and use a second terminal for the next steps.&lt;/p>

&lt;h2 id="step-2-deploy-the-connectors" data-numberify>Step 2: Deploy the Connectors&lt;a class="anchor ms-1" href="#step-2-deploy-the-connectors" aria-label="Link to this section: Step 2: Deploy the Connectors">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m ecommerce.cdc.connectors
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It sends two JSON files to Kafka Connect. Connect creates each connector, or updates it if it exists. The source connector&amp;rsquo;s settings are in &lt;code>ecommerce/cdc/source.json&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;connector.class&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.debezium.connector.postgresql.PostgresConnector&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;tasks.max&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;database.hostname&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;postgres&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;database.port&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;5432&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;database.user&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;user&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;database.password&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;password&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;database.dbname&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;odctl&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;plugin.name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;pgoutput&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;publication.name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;cdc_pub&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;publication.autocreate.mode&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;disabled&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;slot.name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ecommerce_cdc&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;table.include.list&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;cdc.users,cdc.products,cdc.dist_centers,cdc.orders,cdc.order_items,cdc.events&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;topic.prefix&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ecommerce&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;snapshot.mode&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;initial&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key.converter&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.confluent.connect.avro.AvroConverter&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key.converter.schema.registry.url&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;http://karapace:8081&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value.converter&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.confluent.connect.avro.AvroConverter&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value.converter.schema.registry.url&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;http://karapace:8081&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>These settings tell Debezium to:&lt;/p>
&lt;ul>
&lt;li>read the database &lt;code>odctl&lt;/code> with PostgreSQL&amp;rsquo;s built-in &lt;code>pgoutput&lt;/code> decoder;&lt;/li>
&lt;li>use the existing publication &lt;code>cdc_pub&lt;/code>, and never create one (&lt;code>publication.autocreate.mode&lt;/code> is &lt;code>disabled&lt;/code>);&lt;/li>
&lt;li>keep its place in the WAL in its own replication slot, &lt;code>ecommerce_cdc&lt;/code>;&lt;/li>
&lt;li>capture only this project&amp;rsquo;s six tables, and name the topics &lt;code>ecommerce.cdc.&amp;lt;table&amp;gt;&lt;/code>;&lt;/li>
&lt;li>take a snapshot of the existing rows first (&lt;code>snapshot.mode&lt;/code> is &lt;code>initial&lt;/code>);&lt;/li>
&lt;li>write Avro, a compact binary format, with each table&amp;rsquo;s key and value schemas registered in Karapace as the subjects &lt;code>ecommerce.cdc.&amp;lt;table&amp;gt;-key&lt;/code> and &lt;code>ecommerce.cdc.&amp;lt;table&amp;gt;-value&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>The sink connector&amp;rsquo;s settings are in &lt;code>ecommerce/cdc/s3-sink.json&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;connector.class&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.aiven.kafka.connect.s3.AivenKafkaConnectS3SinkConnector&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;tasks.max&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;topics.regex&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ecommerce\\.cdc\\..*&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;consumer.override.metadata.max.age.ms&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;30000&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;aws.access.key.id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;user&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;aws.secret.access.key&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;password&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;aws.s3.endpoint&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;http://seaweed:8333&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;aws.s3.region&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;us-east-1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;aws.s3.bucket.name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;odctl-dev&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;file.name.template&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ecommerce-cdc/{{topic}}/{{partition}}-{{start_offset}}.jsonl&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;file.compression.type&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;none&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;format.output.type&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;jsonl&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;format.output.fields&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;key,value,offset,timestamp&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;format.output.fields.value.encoding&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;none&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key.converter&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.confluent.connect.avro.AvroConverter&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key.converter.schema.registry.url&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;http://karapace:8081&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value.converter&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;io.confluent.connect.avro.AvroConverter&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value.converter.schema.registry.url&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;http://karapace:8081&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It reads every topic whose name matches &lt;code>ecommerce.cdc.*&lt;/code>, and decodes each message with its schema from Karapace. It writes JSON lines files to the bucket &lt;code>odctl-dev&lt;/code>, under &lt;code>ecommerce-cdc/&amp;lt;topic&amp;gt;/&lt;/code>, with one change event on each line. The sink writes a file each time Connect commits its offsets, which record how far the sink has read. That happens about once a minute. The &lt;code>orders&lt;/code> topic is created only when the first order is placed, after the sink has started. The sink subscribes by a topic pattern, and a Kafka consumer looks for new topics only when it refreshes its list of topics, every five minutes by default. &lt;code>consumer.override.metadata.max.age.ms&lt;/code> makes the sink&amp;rsquo;s consumer refresh it every 30 seconds instead. In a test run, the first &lt;code>orders&lt;/code> file appeared after about a minute.&lt;/p>
&lt;p>Each connector&amp;rsquo;s status should show &lt;code>RUNNING&lt;/code> for the connector and its task:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">curl -s http://127.0.0.1:8083/connectors/ecommerce-cdc-source/status
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">curl -s http://127.0.0.1:8083/connectors/ecommerce-cdc-s3/status
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Kafka UI lists both connectors under Kafka Connect.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI listing the Debezium source and the S3 sink, both running" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/kafka-ui-connectors.png" loading="lazy" title="Both connectors in Kafka UI" width="1400" height="722" />
&lt;/picture>&lt;figcaption class="figure-caption">Both connectors in Kafka UI&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="step-3-look-at-the-changes" data-numberify>Step 3: Look at the Changes&lt;a class="anchor ms-1" href="#step-3-look-at-the-changes" aria-label="Link to this section: Step 3: Look at the Changes">&lt;/a>&lt;/h2>
&lt;p>Debezium creates one topic per table. The simulation creates &lt;code>ecommerce-control&lt;/code>, which carries parameter changes. The topics after a run:&lt;/p>
&lt;pre tabindex="0">&lt;code>ecommerce-control
ecommerce.cdc.dist_centers
ecommerce.cdc.events
ecommerce.cdc.order_items
ecommerce.cdc.orders
ecommerce.cdc.products
ecommerce.cdc.users
&lt;/code>&lt;/pre>&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI listing the six ecommerce.cdc topics and the control topic, with their message counts" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/kafka-ui-topics.png" loading="lazy" title="The change topics in Kafka UI" width="1400" height="586" />
&lt;/picture>&lt;figcaption class="figure-caption">The change topics in Kafka UI&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI&amp;amp;rsquo;s schema registry page listing a key and a value subject for each ecommerce.cdc topic" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/kafka-ui-schemas.png" loading="lazy" title="The Avro subjects in Karapace" width="1400" height="711" />
&lt;/picture>&lt;figcaption class="figure-caption">The Avro subjects in Karapace&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>In Kafka UI, open a topic such as &lt;code>ecommerce.cdc.orders&lt;/code>. Each message is one change, and its &lt;code>op&lt;/code> field says which kind:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>&lt;code>op&lt;/code>&lt;/th>
&lt;th>Meaning&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>&lt;code>r&lt;/code>&lt;/td>
&lt;td>a row read in the first snapshot&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>c&lt;/code>&lt;/td>
&lt;td>a new row (create)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>&lt;code>u&lt;/code>&lt;/td>
&lt;td>an update: &lt;code>after&lt;/code> holds the row&amp;rsquo;s new values&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka UI showing the newest ecommerce.cdc.orders messages, decoded with their Avro schemas" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/kafka-ui-orders-messages.png" loading="lazy" title="Change events on the orders topic" width="1400" height="813" />
&lt;/picture>&lt;figcaption class="figure-caption">Change events on the orders topic&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>This update shows an order moving from Processing to Shipped, with the time it shipped. &lt;code>source&lt;/code> records where the change came from: the table, the transaction and its position in the WAL (&lt;code>lsn&lt;/code>).&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;before&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;after&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;23b2d4a9-360d-4973-a6da-fd1cc394ca10&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;user_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;862dd089-c746-4127-89d7-db4fcab59a2e&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;status&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Shipped&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;num_of_items&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;created_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:38:56+00:00&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;updated_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:39:01+00:00&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;shipped_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:39:01+00:00&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;delivered_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;cancelled_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;returned_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;source&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;version&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;3.5.1.Final&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;connector&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;postgresql&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;ecommerce&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_ms&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541329&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;snapshot&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;false&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;db&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;odctl&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;sequence&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;[\&amp;#34;161004032\&amp;#34;,\&amp;#34;161004832\&amp;#34;]&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_us&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541329358&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_ns&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541329358000&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;schema&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;cdc&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;table&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;orders&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;txId&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">45922&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;lsn&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">161004832&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;xmin&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;origin&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;origin_lsn&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">33&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;transaction&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">34&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;op&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;u&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">35&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_ms&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541830&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">36&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_us&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541830602&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">37&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;ts_ns&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1790775541830602065&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">38&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>before&lt;/code> is null for updates, because PostgreSQL logs only the new row by default.&lt;/p>
&lt;p>In the SeaweedFS file browser, go to &lt;code>odctl-dev/ecommerce-cdc/&lt;/code>. There is a folder per topic. Each file holds the change events of one partition, one per line. The first line of the first &lt;code>orders&lt;/code> file, &lt;code>0-0.jsonl&lt;/code>, is a create event. The sink adds the message&amp;rsquo;s key, offset and time around the change event:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;offset&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">0&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;before&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;after&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;23b2d4a9-360d-4973-a6da-fd1cc394ca10&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;user_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;862dd089-c746-4127-89d7-db4fcab59a2e&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;status&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Processing&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;num_of_items&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;created_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:38:56+00:00&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;updated_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:38:56+00:00&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;shipped_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;delivered_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;cancelled_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;returned_at&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="kc">null&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;op&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;c&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;23b2d4a9-360d-4973-a6da-fd1cc394ca10&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;timestamp&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2026-09-30T13:38:57.992Z&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The line is shown across several lines here. &lt;code>source&lt;/code>, &lt;code>transaction&lt;/code> and the &lt;code>ts_*&lt;/code> times are left out of &lt;code>value&lt;/code>, because they have the same fields as the change event above.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="SeaweedFS file browser listing the JSON lines files the sink wrote for the orders topic" src="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/seaweedfs-files.png" loading="lazy" title="Files the S3 sink wrote to SeaweedFS" width="1400" height="797" />
&lt;/picture>&lt;figcaption class="figure-caption">Files the S3 sink wrote to SeaweedFS&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="step-4-change-the-simulation-while-it-runs" data-numberify>Step 4: Change the Simulation While It Runs&lt;a class="anchor ms-1" href="#step-4-change-the-simulation-while-it-runs" aria-label="Link to this section: Step 4: Change the Simulation While It Runs">&lt;/a>&lt;/h2>
&lt;p>The simulation reads parameter changes from the Kafka topic &lt;code>ecommerce-control&lt;/code>, through desflow&amp;rsquo;s &lt;code>KafkaIngress&lt;/code>. It uses each new value from the next time it draws one. &lt;code>control.py&lt;/code> sends a change. With the simulation and both connectors running, cut the pickers from three to one:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m ecommerce.simulation.control ecommerce.resources.pickers.current_cap &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;pre tabindex="0">&lt;code>Sent ecommerce.resources.pickers.current_cap = 1.0 to ecommerce-control
&lt;/code>&lt;/pre>&lt;p>One picker packs about 12 orders a minute, which is fewer than the 20 or so placed, so the queue grows. The &lt;code>Shipped&lt;/code> updates in &lt;code>ecommerce.cdc.orders&lt;/code> fall from about 20 a minute to about 11. &lt;code>Cancelled&lt;/code> updates appear as customers&amp;rsquo; patience runs out, which never happens with three pickers. In one run, the change was sent at 12:45:02 UTC. These are the changes per minute in &lt;code>ecommerce.cdc.orders&lt;/code>, counted by the minute of each Debezium event:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Minute (UTC)&lt;/th>
&lt;th>Pickers&lt;/th>
&lt;th>New orders&lt;/th>
&lt;th>Shipped&lt;/th>
&lt;th>Cancelled&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>12:40&lt;/td>
&lt;td>3&lt;/td>
&lt;td>22&lt;/td>
&lt;td>22&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:41&lt;/td>
&lt;td>3&lt;/td>
&lt;td>18&lt;/td>
&lt;td>18&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:42&lt;/td>
&lt;td>3&lt;/td>
&lt;td>23&lt;/td>
&lt;td>22&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:43&lt;/td>
&lt;td>3&lt;/td>
&lt;td>20&lt;/td>
&lt;td>21&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:44&lt;/td>
&lt;td>3&lt;/td>
&lt;td>27&lt;/td>
&lt;td>27&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:45&lt;/td>
&lt;td>1 from 12:45:02&lt;/td>
&lt;td>22&lt;/td>
&lt;td>13&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:46&lt;/td>
&lt;td>1&lt;/td>
&lt;td>18&lt;/td>
&lt;td>10&lt;/td>
&lt;td>3&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:47&lt;/td>
&lt;td>1&lt;/td>
&lt;td>10&lt;/td>
&lt;td>13&lt;/td>
&lt;td>6&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:48&lt;/td>
&lt;td>1&lt;/td>
&lt;td>20&lt;/td>
&lt;td>10&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:49&lt;/td>
&lt;td>1&lt;/td>
&lt;td>16&lt;/td>
&lt;td>13&lt;/td>
&lt;td>4&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:50&lt;/td>
&lt;td>1&lt;/td>
&lt;td>24&lt;/td>
&lt;td>12&lt;/td>
&lt;td>7&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:51&lt;/td>
&lt;td>1&lt;/td>
&lt;td>16&lt;/td>
&lt;td>9&lt;/td>
&lt;td>4&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:52&lt;/td>
&lt;td>1&lt;/td>
&lt;td>17&lt;/td>
&lt;td>12&lt;/td>
&lt;td>10&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>12:53&lt;/td>
&lt;td>1&lt;/td>
&lt;td>13&lt;/td>
&lt;td>10&lt;/td>
&lt;td>8&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Other parameters work the same way, such as &lt;code>ecommerce.arrival.visitor.rate&lt;/code> for visitors a second, or &lt;code>ecommerce.variables.buy&lt;/code> for the chance a visitor buys. &lt;code>python -m ecommerce.simulation.control --help&lt;/code> lists them all. A change lasts until the simulation stops.&lt;/p>

&lt;h2 id="clean-up" data-numberify>Clean Up&lt;a class="anchor ms-1" href="#clean-up" aria-label="Link to this section: Clean Up">&lt;/a>&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m ecommerce.stores.cleanup
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It removes only this project&amp;rsquo;s objects, and keeps the services running:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Connectors:&lt;/strong> it stops both and deletes their stored offsets, so a new source connector takes a fresh snapshot. It then deletes them.&lt;/li>
&lt;li>&lt;strong>Replication slot:&lt;/strong> it drops &lt;code>ecommerce_cdc&lt;/code>. PostgreSQL keeps the WAL for an unused slot forever, so a slot left behind makes the WAL grow without limit.&lt;/li>
&lt;li>&lt;strong>Tables:&lt;/strong> it drops the six tables in &lt;code>cdc&lt;/code>.&lt;/li>
&lt;li>&lt;strong>Kafka:&lt;/strong> it deletes the topics starting with &lt;code>ecommerce.&lt;/code>, and &lt;code>ecommerce-control&lt;/code>.&lt;/li>
&lt;li>&lt;strong>SeaweedFS:&lt;/strong> it deletes the files under &lt;code>odctl-dev/ecommerce-cdc/&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>In a test run, the first clean-up deleted both connectors, the seven topics and 45 files. A second run found nothing left to delete, so it is safe to run twice.&lt;/p>
&lt;p>To stop the services and delete their data:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">odctl down --all --volumes &lt;span class="c1"># answer y; --volumes also deletes the data&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">deactivate
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">rm -rf .venv
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2024-11-07-cdc-local-dev/">Change Data Capture (CDC) Local Development with PostgreSQL, Debezium Server and Pub/Sub Emulator&lt;/a> - change data capture from PostgreSQL with Debezium Server and a Pub/Sub emulator instead of Kafka Connect&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2021-12-05-datalake-demo-part1/">Data Lake Demo using Change Data Capture (CDC) on AWS - Part 1 Local Development&lt;/a> - an earlier local setup with Debezium and an S3 sink connector on Kafka Connect, the start of a data lake series&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-06-simulations-in-yaml-dynamic-des/">Defining Data-Streaming Simulations in YAML, Without Writing Python&lt;/a> - the simulation library that runs the shop, now configurable in plain YAML&lt;/li>
&lt;/ul></content:encoded></item><item><title>Data Streaming and Machine Learning Projects That Run on Your Laptop</title><link>https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/</link><guid>https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/</guid><pubDate>Wed, 30 Sep 2026 00:00:00 +0000</pubDate><category>Apache Kafka</category><category>Apache Flink</category><category>MLOps</category><category>odctl</category><category>desflow</category><category>Benchtop</category><description>&lt;p>Reading about a data tool only goes so far. The hard part is getting several tools to run together and seeing how data moves between them. &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">Benchtop&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> is a collection of hands-on demos for data engineering, stream processing, machine learning, AI engineering and MLOps. Each demo runs locally on the odctl stack, and works from a fresh clone of the repository, with nothing to install beforehand but Docker and uv. Each one builds a working system from open-source tools and explains the ideas behind it, so you learn a tool by running it rather than only reading about it.&lt;/p></description><content:encoded>&lt;p>Reading about a data tool only goes so far. The hard part is getting several tools to run together and seeing how data moves between them. &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">Benchtop&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> is a collection of hands-on demos for data engineering, stream processing, machine learning, AI engineering and MLOps. Each demo runs locally on the odctl stack, and works from a fresh clone of the repository, with nothing to install beforehand but Docker and uv. Each one builds a working system from open-source tools and explains the ideas behind it, so you learn a tool by running it rather than only reading about it.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>

&lt;h2 id="why-one-repository" data-numberify>Why One Repository&lt;a class="anchor ms-1" href="#why-one-repository" aria-label="Link to this section: Why One Repository">&lt;/a>&lt;/h2>
&lt;p>My earlier examples were spread across several repositories, each with its own setup. Some needed a cloud account, and some had drifted out of date with the tools they used. Benchtop brings them together under one set of rules:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Each project stands on its own.&lt;/strong> It has its own folder, its own README and its own dependencies, and you can read and run it without the others.&lt;/li>
&lt;li>&lt;strong>Everything runs locally.&lt;/strong> No step calls a cloud service, so a project costs nothing to run and works offline.&lt;/li>
&lt;li>&lt;strong>Each project teaches.&lt;/strong> Its README walks through the steps, and says what to run and what to look at. A &lt;code>docs&lt;/code> folder explains the concepts behind it from the start, for readers who are new to the tools.&lt;/li>
&lt;li>&lt;strong>Each project is tested.&lt;/strong> Its unit tests need none of the services, and GitHub runs them for every project on every push.&lt;/li>
&lt;/ul>

&lt;h2 id="what-the-projects-share" data-numberify>What the Projects Share&lt;a class="anchor ms-1" href="#what-the-projects-share" aria-label="Link to this section: What the Projects Share">&lt;/a>&lt;/h2>
&lt;p>Two tools of mine sit underneath almost every project.&lt;/p>
&lt;p>&lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> starts an open data stack with Docker Compose, as introduced in an &lt;a href="https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/">earlier post&lt;/a>. Each project starts only the services it needs, such as PostgreSQL, Kafka, Flink or Valkey, with one command like &lt;code>odctl up kafka-lite flink-lite postgres&lt;/code>. The same addresses and credentials work in every project, so there is nothing to configure.&lt;/p>
&lt;p>&lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/">documentation&lt;/a>) is a Python library for discrete-event simulations that stream their output to Kafka, PostgreSQL and Iceberg. Most projects use it to produce their data: a shop where visitors arrive, browse and buy, or a game where players join teams and score. Its parameters can change while it runs, so you can double the visitors or send the warehouse pickers home, and watch the rest of the system respond.&lt;/p>

&lt;h2 id="projects" data-numberify>Projects&lt;a class="anchor ms-1" href="#projects" aria-label="Link to this section: Projects">&lt;/a>&lt;/h2>
&lt;p>These are the projects so far. More will be added over time, and the &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">repository&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and the &lt;a href="https://jaehyeon.me/tags/benchtop/">benchtop tag&lt;/a> always list the current set. The projects are independent of each other, so start with whichever matches what you want to learn:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>&lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/live-dashboard" target="_blank" rel="noopener noreferrer">live-dashboard&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>:&lt;/strong> two dashboards that update themselves as a simulated shop takes orders. You learn how a server pushes new data to a web page as it arrives, with WebSockets, and build the same dashboard in Streamlit and in Next.js. A series of three posts starts with the &lt;a href="https://jaehyeon.me/blog/2025-02-18-realtime-dashboard-1/">data producer&lt;/a>.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/ecommerce-cdc" target="_blank" rel="noopener noreferrer">ecommerce-cdc&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>:&lt;/strong> every change to a shop&amp;rsquo;s database, captured as it happens and saved as files. You learn change data capture: reading a database&amp;rsquo;s own log of changes with Debezium, rather than querying its tables. The &lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">post&lt;/a> walks through it.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/order-streams" target="_blank" rel="noopener noreferrer">order-streams&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>:&lt;/strong> a stream of orders, first sent and read by small programs, then summarised every few seconds. You learn how Kafka moves messages, and two ways to process a stream as it flows, Kafka Streams and Flink, all in Kotlin. A series of five posts starts with &lt;a href="https://jaehyeon.me/blog/2025-05-20-kotlin-getting-started-kafka-json-clients/">Kafka clients with JSON&lt;/a>.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/game-leaderboard" target="_blank" rel="noopener noreferrer">game-leaderboard&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>:&lt;/strong> live leaderboards for a simulated mobile game. You learn to write Flink SQL queries that never finish and keep their answer up to date as scores arrive, including late scores and top 10 rankings. The &lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/">post&lt;/a> walks through it.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/product-recommender" target="_blank" rel="noopener noreferrer">product-recommender&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>:&lt;/strong> a shop that learns which products to show each visitor from what they click. You learn contextual bandits, first in plain Python, then split into a live service and a Flink job that trains the models. The &lt;a href="https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/">prototype&lt;/a> and &lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">production&lt;/a> posts explain it.&lt;/li>
&lt;li>&lt;strong>&lt;a href="https://jaehyeon.me/blog/2026-09-28-mlops-with-a-feature-store/">MLOps with a Feature Store&lt;/a>:&lt;/strong> a series that rebuilds the three projects in Jim Dowling&amp;rsquo;s book on feature stores with open-source tools. The first, &lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/air-quality" target="_blank" rel="noopener noreferrer">air-quality&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, forecasts daily air pollution for the week ahead. Fraud detection and a video recommender will follow.&lt;/li>
&lt;/ul>

&lt;h2 id="getting-started" data-numberify>Getting Started&lt;a class="anchor ms-1" href="#getting-started" aria-label="Link to this section: Getting Started">&lt;/a>&lt;/h2>
&lt;p>You need Docker, &lt;a href="https://docs.astral.sh/uv/" target="_blank" rel="noopener noreferrer">uv&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> for Python, and a copy of the repository:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">git clone https://github.com/jaehyeon-kim/benchtop.git
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each project is one folder, and the root holds only the checks they share:&lt;/p>
&lt;pre tabindex="0">&lt;code>benchtop/
├── live-dashboard/
├── ecommerce-cdc/
├── order-streams/
├── game-leaderboard/
├── product-recommender/
├── air-quality/
├── ...
&lt;/code>&lt;/pre>&lt;p>Inside, every Python project has the same core. game-leaderboard, for example:&lt;/p>
&lt;pre tabindex="0">&lt;code>game-leaderboard/
├── README.md the steps: what to run and what to look at
├── docs/ the concepts behind it, and its data
├── images/ the architecture diagram and screenshots
├── leaderboard/ the code, one folder per part
├── tests/ unit tests that need none of the services
└── requirements.txt
&lt;/code>&lt;/pre>&lt;p>Some projects add a folder for one more part: &lt;code>nextjs/&lt;/code> for live-dashboard&amp;rsquo;s Next.js dashboard, &lt;code>dags/&lt;/code> for air-quality&amp;rsquo;s Airflow pipelines, and &lt;code>recsys-trainer/&lt;/code> for product-recommender&amp;rsquo;s Flink job, written in Kotlin. order-streams is written in Kotlin throughout and built with Gradle, so it has one folder per application in place of a Python package.&lt;/p>
&lt;p>Pick a project, open its folder, and follow its README from the top. Each README ends with a clean-up command that removes what the project created and keeps the services running, so you can move on to the next project without starting again.&lt;/p></content:encoded></item><item><title>Learning MLOps with a Feature Store: A New Series</title><link>https://jaehyeon.me/blog/2026-09-28-mlops-with-a-feature-store/</link><guid>https://jaehyeon.me/blog/2026-09-28-mlops-with-a-feature-store/</guid><pubDate>Mon, 28 Sep 2026 00:00:00 +0000</pubDate><category>MLOps</category><category>Feature Store</category><category>Feast</category><category>MLflow</category><category>Apache Airflow</category><category>Apache Iceberg</category><category>odctl</category><category>desflow</category><description>&lt;p>Learning MLOps one tool at a time leaves out the hard part: how features, training and serving fit together as one system. This series learns it by building complete systems. I follow Jim Dowling&amp;rsquo;s &lt;a href="https://www.oreilly.com/library/view/building-machine-learning/9781098165222/" target="_blank" rel="noopener noreferrer">&lt;em>Building Machine Learning Systems with a Feature Store&lt;/em>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and rebuild its hands-on projects with open-source tools, running on my laptop.&lt;/p></description><content:encoded>&lt;p>Learning MLOps one tool at a time leaves out the hard part: how features, training and serving fit together as one system. This series learns it by building complete systems. I follow Jim Dowling&amp;rsquo;s &lt;a href="https://www.oreilly.com/library/view/building-machine-learning/9781098165222/" target="_blank" rel="noopener noreferrer">&lt;em>Building Machine Learning Systems with a Feature Store&lt;/em>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and rebuild its hands-on projects with open-source tools, running on my laptop.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>

&lt;h2 id="why-this-book" data-numberify>Why This Book&lt;a class="anchor ms-1" href="#why-this-book" aria-label="Link to this section: Why This Book">&lt;/a>&lt;/h2>
&lt;p>Rather than teaching one tool, this book teaches one way to build any ML system, and applies it to classic machine learning and to systems built on large language models alike: batch predictions, real-time predictions and agents.&lt;/p>
&lt;p>It rests on one architecture, split into three kinds of pipeline, known as FTI pipelines:&lt;/p>
&lt;ul>
&lt;li>a &lt;strong>feature pipeline&lt;/strong> turns raw data into features, the inputs a model learns from;&lt;/li>
&lt;li>a &lt;strong>training pipeline&lt;/strong> turns features and labels into a model;&lt;/li>
&lt;li>an &lt;strong>inference pipeline&lt;/strong> turns features and a model into predictions.&lt;/li>
&lt;/ul>
&lt;p>The pipelines never call each other. They share only two stores: a &lt;strong>feature store&lt;/strong>, which keeps features so that training and prediction read exactly the same values, and a &lt;strong>model registry&lt;/strong>, which keeps every trained model as a numbered version and marks the one in use. So each pipeline can be built, tested, scheduled and changed on its own.&lt;/p>

&lt;h2 id="three-projects" data-numberify>Three Projects&lt;a class="anchor ms-1" href="#three-projects" aria-label="Link to this section: Three Projects">&lt;/a>&lt;/h2>
&lt;p>The book teaches through three systems, each adding something new:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Air quality forecasting:&lt;/strong> a batch system that predicts daily air pollution for the week ahead from weather forecasts.&lt;/li>
&lt;li>&lt;strong>Credit card fraud detection:&lt;/strong> a real-time system, with features computed from a stream of transactions.&lt;/li>
&lt;li>&lt;strong>A personalised video recommender:&lt;/strong> a real-time system modelled on TikTok, which retrieves and ranks videos for each user.&lt;/li>
&lt;/ol>

&lt;h2 id="rebuilding-on-open-source" data-numberify>Rebuilding on Open Source&lt;a class="anchor ms-1" href="#rebuilding-on-open-source" aria-label="Link to this section: Rebuilding on Open Source">&lt;/a>&lt;/h2>
&lt;p>The book builds its projects on Hopsworks, a platform from the author&amp;rsquo;s company that bundles a feature store, a model registry and model serving. I am rebuilding each one with open-source tools instead:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>What the system needs&lt;/th>
&lt;th>Open-source tool&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Feature storage&lt;/td>
&lt;td>Apache Iceberg tables&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Feature store&lt;/td>
&lt;td>Feast, with an online store in Valkey&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Model registry and experiment tracking&lt;/td>
&lt;td>MLflow&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Scheduling the pipelines&lt;/td>
&lt;td>Apache Airflow&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Streaming features&lt;/td>
&lt;td>Apache Kafka and Apache Flink&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>All of them run locally with &lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, a command-line tool I built that starts an open data stack with Docker Compose, as introduced in an &lt;a href="https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/">earlier post&lt;/a>. The data comes from simulations built with &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/latest/guides/backfill-then-live/">documentation&lt;/a>), a Python library I built for discrete-event simulations that stream their output to Kafka, files and Iceberg. So every project runs offline, and nothing needs a cloud account.&lt;/p>

&lt;h2 id="what-comes-next" data-numberify>What Comes Next&lt;a class="anchor ms-1" href="#what-comes-next" aria-label="Link to this section: What Comes Next">&lt;/a>&lt;/h2>
&lt;p>Each project gets its own post in this series, covering how the design maps onto open-source tools, what worked, and what broke along the way. I will share them as each one is ready.&lt;/p></content:encoded></item><item><title>Building an Agentic Analytics System over an Iceberg Lakehouse</title><link>https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/</link><guid>https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/</guid><pubDate>Sat, 18 Jul 2026 00:00:00 +0000</pubDate><category>Agentic AI</category><category>WrenAI</category><category>Strands</category><category>Trino</category><category>Apache Iceberg</category><category>Mem0</category><category>desflow</category><category>odctl</category><description>&lt;p>Generative AI has made conversational analytics feel within reach, yet direct text-to-SQL systems remain hard to operate reliably. A database schema tells you the tables, columns, and types, but it says nothing about which datasets are canonical, which join paths are approved, how a governed metric is calculated, or what the business actually means by &amp;ldquo;revenue&amp;rdquo; or &amp;ldquo;active customer&amp;rdquo;. Ask a language model to infer all of that from raw tables and, sooner or later, it will confidently invent an answer.&lt;/p>
&lt;p>I built a proof of concept to explore a more disciplined approach: put a semantic layer between the model and the data, and let the agent reason over governed business concepts instead of guessing at raw schemas. It runs entirely locally on an open-source stack, and it happens to tie together two of my other projects along the way.&lt;/p></description><content:encoded>&lt;p>Generative AI has made conversational analytics feel within reach, yet direct text-to-SQL systems remain hard to operate reliably. A database schema tells you the tables, columns, and types, but it says nothing about which datasets are canonical, which join paths are approved, how a governed metric is calculated, or what the business actually means by &amp;ldquo;revenue&amp;rdquo; or &amp;ldquo;active customer&amp;rdquo;. Ask a language model to infer all of that from raw tables and, sooner or later, it will confidently invent an answer.&lt;/p>
&lt;p>I built a proof of concept to explore a more disciplined approach: put a semantic layer between the model and the data, and let the agent reason over governed business concepts instead of guessing at raw schemas. It runs entirely locally on an open-source stack, and it happens to tie together two of my other projects along the way.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>

&lt;h2 id="why-a-semantic-layer" data-numberify>Why a Semantic Layer&lt;a class="anchor ms-1" href="#why-a-semantic-layer" aria-label="Link to this section: Why a Semantic Layer">&lt;/a>&lt;/h2>
&lt;p>A semantic layer defines models, relationships, reusable metrics, business rules, and approved query patterns independently of the language model. That separation is the whole point. When the definition of a metric lives in a governed contract rather than in a prompt, the agent cannot quietly redefine it, and the same question produces the same number every time.&lt;/p>
&lt;p>I evaluated several options (WrenAI, Vanna AI, Nao, and MetricFlow) and selected &lt;a href="https://www.getwren.ai/" target="_blank" rel="noopener noreferrer">WrenAI&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. It uses a Modeling Definition Language (MDL) to define models, relationships, views, calculated fields, and cubes as a structured, executable contract, and critically it generates SQL without executing it. That last property lets the orchestrator own authorization, validation, and controlled execution as a separate concern, which is exactly the boundary you want when adapting a system for enterprise governance.&lt;/p>

&lt;h2 id="architecture" data-numberify>Architecture&lt;a class="anchor ms-1" href="#architecture" aria-label="Link to this section: Architecture">&lt;/a>&lt;/h2>
&lt;p>The stack is fully decoupled. An AI orchestrator interprets requests and talks to the semantic engine over the Model Context Protocol (MCP). The semantic engine plans deterministic queries from its business models and vector memory, and those queries execute against the physical lakehouse.&lt;/p>
&lt;p>The diagram above traces a request end to end, and the table below maps each component to its role.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left">Component&lt;/th>
&lt;th style="text-align:left">Technology&lt;/th>
&lt;th style="text-align:left">Role&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left">Agent Orchestrator&lt;/td>
&lt;td style="text-align:left">Strands SDK&lt;/td>
&lt;td style="text-align:left">Interprets natural language, plans multi-step tool calls, coordinates the pipeline over MCP&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Semantic Engine&lt;/td>
&lt;td style="text-align:left">WrenAI&lt;/td>
&lt;td style="text-align:left">Governed semantic compiler: defines business logic as MDL, plans physical SQL, validates before execution&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Retrieval Index&lt;/td>
&lt;td style="text-align:left">LanceDB&lt;/td>
&lt;td style="text-align:left">Local vector store for RAG-based table discovery over MDL descriptions&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Agent Memory&lt;/td>
&lt;td style="text-align:left">Mem0 v3 over Valkey&lt;/td>
&lt;td style="text-align:left">Long-term user and conversational memory with hybrid retrieval&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Historical Data&lt;/td>
&lt;td style="text-align:left">Trino / Apache Iceberg&lt;/td>
&lt;td style="text-align:left">Distributed SQL over an open table format&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Object Storage&lt;/td>
&lt;td style="text-align:left">SeaweedFS&lt;/td>
&lt;td style="text-align:left">Local S3-compatible backend for Iceberg data&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The infrastructure is launched with &lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">&lt;code>odctl&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, and the sample e-commerce dataset (customers, products, orders, order items, payments, returns) is generated with &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">&lt;code>desflow&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/latest/getting-started/">documentation&lt;/a>) running a fast-forward clock and writing Parquet straight to SeaweedFS. Both projects earn their keep here: one stands up the platform, the other fills it with realistic data in seconds.&lt;/p>

&lt;h2 id="two-layers-that-keep-the-model-honest" data-numberify>Two Layers That Keep the Model Honest&lt;a class="anchor ms-1" href="#two-layers-that-keep-the-model-honest" aria-label="Link to this section: Two Layers That Keep the Model Honest">&lt;/a>&lt;/h2>
&lt;p>A major failure point for generative AI in data work is raw SQL hallucination. If you hand a model raw tables and ask it to join them on the fly, it will eventually produce a plausible but wrong metric. This system separates the problem into two layers so the model never sees a raw table.&lt;/p>
&lt;p>&lt;strong>Layer 1, the semantic compiler (WrenAI):&lt;/strong> supplies governed context (models, relationships, cubes, metrics), expands modeled SQL through MDL with &lt;code>dry_plan&lt;/code>, validates against the live backend with &lt;code>dry_run&lt;/code>, and executes through Trino with &lt;code>run_sql&lt;/code>.&lt;/p>
&lt;p>&lt;strong>Layer 2, the autonomous agent (Strands):&lt;/strong> interprets the question, calls tools such as &lt;code>get_context&lt;/code> and &lt;code>recall_queries&lt;/code>, selects a cube or constructs modeled SQL, decides which execution tool to invoke, and maintains conversational context. Strands spawns WrenAI&amp;rsquo;s native MCP server as a subprocess and discovers the semantic tools automatically, so there is no hardcoded database wiring.&lt;/p>
&lt;p>Because the agent only ever interacts with WrenAI&amp;rsquo;s governed semantic API, the set of available models and joins is constrained by design, which is what reduces the hallucination risk.&lt;/p>

&lt;h2 id="a-query-end-to-end" data-numberify>A Query, End to End&lt;a class="anchor ms-1" href="#a-query-end-to-end" aria-label="Link to this section: A Query, End to End">&lt;/a>&lt;/h2>
&lt;ol>
&lt;li>&lt;strong>Context check:&lt;/strong> the orchestrator queries Mem0 on Valkey to pull long-term preferences.&lt;/li>
&lt;li>&lt;strong>Semantic translation:&lt;/strong> the request goes to WrenAI, which uses its MDL and LanceDB memory to map intent to accurate SQL.&lt;/li>
&lt;li>&lt;strong>Validation and execution:&lt;/strong> the agent validates the physical schema against live metadata, then executes against Iceberg through Trino and returns structured data.&lt;/li>
&lt;/ol>
&lt;p>For a governed question like &amp;ldquo;what is the revenue for delivered orders?&amp;rdquo;, the agent discovers the &lt;code>daily_revenue&lt;/code> cube and maps &amp;ldquo;revenue&amp;rdquo; to &lt;code>gross_revenue&lt;/code>. It never writes the &lt;code>SUM()&lt;/code> or &lt;code>GROUP BY&lt;/code> itself, because WrenAI compiles the deterministic SQL defined in the cube.&lt;/p>

&lt;h2 id="memory-that-turns-usage-into-learning" data-numberify>Memory That Turns Usage into Learning&lt;a class="anchor ms-1" href="#memory-that-turns-usage-into-learning" aria-label="Link to this section: Memory That Turns Usage into Learning">&lt;/a>&lt;/h2>
&lt;p>Two forms of memory make the system better over time.&lt;/p>
&lt;p>WrenAI builds a local LanceDB index over schema items, seed queries, and approved SQL examples, so it can retrieve only the relevant models for a question and offer proven queries as few-shot context. Most text-to-SQL systems treat every question as the first one. Adding a retrieval layer means successful work improves future work as the knowledge base grows.&lt;/p>
&lt;p>On top of that, Mem0 gives the agent long-term memory for subjective business logic. Ask &amp;ldquo;how many high-value orders did we have yesterday?&amp;rdquo; with no memory, and the model will invent a threshold. Teach it once that high-value means a total amount greater than $1,000, and that definition is stored in Valkey and applied consistently from then on, without editing the underlying semantic model.&lt;/p>

&lt;h2 id="evaluation" data-numberify>Evaluation&lt;a class="anchor ms-1" href="#evaluation" aria-label="Link to this section: Evaluation">&lt;/a>&lt;/h2>
&lt;p>Measuring an agent means judging both its decision-making and its safety. The repository includes a golden test suite and an automated harness that spins up an isolated agent per case and grades the output with an LLM-as-a-judge, covering three things: cube discovery (does it route governed questions to governed metrics), raw table navigation (can it handle joins and aggregations when no cube fits), and hallucination prevention (does it refuse gracefully when asked for data that does not exist, such as a missing &lt;code>return_reason&lt;/code> column, rather than fabricating it).&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/">Running Kafka, Flink, Spark, Trino and Iceberg Locally with One CLI&lt;/a> - the CLI that starts the local open source stack this system runs on&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/">One Simulation, Two Pipelines: Batch Training and Live Inference with desflow&lt;/a> - generating the batch Parquet and live streaming data such a stack reads&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/">Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning&lt;/a> - the real-time side that this batch-only phase leaves for later&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-05-06-streamhouse-trio/">Meet the Streamhouse Trio - Paimon, Fluss, and Iceberg for Unified Data Architectures&lt;/a> - compares Paimon, Fluss and Iceberg as table layers for streaming and batch&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-07-17-self-service-data-platform-via-sql-gateway/">Self-service Data Platform via a Multi-tenant SQL Gateway&lt;/a> - a multi-tenant SQL gateway that starts Spark, Flink and Trino engines on demand over tables like these&lt;/li>
&lt;/ul>

&lt;h2 id="where-this-is-going" data-numberify>Where This Is Going&lt;a class="anchor ms-1" href="#where-this-is-going" aria-label="Link to this section: Where This Is Going">&lt;/a>&lt;/h2>
&lt;p>This is a foundational phase, deliberately scoped to batch data over cold storage. Real-time integration and dynamic routing between hot and cold storage are natural next steps, and the decoupled architecture is designed to absorb them.&lt;/p>
&lt;p>The interesting takeaway for me is that a reliable data assistant is less about a smarter model and more about the boundaries you draw around it. A governed semantic layer, a clear split between generation and execution, and a memory that learns do far more for trustworthiness than any single prompt.&lt;/p>
&lt;p>The full walkthrough, including infrastructure setup, semantic modeling, the orchestrator, and the evaluation harness, is on GitHub.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/agentic-analytics-system" target="_blank" rel="noopener noreferrer">jaehyeon-kim/agentic-analytics-system&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>desflow: A Declarative API with Postgres and Redis Connectors</title><link>https://jaehyeon.me/blog/2026-07-17-dynamic-des-declarative-connectors/</link><guid>https://jaehyeon.me/blog/2026-07-17-dynamic-des-declarative-connectors/</guid><pubDate>Fri, 17 Jul 2026 00:00:00 +0000</pubDate><category>Discrete Event Simulation</category><category>desflow</category><category>Digital Twin</category><category>SimPy</category><category>Apache Kafka</category><category>Redis</category><category>PostgreSQL</category><category>Python</category><description>&lt;p>Simulated data is most useful when the code that produces it is easy to read, and when it reaches the same places as production data, such as a PostgreSQL table or a Redis stream. Since I wrote about &lt;a href="https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/">Dynamic DES v0.8.1&lt;/a> and its data lake support, the project has grown in both directions: a cleaner way to write a simulation, and more places to send and receive data.&lt;/p>
&lt;p>Here is what changed on the way to &lt;strong>v0.11.1&lt;/strong>.&lt;/p></description><content:encoded>&lt;p>Simulated data is most useful when the code that produces it is easy to read, and when it reaches the same places as production data, such as a PostgreSQL table or a Redis stream. Since I wrote about &lt;a href="https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/">Dynamic DES v0.8.1&lt;/a> and its data lake support, the project has grown in both directions: a cleaner way to write a simulation, and more places to send and receive data.&lt;/p>
&lt;p>Here is what changed on the way to &lt;strong>v0.11.1&lt;/strong>.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-13&lt;/strong>] From dynamic-des 0.19.0 every example models an online shop: orders arrive, packers are the resource and packing is the service. The code and records below use those names: &lt;code>Shop&lt;/code>, &lt;code>packers&lt;/code>, &lt;code>packing&lt;/code> and &lt;code>orders&lt;/code>.&lt;/p>

&lt;h2 id="a-declarative-simulationcontext-api" data-numberify>A Declarative SimulationContext API&lt;a class="anchor ms-1" href="#a-declarative-simulationcontext-api" aria-label="Link to this section: A Declarative SimulationContext API">&lt;/a>&lt;/h2>
&lt;p>The biggest change is how you write a simulation. Earlier versions exposed a low-level, imperative API where you wired up the environment, registry, connectors, and processes by hand. That is still available and fully supported for advanced control flows, but it is now the low-level paradigm rather than the default one.&lt;/p>
&lt;p>The new &lt;strong>Standard API&lt;/strong> is built around &lt;code>SimulationContext&lt;/code>, a declarative builder that describes the whole simulation in one readable block:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">ConsoleEgress&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">LocalIngress&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1"># Schedule capacity to jump to 3 at t=10s, then drop to 2 at t=20s&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="n">app&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">sim_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;Shop&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">factor&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">random_seed&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">42&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="o">.&lt;/span>&lt;span class="n">add_resource&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;packers&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">current_cap&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">max_cap&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">5&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="o">.&lt;/span>&lt;span class="n">add_arrival&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;orders&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;exponential&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">rate&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.0&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="o">.&lt;/span>&lt;span class="n">add_service&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;packing&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">dist&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;normal&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">mean&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">3.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">std&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">0.5&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="o">.&lt;/span>&lt;span class="n">add_ingress&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">LocalIngress&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">schedule&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">[&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="p">(&lt;/span>&lt;span class="mf">10.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Shop.resources.packers.current_cap&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="p">(&lt;/span>&lt;span class="mf">20.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Shop.resources.packers.current_cap&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">2&lt;/span>&lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="o">.&lt;/span>&lt;span class="n">add_egress&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">ConsoleEgress&lt;/span>&lt;span class="p">())&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Simulation logic is then attached with decorators, so the mechanics of queuing, resource locking, duration sampling, and telemetry emission are handled for you:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="nd">@app.arrival_loop&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;orders&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">arrival_process&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">context&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">SimulationContext&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="n">task_id&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="mi">0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="k">while&lt;/span> &lt;span class="kc">True&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="k">yield&lt;/span> &lt;span class="n">context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">wait_for_arrival&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;orders&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="n">context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">spawn&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">pack_order&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">task_id&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="n">task_id&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mi">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="nd">@app.task&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">service_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;packing&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">resource_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;packers&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">pack_order&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">task_id&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">int&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;order_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">task_id&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="n">app&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">run&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">until&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">25.0&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The value here is separation of concerns. The builder declares parameters, connectors, and configuration, while the decorators express only the behavior that is unique to your model. Swapping a console for Kafka, or a fast-forward clock for a real-time one, does not touch the simulation logic at all.&lt;/p>

&lt;h2 id="postgres-and-redis-connectors" data-numberify>Postgres and Redis Connectors&lt;a class="anchor ms-1" href="#postgres-and-redis-connectors" aria-label="Link to this section: Postgres and Redis Connectors">&lt;/a>&lt;/h2>
&lt;p>desflow uses a &lt;strong>Switchboard Pattern&lt;/strong> that decouples data sourcing from simulation logic. Background threads handle heavy I/O, a central registry flattens incoming data into dot-notation paths, and SimPy resources wake up only when the registry signals a change. Because that boundary is clean, adding a new backend is mostly a connector implementation.&lt;/p>
&lt;p>Recent releases added two important ones, each with matching ingress and egress:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>PostgreSQL:&lt;/strong> stream telemetry and lifecycle events into a relational store, or drive simulation parameters from a table. Useful when your control plane or your analytics already live in Postgres.&lt;/li>
&lt;li>&lt;strong>Redis:&lt;/strong> a low-latency, in-memory option for pushing state out to dashboards or reading live parameter updates, alongside the existing Kafka path.&lt;/li>
&lt;/ul>
&lt;p>These sit next to the connectors that were already there, so a single simulation can now ingest from and emit to Kafka, Redis, or PostgreSQL, and export historical data to local disk, AWS S3, Google Cloud Storage, Azure Blob, or SeaweedFS through PyArrow&amp;rsquo;s virtual filesystem, all with strict schema drift prevention.&lt;/p>
&lt;!-- raw HTML omitted -->

&lt;h2 id="strict-data-contracts" data-numberify>Strict Data Contracts&lt;a class="anchor ms-1" href="#strict-data-contracts" aria-label="Link to this section: Strict Data Contracts">&lt;/a>&lt;/h2>
&lt;p>Regardless of the backend, outbound payloads are validated with Pydantic, so external consumers receive a predictable shape. There are two stream types. Telemetry carries scalar metrics such as utilization and queue length:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;stream_type&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;telemetry&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;path_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Shop.packers.utilization&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">85.5&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;sim_ts&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">120.5&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;timestamp&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2023-10-25T14:30:00.000Z&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Events carry discrete lifecycle transitions such as a task arriving, queuing, or finishing:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;stream_type&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;event&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;key&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;task-001&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;value&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;status&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;finished&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;duration&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">45.2&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;path_id&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Shop.service.packing&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;sim_ts&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mf">125.0&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="nt">&amp;#34;timestamp&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;2023-10-25T14:30:04.500Z&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="stability" data-numberify>Stability&lt;a class="anchor ms-1" href="#stability" aria-label="Link to this section: Stability">&lt;/a>&lt;/h2>
&lt;p>The v0.11.1 patch closed out threading race conditions in the egress providers during environment teardown. When a simulation shuts down, the background I/O threads now drain and stop cleanly instead of racing the main thread, which matters for both test suites and long-running twins.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - the Postgres connector in use: a simulated shop writes its tables, and Debezium streams every change to Kafka&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - the library generating a lakehouse dataset that an agent queries through a semantic layer&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;p>Everything above is available now:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># Core library&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">pip install desflow
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># Everything: Kafka, Redis, Postgres, dashboard, Avro, Parquet&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">pip install &lt;span class="s2">&amp;#34;desflow[all]&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The repository ships zero-setup demos for the local, Kafka, Postgres, and Redis paths in both the declarative and imperative styles.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/desflow/" target="_blank" rel="noopener noreferrer">pypi.org/project/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">jaehyeon-kim/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>Documentation:&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/latest/">jaehyeon.me/desflow&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>Running Kafka, Flink, Spark, Trino and Iceberg Locally with One CLI</title><link>https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/</link><guid>https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/</guid><pubDate>Thu, 16 Jul 2026 00:00:00 +0000</pubDate><category>Open Data Stack</category><category>odctl</category><category>Apache Kafka</category><category>Apache Flink</category><category>Apache Spark</category><category>Trino</category><category>Apache Iceberg</category><category>Python</category><description>&lt;p>Anyone who has tried to stand up a realistic data platform on their laptop knows the pain. You want Kafka talking to Flink, Spark writing to Iceberg, Trino querying the result, and maybe a catalog and a lineage tool watching over all of it. What you actually get is an afternoon lost to dependency conflicts, port clashes, and a Docker Compose file that grows a new bug every time you touch it.&lt;/p>
&lt;p>I built &lt;code>odctl&lt;/code> to make that afternoon disappear. It is a curated collection of open-source data technologies wrapped in a small CLI, and it is now available on PyPI.&lt;/p></description><content:encoded>&lt;p>Anyone who has tried to stand up a realistic data platform on their laptop knows the pain. You want Kafka talking to Flink, Spark writing to Iceberg, Trino querying the result, and maybe a catalog and a lineage tool watching over all of it. What you actually get is an afternoon lost to dependency conflicts, port clashes, and a Docker Compose file that grows a new bug every time you touch it.&lt;/p>
&lt;p>I built &lt;code>odctl&lt;/code> to make that afternoon disappear. It is a curated collection of open-source data technologies wrapped in a small CLI, and it is now available on PyPI.&lt;/p>
&lt;h2 id="what-odctl-is" data-numberify>What odctl Is&lt;a class="anchor ms-1" href="#what-odctl-is" aria-label="Link to this section: What odctl Is">&lt;/a>&lt;/h2>
&lt;p>&lt;code>odctl&lt;/code> provides a cohesive, Docker-based blueprint for experimenting with modern data architecture and MLOps locally. Instead of hand-writing Compose files, you describe what you want in terms of profiles, and the CLI resolves the dependency graph, network routing, and configuration for you.&lt;/p>
&lt;p>Install it as an isolated CLI tool:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">uv tool install odctl
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="c1"># or&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">pipx install odctl
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="bundled-technologies" data-numberify>Bundled Technologies&lt;a class="anchor ms-1" href="#bundled-technologies" aria-label="Link to this section: Bundled Technologies">&lt;/a>&lt;/h2>
&lt;p>Rather than shipping one monolithic cluster, the stack is organized into profiles you can launch independently or together:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Messaging:&lt;/strong> Kafka (KRaft), Schema Registry (Karapace), Kafka Connect&lt;/li>
&lt;li>&lt;strong>Stream Processing:&lt;/strong> Apache Flink&lt;/li>
&lt;li>&lt;strong>Data Processing:&lt;/strong> Apache Spark&lt;/li>
&lt;li>&lt;strong>Analytics:&lt;/strong> ClickHouse, Trino, Metabase&lt;/li>
&lt;li>&lt;strong>Orchestration:&lt;/strong> Apache Airflow&lt;/li>
&lt;li>&lt;strong>MLOps:&lt;/strong> MLflow, Ray Serve&lt;/li>
&lt;li>&lt;strong>Metadata:&lt;/strong> OpenMetadata&lt;/li>
&lt;li>&lt;strong>Observability:&lt;/strong> Prometheus, Alertmanager, Grafana&lt;/li>
&lt;li>&lt;strong>Lineage:&lt;/strong> OpenLineage, Marquez&lt;/li>
&lt;li>&lt;strong>Foundational storage and catalog:&lt;/strong> PostgreSQL (pgvector), SeaweedFS (S3), Iceberg REST Catalog, Valkey, Apache Fluss&lt;/li>
&lt;/ul>
&lt;p>That covers ingestion, streaming, batch, OLAP, orchestration, machine learning, governance, and observability, all from a single tool.&lt;/p>

&lt;h2 id="dependencies-resolve-themselves" data-numberify>Dependencies Resolve Themselves&lt;a class="anchor ms-1" href="#dependencies-resolve-themselves" aria-label="Link to this section: Dependencies Resolve Themselves">&lt;/a>&lt;/h2>
&lt;p>Manual Compose setups fall apart because the interesting engines depend on foundational infrastructure that you have to remember to start first. &lt;code>odctl&lt;/code> treats those dependencies as a graph and walks it for you.&lt;/p>
&lt;p>Getting a full data engineering environment running takes three steps:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># 1. Copy the default Compose files and configs into a local .odctl workspace&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">odctl init
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># 2. See what you can launch&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">odctl list
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="c1"># 3. Launch the streaming and batch engines&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">odctl up kafka-lite flink1-lite spark-lite
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You do not need to memorize what depends on what. When you ask for Kafka, Flink, and Spark, the CLI detects that they require foundational infrastructure and quietly launches PostgreSQL, SeaweedFS (S3), and the Iceberg REST Catalog before starting the compute engines.&lt;/p>

&lt;h2 id="a-small-predictable-command-surface" data-numberify>A Small, Predictable Command Surface&lt;a class="anchor ms-1" href="#a-small-predictable-command-surface" aria-label="Link to this section: A Small, Predictable Command Surface">&lt;/a>&lt;/h2>
&lt;p>Commands are grouped by intent, and every one accepts &lt;code>--help&lt;/code>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Inspect:&lt;/strong> &lt;code>odctl list&lt;/code>, &lt;code>odctl explain &amp;lt;profile&amp;gt;&lt;/code>, &lt;code>odctl ps&lt;/code>, &lt;code>odctl info&lt;/code>&lt;/li>
&lt;li>&lt;strong>Lifecycle:&lt;/strong> &lt;code>odctl pull&lt;/code>, &lt;code>odctl up&lt;/code>, &lt;code>odctl down&lt;/code>&lt;/li>
&lt;li>&lt;strong>Manage:&lt;/strong> &lt;code>odctl logs&lt;/code>, &lt;code>odctl restart&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>A few examples:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># View all profiles and exposed ports&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">odctl list -d
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># See exactly what the kafka profile provisions&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">odctl explain kafka-lite
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="c1"># Complete teardown and wipe all data&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">odctl down --all --volumes
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="hackable-by-design" data-numberify>Hackable by Design&lt;a class="anchor ms-1" href="#hackable-by-design" aria-label="Link to this section: Hackable by Design">&lt;/a>&lt;/h2>
&lt;p>&lt;code>odctl&lt;/code> does not hide the underlying configuration from you. Running &lt;code>odctl init&lt;/code> generates a local &lt;code>./.odctl/&lt;/code> workspace containing everything that powers the stack:&lt;/p>
&lt;ul>
&lt;li>&lt;code>compose-*.yml&lt;/code>: the actual Docker Compose definitions. Edit these to change exposed ports, adjust memory limits, or inject environment variables.&lt;/li>
&lt;li>&lt;code>registry.yml&lt;/code>: the internal dependency graph.&lt;/li>
&lt;li>&lt;code>.env&lt;/code>: shared environment variables such as default credentials and timezones.&lt;/li>
&lt;/ul>
&lt;p>The CLI always prioritizes the files in your local workspace, so you can tweak freely. If you break something, &lt;code>odctl init --force&lt;/code> restores the pristine defaults.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">Productionizing an Online Product Recommender using Event Driven Architecture&lt;/a> - Flink, Kafka and Valkey turn a contextual bandit recommender into an event driven service&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - a local proof of concept built on this stack, putting a semantic layer between a model and Iceberg&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2022-06-26-iceberg-etl-demo/">Data Warehousing ETL Demo with Apache Iceberg&lt;/a> - an Iceberg and PySpark ETL job run in an EMR local environment, then verified in Athena&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try It Out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try It Out">&lt;/a>&lt;/h2>
&lt;p>&lt;code>odctl&lt;/code> is on PyPI now. If you build demos, write technical content, or just want a realistic data platform to experiment with, it removes the setup tax so you can get to the interesting part.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>PyPI:&lt;/strong> &lt;a href="https://pypi.org/project/odctl/" target="_blank" rel="noopener noreferrer">pypi.org/project/odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>GitHub:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">jaehyeon-kim/odctl&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>It is licensed under Apache 2.0, and contributions are welcome.&lt;/p></content:encoded></item><item><title>One Simulation, Two Pipelines: Batch Training and Live Inference with desflow</title><link>https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/</link><guid>https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/</guid><pubDate>Mon, 25 May 2026 00:00:00 +0000</pubDate><category>Parquet</category><category>Data Lake</category><category>Apache Kafka</category><category>Discrete Event Simulation</category><category>desflow</category><category>SimPy</category><category>Python</category><description>Training a machine learning model on simulated data is straightforward until you try to deploy it. The disconnect usually happens at the pipeline level: training requires massive, historical batch data (like Parquet files in an S3 bucket), but production inference requires real-time, event-driven streams (like Kafka or Redis).
Maintaining two separate simulation codebases, one for generating training data and another for streaming live events, introduces friction, schema mismatches, and duplicated engineering effort.</description><content:encoded>&lt;p>Training a machine learning model on simulated data is straightforward until you try to deploy it. The disconnect usually happens at the pipeline level: training requires massive, historical batch data (like Parquet files in an S3 bucket), but production inference requires real-time, event-driven streams (like Kafka or Redis).&lt;/p>
&lt;p>Maintaining two separate simulation codebases, &lt;em>one for generating training data and another for streaming live events&lt;/em>, introduces friction, schema mismatches, and duplicated engineering effort.&lt;/p>
&lt;p>desflow was originally designed to solve the real-time problem by acting as a control plane for SimPy models. With the release of &lt;strong>v0.8.1&lt;/strong> (then named dynamic-des), it now natively handles high-performance historical data generation, allowing you to use the &lt;strong>exact same simulation code&lt;/strong> for both batch training and live inference.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>

&lt;h2 id="step-1-batch-training-historical-fast-forward" data-numberify>Step 1: Batch Training (Historical Fast-Forward)&lt;a class="anchor ms-1" href="#step-1-batch-training-historical-fast-forward" aria-label="Link to this section: Step 1: Batch Training (Historical Fast-Forward)">&lt;/a>&lt;/h2>
&lt;p>To generate training data efficiently, the simulation must be decoupled from the real-world clock. By setting the environment&amp;rsquo;s &lt;code>factor&lt;/code> to &lt;code>0.0&lt;/code>, the engine initiates a &amp;ldquo;fast-forward,&amp;rdquo; executing logical events as fast as the CPU allows.&lt;/p>
&lt;p>To capture this massive data throughput without exhausting memory or fragmenting storage, v0.8.1 introduces &lt;strong>&lt;code>ParquetStorageEgress&lt;/code>&lt;/strong> and &lt;strong>&lt;code>JsonlStorageEgress&lt;/code>&lt;/strong>. Built on PyArrow, these connectors handle the complexities of Data Lake ingestion natively:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Schema Drift Protection:&lt;/strong> The engine infers the schema from the initial data batch and strictly casts all subsequent records. This guarantees deterministic data types, preventing downstream ML pipelines from failing due to unexpected schema mutations.&lt;/li>
&lt;li>&lt;strong>Native Data Lake Integration:&lt;/strong> Data is automatically chunked, compressed, and written directly to local disk, AWS S3, or SeaweedFS clusters via PyArrow&amp;rsquo;s virtual filesystem.&lt;/li>
&lt;/ul>
&lt;p>You can simulate months of system operations in seconds, outputting optimized Parquet chunks ready for model training.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="VS Code terminal running the ddes-history-example command with S3 endpoint and credential environment variables set" src="https://jaehyeon.me/blog/2026-05-25-dynamic-des-parquet-support/parquet-example.webp" loading="lazy" title="Data Generation Example" width="2062" height="1928" />
&lt;/picture>&lt;figcaption class="figure-caption">Data Generation Example&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="step-2-live-inference-production-streaming" data-numberify>Step 2: Live Inference (Production Streaming)&lt;a class="anchor ms-1" href="#step-2-live-inference-production-streaming" aria-label="Link to this section: Step 2: Live Inference (Production Streaming)">&lt;/a>&lt;/h2>
&lt;p>Once the model is trained on the historical Parquet data, transitioning to production testing requires zero changes to your core simulation logic.&lt;/p>
&lt;p>By changing the environment&amp;rsquo;s &lt;code>factor&lt;/code> from &lt;code>0.0&lt;/code> to &lt;code>1.0&lt;/code>, logical time synchronizes with wall-clock time. You simply swap the &lt;code>ParquetStorageEgress&lt;/code> for a &lt;code>KafkaEgress&lt;/code>. The simulation now runs in real-time, streaming the exact same schema to your message broker, providing a live data feed for your deployed ML model to consume.&lt;/p>
&lt;p>This provides an end-to-end data engineering toolkit for simulation-based Machine Learning, from historical batch generation to live streaming pipelines.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/">Building an Event-Driven Hybrid Digital Twin with desflow&lt;/a> - the earlier release that turns a static SimPy model into a synchronized forecasting engine&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-17-dynamic-des-declarative-connectors/">desflow: A Declarative API with Postgres and Redis Connectors&lt;/a> - the later release, which adds a declarative API and native Postgres and Redis connectors&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/">Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning&lt;/a> - the Flink pipeline that learns online from the kind of stream this simulation feeds&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - the fast-forward clock used to fill an Iceberg lakehouse with Parquet data that an agent queries&lt;/li>
&lt;/ul>

&lt;h2 id="try-it-out" data-numberify>Try it out&lt;a class="anchor ms-1" href="#try-it-out" aria-label="Link to this section: Try it out">&lt;/a>&lt;/h2>
&lt;p>The v0.8.1 release and the new storage connectors are available now. You can view the source code and run the historical data generation example below.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>GitHub Repository:&lt;/strong> &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">jaehyeon-kim/desflow&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/li>
&lt;li>&lt;strong>Documentation &amp;amp; Examples:&lt;/strong> &lt;a href="https://jaehyeon.me/desflow/latest/guides/backfill-then-live/">Backfill Then Go Live guide&lt;/a>&lt;/li>
&lt;/ul></content:encoded></item><item><title>Current London 2026: Building End-to-End Data Lineage</title><link>https://jaehyeon.me/blog/2026-05-22-end-to-end-data-lineage/</link><guid>https://jaehyeon.me/blog/2026-05-22-end-to-end-data-lineage/</guid><pubDate>Fri, 22 May 2026 00:00:00 +0000</pubDate><category>Apache Kafka</category><category>Apache Flink</category><category>Apache Spark</category><category>OpenLineage</category><category>Data Lineage</category><description>When a number in a report looks wrong, the first question is where it came from, and the next is what else a change will break. Answering both needs data lineage: a record of which jobs read and wrote which datasets, across every system the data passes through. At Current London 2026 I presented a session on this, Building End-to-End Data Lineage with Kafka, Flink, and Spark, which captures that metadata across parallel pipelines using OpenLineage.</description><content:encoded>&lt;p>When a number in a report looks wrong, the first question is where it came from, and the next is what else a change will break. Answering both needs data lineage: a record of which jobs read and wrote which datasets, across every system the data passes through. At Current London 2026 I presented a session on this, &lt;a href="https://current.confluent.io/london/sessions#session-SESS-70" target="_blank" rel="noopener noreferrer">&lt;strong>Building End-to-End Data Lineage with Kafka, Flink, and Spark&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, which captures that metadata across parallel pipelines using &lt;a href="https://openlineage.io/" target="_blank" rel="noopener noreferrer">OpenLineage&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>

&lt;h2 id="presentation-slides" data-numberify>Presentation Slides&lt;a class="anchor ms-1" href="#presentation-slides" aria-label="Link to this section: Presentation Slides">&lt;/a>&lt;/h2>
&lt;p>Below are the slides from my session, rendered natively here on the blog.&lt;/p>
&lt;div class="ratio ratio-16x9 my-4 shadow-sm rounded overflow-hidden">
 &lt;iframe 
 src="https://jaehyeon.me/slides/2026-05-22-end-to-end-data-lineage/" 
 title="Presentation" 
 allowfullscreen="true" 
 style="border: 0;">
 &lt;/iframe>
&lt;/div>
&lt;div class="text-center mb-4">
 &lt;a href="https://jaehyeon.me/slides/2026-05-22-end-to-end-data-lineage/" target="_blank" class="btn btn-sm btn-outline-primary">
 &lt;i class="fas fa-external-link-alt">&lt;/i> View Full Screen
 &lt;/a>
&lt;/div>
&lt;p>&lt;em>You can use your arrow keys to navigate the slides above, or click the &amp;ldquo;View Full Screen&amp;rdquo; button for the complete experience.&lt;/em>&lt;/p>

&lt;h2 id="session-breakdown-tracking-the-data-lifecycle" data-numberify>Session Breakdown: Tracking the Data Lifecycle&lt;a class="anchor ms-1" href="#session-breakdown-tracking-the-data-lifecycle" aria-label="Link to this section: Session Breakdown: Tracking the Data Lifecycle">&lt;/a>&lt;/h2>
&lt;p>The presentation focused on tracking data from a single Kafka topic as it distributed across concurrent architectural paths. To demonstrate the lineage graph, we analyzed a standard production stack.&lt;/p>

&lt;h3 id="1-real-time--archival-fan-out" data-numberify>1. Real-Time &amp; Archival Fan-Out&lt;a class="anchor ms-1" href="#1-real-time--archival-fan-out" aria-label="Link to this section: 1. Real-Time &amp;amp; Archival Fan-Out">&lt;/a>&lt;/h3>
&lt;p>The architecture begins with a primary Kafka topic. From there, the data splits into distinct operational domains:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Archival:&lt;/strong> A Kafka S3 Sink Connector handling raw data offloading to object storage.&lt;/li>
&lt;li>&lt;strong>Live Analytics:&lt;/strong> A real-time Flink DataStream job consuming events for stateful processing.&lt;/li>
&lt;li>&lt;strong>Data Lakehouse:&lt;/strong> A Flink Table API job ingesting the continuous stream into an Apache Iceberg table for query engines.&lt;/li>
&lt;/ul>

&lt;h3 id="2-completing-the-picture-with-spark" data-numberify>2. Completing the Picture with Spark&lt;a class="anchor ms-1" href="#2-completing-the-picture-with-spark" aria-label="Link to this section: 2. Completing the Picture with Spark">&lt;/a>&lt;/h3>
&lt;p>To demonstrate a complete end-to-end lifecycle, the session traced the lineage as a batch Apache Spark job consumed from the populated Iceberg table to generate downstream aggregations.&lt;/p>

&lt;h3 id="3-instrumenting-with-openlineage" data-numberify>3. Instrumenting with OpenLineage&lt;a class="anchor ms-1" href="#3-instrumenting-with-openlineage" aria-label="Link to this section: 3. Instrumenting with OpenLineage">&lt;/a>&lt;/h3>
&lt;p>Visualizing this multi-path journey, including column-level details, was achieved using &lt;strong>Marquez&lt;/strong> as the lineage repository and visualization layer. The metadata extraction was handled through &lt;strong>OpenLineage&lt;/strong>. Integrating these systems to output a unified lineage graph requires specific strategies:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Kafka Connect:&lt;/strong> Lineage is established at the connector level using a custom Single Message Transform (SMT) to capture operational state without altering the payload.&lt;/li>
&lt;li>&lt;strong>Apache Flink:&lt;/strong> Two distinct patterns were evaluated: a low-overhead listener-based approach, and a manual orchestration method necessary for capturing application cancellations.&lt;/li>
&lt;li>&lt;strong>Apache Spark:&lt;/strong> Spark&amp;rsquo;s &lt;code>extraListeners&lt;/code> were configured to auto-detect inputs and outputs, linking the batch jobs to upstream Flink outputs via aligned physical namespaces.&lt;/li>
&lt;/ul>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2023-12-07-flink-spark-local-dev/">Setup Local Development Environment for Apache Flink and Spark Using EMR Container Images&lt;/a> - a local Flink and Spark environment of the kind this lineage work instruments.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-07-17-self-service-data-platform-via-sql-gateway/">Self-service Data Platform via a Multi-tenant SQL Gateway&lt;/a> - Apache Kyuubi giving on-demand Spark, Flink and Trino engines with central governance.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-16-odctl-open-data-stack/">Running Kafka, Flink, Spark, Trino and Iceberg Locally with One CLI&lt;/a> - a CLI that starts Kafka, Flink, Spark, Trino, Iceberg and observability tooling as one local stack.&lt;/li>
&lt;/ul>

&lt;h2 id="moving-forward" data-numberify>Moving Forward&lt;a class="anchor ms-1" href="#moving-forward" aria-label="Link to this section: Moving Forward">&lt;/a>&lt;/h2>
&lt;p>By instrumenting event streams, streaming compute, and batch processing engines with a unified standard like OpenLineage, organizations can establish observable and reliable data architectures.&lt;/p>
&lt;p>Thank you to everyone who attended the session at Current London. For those unable to attend, the slides are provided above for reference.&lt;/p></content:encoded></item><item><title>Building an Event-Driven Hybrid Digital Twin with desflow</title><link>https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/</link><guid>https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/</guid><pubDate>Wed, 29 Apr 2026 00:00:00 +0000</pubDate><category>Digital Twin</category><category>Discrete Event Simulation</category><category>IoT</category><category>Apache Kafka</category><category>desflow</category><category>SimPy</category><category>Python</category><description>Asynchronous Gap A true Hybrid Digital Twin does more than just mirror reality. It actively forecasts the future by running a simulation against live operational states. We established this in Part 1.
If you have ever tried to build one of these systems from scratch, you immediately hit a fundamental architectural clash.
Standard simulation clocks (like those in traditional SimPy implementations) are logically synchronous and not designed to handle high-frequency asynchronous I/O without explicit decoupling.</description><content:encoded>
&lt;h2 id="asynchronous-gap" data-numberify>Asynchronous Gap&lt;a class="anchor ms-1" href="#asynchronous-gap" aria-label="Link to this section: Asynchronous Gap">&lt;/a>&lt;/h2>
&lt;p>A true Hybrid Digital Twin does more than just mirror reality. It actively forecasts the future by running a simulation against live operational states. We established this in &lt;a href="https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/">Part 1&lt;/a>.&lt;/p>
&lt;p>If you have ever tried to build one of these systems from scratch, you immediately hit a fundamental architectural clash.&lt;/p>
&lt;p>Standard simulation clocks (like those in traditional SimPy implementations) are logically synchronous and not designed to handle high-frequency asynchronous I/O without explicit decoupling. They step through logical time deterministically. Real-world telemetry (like a Kafka stream or a Redis Pub/Sub channel) is asynchronous and non-deterministic in arrival timing and ordering at the system level. If you naively wire a live data feed directly into a standard simulation loop, you will stall the simulation loop while awaiting network I/O. The simulation drifts from real-world time, making your live twin useless.&lt;/p>
&lt;p>To solve this, the compute layer must be explicitly decoupled from the network layer. This is the exact design problem the open-source &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">&lt;strong>&lt;code>desflow&lt;/code>&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/desflow/latest/architecture/environment/">documentation&lt;/a>) package addresses.&lt;/p>

&lt;h2 id="core-architecture-switchboard-pattern" data-numberify>Core Architecture: Switchboard Pattern&lt;a class="anchor ms-1" href="#core-architecture-switchboard-pattern" aria-label="Link to this section: Core Architecture: Switchboard Pattern">&lt;/a>&lt;/h2>
&lt;p>&lt;code>desflow&lt;/code> operates as a real-time control plane wrapped around a discrete-event simulation core. It bridges the asynchronous gap using the Switchboard Pattern, which isolates the network from the math:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Connectors (I/O Layer):&lt;/strong> Background async I/O workers handle heavy network traffic (Kafka, Redis, Postgres) entirely independently of the simulation clock.&lt;/li>
&lt;li>&lt;strong>Simulation Registry (Switchboard):&lt;/strong> This is the central state manager. It intercepts incoming network data, safely casts types, and flattens complex system schemas into addressable paths that act as reactive state bindings, allowing any component in the simulation to subscribe to and react to state transitions.&lt;/li>
&lt;li>&lt;strong>Simulation Objects:&lt;/strong> The actual mathematical models and resources that consume these state transitions.&lt;/li>
&lt;/ol>
&lt;p>Because the background I/O workers and the simulation clock communicate through thread-safe queues, network latency does not block your forecasting logic. However, decoupling is not a magic bullet. If ingestion rates exceed processing rates, drift can still occur, which is why system observability is critical.&lt;/p>

&lt;h2 id="physics-of-change-mutable-resources" data-numberify>Physics of Change: Mutable Resources&lt;a class="anchor ms-1" href="#physics-of-change-mutable-resources" aria-label="Link to this section: Physics of Change: Mutable Resources">&lt;/a>&lt;/h2>
&lt;p>With the data flowing safely, the simulation objects themselves must be able to react. In a traditional DES model, resources are static. If you initialize &lt;code>simpy.Resource(capacity=5)&lt;/code>, it stays 5 forever. In the real world, machines break, operators go on break, and routing logic shifts dynamically.&lt;/p>
&lt;p>Instead of polling the network, &lt;code>desflow&lt;/code> introduces mutable resources (&lt;code>DynamicResource&lt;/code>, &lt;code>DynamicContainer&lt;/code>, &lt;code>DynamicStore&lt;/code>) that adapt safely:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Growing is easy:&lt;/strong> If capacity increases from 5 to 10, new tokens are immediately added to the pool, instantly unblocking pending requests.&lt;/li>
&lt;li>&lt;strong>Shrinking is complex:&lt;/strong> If capacity drops from 5 to 2 while 4 tasks are actively being processed, what happens? Work-In-Progress (WIP) is never destroyed. The resource enters a temporary over-capacity state. It waits for the active tasks to naturally finish, intercepting their returned tokens until the physical pool matches the new, smaller capacity limit.&lt;/li>
&lt;/ul>

&lt;h2 id="system-observability" data-numberify>System Observability&lt;a class="anchor ms-1" href="#system-observability" aria-label="Link to this section: System Observability">&lt;/a>&lt;/h2>
&lt;p>When running a real-time simulation, your biggest operational risk is simulation drift. If your compute layer cannot process the incoming event stream fast enough, the simulation clock will fall behind relative to event time or wall-clock time.&lt;/p>
&lt;p>To solve this, &lt;code>desflow&lt;/code> includes built-in observability. The egress layer automatically calculates this drift and continuously streams a &lt;code>system.simulation.lag_seconds&lt;/code> telemetry metric. This makes it easy to set up Grafana dashboards and trigger alerts if the forecasting engine begins lagging behind reality.&lt;/p>

&lt;h2 id="designing-for-failure-modes" data-numberify>Designing for Failure Modes&lt;a class="anchor ms-1" href="#designing-for-failure-modes" aria-label="Link to this section: Designing for Failure Modes">&lt;/a>&lt;/h2>
&lt;p>Moving from a static offline script to a live, event-driven twin introduces distributed systems problems. A production-grade deployment must account for specific failure modes:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Backlog Growth:&lt;/strong> Queues prevent the network from blocking the simulation loop, but if your computational logic is too heavy, the event backlog will grow and cause severe simulation drift.&lt;/li>
&lt;li>&lt;strong>Event Ordering:&lt;/strong> In distributed systems, events can arrive out of order. You must implement proper Kafka partitioning strategies to ensure causal events are processed sequentially within a partition; cross-partition ordering must be handled at the application level.&lt;/li>
&lt;li>&lt;strong>State Recovery:&lt;/strong> If a simulation node crashes, it must be able to restart, rebuild its state tree from the Registry or by replaying the event log, and resume processing from the correct Kafka offsets without losing WIP logic.&lt;/li>
&lt;/ul>

&lt;h2 id="developer-journey-from-laptop-to-cluster" data-numberify>Developer Journey: From Laptop to Cluster&lt;a class="anchor ms-1" href="#developer-journey-from-laptop-to-cluster" aria-label="Link to this section: Developer Journey: From Laptop to Cluster">&lt;/a>&lt;/h2>
&lt;p>A good digital twin framework must be portable. You should be able to validate your business logic locally before introducing the complexity of enterprise message brokers. &lt;code>desflow&lt;/code> achieves this by making the I/O layer entirely pluggable.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-16&lt;/strong>] dynamic-des is now desflow (0.20.0). The code and commands below use the new name: pip install desflow, import desflow and the desflow command.&lt;/p>
&lt;p>[&lt;strong>Update 2026-10-13&lt;/strong>] From dynamic-des 0.19.0 every example models an online shop: orders arrive, packers are the resource and packing is the service. The code below uses those names, so the resource is &lt;code>packers&lt;/code> in the &lt;code>Shop&lt;/code> simulation.&lt;/p>

&lt;h3 id="phase-1-local-deterministic-testing" data-numberify>Phase 1: Local Deterministic Testing&lt;a class="anchor ms-1" href="#phase-1-local-deterministic-testing" aria-label="Link to this section: Phase 1: Local Deterministic Testing">&lt;/a>&lt;/h3>
&lt;p>Before touching Kafka, you can use &lt;code>LocalIngress&lt;/code> to schedule mutations deterministically. This allows you to unit-test exactly how your factory floor reacts to a breakdown at a specific timestamp.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl"> &lt;span class="n">DynamicRealtimeEnvironment&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">DynamicResource&lt;/span>&lt;span class="p">,&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="n">LocalIngress&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">ConsoleEgress&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">SimParameter&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">CapacityConfig&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="c1"># 1. Define the initial state&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="n">params&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">SimParameter&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="n">sim_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;Shop&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="n">resources&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>&lt;span class="s2">&amp;#34;packers&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">CapacityConfig&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">current_cap&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">max_cap&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">5&lt;/span>&lt;span class="p">)},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="c1"># 2. Schedule a deterministic capacity jump (1 -&amp;gt; 3) at t=5.0s&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="n">ingress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">LocalIngress&lt;/span>&lt;span class="p">([&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="p">(&lt;/span>&lt;span class="mf">5.0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Shop.resources.packers.current_cap&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mi">3&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="n">egress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ConsoleEgress&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="c1"># 3. Initialize Environment &amp;amp; Registry&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">DynamicRealtimeEnvironment&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">factor&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mf">1.0&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">registry&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">register_sim_parameter&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">params&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">setup_ingress&lt;/span>&lt;span class="p">([&lt;/span>&lt;span class="n">ingress&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">setup_egress&lt;/span>&lt;span class="p">([&lt;/span>&lt;span class="n">egress&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl">&lt;span class="n">res&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">DynamicResource&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;Shop&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;packers&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">run&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">until&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">10&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Notice the &lt;code>factor&lt;/code> parameter in the environment setup. The environment can run in real-time or accelerated logical time, depending on the configured factor. A factor of 1.0 syncs the simulation strictly to the wall-clock, while lower factors accelerate it for rapid scenario testing.&lt;/p>

&lt;h3 id="phase-2-live-kafka-integration" data-numberify>Phase 2: Live Kafka Integration&lt;a class="anchor ms-1" href="#phase-2-live-kafka-integration" aria-label="Link to this section: Phase 2: Live Kafka Integration">&lt;/a>&lt;/h3>
&lt;p>Once your logic is sound, transitioning to an operational digital twin requires changing exactly two lines of code. You swap the local connectors for Kafka connectors.&lt;/p>
&lt;p>While this example is kept clean for readability, real production deployments will naturally require additional configurations for retries, authentication, and backpressure handling.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">KafkaIngress&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">KafkaEgress&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="c1"># Swap connectors: no changes needed to business logic&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="n">ingress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">KafkaIngress&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="n">topic&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;shop-config&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="n">bootstrap_servers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;broker.internal:9092&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="n">egress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">KafkaEgress&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">telemetry_topic&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;sim-vitals&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">event_topic&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;sim-events&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="n">bootstrap_servers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;broker.internal:9092&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">setup_ingress&lt;/span>&lt;span class="p">([&lt;/span>&lt;span class="n">ingress&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">setup_egress&lt;/span>&lt;span class="p">([&lt;/span>&lt;span class="n">egress&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">run&lt;/span>&lt;span class="p">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="evolution-into-a-state-machine-engine" data-numberify>Evolution into a State-Machine Engine&lt;a class="anchor ms-1" href="#evolution-into-a-state-machine-engine" aria-label="Link to this section: Evolution into a State-Machine Engine">&lt;/a>&lt;/h2>
&lt;p>Traditional Discrete-Event Simulation is strictly quantitative, focusing mostly on queue lengths, processing times, and resource capacities. However, &lt;code>desflow&lt;/code> has evolved beyond this to blend DES with reactive programming, behaving much like a state-machine engine.&lt;/p>
&lt;p>By utilizing the &lt;code>variables&lt;/code> registry and the &lt;code>wait_for_change()&lt;/code> observer pattern, developers can track arbitrary logical states. Instead of just yielding on time delays, simulation processes can yield on external network events. A Kafka payload no longer just updates a number. It acts as a state-transition trigger that dictates the logical flow of the entire application.&lt;/p>
&lt;p>This flexibility means digital twins can incorporate sophisticated modeling well beyond tracking discrete items in a queue. You can map real-time telemetry directly to custom mathematical logic. For a practical example, you can view the &lt;a href="https://github.com/jaehyeon-kim/oml-digital-twin-hotrolling" target="_blank" rel="noopener noreferrer">&lt;strong>Hot Strip Mill: Real-Time Online Machine Learning &amp;amp; Digital Twin&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> project, which demonstrates how to build a real-time tracking dashboard for steel manufacturing using advanced physical simulations.&lt;/p>

&lt;h3 id="-domain-agnostic-applications" data-numberify>💡 Domain-Agnostic Applications&lt;a class="anchor ms-1" href="#-domain-agnostic-applications" aria-label="Link to this section: 💡 Domain-Agnostic Applications">&lt;/a>&lt;/h3>
&lt;p>Because it behaves as a reactive state engine, &lt;code>desflow&lt;/code> is not restricted to factory floors. I am currently building out additional demo projects to highlight its versatility across entirely different domains:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Continuous-Time Crypto Trading Bot:&lt;/strong> This demo proves the package can handle high-velocity financial simulations. It utilizes the variables registry to track live BTC prices and portfolio balances, while using dynamic resources to model exchange API rate limits. Concurrent processes simulate the market maker (random walk price updates), strategy monitor (moving average crossovers), and order execution (yielding realistic network latency delays).&lt;/li>
&lt;li>&lt;strong>RPG Adventure Game State Machine:&lt;/strong> This demo highlights the stochastic and time-delay strengths of the engine by modeling complex entity interactions over continuous time. Primitive game states (player HP, enemy HP, current room) live in the variables registry. Continuous processes handle room exploration with travel time delays and active combat loops. The combat process yields attack cooldown timeouts and uses the global &lt;code>Sampler&lt;/code> for randomized damage, tracking causality back to the root encounter event.&lt;/li>
&lt;/ul>

&lt;h2 id="enforcing-data-contracts-and-routing" data-numberify>Enforcing Data Contracts and Routing&lt;a class="anchor ms-1" href="#enforcing-data-contracts-and-routing" aria-label="Link to this section: Enforcing Data Contracts and Routing">&lt;/a>&lt;/h2>
&lt;p>A hybrid twin is only as useful as the data it outputs. To ensure downstream analytics systems do not break, &lt;code>desflow&lt;/code> natively enforces strict data contracts and provides granular control over serialization.&lt;/p>

&lt;h3 id="type-safety-and-pydantic-integration" data-numberify>Type Safety and Pydantic Integration&lt;a class="anchor ms-1" href="#type-safety-and-pydantic-integration" aria-label="Link to this section: Type Safety and Pydantic Integration">&lt;/a>&lt;/h3>
&lt;p>Data hygiene works in both directions. On the ingress side, the framework performs dynamic type casting to prevent malformed sensor data from crashing your active simulation.&lt;/p>
&lt;p>On the egress side, you can yield strongly typed Pydantic models directly from your simulation logic. You do not need to manually convert objects to dictionaries. The egress layer automatically detects, extracts, and serializes your models:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">pydantic&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">BaseModel&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">DynamicRealtimeEnvironment&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="k">class&lt;/span> &lt;span class="nc">TaskEvent&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">BaseModel&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> &lt;span class="n">path_id&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl"> &lt;span class="n">status&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">work_task&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">env&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">DynamicRealtimeEnvironment&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">task_id&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">int&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Pass the Pydantic model directly to the framework&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">event&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">TaskEvent&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">path_id&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;Shop.packers&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">status&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;queued&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl"> &lt;span class="n">env&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">publish_event&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="sa">f&lt;/span>&lt;span class="s2">&amp;#34;task-&lt;/span>&lt;span class="si">{&lt;/span>&lt;span class="n">task_id&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">event&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="pluggable-routing-and-avro-schemas" data-numberify>Pluggable Routing and Avro Schemas&lt;a class="anchor ms-1" href="#pluggable-routing-and-avro-schemas" aria-label="Link to this section: Pluggable Routing and Avro Schemas">&lt;/a>&lt;/h3>
&lt;p>In an enterprise architecture, you rarely dump all events into a single Kafka topic. You might want standard telemetry routed as lightweight JSON, while high-velocity machine learning payloads are serialized as binary Avro.&lt;/p>
&lt;p>&lt;code>desflow&lt;/code> handles this through pluggable topic routers and serializers. You can map specific topics to a &lt;code>ConfluentAvroSerializer&lt;/code> or a &lt;code>GlueAvroSerializer&lt;/code>, and any unmapped topics will safely fall back to default JSON.&lt;/p>
&lt;p>Because hardcoding Avro schemas is prone to drift, the framework natively supports &lt;code>dataclasses-avroschema&lt;/code>. This allows you to auto-generate your schema directly from your Pydantic model and route it seamlessly:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">pydantic&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">Field&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">dataclasses_avroschema.pydantic&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">AvroBaseModel&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">KafkaEgress&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">desflow.connectors.egress.kafka&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">ConfluentAvroSerializer&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="c1"># 1. Define your model and auto-generate the schema&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="k">class&lt;/span> &lt;span class="nc">MLPrediction&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">AvroBaseModel&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;High-velocity ML payload&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl"> &lt;span class="n">task_id&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">str&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">Field&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">...&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl"> &lt;span class="n">confidence&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">float&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">Field&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">...&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl"> &lt;span class="k">class&lt;/span> &lt;span class="nc">Meta&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="n">namespace&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;com.desflow.ml&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl"> &lt;span class="n">schema_name&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;PredictionEvent&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="c1"># 2. Define a topic router to separate standard events from ML events&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">ml_topic_router&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">data&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">dict&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="nb">str&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">data&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;value&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{})&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;event_type&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;prediction&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="s2">&amp;#34;ml-predictions&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="s2">&amp;#34;sim-lifecycle&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">&lt;span class="c1"># 3. Configure the Egress Connector&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">&lt;span class="n">egress&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">KafkaEgress&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl"> &lt;span class="n">bootstrap_servers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;localhost:9092&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl"> &lt;span class="n">topic_router&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">ml_topic_router&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl"> &lt;span class="n">topic_serializers&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Map the auto-generated Avro serializer to the ML topic&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;ml-predictions&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">ConfluentAvroSerializer&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl"> &lt;span class="n">registry_url&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;http://localhost:8081&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl"> &lt;span class="n">schema_str&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">MLPrediction&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">avro_schema&lt;/span>&lt;span class="p">()&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl"> &lt;span class="p">),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl"> &lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">33&lt;/span>&lt;span class="cl">&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="moving-to-production" data-numberify>Moving to Production&lt;a class="anchor ms-1" href="#moving-to-production" aria-label="Link to this section: Moving to Production">&lt;/a>&lt;/h2>
&lt;p>By decoupling network I/O from the simulation clock, enforcing strict data schemas, and blending DES with reactive programming, &lt;code>desflow&lt;/code> provides a clean architectural pattern for modern event-driven systems.&lt;/p>
&lt;p>This enables a new class of systems beyond static models and passive dashboards. You can build resilient, synchronized forecasting engines that adapt to reality the moment it changes.&lt;/p>
&lt;p>To explore the source code, view the full API documentation, or test out the zero-setup Docker examples, check out the &lt;a href="https://github.com/jaehyeon-kim/desflow" target="_blank" rel="noopener noreferrer">&lt;strong>&lt;code>desflow&lt;/code> repository on GitHub&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - the library running an online shop on PostgreSQL, with Debezium streaming every change&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/">Keeping Game Leaderboards Up to Date in Real Time with Kafka and Flink SQL&lt;/a> - the library playing a mobile game, with Flink SQL keeping leaderboards up to date&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - the library generating Parquet data for an Iceberg lakehouse that an agent queries&lt;/li>
&lt;/ul></content:encoded></item><item><title>Why Digital Twins Are Rewiring Industry 4.0</title><link>https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/</link><guid>https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/</guid><pubDate>Wed, 22 Apr 2026 00:00:00 +0000</pubDate><category>Digital Twin</category><category>Discrete Event Simulation</category><category>IoT</category><category>desflow</category><category>SimPy</category><category>Python</category><description>A simulation and a digital twin are often treated as the same thing, but they are built differently. A traditional simulation runs offline on assumed inputs. An operational digital twin stays aligned with one real system as it changes. This post walks through the layers that separate the two, and an event-driven hybrid that combines them.
Beyond CAD Models There is a project by Dassault Systèmes called the Living Heart that illustrates the trajectory of this technology.</description><content:encoded>&lt;p>A simulation and a digital twin are often treated as the same thing, but they are built differently. A traditional simulation runs offline on assumed inputs. An operational digital twin stays aligned with one real system as it changes. This post walks through the layers that separate the two, and an event-driven hybrid that combines them.&lt;/p>

&lt;h2 id="beyond-cad-models" data-numberify>Beyond CAD Models&lt;a class="anchor ms-1" href="#beyond-cad-models" aria-label="Link to this section: Beyond CAD Models">&lt;/a>&lt;/h2>
&lt;p>There is a project by Dassault Systèmes called the &lt;a href="https://www.3ds.com/3dexperiencelab/portfolio/living-heart" target="_blank" rel="noopener noreferrer">&lt;strong>Living Heart&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> that illustrates the trajectory of this technology. Instead of relying on standard 2D scans, surgeons can pull up a high-fidelity 3D model of a patient&amp;rsquo;s heart that simulates blood flow, mechanics, and electricity based on imaging-derived reconstructions and population-based physiological calibration.&lt;/p>
&lt;p>While the Living Heart is closer to a personalized, highly-parameterized simulation than a continuously streaming IoT system, it highlights the core philosophy of a modern digital twin: moving past static CAD files to create models that are fundamentally aligned with a specific, real-world physical instance.&lt;/p>

&lt;h2 id="industry-applications" data-numberify>Industry Applications&lt;a class="anchor ms-1" href="#industry-applications" aria-label="Link to this section: Industry Applications">&lt;/a>&lt;/h2>
&lt;p>While manufacturing initially drove this technology, digital twins, &lt;em>systems that periodically or continuously align a digital model with physical reality&lt;/em>, exist on a wide spectrum. Some are event-driven, some are periodically synchronized, and others are simulation-first with occasional calibration.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Smart Manufacturing:&lt;/strong> On automated factory floors, robotic arms stream telemetry to virtual models. This allows plant managers to track maintenance needs and test routing scenarios based on the current state of the factory.&lt;/li>
&lt;li>&lt;strong>Supply Chain and Logistics:&lt;/strong> Global supply chains use digital twins to track everything from warehouse inventory levels to fleet movements, achieving end-to-end visibility.&lt;/li>
&lt;li>&lt;strong>Cybersecurity and Infrastructure:&lt;/strong> Network digital twins are increasingly used to replicate network topology and traffic models. Security teams use these digital replicas as testbeds to safely test rate-limiting and AI behavioral rules against simulated DDoS attacks without risking live infrastructure.&lt;/li>
&lt;li>&lt;strong>Next-Generation Telecommunications:&lt;/strong> Telecom operators build digital twins of their networks to manage dynamic bandwidth allocation and network slicing, analyzing traffic spikes across thousands of nodes before they cause outages.&lt;/li>
&lt;/ul>

&lt;h2 id="reality-gap-simulations-vs-digital-twins" data-numberify>Reality Gap: Simulations vs. Digital Twins&lt;a class="anchor ms-1" href="#reality-gap-simulations-vs-digital-twins" aria-label="Link to this section: Reality Gap: Simulations vs. Digital Twins">&lt;/a>&lt;/h2>
&lt;p>The tech industry frequently uses the terms &amp;ldquo;simulation&amp;rdquo; and &amp;ldquo;digital twin&amp;rdquo; interchangeably. Clarifying how these systems handle state and time is critical for evaluating modern control architectures. To understand this practically, let&amp;rsquo;s look at a factory floor where &amp;ldquo;Machine B&amp;rdquo; suddenly breaks down.&lt;/p>
&lt;p>Here is how different architectural patterns handle that exact event:&lt;/p>

&lt;h3 id="1-traditional-simulation-isolated-execution" data-numberify>1. Traditional Simulation (Isolated Execution)&lt;a class="anchor ms-1" href="#1-traditional-simulation-isolated-execution" aria-label="Link to this section: 1. Traditional Simulation (Isolated Execution)">&lt;/a>&lt;/h3>
&lt;p>A &lt;strong>simulation&lt;/strong> is a predictive model designed to explore &amp;ldquo;what if&amp;rdquo; scenarios.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Scenario:&lt;/strong> On Monday morning, an engineer runs a Discrete-Event Simulation (DES) of the factory. On Tuesday, Machine B physically breaks.&lt;/li>
&lt;li>&lt;strong>Limitation:&lt;/strong> A traditional simulation run is an isolated execution model. Unless the system is built to re-seed its parameters from a database and trigger a new batch run, the ongoing execution remains unaware of the real-world breakdown. It will incorrectly continue assuming Machine B is running at full capacity.&lt;/li>
&lt;/ul>

&lt;h3 id="2-operational-digital-twin-state-synchronization" data-numberify>2. Operational Digital Twin (State Synchronization)&lt;a class="anchor ms-1" href="#2-operational-digital-twin-state-synchronization" aria-label="Link to this section: 2. Operational Digital Twin (State Synchronization)">&lt;/a>&lt;/h3>
&lt;p>An &lt;strong>operational digital twin&lt;/strong> extends a model by aligning it with reality using time-series databases and IoT connectivity.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Scenario:&lt;/strong> The moment Machine B breaks, sensors ping the database. The twin immediately updates the machine&amp;rsquo;s live status to &amp;ldquo;Offline.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Limitation:&lt;/strong> In practice, a digital twin is usually a stack of overlapping layers. The core synchronization layer provides &lt;em>state fidelity&lt;/em>, while separate analytics layers handle forecasting. However, if a system only utilizes the synchronization layer as a dashboard without integrating predictive models, it often relies on separate analytics modules, external simulation tools, or human operators to forecast future cascading consequences.&lt;/li>
&lt;/ul>

&lt;h3 id="3-event-driven-hybrid-execution-dynamic-adaptation" data-numberify>3. Event-Driven Hybrid Execution (Dynamic Adaptation)&lt;a class="anchor ms-1" href="#3-event-driven-hybrid-execution-dynamic-adaptation" aria-label="Link to this section: 3. Event-Driven Hybrid Execution (Dynamic Adaptation)">&lt;/a>&lt;/h3>
&lt;p>Advanced architectures blend these layers into unified ecosystems, feeding real-world states directly into predictive engines. While many systems accomplish this through windowed re-execution or micro-batch propagation, another opinionated architectural pattern is &lt;strong>event-driven mutation&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Scenario:&lt;/strong> The operational twin registers the breakdown and emits a live Kafka event. A background hybrid simulation engine ingests that event &lt;em>while it is actively running&lt;/em>. It dynamically mutates Machine B&amp;rsquo;s capacity parameter to zero and recalculates its logic on the fly. It immediately outputs new telemetry: &amp;ldquo;Machine B&amp;rsquo;s queue is backing up, routing logic must shift, and a bottleneck will form at Machine C in exactly 45 minutes.&amp;rdquo;&lt;/li>
&lt;/ul>

&lt;h2 id="upgraded-simulation-fallacy" data-numberify>Upgraded Simulation Fallacy&lt;a class="anchor ms-1" href="#upgraded-simulation-fallacy" aria-label="Link to this section: Upgraded Simulation Fallacy">&lt;/a>&lt;/h2>
&lt;p>Because of overlapping definitions, organizations often fall into what we might call the &amp;ldquo;Upgraded Simulation Fallacy&amp;rdquo;. They purchase standard simulation software, attach a 3D dashboard, and market the project as a digital twin. If the system lacks the data pipelines to periodically align with live operational states, it sits in an ambiguous middle zone, delivering some monitoring value, but falling short of a fully integrated twin ecosystem.&lt;/p>
&lt;p>Transitioning to an operational twin introduces strict engineering realities:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Cost and Complexity:&lt;/strong> Building them requires heavy investments in sensors, edge computing, and complex data architecture.&lt;/li>
&lt;li>&lt;strong>Data Pipelines and Standardization:&lt;/strong> Getting disparate sensors to speak the same language and pipe into a central engine requires robust schemas (like Pydantic, Avro, or Protobuf).&lt;/li>
&lt;/ul>
&lt;p>This last point is where many hybrid projects stall. If you naively force real-time asynchronous updates directly into a standard simulation loop, the engine&amp;rsquo;s internal clock can bottleneck while waiting for network requests. To avoid this design anti-pattern, teams must explicitly decouple their network I/O from the simulation execution or rely on rolling micro-batches.&lt;/p>

&lt;h2 id="taking-control-with-dynamic-des" data-numberify>Taking Control with dynamic-des&lt;a class="anchor ms-1" href="#taking-control-with-dynamic-des" aria-label="Link to this section: Taking Control with dynamic-des">&lt;/a>&lt;/h2>
&lt;p>Organizations shouldn&amp;rsquo;t have to choose between an isolated simulation and an operational dashboard. The goal is to combine them, running &amp;ldquo;what-if&amp;rdquo; forecasts against synchronized, real-world states.&lt;/p>
&lt;p>But if you want to implement an event-driven architecture where a running mathematical model ingests Kafka streams and alters its parameters on the fly without stopping, you face a complex I/O challenge.&lt;/p>
&lt;p>One purpose-built approach to solving this specific pattern is the &lt;a href="https://github.com/jaehyeon-kim/dynamic-des" target="_blank" rel="noopener noreferrer">&lt;strong>&lt;code>dynamic-des&lt;/code>&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/dynamic-des/">documentation&lt;/a>) package. In &lt;a href="https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/">Part 2&lt;/a> of this series, we will look at how this open-source tool uses a Switchboard pattern and dynamic mutable resources to turn a static SimPy script into a synchronized, event-driven engine.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-01-ecommerce-cdc-debezium-kafka-connect/">Change Data Capture on a Simulated Online Shop with Debezium and Kafka Connect&lt;/a> - a dynamic-des simulation of an online shop feeding a change data capture pipeline&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-10-02-game-leaderboard-flink-sql/">Keeping Game Leaderboards Up to Date in Real Time with Kafka and Flink SQL&lt;/a> - a dynamic-des simulation of a mobile game feeding Flink SQL leaderboards&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-18-agentic-analytics-system/">Building an Agentic Analytics System over an Iceberg Lakehouse&lt;/a> - dynamic-des generating the lakehouse data that an agent queries&lt;/li>
&lt;/ul></content:encoded></item><item><title>Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning</title><link>https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/</link><guid>https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/</guid><pubDate>Tue, 21 Apr 2026 00:00:00 +0000</pubDate><category>Apache Flink</category><category>Apache Kafka</category><category>Kotlin</category><category>Python</category><category>Online Machine Learning</category><category>Digital Twin</category><category>Discrete Event Simulation</category><category>desflow</category><description>Overview Imagine using a rolling pin to flatten out a thick piece of dough. A Hot Strip Mill does the exact same thing, but with glowing red-hot steel slabs (often heated over 1000°C) and massive mechanical rollers. The steel is passed through a series of these rollers, crushing it down from a thick block into a long, thin sheet.
Calculating the exact Rolling Force required to crush the steel is critical.</description><content:encoded>
&lt;h2 id="overview" data-numberify>Overview&lt;a class="anchor ms-1" href="#overview" aria-label="Link to this section: Overview">&lt;/a>&lt;/h2>
&lt;p>Imagine using a rolling pin to flatten out a thick piece of dough. A Hot Strip Mill does the exact same thing, but with glowing red-hot steel slabs (often heated over 1000°C) and massive mechanical rollers. The steel is passed through a series of these rollers, crushing it down from a thick block into a long, thin sheet.&lt;/p>
&lt;p>Calculating the exact &lt;strong>Rolling Force&lt;/strong> required to crush the steel is critical. If the machine pushes too hard, it can severely damage the rollers; if it doesn&amp;rsquo;t push hard enough, the steel doesn&amp;rsquo;t reach the target thickness. Because the rollers are constantly grinding against raw steel, their physical shape slowly degrades over time. As the machinery wears down, the legacy mathematical formulas used to predict that perfect force slowly become inaccurate. This physical degradation is the root of the &lt;strong>Concept Drift&lt;/strong> our real-time ML pipeline is solving.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Hot strip rolling line from slab furnace through descaler, roughing mill, transfer bar, runout table and downcoiler" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/hot-rolling-process.png" loading="lazy" title="Hot Rolling Process" width="1340" height="295" />
&lt;/picture>&lt;figcaption class="figure-caption">Hot Rolling Process&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>In heavy industrial manufacturing, such as steel hot strip rolling, deterministic physics formulas are the traditional standard for calculating the exact force required to deform a slab of steel. However, these pure physics models share a fatal flaw: they assume a pristine factory state. As physical rollers grind against red-hot steel over hours of production, they experience mechanical wear.&lt;/p>
&lt;p>As the machinery degrades, the actual physical force required drifts away from the theoretical prediction. In data science, this is a classic manifestation of &lt;strong>Concept Drift&lt;/strong>.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Error chart where the physics baseline jumps at a wear shock while the AMRules model recovers" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/drift-convergence-lifecycle.png" loading="lazy" title="Drift and Convergence Lifecycle" width="1034" height="588" />
&lt;/picture>&lt;figcaption class="figure-caption">Drift and Convergence Lifecycle&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>To tackle this, I recently built a real-time, fault-tolerant Online Machine Learning (OML) pipeline and Digital Twin. By combining Apache Kafka, Apache Flink (written in Kotlin), and the Massive Online Analysis (MOA) framework, the system learns the &lt;em>new&lt;/em> physical reality of the worn machinery on the fly, autonomously correcting the physics baseline safely behind a deterministic Shadow Mode router.&lt;/p>
&lt;p>Here is an overview of the architecture and the engineering challenges solved along the way. The complete source code for this project, including the Flink stream processor and the Python Digital Twin simulation, is available on &lt;a href="https://github.com/jaehyeon-kim/oml-digital-twin-hotrolling" target="_blank" rel="noopener noreferrer">GitHub&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>

&lt;h2 id="architecture-at-a-glance" data-numberify>Architecture at a Glance&lt;a class="anchor ms-1" href="#architecture-at-a-glance" aria-label="Link to this section: Architecture at a Glance">&lt;/a>&lt;/h2>
&lt;p>The project is split into three highly decoupled domains:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Digital Twin (Python):&lt;/strong> Utilizing the &lt;a href="https://github.com/jaehyeon-kim/dynamic-des" target="_blank" rel="noopener noreferrer">Dynamic DES&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> (&lt;a href="https://jaehyeon.me/dynamic-des/latest/getting-started/">documentation&lt;/a>) Python package, this layer generates synthetic rolling events, applies simulated mechanical wear, calculates theoretical/actual physics, and pushes the data to Kafka.&lt;/li>
&lt;li>&lt;strong>Message Broker (Kafka):&lt;/strong> Handles the asynchronous, high-throughput streaming of prediction requests and delayed ground-truth target forces.&lt;/li>
&lt;li>&lt;strong>Stream Processor (Flink/Kotlin):&lt;/strong> The core engine. It aligns asynchronous streams, trains the machine learning models dynamically, evaluates safety guardrails, and sinks metrics to ClickHouse for evaluation in real-time.&lt;/li>
&lt;/ol>
&lt;p>Moving data from the Digital Twin through Kafka to Flink looks straightforward in a diagram, but real-world industrial physics and distributed systems are highly complex. To make this pipeline robust enough for the unpredictability of a live factory floor, we had to overcome four major engineering challenges.&lt;/p>

&lt;h2 id="challenges" data-numberify>Challenges&lt;a class="anchor ms-1" href="#challenges" aria-label="Link to this section: Challenges">&lt;/a>&lt;/h2>
&lt;p>Taking an online machine learning model out of the lab and deploying it into a live industrial environment introduces a unique set of hurdles. From managing the physical delays of factory sensors to ensuring the model never commands a physically unsafe action, here is a breakdown of the four primary engineering challenges we tackled to make this architecture more robust.&lt;/p>

&lt;h3 id="challenge-1-asynchronous-industrial-streams" data-numberify>Challenge 1: Asynchronous Industrial Streams&lt;a class="anchor ms-1" href="#challenge-1-asynchronous-industrial-streams" aria-label="Link to this section: Challenge 1: Asynchronous Industrial Streams">&lt;/a>&lt;/h3>
&lt;p>Industrial data streams are inherently asynchronous. The factory floor requests a prediction &lt;em>before&lt;/em> the steel enters the rolling stand (Event A), but the ground-truth sensor data confirming the actual required force arrives &lt;em>after&lt;/em> the steel is crushed (Event B).&lt;/p>
&lt;p>To execute machine learning, these two events must be perfectly joined. However, network latency and partition skew in Kafka mean that these events might arrive out of order.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafka topics joined, keyed by steel grade, then a stateful operator trains AMRules and writes to ClickHouse" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/stream-processing-flink.png" loading="lazy" title="Flink Application DAG" width="1365" height="613" />
&lt;/picture>&lt;figcaption class="figure-caption">Flink Application DAG&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>In Flink, this is solved using a &lt;code>KeyedCoProcessFunction&lt;/code>. By keying the streams on a composite &lt;code>Slab ID&lt;/code> + &lt;code>Pass Number&lt;/code>, Flink guarantees both events route to the exact same TaskManager. The &lt;code>EventMatchProcessFunction&lt;/code> utilizes Flink&amp;rsquo;s &lt;code>ValueState&lt;/code> to buffer whichever event arrives first. It then registers a processing-time timer (Time-To-Live). If the matching event arrives, they are joined and emitted. If the timer expires (e.g., a physical sensor failure), the orphaned state is safely purged to prevent memory leaks.&lt;/p>

&lt;h3 id="challenge-2-online-residual-learning" data-numberify>Challenge 2: Online Residual Learning&lt;a class="anchor ms-1" href="#challenge-2-online-residual-learning" aria-label="Link to this section: Challenge 2: Online Residual Learning">&lt;/a>&lt;/h3>
&lt;p>Instead of training a batch model offline on historical data, which would instantly become obsolete the moment the rollers wore down further, the Flink pipeline trains on the continuous stream of slabs using a strict &lt;strong>Test-then-Train&lt;/strong> (prequential) paradigm.&lt;/p>
&lt;p>Crucially, the ML models do not predict the absolute rolling force from scratch. Instead, they utilize &lt;strong>Residual Learning&lt;/strong>. They predict the residual error between the theoretical formula and the actual physical force.&lt;/p>
&lt;p>To execute this, the pipeline integrates the Java MOA framework. The primary production model is &lt;strong>AMRules&lt;/strong> (Adaptive Model Rules), a streaming rule-learning algorithm. It builds an ensemble of rules that calculate a linear combination of physical attributes, continuously updating weights via the Delta rule.&lt;/p>
&lt;p>Unlike static models, AMRules runs an internal Page-Hinkley test to detect sudden physical shocks (like a roller bearing breaking) and instantly prunes obsolete rules, allowing rapid convergence to new physical realities. Furthermore, because ML models require normalized inputs, the pipeline implements &lt;strong>Welford&amp;rsquo;s Online Algorithm&lt;/strong> in a custom Flink state object to calculate streaming Z-scores on the fly without needing to load full datasets into memory.&lt;/p>

&lt;h3 id="challenge-3-shadow-mode-router" data-numberify>Challenge 3: Shadow Mode Router&lt;a class="anchor ms-1" href="#challenge-3-shadow-mode-router" aria-label="Link to this section: Challenge 3: Shadow Mode Router">&lt;/a>&lt;/h3>
&lt;p>Industrial Machine Learning cannot operate without strict safety boundaries. A model error that generates excessive rolling force could severely damage a multi-million-dollar rolling stand or create a major production bottleneck in the factory.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Decision flow where two guardrails approve the model prediction or reject it back to the physics baseline" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/shadow-mode-router.png" loading="lazy" title="Shadow Mode Router Decision Logic" width="557" height="747" />
&lt;/picture>&lt;figcaption class="figure-caption">Shadow Mode Router Decision Logic&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>Before any ML-adjusted prediction is allowed to influence the factory floor, it must pass through a deterministic &lt;strong>Shadow Mode Router&lt;/strong> consisting of two guardrails:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Stateless Mechanical Limits:&lt;/strong> Calculates the residual difference between the model&amp;rsquo;s requested force and the physics baseline. If the model requests a deviation outside of physical safety bounds (e.g., &amp;gt; +25% or &amp;lt; -20%), the prediction is instantly rejected.&lt;/li>
&lt;li>&lt;strong>Stateful Trust Score:&lt;/strong> Using an Exponentially Weighted Moving Average (EWMA), the router continuously tracks the Absolute Percentage Error (APE) of both the model and the pure physics baseline. If the model&amp;rsquo;s average error trails the physics baseline by a defined &amp;ldquo;Trust Deficit&amp;rdquo; margin, it is benched.&lt;/li>
&lt;/ol>
&lt;p>If either guardrail is triggered, the router safely falls back to the deterministic Physics Baseline. The ML model continues to train in the background (Shadow Mode) until it re-learns the physical reality, improves its EWMA trust score, and is autonomously promoted back to active control.&lt;/p>

&lt;h3 id="challenge-4-checkpointing-complex-ml-state" data-numberify>Challenge 4: Checkpointing Complex ML State&lt;a class="anchor ms-1" href="#challenge-4-checkpointing-complex-ml-state" aria-label="Link to this section: Challenge 4: Checkpointing Complex ML State">&lt;/a>&lt;/h3>
&lt;p>For Flink to provide exactly-once processing guarantees, it must asynchronously snapshot the state of operators to a durable backend like RocksDB.&lt;/p>
&lt;p>While simple tracking states serialize perfectly into basic Flink &lt;code>ValueState&amp;lt;Double&amp;gt;&lt;/code>, the AMRules model is a deeply complex, dynamic tree structure. Allowing Flink&amp;rsquo;s default Kryo serializers to traverse this massive object graph during checkpointing causes severe performance degradation and frequent serialization crashes.&lt;/p>
&lt;p>To bypass this, the &lt;code>MoaEvaluationProcessFunction&lt;/code> interacts with the MOA model as a standard Java object in memory for high-performance execution. However, upon every state update, it manually serializes the model into a raw byte array (&lt;code>ValueState&amp;lt;ByteArray&amp;gt;&lt;/code>) using Java&amp;rsquo;s native &lt;code>ObjectOutputStream&lt;/code>. When Flink triggers a checkpoint, it simply flushes these pre-serialized byte arrays to disk. Upon recovery, Flink deserializes the bytes, instantly restoring the exact computational brain-state of the algorithm.&lt;/p>

&lt;h2 id="system-in-action" data-numberify>System in Action&lt;a class="anchor ms-1" href="#system-in-action" aria-label="Link to this section: System in Action">&lt;/a>&lt;/h2>
&lt;p>You can easily spin up the entire architecture locally to see the system in action. The project repository includes a complete Docker Compose environment that provisions Kafka, Flink, and ClickHouse.&lt;/p>
&lt;p>To get started, simply clone the repository, build the Flink application using the provided Gradle wrapper, and bring up the infrastructure. Once the environment is running, you can launch the Python-based data generator to simulate the steel rolling process and open the NiceGUI control dashboard to monitor the machine learning metrics in real-time.&lt;/p>
&lt;p>For detailed, step-by-step instructions on cloning the repository, configuring licenses, and deploying the Flink job, refer to the &lt;a href="https://github.com/jaehyeon-kim/oml-digital-twin-hotrolling/tree/main?tab=readme-ov-file#-getting-started" target="_blank" rel="noopener noreferrer">Getting Started&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> section in the repository.&lt;/p>

&lt;h2 id="simulation-scenarios" data-numberify>Simulation Scenarios&lt;a class="anchor ms-1" href="#simulation-scenarios" aria-label="Link to this section: Simulation Scenarios">&lt;/a>&lt;/h2>
&lt;p>Using the NiceGUI Dashboard, you can actively manipulate the physical state of the Digital Twin to observe the Flink pipeline&amp;rsquo;s reaction in real-time.&lt;/p>
&lt;p>Here is how the pipeline handles different industrial scenarios:&lt;/p>

&lt;h3 id="abrupt-drift-mechanical-shock" data-numberify>Abrupt Drift (Mechanical Shock)&lt;a class="anchor ms-1" href="#abrupt-drift-mechanical-shock" aria-label="Link to this section: Abrupt Drift (Mechanical Shock)">&lt;/a>&lt;/h3>
&lt;p>Simulates a sudden mechanical failure (e.g., a roller bearing breaking), instantly altering the physics of the mill.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Simulation Settings:&lt;/strong> Trigger Abrupt Shock (Wear Level: 60.0).&lt;/li>
&lt;li>&lt;strong>Observation:&lt;/strong> The pure physics baseline error instantly spikes and remains high (often &amp;gt;10% APE) because the physical reality no longer matches the math. The &lt;strong>AMRules&lt;/strong> model initially spikes alongside it, but its Page-Hinkley change detector immediately drops obsolete rules, allowing it to rapidly converge back to lower error as it learns the new broken state.&lt;/li>
&lt;/ul>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Control plane dashboard on the structural tab at wear 60, baseline error running above both AMRules lines" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/abrupt-drift.gif" loading="lazy" title="Abrupt Drift" width="1770" height="1166" />
&lt;/picture>&lt;figcaption class="figure-caption">Abrupt Drift&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h3 id="gradual-drift-standard-wear" data-numberify>Gradual Drift (Standard Wear)&lt;a class="anchor ms-1" href="#gradual-drift-standard-wear" aria-label="Link to this section: Gradual Drift (Standard Wear)">&lt;/a>&lt;/h3>
&lt;p>Simulates the continuous, bi-directional cycle of slow roller degradation and subsequent maintenance recovery over hours of production.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Simulation Settings:&lt;/strong> Gradual Wear (Step Size: 5.0 units, Frequency: 30 seconds).&lt;/li>
&lt;li>&lt;strong>Observation:&lt;/strong> The physics baseline error slowly and persistently creeps upward/download over time (e.g. ranging from 2% to 7% APE) as the wear level drifts gradually. The &lt;strong>AMRules&lt;/strong> model gracefully tracks this changing reality, updating its linear weights incrementally to maintain a smooth error rate.&lt;/li>
&lt;/ul>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Control plane dashboard on the microalloyed tab under gradual drift, baseline error above the AMRules lines" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/gradual-drift.gif" loading="lazy" title="Gradual Drift" width="1770" height="1166" />
&lt;/picture>&lt;figcaption class="figure-caption">Gradual Drift&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h3 id="no-drift-pristine-state" data-numberify>No Drift (Pristine State)&lt;a class="anchor ms-1" href="#no-drift-pristine-state" aria-label="Link to this section: No Drift (Pristine State)">&lt;/a>&lt;/h3>
&lt;p>Simulates a pristine factory state, such as immediately after a maintenance shift replaces the rollers.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Simulation Settings:&lt;/strong> Wear Level: 0.0.&lt;/li>
&lt;li>&lt;strong>Observation:&lt;/strong> The physical reality of the factory floor perfectly aligns with the deterministic mathematical formulas. The physics baseline maintains a highly accurate, near-zero error rate (&amp;lt; 0.3% APE). &lt;strong>AMRules&lt;/strong> remains stable under this condition.&lt;/li>
&lt;/ul>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Control plane dashboard on the high alloy tab at zero wear, baseline and AMRules errors overlapping below 0.3 percent" src="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/no-drift.gif" loading="lazy" title="No Drift" width="1770" height="1166" />
&lt;/picture>&lt;figcaption class="figure-caption">No Drift&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-28-digital-twin-dynamic-des/">Building an Event-Driven Hybrid Digital Twin with dynamic-des&lt;/a> - the simulation package behind the twin, built on the Switchboard pattern and dynamic topic routing.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-07-17-dynamic-des-declarative-connectors/">Dynamic DES: A Declarative API with Postgres and Redis Connectors&lt;/a> - the later release that adds a declarative API and native Postgres and Redis connectors.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-23-digital-twin-industry-4-0/">Why Digital Twins Are Rewiring Industry 4.0&lt;/a> - the architectural layers that separate a traditional simulation from an event-driven hybrid pipeline.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-12-10-streaming-processing-with-flink-in-kotlin/">Stream Processing with Flink in Kotlin&lt;/a> - more Flink examples in Kotlin, the language this pipeline is written in.&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">Productionizing an Online Product Recommender using Event Driven Architecture&lt;/a> - a contextual bandit recommender trained and served online with Flink, Kafka and Valkey.&lt;/li>
&lt;/ul>

&lt;h2 id="conclusion" data-numberify>Conclusion&lt;a class="anchor ms-1" href="#conclusion" aria-label="Link to this section: Conclusion">&lt;/a>&lt;/h2>
&lt;p>Combining Apache Flink with Online Machine Learning bridges the gap between theoretical physics and the harsh reality of a degrading factory floor. To make this safe and effective, the architecture relies on three core ideas: predicting residual errors instead of absolute forces, isolating models by product line to prevent forgetting, and enforcing strict safety guardrails. Together, these techniques ensure that heavy machinery operates optimally, even as it breaks down over time.&lt;/p>
&lt;p>You can explore the complete codebase, run the simulation locally, and dive deeper into the architecture on the project&amp;rsquo;s &lt;a href="https://github.com/jaehyeon-kim/oml-digital-twin-hotrolling" target="_blank" rel="noopener noreferrer">GitHub repository&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p></content:encoded></item><item><title>Slides as Code: Integrating Reveal.js into my Hugo Blog</title><link>https://jaehyeon.me/blog/2026-03-09-integrate-revealjs/</link><guid>https://jaehyeon.me/blog/2026-03-09-integrate-revealjs/</guid><pubDate>Mon, 09 Mar 2026 00:00:00 +0000</pubDate><category>Hugo</category><category>Reveal.js</category><category>JavaScript</category><category>Markdown</category><category>Marp</category><description>&lt;p>For a long time, I wanted a way to host my technical presentations directly on my website without relying on external platforms or bulky PDF exports. I wanted a &lt;strong>&amp;ldquo;Slides as Code&amp;rdquo;&lt;/strong> approach: version-controlled Markdown files that live natively alongside my blog posts.&lt;/p></description><content:encoded>&lt;p>For a long time, I wanted a way to host my technical presentations directly on my website without relying on external platforms or bulky PDF exports. I wanted a &lt;strong>&amp;ldquo;Slides as Code&amp;rdquo;&lt;/strong> approach: version-controlled Markdown files that live natively alongside my blog posts.&lt;/p>
&lt;h2 id="choosing-between-marp-and-revealjs" data-numberify>Choosing between Marp and Reveal.js&lt;a class="anchor ms-1" href="#choosing-between-marp-and-revealjs" aria-label="Link to this section: Choosing between Marp and Reveal.js">&lt;/a>&lt;/h2>
&lt;p>When looking for a solution, I initially considered &lt;strong>Marp&lt;/strong>.&lt;/p>
&lt;p>Marp is undeniably easier to author initially; its VS Code extension with live preview makes designing slides feel like magic. However, it presented a hurdle for my Hugo integration as it requires separate compilation.&lt;/p>
&lt;p>Marp requires a separate build step to convert Markdown into standalone HTML. To integrate it, I would have to manually export each deck and move the resulting HTML into my &lt;code>static/&lt;/code> folder. This felt &amp;ldquo;disconnected&amp;rdquo; from the Hugo ecosystem.&lt;/p>
&lt;p>&lt;a href="https://revealjs.com/" target="_blank" rel="noopener noreferrer">&lt;strong>Reveal.js&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, on the other hand, is a browser-based engine. By using Reveal.js, I can:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Stay Native:&lt;/strong> Write pure Markdown in my &lt;code>content/&lt;/code> folder just like a blog post.&lt;/li>
&lt;li>&lt;strong>Zero Manual Export:&lt;/strong> Hugo handles the routing, and the browser handles the rendering at runtime.&lt;/li>
&lt;li>&lt;strong>Automatic Updates:&lt;/strong> When I &lt;code>git push&lt;/code> a Markdown file, the slides are live instantly. No separate compilation, no manual file moving.&lt;/li>
&lt;/ol>

&lt;h2 id="see-it-in-action" data-numberify>See it in Action&lt;a class="anchor ms-1" href="#see-it-in-action" aria-label="Link to this section: See it in Action">&lt;/a>&lt;/h2>
&lt;p>Before diving into the configuration, you can interact with the live demo below. This is the actual slide deck, rendered natively by Hugo and embedded directly into this post:&lt;/p>
&lt;div class="ratio ratio-16x9 my-4 shadow-sm rounded overflow-hidden">
 &lt;iframe 
 src="https://jaehyeon.me/slides/2026-03-09-reavealjs-demo/" 
 title="Presentation" 
 allowfullscreen="true" 
 style="border: 0;">
 &lt;/iframe>
&lt;/div>
&lt;div class="text-center mb-4">
 &lt;a href="https://jaehyeon.me/slides/2026-03-09-reavealjs-demo/" target="_blank" class="btn btn-sm btn-outline-primary">
 &lt;i class="fas fa-external-link-alt">&lt;/i> View Full Screen
 &lt;/a>
&lt;/div>
&lt;p>&lt;em>You can use your arrow keys to navigate the slides above, or click the &amp;ldquo;View Full Screen&amp;rdquo; button for the complete experience.&lt;/em>&lt;/p>

&lt;h2 id="how-it-works" data-numberify>How It Works&lt;a class="anchor ms-1" href="#how-it-works" aria-label="Link to this section: How It Works">&lt;/a>&lt;/h2>
&lt;p>I chose to keep slides independent from my main blog posts to avoid cluttering my main feed. Using Hugo&amp;rsquo;s &lt;strong>Leaf Bundles&lt;/strong>, each presentation gets its own folder for local assets like images.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">content/slides/
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">└── 2026-03-06-revealjs-demo/ 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl"> ├── index.md # The slide content
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> └── featured.jpg # Thumbnail for list view
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="custom-shell-layout" data-numberify>Custom &amp;ldquo;Shell&amp;rdquo; Layout&lt;a class="anchor ms-1" href="#custom-shell-layout" aria-label="Link to this section: Custom &amp;amp;ldquo;Shell&amp;amp;rdquo; Layout">&lt;/a>&lt;/h3>
&lt;p>The secret is a custom layout at &lt;a href="https://github.com/jaehyeon-kim/jaehyeon-kim.github.io/blob/master/layouts/slides/single.html" target="_blank" rel="noopener noreferrer">&lt;code>layouts/slides/single.html&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. Instead of using my theme&amp;rsquo;s standard blog layout (which includes headers and sidebars), I created a clean &amp;ldquo;shell&amp;rdquo; that loads the Reveal.js engine from a CDN.&lt;/p>
&lt;p>Crucially, I use &lt;code>{{ .RawContent }}&lt;/code> instead of &lt;code>{{ .Content }}&lt;/code>. This is because Reveal.js needs the raw Markdown syntax (like &lt;code>---&lt;/code> for slide breaks) to calculate transitions in the browser, rather than the pre-rendered HTML Hugo usually provides.&lt;/p>

&lt;h3 id="advanced-technical-features" data-numberify>Advanced Technical Features&lt;a class="anchor ms-1" href="#advanced-technical-features" aria-label="Link to this section: Advanced Technical Features">&lt;/a>&lt;/h3>
&lt;p>Because Reveal.js is a web-based runtime, it allows for high-end technical storytelling that traditional slide software can&amp;rsquo;t easily handle:&lt;/p>

&lt;h4 id="interactive-code-storytelling" data-numberify>Interactive Code Storytelling&lt;a class="anchor ms-1" href="#interactive-code-storytelling" aria-label="Link to this section: Interactive Code Storytelling">&lt;/a>&lt;/h4>
&lt;p>Standard syntax highlighting is good, but Reveal.js allows for &lt;strong>directed walkthroughs&lt;/strong>. Using a specific syntax like &lt;code>[1|3-5|7]&lt;/code>, I can guide the audience through a specific block of code:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Workflow:&lt;/strong> First, I highlight the function definition, then step into the processing logic, and finally focus on the return statement. This &amp;ldquo;guided tour&amp;rdquo; of the code prevents the audience from getting overwhelmed by a large block of text.&lt;/li>
&lt;/ul>

&lt;h4 id="precision-media-styling" data-numberify>Precision Media Styling&lt;a class="anchor ms-1" href="#precision-media-styling" aria-label="Link to this section: Precision Media Styling">&lt;/a>&lt;/h4>
&lt;p>Using the &lt;code>.element&lt;/code> comment syntax, I have granular control over CSS directly within the Markdown.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Dynamic Backgrounds:&lt;/strong> I can switch the entire slide context using the &lt;code>data-background&lt;/code> attribute (like a high-impact &amp;ldquo;Cosmic&amp;rdquo; space background).&lt;/li>
&lt;li>&lt;strong>CSS Overrides:&lt;/strong> I can apply borders, rounded corners, and specific alignments, for example, centering a gold-bordered image without ever leaving the Markdown file.&lt;/li>
&lt;/ul>

&lt;h4 id="managing-cognitive-load-with-fragments" data-numberify>Managing Cognitive Load with Fragments&lt;a class="anchor ms-1" href="#managing-cognitive-load-with-fragments" aria-label="Link to this section: Managing Cognitive Load with Fragments">&lt;/a>&lt;/h4>
&lt;p>To keep the audience focused on the current talking point, I use &lt;strong>Fragments&lt;/strong>. This provides progressive disclosure of information. Beyond just &amp;ldquo;appearing,&amp;rdquo; I can use classes like &lt;code>grow&lt;/code> to emphasize a specific item or &lt;code>highlight-red&lt;/code> to signal a warning, all triggered by a simple keypress.&lt;/p>

&lt;h4 id="mathematical-precision-latexmathjax" data-numberify>Mathematical Precision (LaTeX/MathJax)&lt;a class="anchor ms-1" href="#mathematical-precision-latexmathjax" aria-label="Link to this section: Mathematical Precision (LaTeX/MathJax)">&lt;/a>&lt;/h4>
&lt;p>For engineering-heavy talks, raw text equations look unprofessional. By integrating &lt;strong>MathJax&lt;/strong>, I can render complex mathematical models, such as Probability Density Functions, with the same precision found in a LaTeX paper. They remain sharp at any zoom level because they are rendered as vectors in the browser.&lt;/p>

&lt;h4 id="diagrams-as-code-mermaidjs" data-numberify>Diagrams as Code (Mermaid.js)&lt;a class="anchor ms-1" href="#diagrams-as-code-mermaidjs" aria-label="Link to this section: Diagrams as Code (Mermaid.js)">&lt;/a>&lt;/h4>
&lt;p>Instead of embedding static PNGs of architecture diagrams that inevitably become outdated, I use &lt;strong>Mermaid.js&lt;/strong>. This allows me to define data pipelines, like a Kafka-to-Flink-to-Redis flow, directly in the Markdown text.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Engineering Value:&lt;/strong> Diagrams are now version-controlled and searchable. If the architecture changes, I update a few lines of text, and the visual layout re-renders automatically.&lt;/li>
&lt;/ul>

&lt;h2 id="road-ahead-professional-diagrams-with-drawio" data-numberify>Road Ahead: Professional Diagrams with Draw.io&lt;a class="anchor ms-1" href="#road-ahead-professional-diagrams-with-drawio" aria-label="Link to this section: Road Ahead: Professional Diagrams with Draw.io">&lt;/a>&lt;/h2>
&lt;p>While Mermaid.js is excellent for rapid, text-based flowcharts, complex system architectures often require the precision of a dedicated design tool. I am currently investigating the best way to integrate &lt;strong>Draw.io&lt;/strong> into this &amp;ldquo;Slides-as-Code&amp;rdquo; workflow without losing the benefits of version control.&lt;/p>
&lt;p>Currently, I am evaluating two brief paths:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Editable SVGs:&lt;/strong> Exporting diagrams as SVGs with embedded XML. This allows the slides to treat the diagram as a standard image while keeping the source file fully editable and version-controlled.&lt;/li>
&lt;li>&lt;strong>The Draw.io JS Viewer:&lt;/strong> Utilizing the official Diagrams.net JavaScript library to render &lt;code>.drawio&lt;/code> XML files dynamically. This would enable interactive features like zooming and layer toggling directly during a presentation.&lt;/li>
&lt;/ul>
&lt;p>Moving forward, the goal is to ensure that even the most complex architectural designs remain as easy to maintain as a line of Markdown.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2023-04-24-self-hosted-blog/">Self-managed Blog with Hugo and GitHub Pages&lt;/a> - sets up the Hugo site that this Reveal.js layout plugs into&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2018-05-19-asyn-shiny-and-its-limitation/">Async Shiny and Its Limitation&lt;/a> - an earlier comparison of a JavaScript frontend against a framework that renders the page for you&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2018-05-26-shiny-to-vue.js/">Shiny to Vue.js&lt;/a> - replaces R widgets with native JavaScript libraries, the same shift from generated output to hand-written frontend&lt;/li>
&lt;/ul>

&lt;h2 id="why-this-works-for-me" data-numberify>Why this works for me&lt;a class="anchor ms-1" href="#why-this-works-for-me" aria-label="Link to this section: Why this works for me">&lt;/a>&lt;/h2>
&lt;p>By leveraging Hugo&amp;rsquo;s &lt;strong>Directory Inference&lt;/strong>, any file I drop into &lt;code>content/slides/&lt;/code> automatically inherits this powerful shell. I no longer &amp;ldquo;design&amp;rdquo; slides; I &lt;strong>engineer&lt;/strong> them.&lt;/p>
&lt;p>While Marp might be slightly easier to &lt;strong>write&lt;/strong>, the Reveal.js integration is much easier to &lt;strong>maintain&lt;/strong>. It turns my site into a single source of truth for both my articles and my presentations.&lt;/p>
&lt;p>You can check out my latest presentations in the &lt;a href="https://jaehyeon.me/slides/">Slides&lt;/a> section!&lt;/p></content:encoded></item><item><title>Productionizing an Online Product Recommender using Event Driven Architecture</title><link>https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/</link><guid>https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/</guid><pubDate>Mon, 23 Feb 2026 00:00:00 +0000</pubDate><category>Python</category><category>Apache Kafka</category><category>Apache Flink</category><category>Valkey</category><category>Open Data Stack</category><category>Contextual Bandits</category><category>Online Machine Learning</category><category>Recommender System</category><category>Benchtop</category><description>&lt;p>Real-world recommendation systems require low-latency inference for users and high-throughput training for model updates. A monolithic script cannot handle production scale, while it is effective for testing algorithms locally. In &lt;a href="https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/">&lt;strong>Part 1&lt;/strong>&lt;/a>, we built a contextual bandit prototype using Python and &lt;a href="https://github.com/fidelity/mab2rec" target="_blank" rel="noopener noreferrer">&lt;code>Mab2Rec&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>
&lt;p>This post demonstrates how to decouple these concerns using an event-driven architecture with Apache Flink, Kafka, and Valkey.&lt;/p></description><content:encoded>&lt;p>Real-world recommendation systems require low-latency inference for users and high-throughput training for model updates. A monolithic script cannot handle production scale, while it is effective for testing algorithms locally. In &lt;a href="https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/">&lt;strong>Part 1&lt;/strong>&lt;/a>, we built a contextual bandit prototype using Python and &lt;a href="https://github.com/fidelity/mab2rec" target="_blank" rel="noopener noreferrer">&lt;code>Mab2Rec&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>
&lt;p>This post demonstrates how to decouple these concerns using an event-driven architecture with Apache Flink, Kafka, and Valkey.&lt;/p>
&lt;h2 id="system-architecture" data-numberify>System Architecture&lt;a class="anchor ms-1" href="#system-architecture" aria-label="Link to this section: System Architecture">&lt;/a>&lt;/h2>
&lt;p>To move from prototype to production, we split the application into two distinct layers: Serving and Training.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Python Client (Serving):&lt;/strong> A lightweight, stateless client responsible for inference. It fetches pre-calculated model parameters from Valkey, computes scores locally to make product recommendations, and captures user feedback.&lt;/li>
&lt;li>&lt;strong>Kafka (Transport):&lt;/strong> Buffers feedback events asynchronously, decoupling the speed of serving from the speed of training.&lt;/li>
&lt;li>&lt;strong>Flink (Training):&lt;/strong> A stateful streaming application. It consumes feedback events, updates the model parameters (LinUCB matrices $A$ and $b$), and pushes the inverted matrices back to Valkey.
&lt;ul>
&lt;li>❗ Unlike &lt;em>Part 1&lt;/em>, where training relied on &lt;a href="https://github.com/fidelity/mabwiser" target="_blank" rel="noopener noreferrer">&lt;code>MABWiser&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, here it is performed via explicit matrix operations.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Valkey (Model Store):&lt;/strong> Stores the latest model parameters ($A^{-1}$ and $b$) for low-latency access by the client.
&lt;ul>
&lt;li>❗ Valkey is a fork of Redis and speaks the same wire protocol, so the client still uses &lt;code>redis-py&lt;/code> and the Flink sink still uses Jedis.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>&lt;strong>📂 Source Code for the Post&lt;/strong>&lt;/p>
&lt;p>The source code for this post is available in the &lt;strong>product-recommender&lt;/strong> folder of the &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">benchtop&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> GitHub repository. It is one of the &lt;a href="https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/">Benchtop&lt;/a> projects, which run locally from a fresh clone.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="The Flink job trains the models on the history in SeaweedFS first, then on the feedback the live client sends to Kafka, and writes them to Valkey for the client to read" src="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/part-2.png" loading="lazy" title="Architecture" width="1595" height="948" />
&lt;/picture>&lt;figcaption class="figure-caption">Architecture&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="flink-application-design" data-numberify>Flink Application Design&lt;a class="anchor ms-1" href="#flink-application-design" aria-label="Link to this section: Flink Application Design">&lt;/a>&lt;/h2>
&lt;p>The &lt;a href="https://github.com/jaehyeon-kim/benchtop/tree/main/product-recommender/recsys-trainer" target="_blank" rel="noopener noreferrer">Flink job (&lt;code>recsys-trainer&lt;/code>)&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> ties these concepts together using a few specific patterns.&lt;/p>

&lt;h3 id="stateful-model-training" data-numberify>Stateful Model Training&lt;a class="anchor ms-1" href="#stateful-model-training" aria-label="Link to this section: Stateful Model Training">&lt;/a>&lt;/h3>
&lt;p>The core challenge in distributed online learning is managing state. The &lt;a href="https://github.com/jaehyeon-kim/benchtop/blob/main/product-recommender/recsys-trainer/src/main/kotlin/me/jaehyeon/topology/processing/LinUCBUpdater.kt" target="_blank" rel="noopener noreferrer">&lt;code>LinUCBUpdater&lt;/code> function&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> in the Flink trainer acts as the system&amp;rsquo;s memory. It implements a &lt;strong>disjoint LinUCB&lt;/strong> model, meaning it maintains a completely independent set of matrices for &lt;strong>each unique product&lt;/strong>.&lt;/p>
&lt;p>❗The matrices are used to calculate scores for making recommendations.&lt;/p>
&lt;p>Each feedback event carries the visit&amp;rsquo;s context vector $x$: $d = 15$ numbers, the user&amp;rsquo;s 10 features and the 5 time flags from &lt;em>Part 1&lt;/em>. For each &lt;code>product_id&lt;/code>, Flink maintains two pieces of state in RocksDB:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Matrix $A_a$ ($d \times d$):&lt;/strong> The sum of $x x^T$ over the contexts product $a$ was shown in, plus $\lambda I$. It tracks &lt;strong>Exposure&lt;/strong>, recording how many times specific user contexts (e.g., &amp;ldquo;Morning Users&amp;rdquo; or &amp;ldquo;Weekend Users&amp;rdquo;) have been seen for a specific product.&lt;/li>
&lt;li>&lt;strong>Vector $b_a$ ($d \times 1$):&lt;/strong> The sum of $r x$, the accumulated reward. It tracks &lt;strong>Success&lt;/strong>, recording which features actually led to a click.&lt;/li>
&lt;/ol>
&lt;p>The matrix $A$ is initialized as a scaled identity matrix $A_0 = \lambda I$, with $\lambda = 1$, to ensure invertibility and to encode an initial prior of uniform uncertainty across feature dimensions.&lt;/p>
&lt;p>When a feedback event arrives for product $a$ (context $x$, reward $r$: 1 for a click, 0 for none), Flink performs the updates:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Update A:&lt;/strong> $A_a \leftarrow A_a + x x^T$. The outer product $x x^T$ makes $A_a$ larger along the observed feature directions. As similar contexts repeat, $A$ grows in those directions, reflecting increased confidence.&lt;/li>
&lt;li>&lt;strong>Update b:&lt;/strong> $b_a \leftarrow b_a + r x$. If the user clicked ($r=1$), we add their feature vector to $b$, reinforcing that preference pattern.&lt;/li>
&lt;/ul>
&lt;p>The updated $A$ and $b$ are stored immediately in &lt;strong>Flink keyed state (backed by RocksDB)&lt;/strong>. With checkpointing enabled, this state is durably persisted and recovered in case of failure.&lt;/p>
&lt;p>&lt;strong>Optimization 1: Inversion on Write&lt;/strong>&lt;/p>
&lt;p>To generate a score, we need the inverse matrix $A^{-1}$, which is computationally expensive. If we performed this inversion inside the Python client for every recommendation request, latency would increase significantly. Instead, the Flink training job periodically loads $A$ from state, factorizes it using &lt;strong>LU decomposition&lt;/strong>, computes $A^{-1}$, and stores the inverse in Valkey. Because the contextual feature dimension in this demo is small, recomputing the inverse periodically remains efficient while keeping the serving layer lightweight.&lt;/p>
&lt;p>&lt;strong>Optimization 2: Batched Updates&lt;/strong>&lt;/p>
&lt;p>In a high-traffic environment, a popular product might receive thousands of clicks per second. Inverting the matrix and writing to Valkey for &lt;em>every single click&lt;/em> would be inefficient.&lt;/p>
&lt;p>To solve this, we use Flink timers to buffer updates. The model state ($A$ and $b$) is updated immediately for every event, while the expensive inversion and Valkey write are triggered periodically (e.g., every 5 seconds). This drastically reduces CPU load and network traffic while keeping the model fresh.&lt;/p>

&lt;h3 id="scalable-inference-logic" data-numberify>Scalable Inference Logic&lt;a class="anchor ms-1" href="#scalable-inference-logic" aria-label="Link to this section: Scalable Inference Logic">&lt;/a>&lt;/h3>
&lt;p>The Python client (&lt;a href="https://github.com/jaehyeon-kim/benchtop/blob/main/product-recommender/recommender/run/live.py" target="_blank" rel="noopener noreferrer">&lt;code>recommender/run/live.py&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>) is responsible for ranking items. It uses the &lt;strong>Upper Confidence Bound (UCB)&lt;/strong> formula to balance exploiting known good items and exploring uncertain ones.&lt;/p>
&lt;p>For a given user context vector $x$ and product $a$, the score is calculated as:&lt;/p>
&lt;p>$$ \text{Score}&lt;em>a = \underbrace{x^T \theta_a}&lt;/em>{\text{exploitation}} + \underbrace{\alpha \sqrt{x^T A_a^{-1} x}}_{\text{exploration}}, \quad \text{where } \theta_a = A_a^{-1} b_a $$&lt;/p>
&lt;p>&lt;strong>Prediction ($x^T \theta_a$)&lt;/strong>&lt;br>
This is the standard Linear Regression prediction. It asks: &lt;em>&amp;ldquo;Based on historical data, what reward should we expect from this user?&amp;rdquo;&lt;/em> It is a linear estimate, not a probability. If the user matches features stored in vector $b$ (features that previously led to clicks), this term is high.&lt;/p>
&lt;p>&lt;strong>Exploration ($\alpha \sqrt{x^T A_a^{-1} x}$)&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Familiar User:&lt;/strong> If we have seen this user type many times, the matrix $A$ accumulates repeated contributions of $x x^T$. This increases the magnitude of $A$ in those feature directions. Because the exploration term depends on $x^T A^{-1} x$, a larger $A$ leads to a smaller quadratic form, shrinking the confidence bound. The model therefore relies more on exploitation.&lt;/li>
&lt;li>&lt;strong>Cold Start:&lt;/strong> If we have rarely (or never) observed this feature pattern, $A$ remains close to its initial regularized identity matrix. After inversion, these directions yield larger values of $x^T A^{-1} x$, increasing the confidence bound and encouraging exploration to reduce uncertainty.&lt;/li>
&lt;li>❗ $\alpha$ is a hyperparameter and it is set to 1.0, the value used in &lt;em>Part 1&lt;/em>.&lt;/li>
&lt;/ul>

&lt;h3 id="hybrid-source-for-warm-start" data-numberify>Hybrid Source for Warm Start&lt;a class="anchor ms-1" href="#hybrid-source-for-warm-start" aria-label="Link to this section: Hybrid Source for Warm Start">&lt;/a>&lt;/h3>
&lt;p>Contextual bandits suffer from the &amp;ldquo;Cold Start&amp;rdquo; problem. To mitigate this, we implement a &lt;strong>Hybrid Source&lt;/strong>.&lt;/p>
&lt;ol>
&lt;li>&lt;strong>File Source:&lt;/strong> Reads the historical CSV (&lt;code>training_log.csv&lt;/code>) generated in &lt;em>Part 1&lt;/em> to bootstrap the model state.&lt;/li>
&lt;li>&lt;strong>Kafka Source:&lt;/strong> Automatically switches to the live &lt;code>feedback-events&lt;/code> topic once the historical data is processed.&lt;/li>
&lt;/ol>

&lt;h3 id="custom-valkey-sink" data-numberify>Custom Valkey Sink&lt;a class="anchor ms-1" href="#custom-valkey-sink" aria-label="Link to this section: Custom Valkey Sink">&lt;/a>&lt;/h3>
&lt;p>We implement a custom Sink using the Sink V2 API and Jedis. This allows us to perform efficient &lt;code>SET&lt;/code> operations to update the model parameters in Valkey directly from the Flink stream. Because each update overwrites the full parameter vector, repeated writes remain logically safe under at-least-once delivery semantics. Besides, because the upstream &lt;code>LinUCBUpdater&lt;/code> batches the emissions, this sink receives highly aggregated model updates, preventing Valkey from being overwhelmed by write operations.&lt;/p>

&lt;h2 id="recommender-simulation-design" data-numberify>Recommender Simulation Design&lt;a class="anchor ms-1" href="#recommender-simulation-design" aria-label="Link to this section: Recommender Simulation Design">&lt;/a>&lt;/h2>
&lt;p>To validate the architecture without live user traffic, we designed a Python client (&lt;code>recommender/run/live.py&lt;/code>) that simulates the entire lifecycle of a recommendation request. This script plays two roles simultaneously: it acts as the &lt;strong>Recommendation Service&lt;/strong> (serving predictions) and the &lt;strong>User&lt;/strong> (providing feedback).&lt;/p>

&lt;h3 id="serving-logic" data-numberify>Serving Logic&lt;a class="anchor ms-1" href="#serving-logic" aria-label="Link to this section: Serving Logic">&lt;/a>&lt;/h3>
&lt;p>In a production environment, this logic would live in a high-performance API. For this simulation, the Python client:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Context Generation:&lt;/strong> Creates a synthetic user profile (Age, Gender) and derives key temporal features (e.g., &lt;em>Morning&lt;/em>, &lt;em>Weekend&lt;/em>) from a simulated timestamp to form the full context.&lt;/li>
&lt;li>&lt;strong>Model Retrieval:&lt;/strong> Fetches the latest LinUCB parameters ($A^{-1}$ and $b$) for all active products directly from Valkey.&lt;/li>
&lt;li>&lt;strong>Scoring and Ranking:&lt;/strong> Calculates the UCB score for every product, ranks them in descending order, and returns the &lt;strong>top 5 highest-scoring items&lt;/strong> as the recommendation set.&lt;/li>
&lt;/ol>

&lt;h3 id="feedback-generation" data-numberify>Feedback Generation&lt;a class="anchor ms-1" href="#feedback-generation" aria-label="Link to this section: Feedback Generation">&lt;/a>&lt;/h3>
&lt;p>To prove the model is learning, the simulation follows the same &lt;strong>&amp;ldquo;Ground Truth&amp;rdquo;&lt;/strong> logic used in &lt;em>Part 1&lt;/em>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Morning Routine:&lt;/strong> Users click &amp;ldquo;Drinks &amp;amp; Desserts&amp;rdquo; (Coffee) between 6 AM and 11 AM.&lt;/li>
&lt;li>&lt;strong>Weekend Treats:&lt;/strong> On Saturdays and Sundays, users prefer &amp;ldquo;Pizzas&amp;rdquo; or &amp;ldquo;Burgers.&amp;rdquo;&lt;/li>
&lt;li>&lt;strong>Price Sensitivity:&lt;/strong> Users under 25 avoid expensive items.&lt;/li>
&lt;/ul>
&lt;p>If any of the recommended top 5 items matches the user&amp;rsquo;s current context (e.g., showing a Latte on a Tuesday morning), the script generates a Reward (1). Otherwise, it generates no reward (0). This feedback is serialized to Avro and produced to Kafka, completing the loop.&lt;/p>

&lt;h2 id="environment-setup" data-numberify>Environment Setup&lt;a class="anchor ms-1" href="#environment-setup" aria-label="Link to this section: Environment Setup">&lt;/a>&lt;/h2>
&lt;p>We use &lt;a href="https://github.com/jaehyeon-kim/odctl" target="_blank" rel="noopener noreferrer">&lt;strong>odctl&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> to orchestrate the infrastructure (Kafka, Flink, Valkey) and Gradle to build the Kotlin application. &lt;code>odctl&lt;/code> is a CLI that launches a local open data stack from a single command, resolving the dependency, networking, and connector wiring between engines on our behalf.&lt;/p>

&lt;h3 id="prerequisites" data-numberify>Prerequisites&lt;a class="anchor ms-1" href="#prerequisites" aria-label="Link to this section: Prerequisites">&lt;/a>&lt;/h3>
&lt;p>&lt;code>odctl&lt;/code> is installed alongside the other Python dependencies in &lt;code>requirements.txt&lt;/code>, so the &lt;em>Part 1&lt;/em> environment already covers it. It needs Docker running, ideally with 8GB or more allocated.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">git clone https://github.com/jaehyeon-kim/benchtop.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">&lt;span class="nb">cd&lt;/span> benchtop/product-recommender
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">uv python install 3.11
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">uv venv --python 3.11 venv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="nb">source&lt;/span> venv/bin/activate
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">uv pip install -r requirements.txt
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Every command from here on runs from the &lt;code>product-recommender&lt;/code> folder, as in &lt;em>Part 1&lt;/em>.&lt;/p>

&lt;h3 id="build-and-launch" data-numberify>Build and Launch&lt;a class="anchor ms-1" href="#build-and-launch" aria-label="Link to this section: Build and Launch">&lt;/a>&lt;/h3>
&lt;p>We bootstrap the environment by generating training data, building the Flink JAR, and launching the cluster.&lt;/p>
&lt;p>&lt;em>❗ You do not need Kotlin or Gradle installed locally. The &lt;code>./gradlew&lt;/code> script handles all build dependencies, though it does require a &lt;strong>JDK 17&lt;/strong> toolchain.&lt;/em>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="c1"># Generate Bootstrap Data (skip if already done in Part 1)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">python -m recommender.run.prepare
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">&lt;span class="c1"># Build Flink Application (Shadow Jar)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>&lt;span class="nb">cd&lt;/span> recsys-trainer &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> ./gradlew shadowJar&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="c1"># Launch Kafka, Flink and Valkey&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">odctl up kafka-lite flink-full valkey
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Dependencies resolve themselves: &lt;code>flink-full&lt;/code> quietly pulls in PostgreSQL, SeaweedFS (S3), and the Iceberg REST catalog before starting the compute engines. Run &lt;code>odctl ps --all&lt;/code> to see everything that came up.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th style="text-align:left">Service&lt;/th>
&lt;th style="text-align:left">URL&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td style="text-align:left">Flink JobManager UI&lt;/td>
&lt;td style="text-align:left">&lt;a href="http://localhost:8082" target="_blank" rel="noopener noreferrer">http://localhost:8082&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Kafka UI&lt;/td>
&lt;td style="text-align:left">&lt;a href="http://localhost:8086" target="_blank" rel="noopener noreferrer">http://localhost:8086&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">SeaweedFS (S3) file browser&lt;/td>
&lt;td style="text-align:left">&lt;a href="http://localhost:8889" target="_blank" rel="noopener noreferrer">http://localhost:8889&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td style="text-align:left">Schema Registry (Karapace)&lt;/td>
&lt;td style="text-align:left">&lt;code>http://localhost:8081&lt;/code> (REST API, no UI)&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h3 id="submitting-the-flink-job" data-numberify>Submitting the Flink Job&lt;a class="anchor ms-1" href="#submitting-the-flink-job" aria-label="Link to this section: Submitting the Flink Job">&lt;/a>&lt;/h3>
&lt;p>&lt;code>odctl&lt;/code> provides a Flink &lt;strong>session cluster&lt;/strong> rather than running the job in application mode, so the JAR is submitted to the already-running cluster:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">./submit-job.sh
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The script does three things: it uploads &lt;code>training_log.csv&lt;/code> to SeaweedFS, copies the fat JAR into the JobManager container, and calls &lt;code>flink run -d&lt;/code>.&lt;/p>
&lt;p>The bootstrap CSV goes to object storage rather than a bind mount because Flink&amp;rsquo;s split enumerator runs on the JobManager while the readers run on the TaskManagers. With three TaskManagers, a local file path would need to exist in four separate containers.&lt;/p>

&lt;h2 id="live-recommender-simulation" data-numberify>Live Recommender Simulation&lt;a class="anchor ms-1" href="#live-recommender-simulation" aria-label="Link to this section: Live Recommender Simulation">&lt;/a>&lt;/h2>
&lt;p>Once the infrastructure is running, Flink will first process the historical events to warm up. Once the historical processing is complete, we can run the Python client to simulate live traffic.&lt;/p>
&lt;p>To visualize the system in action, open two terminals.&lt;/p>
&lt;p>&lt;strong>Terminal 1: Client&lt;/strong>&lt;/p>
&lt;p>Run the Python script. It acts as the user, receiving recommendations and sending feedback (clicks) to Kafka.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">python -m recommender.run.live
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>Terminal 2: Trainer&lt;/strong>&lt;/p>
&lt;p>Watch the Flink TaskManager logs. You will see the application reacting to the events sent to the &lt;code>feedback-events&lt;/code> topic in real-time. The job&amp;rsquo;s six tasks need only six of the 15 slots, so Flink may place them on two of the three TaskManagers. Count each one&amp;rsquo;s writes first, then follow one with a count above 0:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="k">for&lt;/span> tm in a b c&lt;span class="p">;&lt;/span> &lt;span class="k">do&lt;/span> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;taskmanager-&lt;/span>&lt;span class="nv">$tm&lt;/span>&lt;span class="s2">: &lt;/span>&lt;span class="k">$(&lt;/span>docker logs flink-taskmanager-&lt;span class="nv">$tm&lt;/span> 2&amp;gt;&lt;span class="p">&amp;amp;&lt;/span>&lt;span class="m">1&lt;/span> &lt;span class="p">|&lt;/span> grep -c &lt;span class="s1">&amp;#39;Updated model&amp;#39;&lt;/span>&lt;span class="k">)&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span> &lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">docker logs flink-taskmanager-b -f
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>Result&lt;/strong>&lt;/p>
&lt;p>You will see a series of feedback events generated by users on the left-hand side. On the right-hand side, you can see the logs confirming that model parameters are being sent to Valkey in batches.&lt;/p>
&lt;p>This confirms the closed loop: &lt;strong>Read (Valkey) -&amp;gt; Act (Kafka) -&amp;gt; Learn (Flink) -&amp;gt; Write (Valkey)&lt;/strong>.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Terminal with user recommendation lines and click results above Redis sink writer logs updating the model for each product" src="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/recommender-output.webp" loading="lazy" title="Live Simulation Result" width="1665" height="1476" />
&lt;/picture>&lt;figcaption class="figure-caption">Live Simulation Result&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;p>You can inspect feedback events on Kafka UI at &lt;a href="http://localhost:8086" target="_blank" rel="noopener noreferrer">http://localhost:8086&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Kafbat UI messages tab for the feedback-events topic showing a JSON record with product id, reward and context vector" src="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/feedback-events.png" loading="lazy" title="Feedback Events" width="1010" height="842" />
&lt;/picture>&lt;figcaption class="figure-caption">Feedback Events&lt;/figcaption>
&lt;/figure>

&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>Troubleshooting:&lt;/strong> the client defaults to &lt;code>127.0.0.1&lt;/code> rather than &lt;code>localhost&lt;/code>. On an IPv6-first host, &lt;code>localhost&lt;/code> resolves to &lt;code>::1&lt;/code> first, and the schema registry client fails outright with a connection reset instead of falling back to IPv4 the way the Kafka and Valkey clients do.&lt;/p>
&lt;/blockquote>

&lt;h2 id="teardown" data-numberify>Teardown&lt;a class="anchor ms-1" href="#teardown" aria-label="Link to this section: Teardown">&lt;/a>&lt;/h2>
&lt;p>To stop the cluster and remove resources:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">odctl down kafka-lite flink-full valkey
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Add &lt;code>--volumes&lt;/code> to also remove &lt;code>odctl-shared-deps&lt;/code>, the named volume holding the downloaded connector JARs. The next &lt;code>odctl up&lt;/code> then re-runs the init container to repopulate it.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/">Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning&lt;/a> - online machine learning on Flink and Kafka for an industrial digital twin, with drift detection and a shadow mode router&lt;/li>
&lt;/ul>

&lt;h2 id="conclusion" data-numberify>Conclusion&lt;a class="anchor ms-1" href="#conclusion" aria-label="Link to this section: Conclusion">&lt;/a>&lt;/h2>
&lt;p>Traditional recommendation systems such as Collaborative Filtering rely on long-term interaction history and often treat user preferences as static. As a result, they struggle to incorporate &lt;strong>immediate context&lt;/strong>, missing situational shifts like a user preferring &lt;strong>coffee in the morning but pizza in the evening&lt;/strong>.&lt;/p>
&lt;p>To overcome this, we use &lt;strong>Contextual Multi-Armed Bandits (CMAB)&lt;/strong>, an online learning approach that balances &lt;strong>exploitation&lt;/strong> and &lt;strong>exploration&lt;/strong> using real-time contextual signals. While our Python prototype in &lt;em>Part 1&lt;/em> validated the concept, it was not built for scale.&lt;/p>
&lt;p>We then evolved it into a production-ready &lt;strong>event-driven architecture&lt;/strong>: &lt;strong>Kafka&lt;/strong> streams feedback events, &lt;strong>Flink&lt;/strong> handles distributed stateful training, and &lt;strong>Valkey&lt;/strong> serves precomputed parameters for low-latency inference. This design enables horizontal scalability and real-time adaptation to user behavior.&lt;/p></content:encoded></item><item><title>Prototyping an Online Product Recommender in Python</title><link>https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/</link><guid>https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/</guid><pubDate>Tue, 27 Jan 2026 00:00:00 +0000</pubDate><category>Python</category><category>Contextual Bandits</category><category>Reinforcement Learning</category><category>Online Machine Learning</category><category>Recommender System</category><category>MABWiser</category><category>Mab2Rec</category><category>Benchtop</category><description>Overview Traditional recommendation approaches such as Collaborative Filtering remain widely adopted, yet they come with notable constraints. They are particularly vulnerable to the cold-start problem, where new users lack sufficient interaction history, and they depend heavily on long-term behavioral data. As a result, they frequently overlook real-time contextual signals, including time of day, device type, location, or session intent. This can prevent them from capturing situational preferences, such as someone preferring coffee in the morning but pizza in the evening.</description><content:encoded>
&lt;h2 id="overview" data-numberify>Overview&lt;a class="anchor ms-1" href="#overview" aria-label="Link to this section: Overview">&lt;/a>&lt;/h2>
&lt;p>Traditional recommendation approaches such as &lt;a href="https://en.wikipedia.org/wiki/Collaborative_filtering" target="_blank" rel="noopener noreferrer">Collaborative Filtering&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> remain widely adopted, yet they come with notable constraints. They are particularly vulnerable to the &lt;strong>cold-start problem&lt;/strong>, where new users lack sufficient interaction history, and they depend heavily on long-term behavioral data. As a result, they frequently overlook &lt;strong>real-time contextual signals&lt;/strong>, including time of day, device type, location, or session intent. This can prevent them from capturing situational preferences, such as someone preferring &lt;strong>coffee in the morning but pizza in the evening&lt;/strong>.&lt;/p>
&lt;p>&lt;a href="https://en.wikipedia.org/wiki/Multi-armed_bandit#Contextual_bandit" target="_blank" rel="noopener noreferrer">&lt;strong>Contextual Multi-Armed Bandits (CMAB)&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> address these gaps through &lt;strong>online learning&lt;/strong>.&lt;/p>
&lt;p>As a practical form of &lt;a href="https://en.wikipedia.org/wiki/Reinforcement_learning" target="_blank" rel="noopener noreferrer">reinforcement learning&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>, CMAB balances two goals in real time:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Exploitation:&lt;/strong> Recommending what is known to work.&lt;/li>
&lt;li>&lt;strong>Exploration:&lt;/strong> Trying less-tested options to discover new favorites.&lt;/li>
&lt;/ol>
&lt;p>By conditioning decisions on live context, CMAB adapts instantly to changing user behavior.&lt;/p>

&lt;h3 id="why-cmab" data-numberify>Why CMAB?&lt;a class="anchor ms-1" href="#why-cmab" aria-label="Link to this section: Why CMAB?">&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Beyond A/B Testing:&lt;/strong> Instead of finding a single global winner, CMAB enables &lt;strong>1:1 personalization&lt;/strong>, selecting the best option for this user in this context.&lt;/li>
&lt;li>&lt;strong>Real-Time Adaptation:&lt;/strong> Unlike batch-trained models that quickly become stale, CMAB updates incrementally, making it ideal for news/products recommendation, dynamic pricing, or inventory-aware ranking.&lt;/li>
&lt;/ul>
&lt;p>Several CMAB implementations exist, including &lt;a href="https://vowpalwabbit.org/" target="_blank" rel="noopener noreferrer">&lt;strong>Vowpal Wabbit&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and &lt;a href="https://riverml.xyz/latest/" target="_blank" rel="noopener noreferrer">&lt;strong>River ML&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. In this post, we use &lt;a href="https://github.com/fidelity/mab2rec" target="_blank" rel="noopener noreferrer">&lt;strong>Mab2Rec&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> for offline policy evaluation and &lt;a href="https://github.com/fidelity/mabwiser" target="_blank" rel="noopener noreferrer">&lt;strong>MABWiser&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> to build the product recommender prototype.&lt;/p>

&lt;h3 id="data-streaming-opportunity" data-numberify>Data Streaming Opportunity&lt;a class="anchor ms-1" href="#data-streaming-opportunity" aria-label="Link to this section: Data Streaming Opportunity">&lt;/a>&lt;/h3>
&lt;p>CMAB performs well in &lt;strong>data streaming environments&lt;/strong>. Integrated with platforms like &lt;strong>Kafka&lt;/strong> and &lt;strong>Flink&lt;/strong>, it learns directly from event streams, creating a feedback loop that responds to trends and shifts in user intent in sub-seconds.&lt;/p>
&lt;p>In this series, &lt;strong>Part 1&lt;/strong> (&lt;em>this post&lt;/em>) builds a complete &lt;strong>Python prototype&lt;/strong> to validate the algorithm and simulate user behavior. &lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">&lt;strong>Part 2&lt;/strong>&lt;/a> will scale this to a distributed, event-driven architecture.&lt;/p>
&lt;p>&lt;figure class="figure mx-auto d-block">&lt;picture>&lt;img class="img-fluid figure-img" alt="Data generation, feature engineering and history simulation build the history; the offline policy evaluation selects LinUCB, which the local simulation trains on the history and then runs" src="https://jaehyeon.me/blog/2026-01-29-prototype-recommender-with-python/part-1.png" loading="lazy" title="Architecture" width="1685" height="614" />
&lt;/picture>&lt;figcaption class="figure-caption">Architecture&lt;/figcaption>
&lt;/figure>

&lt;/p>

&lt;h2 id="tech-stack" data-numberify>Tech Stack&lt;a class="anchor ms-1" href="#tech-stack" aria-label="Link to this section: Tech Stack">&lt;/a>&lt;/h2>
&lt;p>We are building this prototype using &lt;strong>Python 3.11&lt;/strong>.&lt;/p>
&lt;blockquote>
&lt;p>&lt;strong>Engineering Note:&lt;/strong> We explicitly chose Python 3.11 because parts of our stack (specifically &lt;code>mabwiser&lt;/code> dependencies) rely on older versions of &lt;code>pandas&lt;/code> (&amp;lt; 2.0). On Python 3.12+, installing these dependencies often triggers long compilation times or failures due to missing binary wheels.&lt;/p>
&lt;p>For the same reason, &lt;code>requirements.txt&lt;/code> floors &lt;code>flair&lt;/code> at 0.12. &lt;code>TextWiser&lt;/code> only asks for &lt;code>flair&amp;gt;=0.9&lt;/code>, and with &lt;code>pandas&lt;/code>/&lt;code>numpy&lt;/code> held below 2.0 the resolver otherwise backtracks to &lt;code>flair&lt;/code> 0.11.3, which hard-pins &lt;code>sentencepiece==0.1.95&lt;/code>. That release predates Python 3.11 and has no matching wheel, so the install dies trying to build it from source.&lt;/p>
&lt;/blockquote>
&lt;p>We use &lt;a href="https://docs.astral.sh/uv/" target="_blank" rel="noopener noreferrer">&lt;strong>uv&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> for Python environment management. The core libraries include:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://github.com/fidelity/mabwiser" target="_blank" rel="noopener noreferrer">&lt;strong>MABWiser:&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> The engine. It implements the core Contextual Bandit algorithms.&lt;/li>
&lt;li>&lt;a href="https://github.com/fidelity/mab2rec" target="_blank" rel="noopener noreferrer">&lt;strong>Mab2Rec:&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> The vehicle. A high-level wrapper that streamlines Recommender System pipelines.&lt;/li>
&lt;li>&lt;a href="https://github.com/fidelity/textwiser" target="_blank" rel="noopener noreferrer">&lt;strong>TextWiser:&lt;/strong>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> For converting raw text features into numerical embeddings.&lt;/li>
&lt;li>&lt;strong>scikit-learn:&lt;/strong> For feature scaling and encoding.&lt;/li>
&lt;li>&lt;strong>Faker &amp;amp; Pandas:&lt;/strong> For synthetic data generation and simulation.&lt;/li>
&lt;/ul>
&lt;p>The development environment can be constructed as follows:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">$ git clone https://github.com/jaehyeon-kim/benchtop.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">$ &lt;span class="nb">cd&lt;/span> benchtop/product-recommender
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">$ uv python install 3.11
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">$ uv venv --python 3.11 venv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">$ &lt;span class="nb">source&lt;/span> venv/bin/activate
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>venv&lt;span class="o">)&lt;/span> $ uv pip install -r requirements.txt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>venv&lt;span class="o">)&lt;/span> $ uv pip list &lt;span class="p">|&lt;/span> grep -E &lt;span class="s2">&amp;#34;mab|wiser|flair|panda|numpy|scikit|faker&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="c1"># Using Python 3.11.15 environment at: venv&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="c1"># faker 40.36.0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="c1"># flair 0.15.1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="c1"># mab2rec 1.3.1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="c1"># mabwiser 2.7.4&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="c1"># numpy 1.26.4&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="c1"># pandas 1.5.3&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="c1"># scikit-learn 1.9.0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="c1"># textwiser 2.0.3&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>&lt;strong>📂 Source Code for the Post&lt;/strong>&lt;/p>
&lt;p>The source code for this post is available in the &lt;strong>product-recommender&lt;/strong> folder of the &lt;a href="https://github.com/jaehyeon-kim/benchtop" target="_blank" rel="noopener noreferrer">benchtop&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> GitHub repository. It is one of the &lt;a href="https://jaehyeon.me/blog/2026-09-30-introducing-benchtop/">Benchtop&lt;/a> projects, which run locally from a fresh clone.&lt;/p>
&lt;/blockquote>

&lt;h2 id="data-generation" data-numberify>Data Generation&lt;a class="anchor ms-1" href="#data-generation" aria-label="Link to this section: Data Generation">&lt;/a>&lt;/h2>
&lt;p>We first need product and user data to generate the required features.&lt;/p>

&lt;h3 id="products" data-numberify>Products&lt;a class="anchor ms-1" href="#products" aria-label="Link to this section: Products">&lt;/a>&lt;/h3>
&lt;p>We utilize a set of &lt;strong>200 raw products&lt;/strong>, each containing a product ID, name, text description, price, and high-level category.&lt;/p>
&lt;p>Here is a list of sample products:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>product_id&lt;/th>
&lt;th>name&lt;/th>
&lt;th>description&lt;/th>
&lt;th>price&lt;/th>
&lt;th>category&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>8&lt;/td>
&lt;td>The Aussie Burger&lt;/td>
&lt;td>A true classic with beetroot, a fried egg, pineapple, bacon, cheese, lettuce, and tomato.&lt;/td>
&lt;td>16.99&lt;/td>
&lt;td>Burgers &amp;amp; Sandwiches&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>42&lt;/td>
&lt;td>The Aussie Pizza&lt;/td>
&lt;td>Tomato base topped with ham, bacon, onions, and a cracked egg in the center.&lt;/td>
&lt;td>23.99&lt;/td>
&lt;td>Pizzas&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>61&lt;/td>
&lt;td>Chicken Parma&lt;/td>
&lt;td>Classic crumbed chicken breast topped with napoli, ham, and cheese. Served with chips &amp;amp; salad.&lt;/td>
&lt;td>24.99&lt;/td>
&lt;td>Aussie Pub Classics&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>101&lt;/td>
&lt;td>Fish Tacos (Baja Style)&lt;/td>
&lt;td>Three tortillas with battered fish, cabbage, and creamy sauce.&lt;/td>
&lt;td>12.95&lt;/td>
&lt;td>Mexican Specialties&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h3 id="users" data-numberify>Users&lt;a class="anchor ms-1" href="#users" aria-label="Link to this section: Users">&lt;/a>&lt;/h3>
&lt;p>We generate &lt;strong>1,000 Synthetic Users&lt;/strong> using &lt;code>Faker&lt;/code>. Each user is assigned static attributes like Age, Gender, Location, and Traffic Source. These attributes will serve as the &amp;ldquo;Context&amp;rdquo; for our Bandit.&lt;/p>
&lt;p>Here is a sample of our user base:&lt;/p>
&lt;p>&lt;em>Note that street address, postal code, city, state, and country are omitted, as only latitude and longitude are used for feature generation.&lt;/em>&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>user_id&lt;/th>
&lt;th>first_name&lt;/th>
&lt;th>last_name&lt;/th>
&lt;th>email&lt;/th>
&lt;th>&amp;hellip;&lt;/th>
&lt;th>age&lt;/th>
&lt;th>gender&lt;/th>
&lt;th>latitude&lt;/th>
&lt;th>longitude&lt;/th>
&lt;th>traffic_source&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>Stephen&lt;/td>
&lt;td>Parker&lt;/td>
&lt;td>&lt;a href="mailto:stephen.parker@example.net">stephen.parker@example.net&lt;/a>&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>38&lt;/td>
&lt;td>M&lt;/td>
&lt;td>-37.78525508&lt;/td>
&lt;td>144.94969&lt;/td>
&lt;td>Search&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>Brianna&lt;/td>
&lt;td>Williams&lt;/td>
&lt;td>&lt;a href="mailto:brianna.williams@example.net">brianna.williams@example.net&lt;/a>&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>60&lt;/td>
&lt;td>F&lt;/td>
&lt;td>-37.82290733&lt;/td>
&lt;td>145.0040437&lt;/td>
&lt;td>Search&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>Carlos&lt;/td>
&lt;td>Hunt&lt;/td>
&lt;td>&lt;a href="mailto:carlos.hunt@example.com">carlos.hunt@example.com&lt;/a>&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>46&lt;/td>
&lt;td>M&lt;/td>
&lt;td>-37.74295704&lt;/td>
&lt;td>144.8004261&lt;/td>
&lt;td>Search&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>Charles&lt;/td>
&lt;td>Martin&lt;/td>
&lt;td>&lt;a href="mailto:charles.martin@example.com">charles.martin@example.com&lt;/a>&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>41&lt;/td>
&lt;td>M&lt;/td>
&lt;td>-37.80480003&lt;/td>
&lt;td>145.1229819&lt;/td>
&lt;td>Organic&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h2 id="feature-engineering" data-numberify>Feature Engineering&lt;a class="anchor ms-1" href="#feature-engineering" aria-label="Link to this section: Feature Engineering">&lt;/a>&lt;/h2>
&lt;p>Bandit algorithms operate on numerical vectors, not raw text. In other words, they cannot interpret &lt;code>&amp;quot;Burger&amp;quot;&lt;/code> unless it is converted into numbers. To address this, we developed a transformation pipeline to properly prepare our data:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Product Features:&lt;/strong> We used &lt;code>TextWiser&lt;/code> to convert raw product descriptions into vector embeddings. This allows the model to understand that &amp;ldquo;Burger&amp;rdquo; and &amp;ldquo;Sandwich&amp;rdquo; are semantically closer than &amp;ldquo;Burger&amp;rdquo; and &amp;ldquo;Headphones&amp;rdquo;. We also applied One-Hot Encoding to categories (&lt;em>Product Category&lt;/em>) and MinMax scaling to the &lt;em>price&lt;/em>. Finally, we added a binary feature, &lt;code>is_coffee&lt;/code>, which is set to 1 for coffee products (e.g., espresso, cappuccino) and 0 otherwise.&lt;/li>
&lt;li>&lt;strong>User Features:&lt;/strong> Similar to the product features, we applied One-Hot Encoding to categories (&lt;em>Gender&lt;/em> and &lt;em>Traffic Source&lt;/em>) and MinMax scaling to numerical fields (&lt;em>Age&lt;/em>, &lt;em>Latitude&lt;/em>, and &lt;em>Longitude&lt;/em>).&lt;/li>
&lt;li>&lt;strong>Pipeline Artifacts:&lt;/strong> We save these transformers as &lt;code>preprocessing_artifacts.pkl&lt;/code>. This allows our system to instantly transform any new user/product record into a compatible feature vector during inference.&lt;/li>
&lt;/ol>
&lt;p>&lt;strong>Sample Processed Product Features:&lt;/strong>&lt;/p>
&lt;p>&lt;em>Notice how the description is now represented by &lt;code>txt_0&lt;/code>&amp;hellip;&lt;code>txt_9&lt;/code> embeddings.&lt;/em>&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>product_id&lt;/th>
&lt;th>txt_0&lt;/th>
&lt;th>txt_1&lt;/th>
&lt;th>txt_2&lt;/th>
&lt;th>txt_3&lt;/th>
&lt;th>txt_4&lt;/th>
&lt;th>txt_5&lt;/th>
&lt;th>txt_6&lt;/th>
&lt;th>txt_7&lt;/th>
&lt;th>txt_8&lt;/th>
&lt;th>txt_9&lt;/th>
&lt;th>cat_Appetizers &amp;amp; Sides&lt;/th>
&lt;th>cat_Aussie Pub Classics&lt;/th>
&lt;th>cat_Burgers &amp;amp; Sandwiches&lt;/th>
&lt;th>cat_Drinks &amp;amp; Desserts&lt;/th>
&lt;th>cat_Mexican Specialties&lt;/th>
&lt;th>cat_Pasta &amp;amp; Risotto&lt;/th>
&lt;th>cat_Pizzas&lt;/th>
&lt;th>cat_Salads &amp;amp; Healthy Options&lt;/th>
&lt;th>is_coffee&lt;/th>
&lt;th>price&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>8&lt;/td>
&lt;td>0.33545566&lt;/td>
&lt;td>-0.36038476&lt;/td>
&lt;td>0.04443859&lt;/td>
&lt;td>0.1437323&lt;/td>
&lt;td>0.19954793&lt;/td>
&lt;td>-0.17493221&lt;/td>
&lt;td>0.18742238&lt;/td>
&lt;td>0.027762778&lt;/td>
&lt;td>-0.07173552&lt;/td>
&lt;td>-0.11753924&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0.3887&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>42&lt;/td>
&lt;td>0.30156243&lt;/td>
&lt;td>-0.28032556&lt;/td>
&lt;td>-0.030351626&lt;/td>
&lt;td>0.21286383&lt;/td>
&lt;td>-0.04239631&lt;/td>
&lt;td>-0.05454497&lt;/td>
&lt;td>0.10349816&lt;/td>
&lt;td>0.13551421&lt;/td>
&lt;td>-0.045043208&lt;/td>
&lt;td>-0.22818978&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0.5832&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>61&lt;/td>
&lt;td>0.53952336&lt;/td>
&lt;td>0.020042256&lt;/td>
&lt;td>0.36858454&lt;/td>
&lt;td>-0.10636381&lt;/td>
&lt;td>-0.0025809025&lt;/td>
&lt;td>0.15991136&lt;/td>
&lt;td>-0.041545305&lt;/td>
&lt;td>-0.11347411&lt;/td>
&lt;td>-0.024828108&lt;/td>
&lt;td>-0.23473465&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0.6110&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>101&lt;/td>
&lt;td>0.20631199&lt;/td>
&lt;td>0.04121668&lt;/td>
&lt;td>-0.11134566&lt;/td>
&lt;td>-0.21601778&lt;/td>
&lt;td>-0.0050910884&lt;/td>
&lt;td>-0.20131046&lt;/td>
&lt;td>-0.054801166&lt;/td>
&lt;td>0.19731943&lt;/td>
&lt;td>0.35357264&lt;/td>
&lt;td>0.2399257&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0.2765&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>&lt;strong>Sample Processed User Features:&lt;/strong>&lt;/p>
&lt;p>&lt;em>Notice that Age, Latitude and Longitude are normalized between 0 and 1, and categorical fields are binary.&lt;/em>&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>user_id&lt;/th>
&lt;th>age&lt;/th>
&lt;th>latitude&lt;/th>
&lt;th>longitude&lt;/th>
&lt;th>gender_F&lt;/th>
&lt;th>gender_M&lt;/th>
&lt;th>traffic_source_Display&lt;/th>
&lt;th>traffic_source_Email&lt;/th>
&lt;th>traffic_source_Facebook&lt;/th>
&lt;th>traffic_source_Organic&lt;/th>
&lt;th>traffic_source_Search&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>0.4074074&lt;/td>
&lt;td>0.82048548&lt;/td>
&lt;td>0.32804966&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>0.8148148&lt;/td>
&lt;td>0.76928412&lt;/td>
&lt;td>0.41833646&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>0.5555556&lt;/td>
&lt;td>0.87800441&lt;/td>
&lt;td>0.08010776&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>0.4629630&lt;/td>
&lt;td>0.79390730&lt;/td>
&lt;td>0.61590441&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>

&lt;h2 id="bandit-history-simulation" data-numberify>Bandit History Simulation&lt;a class="anchor ms-1" href="#bandit-history-simulation" aria-label="Link to this section: Bandit History Simulation">&lt;/a>&lt;/h2>
&lt;p>To evaluate whether our model can &lt;strong>truly learn user behavior&lt;/strong>, we need a controlled &lt;strong>Ground Truth&lt;/strong>, which is an &lt;em>Oracle&lt;/em> that determines the likelihood of a simulated user clicking on a recommendation.&lt;/p>
&lt;p>Crucially, &lt;strong>this Oracle is hidden from the model&lt;/strong>. The model&amp;rsquo;s task is to infer these patterns purely from trial and error.&lt;/p>
&lt;p>We also inject &lt;strong>Dynamic Context&lt;/strong> features like &lt;strong>Time of Day&lt;/strong> and &lt;strong>Day of Week&lt;/strong> into the user profile at the moment of interaction. These temporal signals create realistic, fluctuating patterns that the model must adapt to.&lt;/p>

&lt;h3 id="simulation-logic" data-numberify>Simulation Logic&lt;a class="anchor ms-1" href="#simulation-logic" aria-label="Link to this section: Simulation Logic">&lt;/a>&lt;/h3>
&lt;p>The simulation is implemented as two functions, &lt;code>click_probability&lt;/code> and &lt;code>will_click&lt;/code>, and we define specific rules that govern user behaviour:&lt;/p>
&lt;ul>
&lt;li>Start from a low base logit (−2.5) to model generally low click probability.&lt;/li>
&lt;li>&lt;strong>Rule 1: Morning coffee preference:&lt;/strong> if the user is browsing in the morning and the item is a &lt;em>coffee&lt;/em> product, add a strong positive boost to the score.&lt;/li>
&lt;li>&lt;strong>Rule 2: Weekend comfort food:&lt;/strong> if the session is on a weekend and the item is &lt;em>Pizza&lt;/em> or &lt;em>Burgers &amp;amp; Sandwiches&lt;/em>, add a moderate positive boost.&lt;/li>
&lt;li>&lt;strong>Rule 3: Budget sensitivity:&lt;/strong> if the user is young (normalized age &amp;lt; 0.25) and the item is expensive (normalized price &amp;gt; 0.8), apply a strong negative penalty.&lt;/li>
&lt;li>&lt;strong>Rule 4: Traffic source bias:&lt;/strong> if the user arrived via Search, add a small intent-based boost.&lt;/li>
&lt;li>Convert the final logit score into a click probability using a sigmoid function, then sample a Bernoulli trial to simulate whether a click occurs.&lt;/li>
&lt;/ul>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="c1"># product-recommender/recommender/engine/simulation.py&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">click_probability&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">user_ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">dict&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">dict&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="nb">float&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="s2"> Returns the chance that a user clicks an item: the hidden formula the bandit learns.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="s2"> Args:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="s2"> user_ctx (dict): The user and time features.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="s2"> item_ctx (dict): The product features.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="s2"> Returns:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="s2"> float: The click probability, from 0 to 1.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="o">-&lt;/span>&lt;span class="mf">2.5&lt;/span> &lt;span class="c1"># the base logit, a low probability&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Rule 1: Morning Coffee&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">user_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;is_morning&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;is_coffee&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mf">2.5&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Rule 2: Weekend Comfort Food&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">user_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;is_weekend&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl"> &lt;span class="n">item_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;cat_Pizzas&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span> &lt;span class="ow">or&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;cat_Burgers &amp;amp; Sandwiches&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl"> &lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mf">1.8&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Rule 3: Budget Constraint (age and price are scaled from 0 to 1)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">user_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;age&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">0.5&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="mf">0.25&lt;/span> &lt;span class="ow">and&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;price&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="mf">0.5&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">&amp;gt;&lt;/span> &lt;span class="mf">0.8&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span> &lt;span class="o">-=&lt;/span> &lt;span class="mf">3.0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl"> &lt;span class="c1"># Rule 4: Traffic Bias&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl"> &lt;span class="k">if&lt;/span> &lt;span class="n">user_ctx&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;traffic_source_Search&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="mi">1&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl"> &lt;span class="n">score&lt;/span> &lt;span class="o">+=&lt;/span> &lt;span class="mf">0.5&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">33&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="mi">1&lt;/span> &lt;span class="o">/&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="mi">1&lt;/span> &lt;span class="o">+&lt;/span> &lt;span class="n">np&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">exp&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="o">-&lt;/span>&lt;span class="n">score&lt;/span>&lt;span class="p">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">34&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">35&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">36&lt;/span>&lt;span class="cl">&lt;span class="k">def&lt;/span> &lt;span class="nf">will_click&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">user_ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">dict&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="nb">dict&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">fake&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="n">Faker&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="o">-&amp;gt;&lt;/span> &lt;span class="nb">int&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">37&lt;/span>&lt;span class="cl"> &lt;span class="s2">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">38&lt;/span>&lt;span class="cl">&lt;span class="s2"> Decides whether a user clicks an item, with the formula&amp;#39;s probability.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">39&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">40&lt;/span>&lt;span class="cl">&lt;span class="s2"> Args:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">41&lt;/span>&lt;span class="cl">&lt;span class="s2"> user_ctx (dict): The user and time features.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">42&lt;/span>&lt;span class="cl">&lt;span class="s2"> item_ctx (dict): The product features.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">43&lt;/span>&lt;span class="cl">&lt;span class="s2"> fake (Faker): The seeded Faker, whose random generator decides.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">44&lt;/span>&lt;span class="cl">&lt;span class="s2">
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">45&lt;/span>&lt;span class="cl">&lt;span class="s2"> Returns:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">46&lt;/span>&lt;span class="cl">&lt;span class="s2"> int: 1 for a click, 0 for none.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">47&lt;/span>&lt;span class="cl">&lt;span class="s2"> &amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">48&lt;/span>&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="mi">1&lt;/span> &lt;span class="k">if&lt;/span> &lt;span class="n">fake&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">random&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">random&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">&amp;lt;&lt;/span> &lt;span class="n">click_probability&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">user_ctx&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">item_ctx&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="k">else&lt;/span> &lt;span class="mi">0&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="data-preparation" data-numberify>Data Preparation&lt;a class="anchor ms-1" href="#data-preparation" aria-label="Link to this section: Data Preparation">&lt;/a>&lt;/h3>
&lt;p>We generate 10,000 historical events to serve as our &amp;ldquo;Offline Training&amp;rdquo; dataset. This process involves picking a random user and a random product, then asking the Oracle &amp;ldquo;Did they click?&amp;rdquo;.&lt;/p>
&lt;p>Because the user and product are matched randomly (not by a recommender), the &lt;strong>Average Click Rate (CTR)&lt;/strong> is naturally low. In this example, it is around &lt;strong>13.65%&lt;/strong>, and this serves as our baseline.&lt;/p>
&lt;p>💡 There are three main scripts for this post: &lt;code>recommender.run.prepare&lt;/code> for feature engineering and bandit history simulation, &lt;code>recommender.run.evaluate&lt;/code> for offline policy evaluation, and &lt;code>recommender.run.local&lt;/code> for running product recommendation locally. Each script accepts a &lt;code>--seed&lt;/code> argument, which defaults to &lt;em>1237&lt;/em>. As long as the seed remains the same, running the scripts will produce identical outputs.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>venv&lt;span class="o">)&lt;/span> $ python -m recommender.run.prepare
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:09&lt;span class="o">]&lt;/span> INFO : Generating &lt;span class="m">1000&lt;/span> synthetic users...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:09&lt;span class="o">]&lt;/span> INFO : Saved raw users to: .../users.csv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:09&lt;span class="o">]&lt;/span> INFO : Starting Feature Engineering...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:09&lt;span class="o">]&lt;/span> INFO : Saved User Features: &lt;span class="o">(&lt;/span>1000, 11&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Saved Product Features: &lt;span class="o">(&lt;/span>200, 21&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Saved Pipeline Artifacts to: .../preprocessing_artifacts.pkl
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Loaded &lt;span class="m">1000&lt;/span> users and &lt;span class="m">200&lt;/span> products.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Generating &lt;span class="m">10000&lt;/span> events...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Done. Saved Training Log to .../training_log.csv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Avg Click Rate: 13.65%
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-01-26 19:16:10&lt;span class="o">]&lt;/span> INFO : Data Preparation Complete.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The main dataset (&lt;code>training_log.csv&lt;/code>) combines &lt;em>user features&lt;/em>, &lt;em>dynamic context&lt;/em> (e.g., &lt;code>is_morning&lt;/code>), &lt;em>product ID&lt;/em>, and the &lt;em>interaction result&lt;/em> (&lt;code>response&lt;/code>):&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>event_id&lt;/th>
&lt;th>age&lt;/th>
&lt;th>&amp;hellip;&lt;/th>
&lt;th>traffic_source_Search&lt;/th>
&lt;th>is_morning&lt;/th>
&lt;th>is_weekend&lt;/th>
&lt;th>is_weekday&lt;/th>
&lt;th>product_id&lt;/th>
&lt;th>response&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>0.5925926&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>1&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>182&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>0.6111111&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>101&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>0.6296296&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>1&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>34&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>0.4814815&lt;/td>
&lt;td>&amp;hellip;&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>0&lt;/td>
&lt;td>1&lt;/td>
&lt;td>101&lt;/td>
&lt;td>0&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>The user&amp;rsquo;s 10 features and the 5 time flags, 15 numbers in all, are the &lt;strong>context vector&lt;/strong> $x$ that the bandit sees for a visit. The product features are not part of it: the bandit keeps a separate model for each product instead, and only the Oracle reads the product features.&lt;/p>

&lt;h2 id="offline-policy-evaluation" data-numberify>Offline Policy Evaluation&lt;a class="anchor ms-1" href="#offline-policy-evaluation" aria-label="Link to this section: Offline Policy Evaluation">&lt;/a>&lt;/h2>
&lt;p>We benchmarked several policies using &lt;code>Mab2Rec&lt;/code> on the 10,000 historical events.&lt;/p>

&lt;h3 id="candidate-policies" data-numberify>Candidate Policies&lt;a class="anchor ms-1" href="#candidate-policies" aria-label="Link to this section: Candidate Policies">&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Random:&lt;/strong> The baseline. Recommends items blindly.&lt;/li>
&lt;li>&lt;strong>Popularity:&lt;/strong> Recommends items drawn at random, each weighted by its &lt;em>global&lt;/em> click rate.
&lt;ul>
&lt;li>&lt;em>Result:&lt;/em> Mediocre (AUC ~0.59). While better than random, it still fails to capture specific rules, such as &amp;ldquo;Morning Coffee&amp;rdquo; vs. &amp;ldquo;Weekend Pizza.&amp;rdquo;&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>LinGreedy:&lt;/strong> Disjoint Linear Regression with $\epsilon$-greedy exploration.&lt;/li>
&lt;li>&lt;strong>LinUCB (The Winner):&lt;/strong> a separate (disjoint) linear regression for each product, with an &lt;strong>Upper Confidence Bound&lt;/strong>.&lt;/li>
&lt;li>&lt;strong>LinTS (Thompson Sampling):&lt;/strong> Bayesian regression that samples from a probability distribution.&lt;/li>
&lt;/ul>

&lt;h3 id="winner-linucb" data-numberify>Winner: LinUCB&lt;a class="anchor ms-1" href="#winner-linucb" aria-label="Link to this section: Winner: LinUCB">&lt;/a>&lt;/h3>
&lt;p>&lt;strong>LinGreedy&lt;/strong> achieved the highest ranking accuracy (AUC ~0.88). Its click rate (CTR ~11%) rests on only about 50 test visits, so it is close to LinUCB rather than behind it.&lt;/p>
&lt;p>&lt;strong>LinUCB&lt;/strong> is the practical winner. It ranks almost as well (&lt;strong>AUC ~0.86&lt;/strong>), and its picks are clicked about twice as often as random picks (&lt;strong>CTR ~20.5%&lt;/strong> against 10.2%).&lt;/p>
&lt;p>This algorithm excels because it balances two competing goals:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Exploitation:&lt;/strong> It uses the predicted reward, $x^T \theta$, to find good items. $\theta = A^{-1} b$ holds the weights the model has learned, where $A$ records the contexts a product was shown in and $b$ the contexts it was clicked in.&lt;/li>
&lt;li>&lt;strong>Exploration:&lt;/strong> It adds an upper confidence bound, $\alpha \sqrt{x^T A^{-1} x}$, to the score. If the model is uncertain about a specific context (e.g., &amp;ldquo;I haven&amp;rsquo;t seen a user drink Coffee at 8 PM before&amp;rdquo;), the bound grows, boosting the score and forcing the model to test that hypothesis. $\alpha$ is a hyperparameter that sets how much exploration counts; it is 1.0 here.&lt;/li>
&lt;/ol>
&lt;p>So LinUCB tries the products it knows least about, while LinGreedy explores by picking at random on one visit in ten.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>venv&lt;span class="o">)&lt;/span> $ python -m recommender.run.evaluate
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">Running Benchmark... &lt;span class="o">(&lt;/span>This trains and scores all models automatically&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">--------------------------------------------------------------------------------
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">Available Metrics: &lt;span class="o">[&lt;/span>&lt;span class="s1">&amp;#39;AUC(score)@5&amp;#39;&lt;/span>, &lt;span class="s1">&amp;#39;CTR(score)@5&amp;#39;&lt;/span>, &lt;span class="s1">&amp;#39;Precision@5&amp;#39;&lt;/span>, &lt;span class="s1">&amp;#39;Recall@5&amp;#39;&lt;/span>&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl"> AUC&lt;span class="o">(&lt;/span>score&lt;span class="o">)&lt;/span>@5 CTR&lt;span class="o">(&lt;/span>score&lt;span class="o">)&lt;/span>@5 Precision@5 Recall@5
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">Random 0.550000 0.102041 0.003876 0.019380
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">Popularity 0.592857 0.192308 0.007752 0.038760
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">LinGreedy 0.885185 0.117647 0.004651 0.023256
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">LinUCB 0.860317 0.204545 0.006977 0.034884
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">LinTS 0.640798 0.211538 0.008527 0.042636
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">ClustersTS 0.550505 0.153846 0.004651 0.023256
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">--------------------------------------------------------------------------------
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="how-the-click-rate-is-measured" data-numberify>How the Click Rate Is Measured&lt;a class="anchor ms-1" href="#how-the-click-rate-is-measured" aria-label="Link to this section: How the Click Rate Is Measured">&lt;/a>&lt;/h3>
&lt;p>The history records only the one product each visit was shown. If a policy would have shown a different product, what the user would have done is unknown. So the benchmark uses the &lt;strong>replay&lt;/strong> method from &lt;a href="https://arxiv.org/abs/1003.5956" target="_blank" rel="noopener noreferrer">Li et al. (2011)&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a>. For each test visit it asks the policy for its five recommendations. If the logged product is among them, the visit is kept and its click counted. Otherwise the visit is skipped. Jurity calls this &lt;strong>matching&lt;/strong>, and keeping the visits that pass a test while discarding the rest is a form of &lt;strong>rejection sampling&lt;/strong>.&lt;/p>
&lt;p>The estimate is fair only because the history picked each product uniformly at random. Every product then had the same chance of being logged, so the kept visits are a fair sample of the visits on which the policy&amp;rsquo;s choice was shown. The chance is 5 in 200, or 1 in 40, whatever the policy.&lt;/p>
&lt;p>Four of LinUCB&amp;rsquo;s test visits:&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Visit&lt;/th>
&lt;th>Time&lt;/th>
&lt;th>Logged product&lt;/th>
&lt;th>Clicked&lt;/th>
&lt;th>Among LinUCB&amp;rsquo;s five&lt;/th>
&lt;th>Result&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>8221&lt;/td>
&lt;td>weekend evening&lt;/td>
&lt;td>Buffalo Chicken Pizza&lt;/td>
&lt;td>yes&lt;/td>
&lt;td>yes&lt;/td>
&lt;td>kept, 1 click&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>8231&lt;/td>
&lt;td>weekday morning&lt;/td>
&lt;td>Grilled Chicken Sandwich&lt;/td>
&lt;td>no&lt;/td>
&lt;td>yes&lt;/td>
&lt;td>kept, 0 clicks&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>8003&lt;/td>
&lt;td>weekend evening&lt;/td>
&lt;td>Dim Sims&lt;/td>
&lt;td>yes&lt;/td>
&lt;td>no&lt;/td>
&lt;td>skipped&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>8024&lt;/td>
&lt;td>weekday morning&lt;/td>
&lt;td>Strawberry Milkshake&lt;/td>
&lt;td>yes&lt;/td>
&lt;td>no&lt;/td>
&lt;td>skipped&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>On weekend evenings LinUCB&amp;rsquo;s five are mostly pizzas, and on weekday mornings they include coffees, as the Oracle rewards. Visit 8003 was clicked, but it does not count, because LinUCB would not have shown Dim Sims. Across the 2,000 test visits, 44 are kept and 9 of those were clicked, so LinUCB&amp;rsquo;s &lt;code>CTR(score)@5&lt;/code> is 9 / 44 = 20.5%.&lt;/p>
&lt;p>So the 20.5% is the click rate of LinUCB&amp;rsquo;s own choices, measured on the visits where the random history happened to show one of them. The other policies&amp;rsquo; click rates rest on 39 to 52 kept visits each. With so few, a difference of a few points can come from chance, and a longer history would make the comparison more certain.&lt;/p>

&lt;h2 id="simulation-of-the-selected-product-recommender-locally" data-numberify>Simulation of the Selected Product Recommender Locally&lt;a class="anchor ms-1" href="#simulation-of-the-selected-product-recommender-locally" aria-label="Link to this section: Simulation of the Selected Product Recommender Locally">&lt;/a>&lt;/h2>
&lt;p>With the model selected, we built a script to simulate the product recommender locally. This script acts as the Server, the User, and the Trainer simultaneously in a continuous loop.&lt;/p>

&lt;h3 id="step-1-pre-training-offline-replay" data-numberify>Step 1: Pre-training (Offline Replay)&lt;a class="anchor ms-1" href="#step-1-pre-training-offline-replay" aria-label="Link to this section: Step 1: Pre-training (Offline Replay)">&lt;/a>&lt;/h3>
&lt;p>We don&amp;rsquo;t want to start with a &amp;ldquo;dumb&amp;rdquo; model. We load the 10,000 historical events (&lt;code>training_log.csv&lt;/code>) and run &lt;code>model.fit()&lt;/code>. This gives the bandit a baseline knowledge of the world before the live loop begins.&lt;/p>

&lt;h3 id="step-2-the-online-loop" data-numberify>Step 2: The Online Loop&lt;a class="anchor ms-1" href="#step-2-the-online-loop" aria-label="Link to this section: Step 2: The Online Loop">&lt;/a>&lt;/h3>
&lt;p>We simulate a sequence of user visits:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>User Arrival:&lt;/strong> Pick a random user from the pool.&lt;/li>
&lt;li>&lt;strong>Contextualize:&lt;/strong> Inject a simulated timestamp (e.g., varying between Mon 08:00 AM and Sat 09:00 PM). This is the key &amp;ldquo;Context&amp;rdquo; the model must react to.&lt;/li>
&lt;li>&lt;strong>Recommend:&lt;/strong> LinUCB calculates scores for all 200 products and returns the Top 5.&lt;/li>
&lt;li>&lt;strong>Reaction:&lt;/strong> The &lt;code>will_click&lt;/code> Oracle decides if the user clicks.&lt;/li>
&lt;li>&lt;strong>Online Update:&lt;/strong> We call &lt;code>model.partial_fit()&lt;/code> with the reward, 1 or 0, for one product: the clicked one, or the first one shown when nothing was clicked. &lt;strong>This updates that product&amp;rsquo;s matrix $A$ and vector $b$ instantly.&lt;/strong> The very next recommendation will reflect this new learning.&lt;/li>
&lt;/ol>
&lt;p>Here is a sample of 30 recommendation records from the local simulation.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="o">(&lt;/span>venv&lt;span class="o">)&lt;/span> $ python -m recommender.run.local
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-02-05 15:47:43&lt;span class="o">]&lt;/span> INFO : Loaded &lt;span class="m">1000&lt;/span> users
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-02-05 15:47:43&lt;span class="o">]&lt;/span> INFO : Loading artifacts...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-02-05 15:47:48&lt;span class="o">]&lt;/span> INFO : Loaded &lt;span class="m">200&lt;/span> products.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-02-05 15:47:48&lt;span class="o">]&lt;/span> INFO : Pre-training model from history...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="o">[&lt;/span>2026-02-05 15:47:48&lt;span class="o">]&lt;/span> INFO : Model pre-trained on &lt;span class="m">10000&lt;/span> events.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">--- STARTING LIVE LOOP &lt;span class="o">(&lt;/span>&lt;span class="m">30&lt;/span> visits&lt;span class="o">)&lt;/span> ---
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">User &lt;span class="m">0153&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">56&lt;/span> yo&lt;span class="o">)&lt;/span> @ Fri 00:58 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>200, 124, 015, 058, 011&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">200&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">User &lt;span class="m">0909&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">21&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 15:53 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>038, 040, 017, 020, 046&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">038&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">User &lt;span class="m">0406&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">30&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 05:24 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>020, 041, 008, 055, 040&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">020&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">020&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">User &lt;span class="m">0317&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">31&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 05:38 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>008, 055, 057, 059, 139&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">055&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">055&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">User &lt;span class="m">0246&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">44&lt;/span> yo&lt;span class="o">)&lt;/span> @ Mon 02:04 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>015, 058, 057, 011, 124&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">015&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">User &lt;span class="m">0974&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">61&lt;/span> yo&lt;span class="o">)&lt;/span> @ Fri 01:16 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>058, 073, 124, 074, 051&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">058&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">User &lt;span class="m">0234&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">26&lt;/span> yo&lt;span class="o">)&lt;/span> @ Thu 12:16 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>036, 103, 002, 186, 070&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">036&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl">User &lt;span class="m">0360&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">35&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 20:23 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>058, 051, 008, 042, 018&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">042&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">042&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">User &lt;span class="m">0513&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">51&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sun 05:37 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>051, 059, 043, 014, 020&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">051&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">051&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">19&lt;/span>&lt;span class="cl">User &lt;span class="m">0640&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">33&lt;/span> yo&lt;span class="o">)&lt;/span> @ Mon 00:49 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>073, 124, 023, 147, 074&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">073&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">20&lt;/span>&lt;span class="cl">User &lt;span class="m">0363&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">31&lt;/span> yo&lt;span class="o">)&lt;/span> @ Fri 23:35 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>200, 126, 085, 058, 018&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">018&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">018&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">21&lt;/span>&lt;span class="cl">User &lt;span class="m">0718&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">58&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 23:05 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>018, 036, 040, 042, 020&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">018&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">018&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">22&lt;/span>&lt;span class="cl">User &lt;span class="m">0390&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">49&lt;/span> yo&lt;span class="o">)&lt;/span> @ Tue 00:56 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>147, 165, 020, 089, 047&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">147&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">23&lt;/span>&lt;span class="cl">User &lt;span class="m">0425&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">39&lt;/span> yo&lt;span class="o">)&lt;/span> @ Thu 03:59 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>147, 165, 062, 028, 055&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">147&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">24&lt;/span>&lt;span class="cl">User &lt;span class="m">0792&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">21&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sun 23:28 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>042, 056, 018, 043, 046&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">042&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">042&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">25&lt;/span>&lt;span class="cl">User &lt;span class="m">0190&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">41&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 10:54 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>192, 139, 189, 008, 055&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">192&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">192&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">26&lt;/span>&lt;span class="cl">User &lt;span class="m">0544&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">41&lt;/span> yo&lt;span class="o">)&lt;/span> @ Tue 20:42 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>018, 058, 090, 043, 147&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">018&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">27&lt;/span>&lt;span class="cl">User &lt;span class="m">0192&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">17&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 18:38 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>042, 056, 018, 046, 043&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">042&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">042&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">28&lt;/span>&lt;span class="cl">User &lt;span class="m">0757&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">55&lt;/span> yo&lt;span class="o">)&lt;/span> @ Thu 16:08 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>015, 171, 165, 037, 126&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">126&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">126&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">29&lt;/span>&lt;span class="cl">User &lt;span class="m">0904&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">60&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 02:07 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>103, 041, 017, 042, 057&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">042&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">042&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">30&lt;/span>&lt;span class="cl">User &lt;span class="m">0552&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">39&lt;/span> yo&lt;span class="o">)&lt;/span> @ Tue 11:33 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>192, 190, 189, 194, 193&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">192&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">192&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">31&lt;/span>&lt;span class="cl">User &lt;span class="m">0540&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">36&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 08:56 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>043, 041, 192, 014, 073&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">043&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">043&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">32&lt;/span>&lt;span class="cl">User &lt;span class="m">0326&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">26&lt;/span> yo&lt;span class="o">)&lt;/span> @ Wed 13:04 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>015, 171, 165, 023, 126&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">015&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">33&lt;/span>&lt;span class="cl">User &lt;span class="m">0834&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">29&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sat 22:58 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>051, 002, 042, 058, 036&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">051&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">051&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">34&lt;/span>&lt;span class="cl">User &lt;span class="m">0290&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">21&lt;/span> yo&lt;span class="o">)&lt;/span> @ Mon 19:09 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>058, 200, 004, 018, 085&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">018&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">018&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">35&lt;/span>&lt;span class="cl">User &lt;span class="m">0275&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">18&lt;/span> yo&lt;span class="o">)&lt;/span> @ Wed 11:10 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>189, 002, 160, 078, 103&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">189&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">36&lt;/span>&lt;span class="cl">User &lt;span class="m">0327&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">23&lt;/span> yo&lt;span class="o">)&lt;/span> @ Wed 19:54 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>200, 126, 018, 085, 058&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">200&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">37&lt;/span>&lt;span class="cl">User &lt;span class="m">0144&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">67&lt;/span> yo&lt;span class="o">)&lt;/span> @ Thu 12:31 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>087, 126, 047, 103, 034&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">087&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">38&lt;/span>&lt;span class="cl">User &lt;span class="m">0497&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">60&lt;/span> yo&lt;span class="o">)&lt;/span> @ Sun 08:26 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>192, 139, 008, 189, 059&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: &lt;span class="m">192&lt;/span> &lt;span class="o">(&lt;/span>✅&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">1&lt;/span> on &lt;span class="m">192&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">39&lt;/span>&lt;span class="cl">User &lt;span class="m">0508&lt;/span> &lt;span class="o">(&lt;/span>&lt;span class="m">64&lt;/span> yo&lt;span class="o">)&lt;/span> @ Tue 12:41 -&amp;gt; Recs: &lt;span class="o">[&lt;/span>165, 087, 026, 171, 037&lt;span class="o">]&lt;/span> -&amp;gt; Clicked: None &lt;span class="o">(&lt;/span>❌&lt;span class="o">)&lt;/span>, Reward: &lt;span class="m">0&lt;/span> on &lt;span class="m">165&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">40&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">41&lt;/span>&lt;span class="cl">--- END LOOP ---
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h3 id="evaluation-of-simulation" data-numberify>Evaluation of Simulation&lt;a class="anchor ms-1" href="#evaluation-of-simulation" aria-label="Link to this section: Evaluation of Simulation">&lt;/a>&lt;/h3>
&lt;p>The system is behaving exactly as a Contextual Bandit should. It is aggressively exploiting known high-probability zones while struggling (realistically) in neutral zones.&lt;/p>

&lt;h4 id="weekend-pizza-strategy-dominates" data-numberify>&amp;ldquo;Weekend Pizza&amp;rdquo; Strategy Dominates&lt;a class="anchor ms-1" href="#weekend-pizza-strategy-dominates" aria-label="Link to this section: &amp;amp;ldquo;Weekend Pizza&amp;amp;rdquo; Strategy Dominates">&lt;/a>&lt;/h4>
&lt;p>The model has learned that Weekends (Sat/Sun) are for &lt;strong>Pizzas (Category 40s)&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>User 0360 (Sat 20:23):&lt;/strong> Recommended &lt;code>[..., 042 (Aussie Pizza), ...]&lt;/code> $\to$ Clicked ✅.&lt;/li>
&lt;li>&lt;strong>User 0513 (Sun 05:37):&lt;/strong> Recommended &lt;code>[051 (Buffalo Pizza), 059 (Lamb Pizza)...]&lt;/code> $\to$ Clicked ✅.&lt;/li>
&lt;li>&lt;strong>User 0834 (Sat 22:58):&lt;/strong> Recommended &lt;code>[051 (Buffalo Pizza)... 042 (Aussie Pizza)]&lt;/code> $\to$ Clicked ✅.&lt;/li>
&lt;li>&lt;strong>Insight:&lt;/strong> The model pushes Pizzas hard on weekends regardless of the specific hour, resulting in a very high conversion rate for these users.&lt;/li>
&lt;/ul>

&lt;h4 id="morning-coffee-precision" data-numberify>&amp;ldquo;Morning Coffee&amp;rdquo; Precision&lt;a class="anchor ms-1" href="#morning-coffee-precision" aria-label="Link to this section: &amp;amp;ldquo;Morning Coffee&amp;amp;rdquo; Precision">&lt;/a>&lt;/h4>
&lt;p>The model correctly switches strategies based on the hour, even distinguishing &amp;ldquo;Weekend Morning&amp;rdquo; from &amp;ldquo;Weekend Night&amp;rdquo;.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>User 0552 (Tue 11:33):&lt;/strong> It is a Weekday Morning. The model recommended &lt;strong>5 Coffees&lt;/strong> &lt;code>[192, 190, 189, 194, 193]&lt;/code>. The user clicked &lt;code>192&lt;/code> (Long Black). ✅&lt;/li>
&lt;li>&lt;strong>User 0497 (Sun 08:26):&lt;/strong> It is a Weekend, but it is Morning. The model prioritized &lt;strong>Coffee (192, 189)&lt;/strong> over Pizza. The user clicked &lt;code>192&lt;/code>. ✅&lt;/li>
&lt;li>&lt;strong>User 0508 (Tue 12:41):&lt;/strong> This is a great edge case. It is &lt;strong>41 minutes past&lt;/strong> the &amp;ldquo;Morning&amp;rdquo; cutoff (12:00). The model stopped recommending Coffee and switched to Lunch items (Steamed Veggies, Burritos). The user did not click ❌, but the &lt;em>behavior&lt;/em> change proves the features are working perfectly.&lt;/li>
&lt;/ul>

&lt;h4 id="perfect-recommendation-no-interaction-realism" data-numberify>Perfect Recommendation, No Interaction (Realism)&lt;a class="anchor ms-1" href="#perfect-recommendation-no-interaction-realism" aria-label="Link to this section: Perfect Recommendation, No Interaction (Realism)">&lt;/a>&lt;/h4>
&lt;ul>
&lt;li>&lt;strong>User 0275 (Wed 11:10):&lt;/strong> The model recommended &lt;code>189&lt;/code> (Flat White). However, the user ignored it, and the result was ❌ (No Reward).&lt;/li>
&lt;li>&lt;strong>Why?&lt;/strong> This mimics real life. Even if the recommendation is perfect, users don&amp;rsquo;t always convert. In &lt;code>click_probability&lt;/code>, the probability caps at ~50% (sigmoid of 0). This &amp;ldquo;Bad Luck&amp;rdquo; outcome confirms your evaluation pipeline is honest.&lt;/li>
&lt;/ul>

&lt;h3 id="conclusion" data-numberify>Conclusion&lt;a class="anchor ms-1" href="#conclusion" aria-label="Link to this section: Conclusion">&lt;/a>&lt;/h3>
&lt;p>Across 30 simulated visits, CTR reached &lt;strong>53%&lt;/strong>.&lt;/p>
&lt;p>This elevated CTR indicates that the model has internalized dominant temporal and demographic patterns encoded in the simulation. It aggressively exploits strong signals such as weekend pizza and morning coffee while still exploring less-certain regions.&lt;/p>
&lt;p>Importantly, the simulation includes realistic noise. Even strong recommendations do not always convert, reflecting probabilistic user behavior.&lt;/p>

&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/">Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning&lt;/a> - another online learning system, where a model learns on Flink as machinery wears and detects concept drift&lt;/li>
&lt;/ul>

&lt;h2 id="whats-next" data-numberify>What&amp;rsquo;s Next?&lt;a class="anchor ms-1" href="#whats-next" aria-label="Link to this section: What&amp;amp;rsquo;s Next?">&lt;/a>&lt;/h2>
&lt;p>We have successfully prototyped a Contextual Bandit that learns time-based preferences. However, this Python script has major limitations for a production environment:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Scalability:&lt;/strong> &lt;code>Disjoint LinUCB&lt;/code> maintains a matrix for &lt;em>every&lt;/em> product. With 10 million products, a single server will run out of memory.&lt;/li>
&lt;li>&lt;strong>Latency:&lt;/strong> The training (&lt;code>partial_fit&lt;/code>) blocks the inference (&lt;code>recommend&lt;/code>). In a real system, you cannot make a user wait for the model to update.&lt;/li>
&lt;li>&lt;strong>Fault Tolerance:&lt;/strong> If the script crashes, the learned state is lost.&lt;/li>
&lt;li>&lt;strong>Concurrency:&lt;/strong> A single Python process cannot handle thousands of concurrent requests.&lt;/li>
&lt;/ol>
&lt;p>In &lt;a href="https://jaehyeon.me/blog/2026-02-23-productionize-recommender-with-eda/">&lt;strong>Part 2&lt;/strong>&lt;/a>, we will transform this prototype into an &lt;em>Event-Driven Architecture&lt;/em>:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Kafka&lt;/strong> will transport click events asynchronously.&lt;/li>
&lt;li>&lt;strong>Flink&lt;/strong> will handle distributed, stateful model training.&lt;/li>
&lt;li>&lt;strong>Redis&lt;/strong> will serve the model matrices for sub-millisecond inference.&lt;/li>
&lt;/ul>
&lt;p>Stay tuned!&lt;/p></content:encoded></item><item><title>Stream Processing with Flink in Kotlin</title><link>https://jaehyeon.me/blog/2025-12-10-streaming-processing-with-flink-in-kotlin/</link><guid>https://jaehyeon.me/blog/2025-12-10-streaming-processing-with-flink-in-kotlin/</guid><pubDate>Wed, 10 Dec 2025 00:00:00 +0000</pubDate><category>Apache Flink</category><category>Kotlin</category><category>Gradle</category><category>Ktor</category><description>&lt;p>A couple of years ago, I read &lt;a href="https://www.oreilly.com/library/view/stream-processing-with/9781491974285/" target="_blank" rel="noopener noreferrer">Stream Processing with Apache Flink&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and worked through the examples using PyFlink. While the book offered a solid introduction to Flink, I frequently hit limitations with the Python API, as many features from the book weren&amp;rsquo;t supported. This time, I decided to revisit the material, but using Kotlin. The experience has been much more rewarding and fun.&lt;/p>
&lt;p>In porting the examples to Kotlin, I also took the opportunity to align the code with modern Flink practices. The complete source for this post is available in the &lt;a href="https://github.com/jaehyeon-kim/flink-demos/tree/master/stream-processing-with-flink" target="_blank" rel="noopener noreferrer">&lt;code>stream-processing-with-flink&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> directory of the &lt;code>flink-demos&lt;/code> GitHub repository.&lt;/p></description><content:encoded>&lt;p>A couple of years ago, I read &lt;a href="https://www.oreilly.com/library/view/stream-processing-with/9781491974285/" target="_blank" rel="noopener noreferrer">Stream Processing with Apache Flink&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> and worked through the examples using PyFlink. While the book offered a solid introduction to Flink, I frequently hit limitations with the Python API, as many features from the book weren&amp;rsquo;t supported. This time, I decided to revisit the material, but using Kotlin. The experience has been much more rewarding and fun.&lt;/p>
&lt;p>In porting the examples to Kotlin, I also took the opportunity to align the code with modern Flink practices. The complete source for this post is available in the &lt;a href="https://github.com/jaehyeon-kim/flink-demos/tree/master/stream-processing-with-flink" target="_blank" rel="noopener noreferrer">&lt;code>stream-processing-with-flink&lt;/code>&lt;i class="fas fa-external-link-square-alt ms-1">&lt;/i>&lt;/a> directory of the &lt;code>flink-demos&lt;/code> GitHub repository.&lt;/p>
&lt;h2 id="updating-the-code-and-apis" data-numberify>Updating the Code and APIs&lt;a class="anchor ms-1" href="#updating-the-code-and-apis" aria-label="Link to this section: Updating the Code and APIs">&lt;/a>&lt;/h2>
&lt;p>The book, while conceptually valuable, is a bit dated. As I worked through the examples, I updated several deprecated features to use their modern equivalents.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>&lt;code>ListCheckpointed&lt;/code> to &lt;code>CheckpointedFunction&lt;/code>&lt;/strong>: Replaced the older checkpointing interface with the more flexible &lt;code>CheckpointedFunction&lt;/code>.&lt;/li>
&lt;li>&lt;strong>&lt;code>SourceFunction&lt;/code> to Source API&lt;/strong>: Migrated from the legacy &lt;code>SourceFunction&lt;/code> to the newer, more robust Source API.&lt;/li>
&lt;li>&lt;strong>&lt;code>SinkFunction&lt;/code> to Sink V2 API&lt;/strong>: Updated the &lt;code>SinkFunction&lt;/code> to the Sink V2 API.&lt;/li>
&lt;li>&lt;strong>Queryable State&lt;/strong>: Ignored as it has been deprecated since Flink 1.18. These examples are built using Flink 1.20.1.&lt;/li>
&lt;/ul>

&lt;h2 id="optimizing-the-build-with-gradle" data-numberify>Optimizing the Build with Gradle&lt;a class="anchor ms-1" href="#optimizing-the-build-with-gradle" aria-label="Link to this section: Optimizing the Build with Gradle">&lt;/a>&lt;/h2>
&lt;p>Figuring out the Gradle build was a valuable lesson in itself. I learned how to create a single &lt;code>build.gradle.kts&lt;/code> to handle two different scenarios: producing a lean production JAR and keeping local execution simple.&lt;/p>
&lt;p>For the production JAR, Flink dependencies are declared with &lt;code>compileOnly&lt;/code>. This correctly excludes them from the final artifact, as the Flink cluster provides these libraries at runtime.&lt;/p>
&lt;blockquote>
&lt;p>❗The test environment also needs the Flink APIs to compile and run. This is handled by &lt;code>testImplementation&lt;/code>. This standard Gradle configuration provides the Flink libraries &lt;em>only&lt;/em> to the test classpath, keeping them completely separate from the production JAR and the local &lt;code>run&lt;/code> task.&lt;/p>
&lt;/blockquote>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-kotlin" data-lang="kotlin">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="n">dependencies&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> &lt;span class="c1">// Flink Dependencies are not bundled into the JAR
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="n">compileOnly&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;org.apache.flink:flink-streaming-java:&lt;/span>&lt;span class="si">$flinkVersion&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> &lt;span class="n">compileOnly&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;org.apache.flink:flink-clients:&lt;/span>&lt;span class="si">$flinkVersion&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl"> &lt;span class="c1">// Flink is available for test compilation and execution
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="n">testImplementation&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;org.apache.flink:flink-streaming-java:&lt;/span>&lt;span class="si">$flinkVersion&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl"> &lt;span class="n">testImplementation&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;org.apache.flink:flink-clients:&lt;/span>&lt;span class="si">$flinkVersion&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">9&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>However, this creates a problem for local development, as the Flink libraries are now missing from the default runtime classpath. The key technique I learned was how to solve this by creating a custom configuration, &lt;code>localRunClasspath&lt;/code>.&lt;/p>
&lt;p>This configuration rebuilds the full classpath specifically for the &lt;code>run&lt;/code> task by adding the &lt;code>compileOnly&lt;/code> dependencies back in, along with the standard &lt;code>implementation&lt;/code> and &lt;code>runtimeOnly&lt;/code> scopes. This makes local development seamless.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-kotlin" data-lang="kotlin">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">&lt;span class="k">val&lt;/span> &lt;span class="py">localRunClasspath&lt;/span> &lt;span class="k">by&lt;/span> &lt;span class="n">configurations&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">creating&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> &lt;span class="n">extendsFrom&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">configurations&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">implementation&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="k">get&lt;/span>&lt;span class="p">(),&lt;/span> &lt;span class="n">configurations&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">compileOnly&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="k">get&lt;/span>&lt;span class="p">(),&lt;/span> &lt;span class="n">configurations&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">runtimeOnly&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="k">get&lt;/span>&lt;span class="p">())&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">&lt;span class="c1">// ...
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">&lt;span class="n">tasks&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">named&lt;/span>&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="n">JavaExec&lt;/span>&lt;span class="p">&amp;gt;(&lt;/span>&lt;span class="s2">&amp;#34;run&amp;#34;&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl"> &lt;span class="n">classpath&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="n">localRunClasspath&lt;/span> &lt;span class="p">+&lt;/span> &lt;span class="n">sourceSets&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">main&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="k">get&lt;/span>&lt;span class="p">().&lt;/span>&lt;span class="n">output&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">9&lt;/span>&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="main-chapters" data-numberify>Main Chapters&lt;a class="anchor ms-1" href="#main-chapters" aria-label="Link to this section: Main Chapters">&lt;/a>&lt;/h2>
&lt;p>So far, I have translated the examples from the following main chapters:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Chapter 5: Basic and Keyed Transformations&lt;/strong>: Covers fundamental data manipulation, including &lt;code>map&lt;/code>, &lt;code>filter&lt;/code>, &lt;code>keyBy&lt;/code>, and rolling sum aggregations, as well as multi-stream transformations.&lt;/li>
&lt;li>&lt;strong>Chapter 6: Event Time and Windowing&lt;/strong>: Focuses on time-based operations, including &lt;code>ProcessFunction&lt;/code> timers, watermark generation strategies, window functions, custom window logic, and side outputs for late data handling.&lt;/li>
&lt;li>&lt;strong>Chapter 7: State Management&lt;/strong>: Explores different types of state in Flink, such as &lt;code>ValueState&lt;/code>, &lt;code>ListState&lt;/code>, &lt;code>MapState&lt;/code>, and &lt;code>BroadcastState&lt;/code>, along with operator state.&lt;/li>
&lt;li>&lt;strong>Chapter 8: Asynchronous I/O and Custom Connectors&lt;/strong>: Demonstrates how to interact with external systems asynchronously and build custom sources and sinks.&lt;/li>
&lt;/ul>

&lt;h2 id="how-to-build-and-run-the-examples" data-numberify>How to Build and Run the Examples&lt;a class="anchor ms-1" href="#how-to-build-and-run-the-examples" aria-label="Link to this section: How to Build and Run the Examples">&lt;/a>&lt;/h2>
&lt;p>The Flink applications can be run directly from the command line for local testing and development. This is useful for quick debugging without needing a full Flink cluster. Moreover, each Flink app has detailed documentation so it is easy to understand, for example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-kotlin" data-lang="kotlin">&lt;span class="line">&lt;span class="ln"> 1&lt;/span>&lt;span class="cl">&lt;span class="cm">/**
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 2&lt;/span>&lt;span class="cl">&lt;span class="cm"> * This Flink job demonstrates transformations on a `KeyedStream`.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 3&lt;/span>&lt;span class="cl">&lt;span class="cm"> *
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 4&lt;/span>&lt;span class="cl">&lt;span class="cm"> * It showcases the `reduce` operator, a powerful tool for maintaining running aggregates
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 5&lt;/span>&lt;span class="cl">&lt;span class="cm"> * for each key in a stream.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 6&lt;/span>&lt;span class="cl">&lt;span class="cm"> *
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 7&lt;/span>&lt;span class="cl">&lt;span class="cm"> * The pipeline is as follows:
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 8&lt;/span>&lt;span class="cl">&lt;span class="cm"> * 1. **Source**: Ingests a stream of `SensorReading` events.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln"> 9&lt;/span>&lt;span class="cl">&lt;span class="cm"> * 2. **KeyBy**: Partitions the stream by the `id` of each sensor. All subsequent
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">10&lt;/span>&lt;span class="cl">&lt;span class="cm"> * operations will run independently for each sensor.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">11&lt;/span>&lt;span class="cl">&lt;span class="cm"> * 3. **Reduce**: For each key, this operator maintains a running state of the `SensorReading`
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">12&lt;/span>&lt;span class="cl">&lt;span class="cm"> * with the maximum temperature seen so far. For every new reading that arrives, it
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">13&lt;/span>&lt;span class="cl">&lt;span class="cm"> * compares it to the current maximum and emits the new maximum downstream.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">14&lt;/span>&lt;span class="cl">&lt;span class="cm"> * 4. **Sink**: Prints the continuous stream of running maximums for each sensor to the console.
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">15&lt;/span>&lt;span class="cl">&lt;span class="cm"> */&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">16&lt;/span>&lt;span class="cl">&lt;span class="k">object&lt;/span> &lt;span class="nc">KeyedTransformations&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">17&lt;/span>&lt;span class="cl"> &lt;span class="c1">// ...
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">18&lt;/span>&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To launch the apps, use the &lt;code>run&lt;/code> task and set the desired main class with the &lt;code>-PmainClass&lt;/code> project property. Here are the full examples:&lt;/p>
&lt;p>&lt;strong>1. Run the Chapter 5 examples.&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter5.BasicTransformations
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter5.KeyedTransformations
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter5.RollingSum
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter5.MultiStreamTransformations
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>2. Run the Chapter 6 examples.&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.ProcessFunctionTimers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.PeriodicWatermarkGeneration
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.MarkerBasedWatermarkGeneration
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.CoProcessFunctionTimers
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.WindowFunctions --args&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;min1&amp;#34;&lt;/span> &lt;span class="c1"># min2, avg, minmax1, or minmax2&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.CustomWindows
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.SideOutputs
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">8&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter6.LateDataHandling --args&lt;span class="o">=&lt;/span>&lt;span class="s2">&amp;#34;filter&amp;#34;&lt;/span> &lt;span class="c1"># sideout or update&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>3. Run the Chapter 7 examples.&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter7.KeyedStateFunction
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter7.StatefulProcessFunction
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter7.BroadcastStateFunction
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter7.OperatorStateFunction
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter7.KeyedAndOperatorStateFunction
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>4. Run the Chapter 8 examples.&lt;/strong>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter8.AsyncFunction
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">./gradlew run -PmainClass&lt;span class="o">=&lt;/span>me.jaehyeon.chapter8.CustomConnectors
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="related-posts" data-numberify>Related posts&lt;a class="anchor ms-1" href="#related-posts" aria-label="Link to this section: Related posts">&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2026-04-21-digital-twin-online-machine-learning/">Building a Real-Time Industrial Digital Twin with Apache Flink and Online Machine Learning&lt;/a> - a larger Flink and Kotlin application, adding online machine learning&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2023-08-10-fraud-detection-part-1/">Kafka, Flink and DynamoDB for Real Time Fraud Detection&lt;/a> - a Flink application built locally on Docker with Kafka and DynamoDB&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2023-08-17-getting-started-with-pyflink-on-aws-part-1/">Getting Started with PyFlink on AWS&lt;/a> - the Python API whose limits led to this Kotlin rewrite, shown against a local Kafka cluster&lt;/li>
&lt;li>&lt;a href="https://jaehyeon.me/blog/2025-06-10-kotlin-getting-started-flink-datastream/">Flink DataStream API - Scalable Event Processing for Supplier Stats&lt;/a> - the DataStream API in Kotlin applied to Avro order events from Kafka, with watermarks and late records&lt;/li>
&lt;/ul>

&lt;h2 id="conclusion" data-numberify>Conclusion&lt;a class="anchor ms-1" href="#conclusion" aria-label="Link to this section: Conclusion">&lt;/a>&lt;/h2>
&lt;p>Working through the examples in Kotlin has been an effective way to dive deeper into Apache Flink. Translating the examples to Kotlin not only forced me to understand the concepts more thoroughly but also provided a great opportunity to get hands-on with the latest APIs and build practices. For those looking to learn Apache Flink through up-to-date examples, I hope sharing my experience and code proves helpful. It&amp;rsquo;s been a fun and effective learning journey.&lt;/p></content:encoded></item></channel></rss>