<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Awiros</title>
    <description>The latest articles on DEV Community by Awiros (@awirostech).</description>
    <link>https://dev.to/awirostech</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4228303%2Fd87b0533-aa55-41dd-86f6-dfea1aeaaad7.jpg</url>
      <title>DEV Community: Awiros</title>
      <link>https://dev.to/awirostech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/awirostech"/>
    <language>en</language>
    <item>
      <title>Freeze the config: snapshotting settings onto the record they govern</title>
      <dc:creator>Awiros</dc:creator>
      <pubDate>Wed, 18 Nov 2026 20:07:49 +0000</pubDate>
      <link>https://dev.to/awirostech/freeze-the-config-snapshotting-settings-onto-the-record-they-govern-5blb</link>
      <guid>https://dev.to/awirostech/freeze-the-config-snapshotting-settings-onto-the-record-they-govern-5blb</guid>
      <description>&lt;p&gt;Here's a made-up shift at a plant that inspects welded frames (every name, time and setting in this post is illustrative). An inspector starts an inspection on frame &lt;code&gt;A118&lt;/code&gt; at 11:20 with a handheld camera. The vision model flags a weld gap, the inspector confirms it, and because the org's rules don't ask for a close-up photo yet, they log the finding and move on to the next section.&lt;/p&gt;

&lt;p&gt;At 11:40 the quality lead changes an org setting: from now on every finding needs a close-up photo. Reasonable change. They made it for the next batch.&lt;/p&gt;

