<?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: Ibad Ahmed Khan</title>
    <description>The latest articles on DEV Community by Ibad Ahmed Khan (@ibadahmedkhan).</description>
    <link>https://dev.to/ibadahmedkhan</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%2F4277059%2F7369543e-0a8b-429c-8f6e-4f0070f34b94.jpg</url>
      <title>DEV Community: Ibad Ahmed Khan</title>
      <link>https://dev.to/ibadahmedkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ibadahmedkhan"/>
    <language>en</language>
    <item>
      <title>Stop guessing why your AI voice agent feels slow: measure the silence, not the model</title>
      <dc:creator>Ibad Ahmed Khan</dc:creator>
      <pubDate>Tue, 01 Dec 2026 19:59:08 +0000</pubDate>
      <link>https://dev.to/ibadahmedkhan/stop-guessing-why-your-ai-voice-agent-feels-slow-measure-the-silence-not-the-model-48ba</link>
      <guid>https://dev.to/ibadahmedkhan/stop-guessing-why-your-ai-voice-agent-feels-slow-measure-the-silence-not-the-model-48ba</guid>
      <description>&lt;p&gt;When an AI voice agent feels slow, the conversation inside most teams goes the same way. Someone says the model is too slow and we should try a faster one. Someone else blames the text-to-speech voice. A third person is sure it's the interruption handling. Everyone has a theory, and the theories get tested by swapping vendors.&lt;/p&gt;

&lt;p&gt;I run an AI phone agent in production. It answers real inbound calls in Urdu and English for a vehicle tracking company: callers look up their vehicles and file complaints, and the agent does that by calling our backend APIs mid-conversation. I'd never worked with telephony or calling systems before building it. When the pauses got long enough for callers to feel them, we had our theories too.&lt;/p&gt;

&lt;p&gt;What actually fixed it wasn't a better model. It was going through &lt;strong&gt;736 real call turns&lt;/strong&gt;, one by one, and finding out where the silence came from. The answer surprised us, and silence per turn dropped from about &lt;strong&gt;3 seconds to 1-2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's my opinion, after doing it: &lt;strong&gt;most voice agent latency problems are diagnosed by gut feeling, and the gut is usually wrong.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The metric that matters is silence
&lt;/h2&gt;

&lt;p&gt;Your caller doesn't experience "model latency" or "tokens per second". They experience one thing: &lt;strong&gt;how long it's quiet after they stop talking.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That silence is the sum of everything between the end of their sentence and the first sound of the reply:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;detecting that the caller has actually finished speaking,&lt;/li&gt;
&lt;li&gt;speech-to-text producing the final transcript,&lt;/li&gt;
&lt;li&gt;the model deciding what to do,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;any tool calls to your backend&lt;/strong&gt;, sometimes more than one,&lt;/li&gt;
&lt;li&gt;the model writing the reply,&lt;/li&gt;
&lt;li&gt;text-to-speech producing the first audio.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Vendor benchmarks measure one of those steps in isolation, under ideal conditions. Your caller gets the sum, under your conditions. If you only look at the step a vendor advertises, you'll optimise the wrong thing with great confidence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why gut feeling fails here
&lt;/h2&gt;

&lt;p&gt;Three things make voice latency especially easy to misjudge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The slow turns are the memorable ones.&lt;/strong&gt; A team hears a recording with a 6-second pause and generalises from it. But a handful of terrible turns and a consistently sluggish median need completely different fixes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Different turns do different work.&lt;/strong&gt; "Hello, how can I help?" needs no backend. "What's the status of my vehicle?" needs a lookup. "I want to file a complaint" might need several calls. Averaging them together hides exactly the turns that hurt.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The layer you can hear gets the blame.&lt;/strong&gt; Voice and interruptions are audible, so they feel like the problem. Your backend is silent, so it's invisible, even when it's the one making the caller wait.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our case, one strong suspect was barge-in handling, the logic that lets a caller interrupt the agent. It sounded plausible: when interruptions are handled badly, conversations feel awkward and slow. The data ruled it out. The dominant cost was our own backend API, the quiet part of the turn that's easy to overlook because it never makes a sound.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to actually measure it
&lt;/h2&gt;

