<?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: Mostafa Tavassoli</title>
    <description>The latest articles on DEV Community by Mostafa Tavassoli (@mostavassoli).</description>
    <link>https://dev.to/mostavassoli</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%2F951075%2Ff26cdb9e-c820-4240-97aa-ff5141e5f188.jpg</url>
      <title>DEV Community: Mostafa Tavassoli</title>
      <link>https://dev.to/mostavassoli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mostavassoli"/>
    <language>en</language>
    <item>
      <title>What happens when a 40-second LLM call outlives Kafka's 30-second lock</title>
      <dc:creator>Mostafa Tavassoli</dc:creator>
      <pubDate>Wed, 25 Nov 2026 17:32:59 +0000</pubDate>
      <link>https://dev.to/mostavassoli/what-happens-when-a-40-second-llm-call-outlives-kafkas-30-second-lock-2ain</link>
      <guid>https://dev.to/mostavassoli/what-happens-when-a-40-second-llm-call-outlives-kafkas-30-second-lock-2ain</guid>
      <description>&lt;p&gt;Kafka 4.2 made share groups production-ready: queue-style consumption where several consumers take records from the same partitions. Each record is locked to one consumer while it works on it. The default lock is 30 seconds.&lt;/p&gt;

&lt;p&gt;Now put an AI agent behind that consumer. One event means a model call, maybe a tool call or two, maybe a retry. Forty seconds is not unusual.&lt;/p&gt;

&lt;p&gt;So what happens when the agent is still thinking when the lock expires?&lt;/p&gt;

&lt;p&gt;I measured it. Short version: the same event is processed again, up to five times, and every side effect it triggers can happen again. The fixes that work are not the ones I expected, and one framework's built-in fix did not hold.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this matters for agents
&lt;/h2&gt;

&lt;p&gt;Classic Kafka consumers are fast: milliseconds per record. A 30-second lock is generous for them. Agents break that assumption:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model latency varies a lot, and the tail is long.&lt;/li&gt;
&lt;li&gt;Agents call tools, and tools have side effects: create a ticket, send an email, issue a refund.&lt;/li&gt;
&lt;li&gt;An agent that runs twice on the same event does not just waste tokens. It can act twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In share groups, a record whose lock expires is delivered again to the next available consumer, with an increasing delivery count, up to a delivery limit (5 by default). That is correct queue behavior. It is just bad news when one delivery takes longer than the lock.&lt;/p&gt;

&lt;h2&gt;
  
  
  The experiment
&lt;/h2&gt;

&lt;p&gt;I built this into &lt;a href="https://github.com/mosTavassoli/streamjack" rel="noopener noreferrer"&gt;Streamjack&lt;/a&gt;, an open-source harness I'm writing that tests AI agents consuming and producing Kafka events. The test runs on a disposable Kafka 4.2.1 broker with two simulated agent instances in one share group. Each record takes 3 seconds of "thinking" and then triggers one side effect. Timings are scaled down (a 2-second lock instead of 30) so the test runs in seconds; the ratio is the same.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Case&lt;/th&gt;
&lt;th&gt;Lock&lt;/th&gt;
&lt;th&gt;Deliveries&lt;/th&gt;
&lt;th&gt;Side effects&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Processing blocks the consumer thread&lt;/td&gt;
&lt;td&gt;2 s&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same, plus deduplication of the side effect&lt;/td&gt;
&lt;td&gt;2 s&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lock longer than processing&lt;/td&gt;
&lt;td&gt;6 s&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lock renewed from a free consumer thread while working&lt;/td&gt;
&lt;td&gt;3 s (4.5 s of work)&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It runs in CI on every commit, and each case also asserts that processing finished before the deadline and that exactly one acknowledgement succeeded where the record is delivered once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things I learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Deduplication fixes the side effects, not the work.&lt;/strong&gt;&lt;br&gt;
With an idempotency key on the side effect, the duplicate actions disappear. But the model still ran five times. For an LLM agent that is five times the cost and latency, and five chances for a non-deterministic model to decide something different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. A longer lock works, if you know your worst case.&lt;/strong&gt;&lt;br&gt;
The broker caps the lock (&lt;code&gt;group.share.max.record.lock.duration.ms&lt;/code&gt;, 60 seconds by default). If your agent's p99.9 is above the cap, you cannot configure your way out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Renewal works, but only from a thread that is free to renew.&lt;/strong&gt;&lt;br&gt;
Kafka 4.2 lets a consumer extend the lock with &lt;code&gt;AcknowledgeType.RENEW&lt;/code&gt;. The catch: &lt;code&gt;KafkaShareConsumer&lt;/code&gt; is not thread-safe, so the thread that talks to Kafka must stay free while another thread does the slow work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;CompletableFuture&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CompletableFuture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runAsync&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;slowWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;work&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;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isDone&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="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toMillis&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="mi"&gt;4&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;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isDone&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;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;acknowledge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AcknowledgeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;RENEW&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commitSync&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;// The renewed record comes back through a later poll(); acknowledge that copy from now on.&lt;/span&gt;
    &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;pollFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&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;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;acknowledge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AcknowledgeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ACCEPT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commitSync&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;A simple poll loop that does the work inline can never renew. That is exactly the "5 deliveries, 5 side effects" row.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about frameworks?