&lt;p&gt;At 11:55 the inspector taps Complete on &lt;code&gt;A118&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11:55:02 POST /inspections/insp-7/complete
11:55:02 422 {"error": "close-up photo required for: weld_gap"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The weld gap was logged 35 minutes earlier under rules that didn't ask for a photo. The frame has since moved down the line. The inspector now has an inspection they can't close, and the only ways out are walking back to a frame that's gone or asking an admin to flip the setting off for a minute (which changes it for everyone else too).&lt;/p&gt;

&lt;p&gt;Nothing here is a bug in the validation. The completion check did exactly what it was told. It read the settings as of 11:55, but the work had started under the 11:20 settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it actually goes wrong
&lt;/h2&gt;

&lt;p&gt;The "before" code, condensed from the toy in this post:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;complete_reading_live&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# settings right now&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"require_closeup_photo"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;missing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;findings&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;photos&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;missing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"close-up photo required for: {', '.join(missing)}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"done"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every place in the backend that reads &lt;code&gt;store.resolve(...)&lt;/code&gt; during an inspection is a seam where the rules can change under in-flight work. Completion is the one that blows up loudly. The quieter ones are worse: a photo-count limit that tightens halfway through so the next finding is rejected, a rework rule that turns on and sends a half-done inspection down a different path, or a report generated tomorrow that describes yesterday's inspection using today's rules.&lt;/p&gt;

&lt;p&gt;Visual inspection is where this comes up for us (aiscope files structured evidence for every inspection, and that evidence has to make sense later), but the same shape shows up for any long-running record: a camera analytics job, a batch annotation task, a scheduled export.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resolve once, freeze, read from the snapshot
&lt;/h2&gt;

&lt;p&gt;The reference pattern we recommend has three parts.&lt;/p&gt;

&lt;p&gt;At creation, resolve the settings once. In the toy that means platform defaults, then org overrides, then site overrides:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DEFAULTS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Store the result on the inspection as a snapshot, with a version, a timestamp and a hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"insp-7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"product"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"frame-A118"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"config_snapshot"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"v"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"resolved_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-11-09T11:20:04+05:30"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"hash"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"c6dd35f140d5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"settings"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"require_closeup_photo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"max_photos_per_finding"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"rework_on_missing_part"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"allow_parallel_runs"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then every behaviour check on that inspection reads the snapshot, never the live store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;insp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspections&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;iid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"require_closeup_photo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="c1"&gt;# frozen at 11:20&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="n"&gt;insp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"done"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same shift, replayed in the toy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;11:20 create insp-7     snapshot c6dd35f140d5 require_closeup_photo=False
11:22 finding weld_gap  photos=0
11:40 org setting       require_closeup_photo -&amp;gt; True
11:45 create insp-8     snapshot require_closeup_photo=True
11:50 finding weld_gap on insp-8 photos=0
11:55 complete insp-7   done
11:58 complete insp-8   422 close-up photo required for: weld_gap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;insp-7&lt;/code&gt; closes under its own rules. &lt;code&gt;insp-8&lt;/code&gt;, created after the change, gets the new rule. That's what the quality lead meant in the first place.&lt;/p&gt;

&lt;p&gt;The UI is the one place that should keep reading live settings, because it's setting up new work. The settings page shows today's values and the "start inspection" form uses them. For an inspection that's already open, show its frozen values with a small line like "rules as of 11:20". Otherwise an inspector looking at the settings page will reasonably ask why their open inspection isn't asking for photos.&lt;/p&gt;

&lt;p&gt;In the toy the snapshot is a &lt;code&gt;MappingProxyType&lt;/code&gt; and &lt;code&gt;settings&lt;/code&gt; is itself a proxy too, so both levels are read-only and &lt;code&gt;snapshot["settings"]["require_closeup_photo"] = True&lt;/code&gt; raises &lt;code&gt;TypeError&lt;/code&gt;. In a real database you'd enforce the same thing by never writing to that field after create, and having a test that fails if any update path touches it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 2: an opt-in flag with inspections already open
&lt;/h2&gt;

&lt;p&gt;A feature request: let two inspectors work on the same product at once (say a large frame split into sections). It's a behaviour change, so it ships as an opt-in flag, &lt;code&gt;allow_parallel_runs&lt;/code&gt;, default off, and like every other setting it goes into the snapshot.&lt;/p&gt;

&lt;p&gt;The interesting moment is when an org turns it on while single-run inspections are still open. Illustrative timeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;14:20 create insp-12 on frame-B204   allow_parallel_runs=False (frozen, hash 7e2a91c4b0f3)
15:00 org setting allow_parallel_runs -&amp;gt; True
15:05 create insp-13 on frame-B204
      422 insp-12 was started under single-run rules (snapshot 7e2a91c4b0f3)
15:20 complete insp-12
15:30 create insp-13 on frame-B204   ok
15:31 create insp-14 on frame-B204   ok
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The start check looks at the new inspection's resolved flag and at every open inspection's own snapshot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;open_runs&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;resolved&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"allow_parallel_runs"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{product} already has an open inspection"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;open_runs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"allow_parallel_runs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{other.id} was started under single-run rules ..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;insp-12&lt;/code&gt; was started by someone who was told they had the frame to themselves. Its code paths (who owns which section, when a finding is final) were written for that. Letting &lt;code&gt;insp-13&lt;/code&gt; in at 15:05 would mix rules that were never tested together. Waiting for the old one to close costs a few minutes once per org, at the moment they turn the feature on, and after that the flag behaves as expected. You can pick the other answer and let new runs in. If you do, make it a deliberate decision and write tests for the mixed state, because a check that reads a live value will make that decision for you without anyone noticing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 3: the support ticket two weeks later
&lt;/h2&gt;

&lt;p&gt;A ticket comes in: "why did inspection &lt;code&gt;insp-7&lt;/code&gt; close with a finding that has no photo? our rules require photos." Without a snapshot, answering means digging through audit logs for settings changes and lining up timestamps by hand. With one, the toy has a small &lt;code&gt;explain&lt;/code&gt; helper that diffs frozen values against today's:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; svc.explain("insp-7")
{'require_closeup_photo': (False, True)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Frozen &lt;code&gt;False&lt;/code&gt;, live &lt;code&gt;True&lt;/code&gt;. The inspection followed the rules it started with, and the rule changed afterwards. The hash also helps with bulk questions: group inspections by &lt;code&gt;config_snapshot.hash&lt;/code&gt; and you can see exactly which rule sets were in use last month, without replaying the settings history.&lt;/p&gt;

&lt;h2&gt;
  
  
  When a setting has to stay live
&lt;/h2&gt;

&lt;p&gt;Some settings shouldn't be frozen. If a capture source gets disabled because a camera is sending corrupt frames, or a security fix turns something off, you want in-flight work to stop using it right away. The toy keeps an explicit, short list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;LIVE_KEYS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;frozenset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"disabled_capture_sources"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the check that uses it reads the live store on purpose, with a comment saying so. The risk is that this list grows until the snapshot means nothing, so treat adding a key to it as a review question: what happens to an inspection that's half done when this changes?&lt;/p&gt;

&lt;h2&gt;
  
  
  Snapshots get old too
&lt;/h2&gt;

&lt;p&gt;Snapshots outlive the code that wrote them. Six months from now there's a new setting, and every inspection created before it has a snapshot without that key. The tempting read is &lt;code&gt;snapshot.get(key, DEFAULTS[key])&lt;/code&gt;, which is wrong if today's default isn't how the system behaved back then. The toy versions snapshots and upgrades on read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;upgrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"settings"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"v"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setdefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"allow_parallel_runs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# behaviour before v2&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;MappingProxyType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The stored record is never rewritten. The upgrade only changes how old snapshots are read, and the test for it pins an old v1 snapshot as a fixture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs
&lt;/h2&gt;

&lt;p&gt;Storage, first. Every record carries a copy of its settings. For a few dozen keys that's small, but a snapshot that grows into a nested config blob makes every inspection heavy. If that happens, store the settings once in a content-addressed table keyed by hash and keep only the hash on the record.&lt;/p&gt;

&lt;p&gt;Admins can be surprised. A setting change only affecting new work is what you want, but someone will still ask why it "didn't work". The UI line saying which rules an open inspection uses saves a lot of tickets.&lt;/p&gt;

&lt;p&gt;Bugs in a setting also get frozen. If a bad value goes out at 9:00 and is fixed at 9:30, every inspection started in between keeps the bad value. You either live with it, add that key to the live list, or write a one-off, audited fix that patches specific snapshots and logs who did it. Pick one before you need it.&lt;/p&gt;

&lt;p&gt;And code has to know the difference. Every flag read needs to go through the record (&lt;code&gt;insp.flag(...)&lt;/code&gt;) and not through the settings service. A lint rule or a grep in CI for direct &lt;code&gt;resolve(&lt;/code&gt; calls outside the create path catches regressions cheaply.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the example
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;snapshot.py&lt;/code&gt; is about 150 lines of standard-library Python: a layered settings store, a frozen snapshot with version and hash, an inspection service, the "before" live-read completion, and an &lt;code&gt;explain&lt;/code&gt; helper. &lt;code&gt;test_snapshot.py&lt;/code&gt; has 10 tests covering the 11:40 change breaking the live-read version, the snapshot version closing cleanly, new inspections picking up new rules, site overrides beating org overrides, the snapshot rejecting writes, the parallel-run flag waiting for older open inspections, a v1 snapshot reading the pre-v2 default, a live key stopping in-flight work, the support diff, and key order not changing the hash. Run &lt;code&gt;python3 test_snapshot.py&lt;/code&gt;, no pytest needed. It's a teaching model, so swap the dicts for your database and keep the tests.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>architecture</category>
      <category>python</category>
      <category>computervision</category>
    </item>
    <item>
      <title>Ops analytics from an event stream: Kafka into ClickHouse without losing or double counting alerts</title>
      <dc:creator>Awiros</dc:creator>
      <pubDate>Wed, 11 Nov 2026 10:22:47 +0000</pubDate>
      <link>https://dev.to/awirostech/ops-analytics-from-an-event-stream-kafka-into-clickhouse-without-losing-or-double-counting-alerts-11hl</link>
      <guid>https://dev.to/awirostech/ops-analytics-from-an-event-stream-kafka-into-clickhouse-without-losing-or-double-counting-alerts-11hl</guid>
      <description>&lt;p&gt;Everything in this post is made up for illustration: the site, the cameras, the event ids, the timestamps, the counts. The problem is one we deal with building dashboards for Vision AI. On &lt;a href="https://awiros.com/products/aura" rel="noopener noreferrer"&gt;Aura&lt;/a&gt; every detection arrives with video evidence, gets routed to the team that acts on it, and rolls up into dashboards across sites. The routing side wants a transactional database: an alert gets raised, acknowledged, attached to a clip, closed. The dashboard side wants "PPE violations per dock per hour for the last 90 days", and asking the operational database for that during a busy shift is how alert pages get slow.&lt;/p&gt;

&lt;p&gt;The pattern we'd recommend is a separate analytics path. Apps publish every alert and stat event to Kafka, a small consumer batches them into ClickHouse, and dashboards only ever read ClickHouse. The consumer is maybe 200 lines of Go. Almost all the interesting bugs live in about ten of them.&lt;/p&gt;

&lt;p&gt;Here's the setup we'll follow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;site:       plant-a (illustrative)
cameras:    gate-3 runs an intrusion app, dock-1 runs a PPE app
topic:      vision-events, 2 partitions, keyed by camera
            (gate-3 -&amp;gt; partition 0, dock-1 -&amp;gt; partition 1)
consumer:   group ops-analytics, buffers events, flushes to ClickHouse
dashboard:  alerts per camera per hour, open vs acknowledged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The event and the table
&lt;/h2&gt;

&lt;p&gt;One alert, as the intrusion app publishes it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"event_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"evt-a1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"site"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"plant-a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"camera"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"gate-3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"intrusion"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nl"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"alert"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"ts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-03-14T02:11:07.412Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"raised"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two fields matter more than the rest. &lt;code&gt;event_id&lt;/code&gt; is minted by the app that saw the person at the gate, at the edge, before anything is published. &lt;code&gt;version&lt;/code&gt; goes up when the same alert changes state. When an operator acknowledges evt-a1 a minute later, the app publishes the same &lt;code&gt;event_id&lt;/code&gt; and &lt;code&gt;ts&lt;/code&gt; with &lt;code&gt;version: 2, status: "acknowledged"&lt;/code&gt;. In our night shift that's six messages for five distinct alerts.&lt;/p&gt;

&lt;p&gt;The table, as a sketch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;vision_events&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;event_id&lt;/span&gt;    &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;site&lt;/span&gt;        &lt;span class="nc"&gt;LowCardinality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;camera&lt;/span&gt;      &lt;span class="nc"&gt;LowCardinality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;         &lt;span class="nc"&gt;LowCardinality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;kind&lt;/span&gt;        &lt;span class="nc"&gt;LowCardinality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ts&lt;/span&gt;          &lt;span class="nc"&gt;DateTime64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'UTC'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;version&lt;/span&gt;     &lt;span class="nc"&gt;UInt32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;      &lt;span class="nc"&gt;LowCardinality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;ENGINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ReplacingMergeTree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nf"&gt;toYYYYMM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://clickhouse.com/docs/engines/table-engines/mergetree-family/replacingmergetree" rel="noopener noreferrer"&gt;ReplacingMergeTree&lt;/a&gt; removes rows that share the same &lt;code&gt;ORDER BY&lt;/code&gt; value, keeping the highest &lt;code&gt;version&lt;/code&gt; (or the last inserted row if versions tie). The catch is in the docs, in plain words: deduplication only happens during background merges, at a time you can't plan for, and the engine "does not guarantee the absence of duplicates". If you need the right answer at query time you read with &lt;code&gt;FINAL&lt;/code&gt;, which finishes the deduplication while the query runs.&lt;/p&gt;

&lt;p&gt;So the sorting key is your dedup key, and three rules fall out of that. Every column in &lt;code&gt;ORDER BY&lt;/code&gt; has to be identical across copies of the same event, which is why &lt;code&gt;ts&lt;/code&gt; is the event time from the camera and not the time the consumer saw it. The partition key has to be identical too, since &lt;a href="https://clickhouse.com/docs/guides/replacing-merge-tree" rel="noopener noreferrer"&gt;merges happen within a partition&lt;/a&gt;. And &lt;code&gt;event_id&lt;/code&gt; has to come from the source. A consumer that mints a fresh UUID per row creates a new key on every replay, and no merge will ever collapse those.&lt;/p&gt;

&lt;h2&gt;
  
  
  The consumer loop
&lt;/h2&gt;

&lt;p&gt;The shape is boring on purpose. Poll, append to a buffer, flush when the buffer reaches a size or when its oldest event reaches an age, then move offsets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flush when:  buffer &amp;gt;= MaxBatch (500)   or   oldest buffered event &amp;gt;= MaxWait (2s)
then:        INSERT batch  -&amp;gt;  commit offsets (last offset + 1 per partition)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Batching is not optional with ClickHouse. Each synchronous insert creates a part on disk, and the docs &lt;a href="https://clickhouse.com/docs/best-practices/selecting-an-insert-strategy" rel="noopener noreferrer"&gt;recommend batches of at least 1,000 rows, ideally 10,000 to 100,000, and roughly one insert query per second&lt;/a&gt;. 500 is below that minimum, which is fine for a single quiet site where the 2-second timer does most of the flushing and inserts stay under one per second. A busier deployment should push MaxBatch up. If you can't batch on the client, &lt;a href="https://clickhouse.com/docs/optimize/asynchronous-inserts" rel="noopener noreferrer"&gt;async inserts&lt;/a&gt; move the buffering to the server. If you go that way keep &lt;code&gt;wait_for_async_insert=1&lt;/code&gt;, so a failed flush still comes back to the consumer as an error.&lt;/p&gt;

&lt;p&gt;The order of "insert" and "commit" in that second line is what the rest of this post is about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1: commit first, insert times out, pod dies
&lt;/h2&gt;

&lt;p&gt;An illustrative timeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;02:14:00.000  consumer has 6 events buffered (3 per partition), flushes
02:14:00.001  offsets committed: p0=3 p1=3
02:14:10.002  INSERT times out (ClickHouse overloaded)
02:14:11      pod OOMKilled before the retry
02:14:40      new pod joins the group, resumes at p0=3 p1=3, nothing to read
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The toy reproduces it (&lt;code&gt;go run ./cmd/demo&lt;/code&gt;, scenario 1):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commit p0=3 p1=3
insert 6 rows failed: insert timeout (will retry)
-- process restarts, resumes from committed offsets --
rows: count()=0  FINAL=0  inserts=0
GROUP          TOPIC          PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
ops-analytics  vision-events  0         3              3              0
ops-analytics  vision-events  1         3              3              0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Six alerts are gone from the dashboard and lag says zero. Kafka is right, the group did consume them. Every monitor that only watches lag stays green.&lt;/p&gt;

&lt;p&gt;Nobody writes "commit, then insert" on purpose. It shows up through defaults. With librdkafka based clients (confluent-kafka-go included), &lt;code&gt;enable.auto.commit&lt;/code&gt; and &lt;code&gt;enable.auto.offset.store&lt;/code&gt; are both true by default, which stores each message's offset as it's handed to your code and commits it from a background thread on a timer. That commit has no idea your batch is still sitting in memory. The &lt;a href="https://docs.confluent.io/platform/current/clients/librdkafka/html/md_CONFIGURATION.html" rel="noopener noreferrer"&gt;librdkafka configuration docs&lt;/a&gt; recommend setting &lt;code&gt;enable.auto.offset.store=false&lt;/code&gt; for this kind of app and storing offsets yourself after processing.&lt;/p&gt;

&lt;p&gt;Before and after, as a sketch with confluent-kafka-go and clickhouse-go (error handling trimmed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// before (illustrative): defaults, offsets move as messages are read&lt;/span&gt;
&lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ConfigMap&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;brokers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"group.id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ops-analytics"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// after: offsets only move once ClickHouse said OK&lt;/span&gt;
&lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ConfigMap&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;        &lt;span class="n"&gt;brokers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"group.id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                 &lt;span class="s2"&gt;"ops-analytics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"enable.auto.offset.store"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PrepareBatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"INSERT INTO vision_events"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AppendStruct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="c1"&gt;// keep buf and offsets, retry next tick&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// enable.auto.commit stays true (the default): StoreOffsets only stores;&lt;/span&gt;
    &lt;span class="c1"&gt;// a background thread still commits those stored offsets. StoreOffsets&lt;/span&gt;
    &lt;span class="c1"&gt;// itself does not commit.&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StoreOffsets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextOffsets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// last offset + 1, per partition&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting &lt;code&gt;enable.auto.commit=false&lt;/code&gt; and calling &lt;code&gt;Commit&lt;/code&gt; right after &lt;code&gt;Send&lt;/code&gt; works just as well. The &lt;a href="https://kafka.apache.org/39/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html" rel="noopener noreferrer"&gt;Java consumer docs&lt;/a&gt; walk through the same batch-then-commit loop and call the result at-least-once, meaning a crash in the wrong place gets you duplicates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2: insert first, pod dies before the commit
&lt;/h2&gt;

&lt;p&gt;Same night, offsets now move after the insert.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;02:14:00.000  flush 6 events, INSERT ok
02:14:00.040  pod killed (node drained) before the commit
02:14:30      new pod resumes at p0=0 p1=0, reads the same 6 messages, INSERT ok, commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Toy, scenario 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert 6 rows ok
-- process restarts, resumes from committed offsets --
insert 6 rows ok
commit p0=3 p1=3
rows: count()=12  FINAL=5  inserts=2
...
FINAL evt-a1: version=2 status=acknowledged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing lost, everything twice. A plain &lt;code&gt;count()&lt;/code&gt; says 12, and with &lt;code&gt;FINAL&lt;/code&gt; it's 5, because the two copies of each event share a sorting key and the acknowledged evt-a1 beats its raised version. That's the better failure to have, since duplicates can be cleaned up and missing alerts can't.&lt;/p&gt;

&lt;p&gt;ClickHouse also has &lt;a href="https://clickhouse.com/docs/guides/developer/deduplicating-inserts-on-retries" rel="noopener noreferrer"&gt;insert deduplication on retries&lt;/a&gt;: each inserted block gets a hash, and a retry of an identical block is dropped. It's enabled by default for replicated tables (a plain MergeTree needs &lt;code&gt;non_replicated_deduplication_window&lt;/code&gt; set). It helps when the same process retries the same batch after a timeout. It does much less after a restart, because the new pod cuts batches at different boundaries (the 2-second timer fires at a different moment) and the hashes differ. You can pass an &lt;code&gt;insert_deduplication_token&lt;/code&gt; built from partition and offset range if you cut batches per partition deterministically, but we treat the idempotent table as the backstop and the token as an optimization.&lt;/p&gt;

&lt;p&gt;Dashboards then read with &lt;code&gt;FINAL&lt;/code&gt;, or aggregate in a way that ignores copies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- illustrative: open vs acknowledged alerts per camera per hour, last 24h&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;toStartOfHour&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;countIf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'raised'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;countIf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'acknowledged'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;acked&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;vision_events&lt;/span&gt; &lt;span class="no"&gt;FINAL&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'plant-a'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'alert'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kt"&gt;INTERVAL&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="no"&gt;DAY&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;FINAL&lt;/code&gt; costs something on big scans. Filtering on the leading &lt;code&gt;ORDER BY&lt;/code&gt; columns (&lt;code&gt;site&lt;/code&gt;, and &lt;code&gt;app&lt;/code&gt; when you can) keeps it cheaper, and plain counts of distinct events can use &lt;code&gt;uniqExact(event_id)&lt;/code&gt; without &lt;code&gt;FINAL&lt;/code&gt; at all. The query above filters on &lt;code&gt;kind&lt;/code&gt; too, which is useful but isn't in the key, so it doesn't help the scan the same way.&lt;/p&gt;

&lt;p&gt;A test in the toy pins down what &lt;code&gt;FINAL&lt;/code&gt; can't fix. An exact resend collapses. A replay with a re-minted id, or with &lt;code&gt;ts&lt;/code&gt; restamped at ingest, stays as a separate row forever.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 3: SIGTERM during a rollout
&lt;/h2&gt;

&lt;p&gt;A deploy rolls the consumer. Kubernetes sends SIGTERM and gives the pod &lt;a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination" rel="noopener noreferrer"&gt;30 seconds by default&lt;/a&gt; before SIGKILL. At that moment the buffer holds our six events, waiting for the 2-second timer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// before (illustrative): stop on signal, close, done&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;sigterm&lt;/span&gt;
&lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the default librdkafka settings from example 1, &lt;code&gt;Close()&lt;/code&gt; commits the offsets it has stored on the way out, and those already cover the buffered messages. Toy scenario 3a:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shutdown: closing with 6 rows buffered
rows: count()=0  FINAL=0  inserts=0
GROUP          TOPIC          PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
ops-analytics  vision-events  0         3              3              0
ops-analytics  vision-events  1         3              3              0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is an ordered shutdown: stop polling, flush, move offsets, then close.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// after: final flush, then commit, then close&lt;/span&gt;
&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stop&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NotifyContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SIGTERM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Interrupt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// poll + flush on size/time&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;flushCtx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cancel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WithTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flushCtx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"final flush failed, offsets left for replay: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Toy scenario 3b:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shutdown: final flush of 6 rows
insert 6 rows ok
commit p0=3 p1=3
rows: count()=6  FINAL=5  inserts=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details. The final flush gets its own timeout, shorter than the grace period, so a hung ClickHouse can't eat the whole 30 seconds. And if that flush fails, offsets stay put, the next owner replays the batch, and example 2 handles the copies. If your offsets only ever move after an insert, a skipped final flush costs a replay. With auto-stored offsets it costs data. The same flush belongs in the rebalance callback, when partitions are revoked from a running consumer, for the same reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 4: ClickHouse is down and the cameras keep sending
&lt;/h2&gt;

&lt;p&gt;Say ClickHouse is unreachable for a few minutes. The cameras don't care, and alerts keep coming. A consumer that keeps polling and appending will grow its buffer until the pod gets OOMKilled, which is example 1 again if anything committed early.&lt;/p&gt;

&lt;p&gt;So the buffer gets a ceiling (MaxBuffered). Above it the consumer stops taking new records and keeps retrying the flush. With a real Kafka client, pause the assigned partitions (&lt;code&gt;Pause&lt;/code&gt;) and keep calling &lt;code&gt;Poll&lt;/code&gt;. If you stop polling for longer than &lt;code&gt;max.poll.interval.ms&lt;/code&gt;, the client leaves the group and a rebalance follows.&lt;/p&gt;

&lt;p&gt;Toy scenario 4, with a buffer ceiling of 4 and the store failing every insert while two cameras trickle in stat events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;insert 4 rows failed: insert timeout (will retry)
paused: 4 buffered &amp;gt;= 4
insert 4 rows failed: insert timeout (will retry)
paused: 4 buffered &amp;gt;= 4
insert 4 rows failed: insert timeout (will retry)
GROUP          TOPIC          PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
ops-analytics  vision-events  0         0              15             15
ops-analytics  vision-events  1         0              15             15
-- store back --
paused: 4 buffered &amp;gt;= 4
insert 4 rows ok
commit p0=2 p1=2
...
commit p0=15 p1=15
rows: count()=30  FINAL=29  inserts=8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;count()=30&lt;/code&gt; is the six night-shift messages plus 24 stat events (three Trickle ticks of four per partition). &lt;code&gt;FINAL=29&lt;/code&gt; because evt-a1's raised and acknowledged versions collapse again, same as in example 2.&lt;/p&gt;

&lt;p&gt;The backlog now sits in Kafka, which is built to hold it (as long as the outage is shorter than the topic's retention), and drains once inserts succeed. Real &lt;code&gt;kafka-consumer-groups --describe&lt;/code&gt; output also shows CONSUMER-ID, HOST and CLIENT-ID. A slow catch-up might look like this (illustrative):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kafka-consumer-groups --bootstrap-server kafka:9092 --describe --group ops-analytics
GROUP          TOPIC          PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG    CONSUMER-ID           HOST         CLIENT-ID
ops-analytics  vision-events  0          418200          431950          13750  ops-analytics-1-7c2e  /10.0.4.17   ops-analytics-1
ops-analytics  vision-events  1          402115          402130          15     ops-analytics-1-7c2e  /10.0.4.17   ops-analytics-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read lag as a trend. A steady small number is normal, a number that grows between two checks a minute apart means inserts are failing or too slow. One hot partition, like partition 0 above, usually points at one camera or one app flooding events, since the topic is keyed by camera.&lt;/p&gt;

&lt;p&gt;Example 1 showed lag can't be the only signal. We'd pair it with two numbers from the consumer itself: rows inserted per minute and time since the last successful insert. Lag at zero with no inserts for ten minutes during a shift is the silent loss case, and only the second number (time since the last insert) catches it. A quiet night also has zero inserts, so to avoid that false alarm compare against log-end-offset growth: events arriving on the topic while nothing gets inserted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tradeoffs
&lt;/h2&gt;

&lt;p&gt;You run one more system and one more consumer. For a single small site, a read replica of the operational database plus a few rollup tables may be enough, and we'd start there.&lt;/p&gt;

&lt;p&gt;Dashboards are seconds behind. Fine for trends and compliance rates. Alert routing should stay on the operational path, where an operator needs the event now.&lt;/p&gt;

&lt;p&gt;At-least-once plus a ReplacingMergeTree means duplicates exist on disk until a merge, so every query has to be written with that in mind (&lt;code&gt;FINAL&lt;/code&gt;, &lt;code&gt;uniqExact&lt;/code&gt;, or &lt;code&gt;argMax&lt;/code&gt; on &lt;code&gt;version&lt;/code&gt;). It's easy for someone to add a dashboard panel with a plain &lt;code&gt;count()&lt;/code&gt; six months later, so put a view in front of the table or at least a note in its comment.&lt;/p&gt;

&lt;p&gt;Schema changes need care in both directions. Add columns with defaults, and let the consumer ignore fields it doesn't know yet, so you can roll apps and the consumer in either order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the toy
&lt;/h2&gt;

&lt;p&gt;The toy is plain Go with no dependencies outside the standard library. &lt;code&gt;sink/broker.go&lt;/code&gt; is a fake broker with partitions, committed offsets, restart and a describe table, &lt;code&gt;sink/store.go&lt;/code&gt; is a fake ReplacingMergeTree with &lt;code&gt;Count()&lt;/code&gt; and &lt;code&gt;Final()&lt;/code&gt;, and &lt;code&gt;sink/consumer.go&lt;/code&gt; is the batching consumer with both commit orders, the buffer ceiling and the two shutdown paths. Tests cover all four examples, flush on size and time, a failed final flush leaving offsets for replay, a bad JSON record, and the re-minted id case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; toy
go &lt;span class="nb"&gt;test&lt;/span&gt; -v ./...
go run ./cmd/demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every block introduced above as a toy scenario comes from &lt;code&gt;go run ./cmd/demo&lt;/code&gt;. Flip &lt;code&gt;Mode&lt;/code&gt; or &lt;code&gt;FinalFlush&lt;/code&gt; in &lt;code&gt;cmd/demo/main.go&lt;/code&gt; and watch the counts move.&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>clickhouse</category>
      <category>go</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>Hello from Awiros: Vision AI on cameras that already exist</title>
      <dc:creator>Awiros</dc:creator>
      <pubDate>Fri, 06 Nov 2026 21:17:47 +0000</pubDate>
      <link>https://dev.to/awirostech/hello-from-awiros-vision-ai-on-cameras-that-already-exist-4b5o</link>
      <guid>https://dev.to/awirostech/hello-from-awiros-vision-ai-on-cameras-that-already-exist-4b5o</guid>
      <description>&lt;p&gt;We're Awiros. We write software that turns &lt;strong&gt;existing&lt;/strong&gt; CCTV and inspection cameras into something operators can actually use: real-time alerts, searchable evidence, and a bit less "stare at a wall of screens forever."&lt;/p&gt;

&lt;p&gt;This is our first post on DEV. We'll keep the product pitch short and talk like engineers who have lived with RTSP drops, flaky discovery, and models that look great in a notebook and weird under sodium lighting.&lt;/p&gt;

&lt;h2&gt;
  
  
  The boring problem that still isn't solved
&lt;/h2&gt;

&lt;p&gt;Cameras are already everywhere: plants, campuses, rail platforms, data centers, event venues. Most of that video is recorded, stored and never watched. People can't watch it at scale. So teams either over-alert, under-alert, or give up and treat video as a forensic archive after something goes wrong.&lt;/p&gt;

&lt;p&gt;Our bet is simple. If you can run reliable computer-vision &lt;strong&gt;agents&lt;/strong&gt; on the feeds you already have, every installed camera becomes a useful sensor instead of a liability.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we build
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Aura&lt;/strong&gt;: connect cameras / NVRs / VMS, place Vision AI agents on channels, route detections with video proof to the people who need them. Edge, on-prem, or cloud.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AppStack&lt;/strong&gt;: a catalog of ready CV apps you can try on your own feeds and deploy through Aura.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ATOM&lt;/strong&gt;: annotation and custom-model path when off-the-shelf apps aren't enough.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;aiscope&lt;/strong&gt;: visual inspection in the field (handheld or fixed), evidence included.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Outcomes we care about in the wild: &lt;strong&gt;safety&lt;/strong&gt;, &lt;strong&gt;inspection&lt;/strong&gt;, and &lt;strong&gt;operations&lt;/strong&gt;. Same platform, same feeds.&lt;/p&gt;

&lt;p&gt;We don't ask sites to rip out their cameras. ONVIF, RTSP, and the VMS they already run are the onboarding UX that matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  One constraint we take seriously
&lt;/h2&gt;

&lt;p&gt;Shipping CV into a demo is easy. Shipping it onto &lt;strong&gt;hundreds of live channels&lt;/strong&gt; is a reliability and ops problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Discovery and onboarding are messy (cameras lie; networks lie harder).&lt;/li&gt;
&lt;li&gt;Video plumbing still involves real streaming stacks (yes, GStreamer still earns its keep under a modern React ops UI).&lt;/li&gt;
&lt;li&gt;Edge boxes are multi-arch, bandwidth-constrained, and unforgiving after quantization.&lt;/li&gt;
&lt;li&gt;A bad model promote can look fine on a dashboard while operators lose trust. Once a channel gets muted, winning it back is slow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So a lot of our engineering energy goes into orchestration, honest pipeline state, human judgment before risky publishes, and making search/evidence useful after the alert fires.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'll see from us here
&lt;/h2&gt;

&lt;p&gt;We'll keep posting about the messy parts of putting CV on real cameras: what broke, what we changed, and what still sucks.&lt;/p&gt;

&lt;p&gt;Coming up, roughly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why we put a human in the loop before a model goes live&lt;/li&gt;
&lt;li&gt;ONVIF discovery (the onboarding UX nobody romanticizes)&lt;/li&gt;
&lt;li&gt;Using embeddings / similarity search as memory for past events&lt;/li&gt;
&lt;li&gt;Multi-arch containers that actually run next to the cameras&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Soft ask
&lt;/h2&gt;

&lt;p&gt;If you run CV, video ops, or MLOps in production: &lt;strong&gt;how do you decide a model is safe to put on a live camera?&lt;/strong&gt; Drop a comment if you've got an take. We're listening more than pitching :)&lt;/p&gt;

&lt;p&gt;Site: &lt;a href="https://awiros.com" rel="noopener noreferrer"&gt;awiros.com&lt;/a&gt; · Account: &lt;a href="https://dev.to/awirostech"&gt;@awirostech&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computervision</category>
      <category>machinelearning</category>
      <category>devops</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