&lt;p&gt;You don't need fancy tooling to start. You need timestamps for each stage of a turn and a way to join them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Get a per-turn timeline.&lt;/strong&gt; Most voice platforms give you conversation transcripts with timing for each turn, and your backend has its own request logs. Put a conversation or call ID on every tool call so you can join the two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Break every turn into stages.&lt;/strong&gt; For each turn, record when the caller stopped speaking, when each tool call started and finished, and when the agent's audio started. Now each turn's silence splits into "waiting on the platform and model" and "waiting on our backend".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Group turns by what they do.&lt;/strong&gt; Greeting turns, lookup turns, complaint turns. Look at the distribution for each group, not one overall average.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Read the bad turns, not just the numbers.&lt;/strong&gt; Go through a real sample, a few hundred turns, and look at the worst ones per group. Patterns jump out quickly when you see the timeline next to the transcript: the same endpoint is slow every time, or the agent calls two APIs one after the other when one would do.&lt;/p&gt;

&lt;p&gt;That's the approach we took, across 736 turns. It isn't glamorous, it's careful reading of real data. But once the timeline is in front of everyone, there's very little left to argue about.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the data usually tells you
&lt;/h2&gt;

&lt;p&gt;Once we knew the backend was the bottleneck, the fixes were ordinary backend work, not AI work. And from what I've seen, that's the common case. Some patterns worth checking in your own agent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;APIs built for humans are slow for agents.&lt;/strong&gt; An endpoint designed for a web page might be fine at 1-2 seconds, because a human is scrolling. In a voice turn, that same 1-2 seconds is pure dead air, on top of everything else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chatty tool design.&lt;/strong&gt; If the agent needs three calls in sequence to answer one question, the caller waits for all three. One agent-shaped endpoint that returns exactly what the agent needs, in one round trip, often beats any model upgrade.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cold paths.&lt;/strong&gt; The first request after a quiet period can be dramatically slower than the rest. Agents are machines with a stopwatch: they hit your worst path more often than you think. (I wrote about &lt;a href="https://dev.to/ibadahmedkhan/cold-starts-are-a-latency-budget-problem-what-a-9-second-first-request-taught-me-about-production-29nh"&gt;a 9-second version of this&lt;/a&gt; separately.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accuracy fixes that add latency to every turn.&lt;/strong&gt; Fix accuracy where it fails, not everywhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last one deserves an example.&lt;/p&gt;

&lt;h2&gt;
  
  
  The accuracy trap: fix the hard cases without taxing the easy ones
&lt;/h2&gt;

&lt;p&gt;Our callers speak Urdu and English, and they say vehicle makes and number plates that general speech models often get wrong. The obvious fix is to run a heavier, slower recognition step on every turn. That makes every turn slower to fix the minority that are hard.&lt;/p&gt;

&lt;p&gt;Instead, we built a hybrid pipeline: a primary speech-to-text model handles the turn, and a fallback for domain terms kicks in only when needed, with a &lt;strong&gt;hard cap of 2.5 seconds&lt;/strong&gt;. Clean turns pay nothing extra. Hard turns get better recognition. And no turn can blow up the conversation, because the fallback has a ceiling.&lt;/p&gt;

&lt;p&gt;The general rule: &lt;strong&gt;when you add something to improve quality, decide its latency budget first, and enforce it.&lt;/strong&gt; An accuracy improvement without a cap is a latency regression waiting to happen.&lt;/p&gt;

&lt;h2&gt;
  
  
  My opinions, plainly
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Measure silence per turn, by turn type, before changing anything.&lt;/strong&gt; If you can't say where the seconds go, you're not ready to switch vendors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The model is rarely your biggest problem.&lt;/strong&gt; Tool calls, backend endpoints and sequential round trips usually are. They're also much cheaper to fix than a model migration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design APIs for the agent, not just for the UI.&lt;/strong&gt; Fewer round trips, smaller responses, predictable latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every quality feature needs a latency budget.&lt;/strong&gt; Caps and fallbacks beat "always run the expensive thing".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read real conversations.&lt;/strong&gt; Dashboards tell you that something is slow. Transcripts next to timelines tell you why.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Building a voice agent with no telephony background taught me that the hard part isn't the AI. It's the same thing that's always hard in production systems: knowing where the time goes, and being honest when the data says your favourite theory is wrong.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>voiceai</category>
      <category>performance</category>
    </item>
    <item>
      <title>Two stores, one truth: building a reconciliation worker that keeps Druid and SQL in sync without hurting either</title>
      <dc:creator>Ibad Ahmed Khan</dc:creator>
      <pubDate>Tue, 01 Dec 2026 19:54:57 +0000</pubDate>
      <link>https://dev.to/ibadahmedkhan/two-stores-one-truth-building-a-reconciliation-worker-that-keeps-druid-and-sql-in-sync-without-aph</link>
      <guid>https://dev.to/ibadahmedkhan/two-stores-one-truth-building-a-reconciliation-worker-that-keeps-druid-and-sql-in-sync-without-aph</guid>
      <description>&lt;p&gt;If you stream the same events into two different stores, they will drift apart. Not "might". Will.&lt;/p&gt;

&lt;p&gt;On a national-scale vehicle telemetry platform I work on, live GPS data flows through Kafka into Apache Druid, which powers real-time alerting and analytics. A relational SQL database sits next to it for reporting and the parts of the product that want normal tables and joins. Both are supposed to tell the same story.&lt;/p&gt;

&lt;p&gt;This post is about the worker I built to make sure they actually do: a daily job that finds gaps, replays the missing data in throttled chunks, proves the fix worked, and never runs twice at the same time. The code below is a simplified version of the approach with example table and column names, in C# and SQL Server, but the design applies to any pair of stores.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why two stores drift
&lt;/h2&gt;

&lt;p&gt;Every one of these will happen eventually in a streaming system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No shared transaction.&lt;/strong&gt; Writing an event to Druid and to SQL are two separate operations. If one succeeds and the other fails, nothing rolls the first one back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At-least-once delivery.&lt;/strong&gt; Kafka consumers can see the same message twice after a rebalance or restart. One sink deduplicates, the other doesn't, and now the counts disagree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outages on one side.&lt;/strong&gt; If the SQL writer is down for twenty minutes and the topic's retention or the consumer's offset handling isn't perfect, that window is just gone from one store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Late data.&lt;/strong&gt; Devices that lose signal buffer readings and send them later. Druid accepts late events into old time intervals. A one-time copy job that already ran won't see them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can reduce each of these, but you can't make the risk zero. So the honest design is: &lt;strong&gt;accept that drift happens, detect it cheaply, repair it safely, and prove the repair.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Design goals
&lt;/h2&gt;

&lt;p&gt;Before writing code, I wrote down what the worker must never do and what it must always do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Never hurt the live system.&lt;/strong&gt; Druid is serving real-time alerts. A background job that floods it with heavy queries is worse than the drift it fixes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be idempotent.&lt;/strong&gt; Running the same repair twice must leave the data exactly as running it once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be resumable.&lt;/strong&gt; If the job dies halfway, the next run continues instead of starting over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prove the result.&lt;/strong&gt; "I wrote some rows" isn't success. Success is "the two stores now agree for this window", verified after the write.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run exactly once at a time.&lt;/strong&gt; Two copies running concurrently would double the load and race each other's writes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything below exists to satisfy one of those five rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of the worker
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each day in the lookback window:
    split the day into 4-hour chunks
    for each chunk not already verified:
        compare Druid vs SQL (cheap)
        if they match: mark verified, continue
        replay the chunk from Druid into SQL (throttled, idempotent)
        verify again, retry with backoff if still different
        record the outcome
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details in that loop carry most of the weight: the chunk size, the comparison, and the lookback window.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why chunks, and why 4 hours
&lt;/h3&gt;

&lt;p&gt;Reconciling a whole day in one query means one huge Druid scan, one huge result set in memory, and one huge write to SQL. If anything fails, the whole day fails.&lt;/p&gt;

&lt;p&gt;We used &lt;strong&gt;4-hour chunks&lt;/strong&gt;. The trade-off is simple: small enough that a chunk's Druid query and SQL write finish in a predictable time and memory footprint, large enough that a day is 6 round trips instead of hundreds. If a chunk fails, you retry 4 hours of data, not 24. Pick yours by measuring how long one chunk takes under real load, not by guessing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compare cheap first, expensive only when needed
&lt;/h3&gt;

&lt;p&gt;Most chunks will match, so the comparison has to be cheap. Start with counts per chunk:&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;-- Druid SQL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS cnt
FROM "vehicle_telemetry"
WHERE __time &amp;gt;= ? AND __time &amp;lt; ?
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- SQL Server&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="cm"&gt;(*) AS cnt
FROM dbo.VehicleTelemetry
WHERE EventTimeUtc &amp;gt;= @From AND EventTimeUtc &amp;lt; @To;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two traps here that will bite you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rollup.&lt;/strong&gt; If your Druid datasource has rollup enabled, &lt;code&gt;COUNT(*)&lt;/code&gt; counts rolled-up rows, not original events. Compare against the ingested count metric instead, usually &lt;code&gt;SUM("count")&lt;/code&gt;, or your counts will never match.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time zones.&lt;/strong&gt; Druid's &lt;code&gt;__time&lt;/code&gt; is UTC. If the SQL side stores local time, every chunk boundary is shifted and you'll "find" gaps that don't exist. Store UTC on both sides and compare UTC windows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only when counts differ do you need anything more expensive, like comparing per device or per key to find exactly which rows are missing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Look back further than one day
&lt;/h3&gt;

&lt;p&gt;Because late data lands in old intervals, a job that only checks "yesterday" will miss it. Re-check a rolling window of several days, and skip chunks that are already verified using a checkpoint table, so the extra lookback costs almost nothing when there's no drift.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not hurting Druid: throttling and query priority
&lt;/h2&gt;

&lt;p&gt;The replay query is the expensive part, and it hits the same cluster that serves live alerts. Two protections:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Tell Druid this query is low priority.&lt;/strong&gt; Druid's query context has a &lt;code&gt;priority&lt;/code&gt; setting. Higher values win when the cluster is busy, so background work should go below the default of 0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; @&lt;span class="s2"&gt;"SELECT __time, ""device_id"", ""lat"", ""lon"", ""speed""
              FROM ""vehicle_telemetry""
              WHERE __time &amp;gt;= ? AND __time &amp;lt; ?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="k"&gt;object&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;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"TIMESTAMP"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromUtc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"yyyy-MM-dd HH:mm:ss"&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;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"TIMESTAMP"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToUtc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"yyyy-MM-dd HH:mm:ss"&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;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120_000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;resultFormat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_druid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PostAsJsonAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/druid/v2/sql/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EnsureSuccessStatusCode&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;&lt;strong&gt;2. Throttle the replay.&lt;/strong&gt; Write in fixed-size batches with a pause between them, and back off when the database is slow. A simple version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;batch&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&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="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartNew&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;await&lt;/span&gt; &lt;span class="nc"&gt;UpsertBatchAsync&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;ct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// If SQL is getting slower, give it more room&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pause&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Elapsed&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromSeconds&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="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromMilliseconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pause&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&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;It's deliberately boring. The goal isn't to repair as fast as possible. It's to repair without anyone noticing the repair is running. Schedule it off-peak too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotent writes: staging table plus MERGE
&lt;/h2&gt;

&lt;p&gt;Replaying a chunk will re-send rows that already exist in SQL. That has to be harmless. The pattern that works well on SQL Server is: bulk copy the batch into a staging table, then merge on the natural key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// #TelemetryStaging is created earlier on the same connection, with the same columns as the target&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;bulk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SqlBulkCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SqlBulkCopyOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tx&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;bulk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DestinationTableName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"#TelemetryStaging"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;bulk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteToServerAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;batchTable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;MERGE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VehicleTelemetry&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;HOLDLOCK&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;target&lt;/span&gt;
&lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="c1"&gt;#TelemetryStaging AS src&lt;/span&gt;
   &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DeviceId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DeviceId&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EventTimeUtc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EventTimeUtc&lt;/span&gt;
&lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="no"&gt;MATCHED&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="no"&gt;TARGET&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
    &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DeviceId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EventTimeUtc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Lat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Lon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Speed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DeviceId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EventTimeUtc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Lat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Lon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Speed&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;A few notes on this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The natural key (here device plus event time) needs a &lt;strong&gt;unique index&lt;/strong&gt;, both for MERGE performance and as the final guard against duplicates.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HOLDLOCK&lt;/code&gt; matters. Without it, MERGE can race with the live writer and insert duplicates under concurrency.&lt;/li&gt;
&lt;li&gt;Only insert what's missing. Updating existing rows during a repair is a different, riskier decision, and you should make it on purpose, not by accident.&lt;/li&gt;
&lt;li&gt;On PostgreSQL, the same idea is &lt;code&gt;INSERT ... ON CONFLICT (device_id, event_time) DO NOTHING&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Verify, then verify again
&lt;/h2&gt;

&lt;p&gt;After the write, the worker runs the same cheap comparison for that chunk. If the counts match, the chunk is marked verified. If not, it retries with exponential backoff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="o"&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;await&lt;/span&gt; &lt;span class="nc"&gt;ReplayChunkAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;druid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;CompareAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;druid&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;MarkVerifiedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;druid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&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="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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pow&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="n"&gt;attempt&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;ct&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;await&lt;/span&gt; &lt;span class="nc"&gt;MarkFailedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// and alert a human&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why would counts still differ right after a successful write? Usually because the source is still moving: late events are landing in that same Druid interval while you compare. Waiting and re-checking resolves that case. If a chunk still disagrees after the retries, it's a real problem, and a human should look at it. The worker's job is to fix the routine cases and escalate the weird ones, not to loop forever.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exactly one runner: a lock with a lease
&lt;/h2&gt;

&lt;p&gt;If a deploy, a scheduler hiccup or an operator starts a second copy, you get double load and racing writes. A database row works well as a distributed lock, as long as it &lt;strong&gt;expires&lt;/strong&gt;. Otherwise a crashed worker holds the lock forever.&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;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;JobLease&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;JobName&lt;/span&gt;      &lt;span class="n"&gt;sysname&lt;/span&gt;      &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Owner&lt;/span&gt;        &lt;span class="nf"&gt;nvarchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ExpiresAtUtc&lt;/span&gt; &lt;span class="n"&gt;datetime2&lt;/span&gt;    &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Seed once: INSERT dbo.JobLease (JobName, Owner, ExpiresAtUtc) VALUES ('druid-sql-resync', NULL, '2000-01-01');&lt;/span&gt;

&lt;span class="c1"&gt;-- Acquire (or renew) the lease. Succeeds only if it's free, expired, or already ours.&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;JobLease&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@Me&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ExpiresAtUtc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DATEADD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MINUTE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SYSUTCDATETIME&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;WHERE&lt;/span&gt; &lt;span class="nc"&gt;JobName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'druid-sql-resync'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ExpiresAtUtc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SYSUTCDATETIME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="nc"&gt;Owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@Me&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- @@ROWCOUNT = 1 means we hold the lease&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The worker renews the lease between chunks, well before it expires. If the process dies, the lease simply runs out and the next run takes over. Because chunks are checkpointed, it continues where the dead one stopped.&lt;/p&gt;

&lt;p&gt;One more rule: &lt;strong&gt;check that you still hold the lease before each chunk's write&lt;/strong&gt;, not only at the start. If your process stalled for longer than the lease (a long GC pause, a frozen VM), another runner may now own the job, and you should stop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observability: make drift visible
&lt;/h2&gt;

&lt;p&gt;The checkpoint table doubles as the report. One row per chunk with the window, Druid count, SQL count, rows replayed, attempts and final status gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a daily answer to "do the two stores agree?" that doesn't require anyone to run queries by hand,&lt;/li&gt;
&lt;li&gt;a trend: if replays suddenly grow, something upstream broke, and the reconciliation numbers tell you before users do,&lt;/li&gt;
&lt;li&gt;a clear list of failed chunks for a human to investigate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point changes the conversation. Instead of "the report looks wrong, can someone check?", it becomes "this 4-hour chunk failed verification, here's the count difference".&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell anyone building one
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Assume drift and design for repair.&lt;/strong&gt; Trying to make two independent writes perfectly consistent is a losing fight. Detecting and repairing is a winnable one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protect the live system first.&lt;/strong&gt; Low query priority, small batches, pauses, off-peak scheduling. A repair job that causes an incident has failed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotency is the whole game.&lt;/strong&gt; If replaying is safe, retries are safe, and if retries are safe, the job can be simple.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify after you write.&lt;/strong&gt; Writing rows isn't success. Agreement is success.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Locks need leases, and leases need re-checking.&lt;/strong&gt; A lock that can't expire will eventually stop your job forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch the trend, not just the failures.&lt;/strong&gt; How much the worker repairs each day is one of the best health signals your pipeline has.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of the pieces here is exotic: counts, chunks, a MERGE and a lease row. The value comes from putting them together so the job is safe to run every day, safe to kill at any moment, and honest about whether it worked.&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>dotnet</category>
      <category>architecture</category>
      <category>kafka</category>
    </item>
    <item>
      <title>Cold starts are a latency budget problem: what a 9-second first request taught me about production AI agents</title>
      <dc:creator>Ibad Ahmed Khan</dc:creator>
      <pubDate>Tue, 01 Dec 2026 19:53:34 +0000</pubDate>
      <link>https://dev.to/ibadahmedkhan/cold-starts-are-a-latency-budget-problem-what-a-9-second-first-request-taught-me-about-production-29nh</link>
      <guid>https://dev.to/ibadahmedkhan/cold-starts-are-a-latency-budget-problem-what-a-9-second-first-request-taught-me-about-production-29nh</guid>
      <description>&lt;p&gt;One weekend, our AI phone agent started handing calls over to human agents. The service wasn't down. When we called the API ourselves, it answered. Real callers were still getting dropped.&lt;/p&gt;

&lt;p&gt;The cause was one number: &lt;strong&gt;the first login after a quiet period took 9.1 seconds.&lt;/strong&gt; After we moved the service to an always-on Linux container, the same login took &lt;strong&gt;0.28 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can read that as "IIS went to sleep, we woke it up". That's true, and it's also the least interesting part. The more useful lesson is about how to think about latency when the client on the other end is a machine with a stopwatch, not a human who will wait. This post covers the incident, where a .NET cold start actually spends its time, and the ladder of fixes from "never be cold" to "make cold impossible to miss".&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The real problem: a turn has a budget
&lt;/h2&gt;

&lt;p&gt;A human on a web page might sit through a slow first load. A voice agent can't. Every conversational turn has a budget, and every component spends from it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;speech-to-text finishing the caller's sentence&lt;/li&gt;
&lt;li&gt;the model deciding what to do&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;the tool call to your backend&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;text-to-speech starting to speak&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The platform running the agent also enforces its own timeout on tool calls. When a tool call blows past it, the agent doesn't wait politely. It falls back, apologises, or in our case hands the call to a human.&lt;/p&gt;

&lt;p&gt;So the question is never "is the API fast?". It's &lt;strong&gt;"does the slowest path a real caller can hit fit inside the turn budget?"&lt;/strong&gt; A 9-second login doesn't just miss that budget. It's several entire turns of dead air.&lt;/p&gt;

&lt;p&gt;That reframing matters, because it changes what you measure. Averages are useless here. A cold start happens once per idle period, gets diluted by thousands of warm requests, and vanishes from your mean and even your p95. The metric that matters is the &lt;strong&gt;first request after a quiet period or a restart&lt;/strong&gt;, and most dashboards don't show it at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Measure the cold path on purpose
&lt;/h2&gt;

&lt;p&gt;Before fixing anything, prove it's a cold start and not a slow endpoint. Warm path first:&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="k"&gt;for&lt;/span&gt; i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$(&lt;/span&gt;seq 1 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  curl -s -o /dev/null \
    -w &lt;span class="s2"&gt;"connect=%{time_connect}s ttfb=%{time_starttransfer}s total=%{time_total}s\n"&lt;/span&gt; \
    -X POST &lt;span class="s2"&gt;"https://api.example.com/auth/login"&lt;/span&gt; \
    -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; -d @login.json
  sleep 1
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the cold path, by waiting past IIS's default 20-minute idle timeout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sleep 1500
curl -s -o /dev/null -w &lt;span class="s2"&gt;"cold ttfb=%{time_starttransfer}s\n"&lt;/span&gt; \
  -X POST &lt;span class="s2"&gt;"https://api.example.com/auth/login"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; -d @login.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;time_connect&lt;/code&gt; low and &lt;code&gt;time_starttransfer&lt;/code&gt; huge means the network is fine and the server is busy doing something before it can answer. Huge first request, fast everything after: cold start. That rules out a whole class of wrong fixes, like tuning SQL for a process that isn't even running yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Where a .NET cold start actually spends its time
&lt;/h2&gt;

&lt;p&gt;"Cold start" isn't one thing. On a classic IIS deployment, the first request after an idle shutdown can pay for all of this, in order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Process activation.&lt;/strong&gt; The app pool's worker process was shut down for being idle, so IIS has to start a new one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime and assembly loading.&lt;/strong&gt; The CLR starts, and your app and its dependencies load from disk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JIT compilation.&lt;/strong&gt; Every method on the hot path gets compiled on first call. Tiered compilation helps, but the first pass is still real work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;App initialisation.&lt;/strong&gt; Configuration binding, building the dependency injection container, ORM model building, anything you do in startup or in constructors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The first database connection.&lt;/strong&gt; The pool is empty, so you pay for TCP, TLS and authentication to the database before the first query runs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The first outbound calls.&lt;/strong&gt; DNS lookups and TLS handshakes to any downstream service the login touches.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each step on its own is often modest. Stacked up on one unlucky request, they add up to seconds.&lt;/p&gt;

&lt;p&gt;You don't have to guess which steps hurt most. Log timings around the phases at startup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Stopwatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartNew&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="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&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;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Host built in {Ms} ms"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ElapsedMilliseconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Restart&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;await&lt;/span&gt; &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SqlConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connectionString&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;await&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OpenAsync&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;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"First DB connection in {Ms} ms"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ElapsedMilliseconds&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;For a deeper look, &lt;code&gt;dotnet-counters&lt;/code&gt; shows JIT and assembly-load activity live, and &lt;code&gt;dotnet-trace&lt;/code&gt; captures a startup trace you can open in PerfView or Visual Studio.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. The ladder of fixes
&lt;/h2&gt;

&lt;p&gt;Think of these as levels. Our fix was level 1, and it alone took the first request from 9.1 seconds to 0.28. The higher levels are what make the result durable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Level 1: never be cold
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;If you must stay on IIS&lt;/strong&gt;, turn off the behaviours that put the app to sleep:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nc"&gt;Import&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Module&lt;/span&gt; &lt;span class="nc"&gt;WebAdministration&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemProperty&lt;/span&gt; &lt;span class="s2"&gt;"IIS:\AppPools\MyApiPool"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;startMode&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="nc"&gt;AlwaysRunning&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemProperty&lt;/span&gt; &lt;span class="s2"&gt;"IIS:\AppPools\MyApiPool"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;processModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;idleTimeout&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemProperty&lt;/span&gt; &lt;span class="s2"&gt;"IIS:\AppPools\MyApiPool"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;recycling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;periodicRestart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;ItemProperty&lt;/span&gt; &lt;span class="s2"&gt;"IIS:\Sites\MyApiSite"&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;applicationDefaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preloadEnabled&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Value&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the &lt;strong&gt;Application Initialization&lt;/strong&gt; module so IIS makes the first, expensive request itself. Be aware the app pool still recycles on configuration changes and deploys, so this reduces cold starts rather than eliminating them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we did instead&lt;/strong&gt; was move the service to Linux and run it in Docker. Kestrel has no idle shutdown: if the container is up, the app is up. &lt;code&gt;restart: always&lt;/code&gt; covers crashes and host reboots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"8080:8080"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"CMD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"curl"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-f"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"http://localhost:8080/health/ready"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;30s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The &lt;code&gt;aspnet&lt;/code&gt; base image doesn't ship &lt;code&gt;curl&lt;/code&gt;, so install it in the final stage or use a small health-check binary.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Level 2: make starting cheap
&lt;/h3&gt;

&lt;p&gt;An always-on process still starts after every deploy and restart. Make that start cheaper:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ReadyToRun.&lt;/strong&gt; Precompile your assemblies so far less code is JIT-compiled at startup:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PublishReadyToRun&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/PublishReadyToRun&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;RuntimeIdentifier&amp;gt;&lt;/span&gt;linux-x64&lt;span class="nt"&gt;&amp;lt;/RuntimeIdentifier&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep connections ready.&lt;/strong&gt; Set a minimum pool size so the pool isn't empty after a quiet stretch, for example &lt;code&gt;Min Pool Size=5&lt;/code&gt; in a SQL Server connection string (Npgsql calls it &lt;code&gt;Minimum Pool Size&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do the expensive work at startup, on purpose.&lt;/strong&gt; A hosted service that resolves your hot-path services and opens one database connection moves DI, JIT and connection costs off the first caller and onto the app itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Level 3: don't take traffic until you're warm
&lt;/h3&gt;

&lt;p&gt;"The process is running" and "the process is ready" are different states. Expose them separately, so your orchestrator or load balancer only routes traffic once warm-up has finished:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddSingleton&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WarmupState&lt;/span&gt;&lt;span class="o"&gt;&amp;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;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddHostedService&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WarmupService&lt;/span&gt;&lt;span class="o"&gt;&amp;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="c1"&gt;// sets WarmupState.IsReady = true when done&lt;/span&gt;
&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddHealthChecks&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="nc"&gt;AddCheck&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WarmupHealthCheck&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"warmup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&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="s2"&gt;"ready"&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="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&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;// Liveness: is the process alive at all?&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MapHealthChecks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/health/live"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HealthCheckOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Predicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;span class="c1"&gt;// Readiness: has warm-up finished? Route traffic only when this passes.&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MapHealthChecks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/health/ready"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HealthCheckOptions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Predicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ready"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;WarmupHealthCheck&lt;/code&gt; just returns &lt;code&gt;Unhealthy&lt;/code&gt; until &lt;code&gt;WarmupState.IsReady&lt;/code&gt; is true. Now a fresh deploy can't serve its cold first request to a caller, because nothing routes to it until it has served that request to itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Level 4: design the caller for the worst case
&lt;/h3&gt;

&lt;p&gt;The backend is only half the contract. On the agent side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Give every tool call an &lt;strong&gt;explicit timeout below the platform's&lt;/strong&gt;, so you decide what happens when it's exceeded instead of the platform deciding for you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't make the slow path the common path.&lt;/strong&gt; If every call starts with a login, cache the session or token and reuse it, so authentication isn't on every turn.&lt;/li&gt;
&lt;li&gt;Have a &lt;strong&gt;graceful fallback the caller hears&lt;/strong&gt;, like a short "one moment while I pull that up", rather than silence followed by a transfer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Level 5: make cold impossible to miss
&lt;/h3&gt;

&lt;p&gt;We found this one through dropped calls, because nothing was measuring the path that broke. Fix the observability, not just the bug:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run a &lt;strong&gt;synthetic check that exercises the real path&lt;/strong&gt; (the login, not &lt;code&gt;/health&lt;/code&gt;) on a schedule, and alert on its latency, not just its status code.&lt;/li&gt;
&lt;li&gt;Track &lt;strong&gt;max and first-request latency after restart&lt;/strong&gt; alongside your percentiles.&lt;/li&gt;
&lt;li&gt;After every deploy, automatically time the first real request and fail loudly if it's outside the budget.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. The result
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before (IIS, defaults)&lt;/th&gt;
&lt;th&gt;After (always-on container)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;First login after a quiet period&lt;/td&gt;
&lt;td&gt;9.1 s&lt;/td&gt;
&lt;td&gt;0.28 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The weekend fallbacks stopped, because there was no longer a cold state for a caller to land in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I took away
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Design for the slowest path a real client can hit&lt;/strong&gt;, not the average. Machines with timeouts experience your p100, not your mean.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Fast" and "fast on the first request" are different claims.&lt;/strong&gt; Measure both, separately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Know your platform's defaults.&lt;/strong&gt; Idle shutdowns and scheduled recycles are sensible for websites and wrong for anything an automated client calls with a short timeout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate alive from ready.&lt;/strong&gt; Most cold-start incidents are traffic arriving one step too early.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor the path that pays the bill.&lt;/strong&gt; A health endpoint that returns 200 tells you nothing about whether your callers can log in within their budget.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The fix that solved our incident was a single platform change. The lesson that will stop the next one is treating latency as a budget that every path, including the cold one, has to fit inside.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>performance</category>
      <category>sre</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