&lt;/h2&gt;

&lt;p&gt;You would expect your messaging framework to handle renewal. It depends.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Spring for Apache Kafka 4.2&lt;/strong&gt; has &lt;code&gt;setShareAutoRenewLocks(true)&lt;/code&gt;. It is off by default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SmallRye Reactive Messaging&lt;/strong&gt; (the Kafka connector behind Quarkus) renews automatically. In my experiments, with processing slower than the lock, the renewal did not keep the lock: the record was delivered again with an increasing delivery count, and a later record was acknowledged by the broker but never reached the handler. In the scaled-down runs, renewal also failed at every interval afterwards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With SmallRye's default settings (30-second lock, renewal checked every 5 seconds) and a 40-second simulated model call, the first event was analyzed twice, and the next event still had not reached the agent 150 seconds later.&lt;/p&gt;

&lt;p&gt;I reproduced this on Quarkus 3.40 and on a Quarkus 4 release candidate, built a &lt;a href="https://github.com/mosTavassoli/smallrye-share-group-renewal-repro" rel="noopener noreferrer"&gt;minimal reproducer&lt;/a&gt; that also fails on GitHub Actions, and &lt;a href="https://github.com/smallrye/smallrye-reactive-messaging/issues/3636" rel="noopener noreferrer"&gt;reported it upstream&lt;/a&gt;. The root cause is not confirmed yet, so treat this as "test it in your setup", not "this framework is broken".&lt;/p&gt;

&lt;p&gt;One more detail from the reproducer: with a very short lock, even a fast handler was sometimes redelivered, apparently because renewal is timed from when the connector received the record. With default settings, renewal starts about 25 seconds in, so this mainly hits slow handlers. Like agents.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd do
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Make side effects idempotent in application code.&lt;/strong&gt; It was the only control that held in every case, including the framework issue. Not in the prompt: in code, keyed by the business id.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Size the lock for your worst-case latency&lt;/strong&gt;, within the broker's cap. Measure the tail, not the average.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If you rely on renewal, test it&lt;/strong&gt; with a handler that is slower than the lock. Check the delivery count, and check that the next record is still processed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The general lesson is older than share groups: an at-least-once system will deliver twice, and an agent that acts must be safe to run twice. LLM latency just makes "twice" much more likely.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;The tests, the opt-in framework experiments, and a longer write-up are in &lt;a href="https://github.com/mosTavassoli/streamjack/blob/main/docs/share-group-redelivery.md" rel="noopener noreferrer"&gt;Streamjack&lt;/a&gt;. There is also a &lt;a href="https://mostavassoli.github.io/streamjack/" rel="noopener noreferrer"&gt;browser demo&lt;/a&gt; of the agent attacks Streamjack tests for.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>ai</category>
      <category>java</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
