<?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: Chedy Missaoui</title>
    <description>The latest articles on DEV Community by Chedy Missaoui (@tech_dominator_chedy).</description>
    <link>https://dev.to/tech_dominator_chedy</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%2F4274346%2Fbc90ab89-f347-4af0-bb21-9e1fc398e48e.png</url>
      <title>DEV Community: Chedy Missaoui</title>
      <link>https://dev.to/tech_dominator_chedy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tech_dominator_chedy"/>
    <language>en</language>
    <item>
      <title>Idempotent Event Processing With Azure Event Hubs Part 2 - Remember What You Processed</title>
      <dc:creator>Chedy Missaoui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 13:47:49 +0000</pubDate>
      <link>https://dev.to/tech_dominator_chedy/idempotent-event-processing-with-azure-event-hubs-part-2-remember-what-you-processed-2kf7</link>
      <guid>https://dev.to/tech_dominator_chedy/idempotent-event-processing-with-azure-event-hubs-part-2-remember-what-you-processed-2kf7</guid>
      <description>&lt;p&gt;In the &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html" rel="noopener noreferrer"&gt;previous post&lt;/a&gt;, we discussed why Azure Event Hubs consumers should be idempotent: duplicates can appear &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#duplicates-while-sending" rel="noopener noreferrer"&gt;while sending&lt;/a&gt; and &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#duplicates-while-receiving" rel="noopener noreferrer"&gt;while receiving&lt;/a&gt;, and checkpointing alone can't remove them. We also reviewed the &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#idempotent-processing-theory-and-state-of-the-art" rel="noopener noreferrer"&gt;theory and the state of the art&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This post is the first of four, each covering one technique I've found useful for implementing idempotent event processing. The techniques are ordered from most generic to most domain-specific. We start with the most generic one: a dedicated store that records the events already handled, and the tradeoffs that come with it.&lt;/p&gt;

&lt;p&gt;For each technique, you'll see whether it uses &lt;em&gt;explicit deduplication&lt;/em&gt; or relies on &lt;em&gt;message semantics&lt;/em&gt;, as discussed in &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#enterprise-integration-patterns" rel="noopener noreferrer"&gt;Enterprise Integration Patterns&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;For simplicity, the examples focus on one consumer's processing logic. Making non-transactional side effects, such as calling an external system, safe is beyond the scope of this series.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full working sample&lt;/strong&gt; &lt;a href="https://github.com/MissaouiChedy/BlogSamples/tree/main/AzureEventHubsIdempotentProcessing" rel="noopener noreferrer"&gt;is available on Github as usual.&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Remember What You Processed
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Type:&lt;/strong&gt; explicit deduplication.&lt;/p&gt;

&lt;p&gt;The first technique is the most generic one: the consumer keeps a record of the events it already handled, and skips an event when its identifier is in that record. It doesn't depend on any business data, which makes it usable when the side effect has &lt;strong&gt;no natural state&lt;/strong&gt; to inspect.&lt;/p&gt;

&lt;p&gt;The code in this section comes from the &lt;code&gt;IdempotencyTechnique1&lt;/code&gt; project of the &lt;code&gt;AzureEventHubsIdempotentProcessing&lt;/code&gt; sample.&lt;/p&gt;

&lt;p&gt;In the sample, the consumer handles &lt;code&gt;OrderPlaced&lt;/code&gt; events and sends an order confirmation email for each one. This is a good fit for the technique because an email can't be queried afterwards ("was this email already sent?") and can't share a transaction with anything else.&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;public&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;OrderPlaced&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;CustomerEmail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nc"&gt;Total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;DateTime&lt;/span&gt; &lt;span class="nc"&gt;PlacedAt&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;The &lt;code&gt;IdempotencyTechnique1&lt;/code&gt; console app runs a sender and a consumer concurrently, on a 3-partition event hub with 1 day of retention. The consumer relies on three elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A checkpoint store:&lt;/strong&gt; a blob container, updated every 2 events per partition to make replays easy to trigger.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A processed events store:&lt;/strong&gt; an Azure Managed Redis instance holding the identifiers of processed events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A fake email sender:&lt;/strong&gt; prints a line to the console, standing in for an email provider.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The deduplication key is the &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/azure.messaging.eventhubs.eventdata.messageid?view=azure-dotnet" rel="noopener noreferrer"&gt;&lt;code&gt;MessageId&lt;/code&gt;&lt;/a&gt; of the event. It is set by the producer, &lt;strong&gt;once per logical event and before sending.&lt;/strong&gt;&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="nc"&gt;EventData&lt;/span&gt; &lt;span class="n"&gt;eventData&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="nc"&gt;BinaryData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FromObjectAsJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&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="cm"&gt;/*
     * Assigned once per logical event,
     * so that SDK send retries carry
     * the same value
     */&lt;/span&gt;
    &lt;span class="nc"&gt;MessageId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Guid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewGuid&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;ToString&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;await&lt;/span&gt; &lt;span class="n"&gt;eventHubProducerClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SendAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;eventData&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="nc"&gt;SendEventOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;PartitionKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OrderId&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;This ordering matters. As discussed in &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#duplicates-while-sending" rel="noopener noreferrer"&gt;Duplicates While Sending&lt;/a&gt;, a retried send creates a second event with its own offset and sequence number. Only a value assigned by the producer before the first attempt can tell the consumer that both events represent the same business fact.&lt;/p&gt;

&lt;p&gt;To reproduce this, the sample can resend an already sent order with the same body and the same &lt;code&gt;MessageId&lt;/code&gt;. This is what a producer retry after a lost acknowledgment looks like from the consumer side.&lt;/p&gt;

&lt;p&gt;The responsibility of the &lt;code&gt;ProcessedEventsStore&lt;/code&gt; is to answer one question ("did I see this &lt;code&gt;MessageId&lt;/code&gt;?") and to record new ones. Each identifier becomes a Redis key with a TTL.&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;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;IsProcessedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;messageId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*
     * EXPIRE only succeeds on an existing
     * key: checks and restarts the TTL
     * in one atomic command
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KeyExpireAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nc"&gt;GetProcessedEventKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messageId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;_processedEventTtl&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;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;MarkAsProcessedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;messageId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StringSetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nc"&gt;GetProcessedEventKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messageId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UtcNow&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;"o"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="cm"&gt;/*
         * The key expires on its own
         */&lt;/span&gt;
        &lt;span class="n"&gt;_processedEventTtl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="cm"&gt;/*
         * SET NX: the first record
         * is kept
         */&lt;/span&gt;
        &lt;span class="nc"&gt;When&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NotExists&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;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;GetProcessedEventKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;messageId&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="s2"&gt;"{EventhubNamespace}|{Eventhub}|{ConsumerGroup}"&lt;/span&gt;
        &lt;span class="o"&gt;+&lt;/span&gt; $&lt;span class="s2"&gt;"|processed|{messageId}"&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;Three details are worth noting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The key is scoped by consumer group.&lt;/strong&gt; Each consumer group is an independent reader of the event hub, so a &lt;code&gt;MessageId&lt;/code&gt; processed by one group must not make another group skip the event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A skipped duplicate restarts the TTL.&lt;/strong&gt; A resent event is retained by the event hub from its own enqueue time, not from the original's. Without the refresh, a duplicate enqueued 20 hours after the original, and replayed 6 hours later, would find the key expired. &lt;code&gt;KeyExpireAsync&lt;/code&gt; only succeeds on an existing key, so the check and the refresh are a single atomic command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SET NX&lt;/code&gt; keeps the first record, it doesn't prevent concurrent duplicates.&lt;/strong&gt; When two consumers record the same &lt;code&gt;MessageId&lt;/code&gt;, only one of them gets &lt;code&gt;true&lt;/code&gt; back, but by then both have already sent the email (see the &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-2-remember-what-you-processed.html#tradeoffs-of-a-dedicated-store" rel="noopener noreferrer"&gt;tradeoffs&lt;/a&gt;). The sample doesn't use the return value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At runtime, the handler runs the same sequence for every received event. Consider the following simplified version of the sample's &lt;code&gt;ProcessEventHandler&lt;/code&gt;:&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="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;messageId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eventArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MessageId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_processedEventsStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsProcessedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messageId&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;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"RECEIVER: ==&amp;gt; Already processed, skipped"&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;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;OrderPlaced&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eventArgs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EventBody&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToObjectFromJson&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OrderPlaced&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="o"&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;_emailSender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SendOrderConfirmationAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/*
     * Failure window: a crash here
     * sends a duplicate email on
     * replay
     */&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_processedEventsStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MarkAsProcessedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messageId&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;CheckpointIfThresholdReachedAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eventArgs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;partitionId&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;The order of operations is the core of the technique: check, perform the side effect, record the identifier, and only then let the checkpoint logic run. A skipped event is still counted towards the checkpoint, otherwise a partition made of duplicates would never advance.&lt;/p&gt;

&lt;p&gt;For brevity, the sample logs failed events instead of retrying them, and assumes every event carries a &lt;code&gt;MessageId&lt;/code&gt;. A production consumer would need to decide what to do with events that don't.&lt;/p&gt;

&lt;p&gt;The main advantage is that &lt;strong&gt;no natural state is required.&lt;/strong&gt; Any handler can be wrapped this way, including ones whose side effect is opaque, such as sending an email or calling an external API.&lt;/p&gt;

&lt;h1&gt;
  
  
  Tradeoffs of a Dedicated Store
&lt;/h1&gt;

&lt;p&gt;The downsides are more numerous, and they explain why the next techniques exist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The two stores can't be updated in the same transaction.&lt;/strong&gt; The email provider and Redis don't share a transaction, so the write order is a choice. Recording the identifier &lt;strong&gt;after&lt;/strong&gt; the side effect (as the sample does) risks a duplicate email if the process crashes in between. Recording it &lt;strong&gt;before&lt;/strong&gt; risks losing the email: the replay is skipped while the email was never sent. As discussed in &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#does-checkpointing-at-every-event-solve-this" rel="noopener noreferrer"&gt;Does Checkpointing At Every Event Solve This?&lt;/a&gt;, I would prefer duplicates over losses here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent duplicates still have a window.&lt;/strong&gt; The &lt;code&gt;IsProcessedAsync&lt;/code&gt; check and the side effect aren't atomic. Handlers of a given partition run sequentially, but when partition ownership changes, two consumers can both pass the check before either one records the identifier. &lt;code&gt;SET NX&lt;/code&gt; keeps the record consistent, it doesn't stop the second email.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The TTL must exceed the worst-case duplicate window.&lt;/strong&gt; If an identifier expires and a duplicate arrives afterwards, the duplicate goes through. The window has two parts: replays, which can't reach further back than the event hub retention (1 day), and producer retries, which can resend an event long after the original. Restarting the TTL on each skipped duplicate covers the replays of a resent event, but not a retry that first arrives after the key expired. In the sample the producer resends within a run, so a 25 hours TTL covers both. A longer retention or a longer producer retry window requires a longer TTL, and a larger store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Durability is on you.&lt;/strong&gt; The sample uses a single Azure Managed Redis node without high availability and with the &lt;code&gt;VolatileTTL&lt;/code&gt; eviction policy, which evicts keys with the shortest TTL under memory pressure. A failover, a restart or an eviction silently forgets identifiers, and the corresponding duplicates are processed again. Treat the store as part of the correctness of your system, not as a disposable cache.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It adds a dependency and a round trip per event.&lt;/strong&gt; Each event costs at least one read, and one write for new events, on a store that must be available for the consumer to make progress.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Closing Thoughts
&lt;/h1&gt;

&lt;p&gt;A dedicated store is the most generic technique, since it can wrap any handler, including ones with an opaque side effect such as sending an email. The price is an extra dependency, a TTL to size, and a failure window between the side effect and the record that no write order fully closes.&lt;/p&gt;

&lt;p&gt;When the side effect leaves a trace in your own data, you can remove the store. The next post, &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-3-rely-on-natural-state.html" rel="noopener noreferrer"&gt;Part 3 - Rely on Natural State&lt;/a&gt;, uses the business data as the record of what was processed.&lt;/p&gt;

</description>
      <category>azureeventhub</category>
      <category>eventdriven</category>
      <category>azure</category>
    </item>
    <item>
      <title>Idempotent Event Processing With Azure Event Hubs Part 1</title>
      <dc:creator>Chedy Missaoui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 13:42:39 +0000</pubDate>
      <link>https://dev.to/tech_dominator_chedy/idempotent-event-processing-with-azure-event-hubs-part-1-p0a</link>
      <guid>https://dev.to/tech_dominator_chedy/idempotent-event-processing-with-azure-event-hubs-part-1-p0a</guid>
      <description>&lt;p&gt;When you consume events from Azure Event Hubs, the same event can reach your handlers more than once. &lt;/p&gt;

&lt;p&gt;This means most consumer processing logic should be &lt;strong&gt;idempotent:&lt;/strong&gt; handling an event a second time should leave the system in the same state as handling it once. &lt;/p&gt;

&lt;p&gt;Idempotency here is a property of the processing logic you write, and not necessarily a feature of Azure Event Hubs.&lt;/p&gt;

&lt;p&gt;This first post covers why idempotency matters and the theory behind it. The upcoming second part will share concrete techniques that have been useful to me for implementing idempotent event processing, stay tuned 😉&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Idempotency?
&lt;/h1&gt;

&lt;p&gt;Event Hubs consumers typically get an &lt;a href="https://learn.microsoft.com/en-us/azure/architecture/serverless/event-hubs-functions/resilient-design#idempotency" rel="noopener noreferrer"&gt;&lt;strong&gt;at-least-once&lt;/strong&gt; event delivery guarantee:&lt;/a&gt; an event can be delivered and processed one or more times; you shouldn't expect exactly once.&lt;/p&gt;

&lt;p&gt;Duplicates can appear at two points of the pipeline: when sending and when receiving.&lt;/p&gt;

&lt;h2&gt;
  
  
  Duplicates While Sending
&lt;/h2&gt;

&lt;p&gt;A producer that doesn't receive an acknowledgment can't tell whether the service stored the event. Typically, &lt;strong&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/azure.messaging.eventhubs.producer.eventhubproducerclientoptions.retryoptions?view=azure-dotnet" rel="noopener noreferrer"&gt;the SDK retry policy&lt;/a&gt; sends it again.&lt;/strong&gt; If the first attempt did succeed and only the acknowledgment was lost, the event hub now holds two copies.&lt;/p&gt;

&lt;p&gt;At runtime, the two copies are separate events with their own offsets and sequence numbers. The consumer has no built-in way to know they represent the same business fact and should &lt;strong&gt;rely on application specific keys&lt;/strong&gt; to determine uniqueness. &lt;/p&gt;

&lt;h2&gt;
  
  
  Duplicates While Receiving
&lt;/h2&gt;

&lt;p&gt;As discussed in the &lt;a href="https://blog.techdominator.com/article/azure-event-hubs-checkpoints-and-rewinding.html" rel="noopener noreferrer"&gt;Azure Event Hubs Checkpoints &amp;amp; Rewinding&lt;/a&gt; post, consumers track progress with a &lt;strong&gt;checkpoint&lt;/strong&gt; stored in a checkpoint store. A new owner of a partition resumes from the last checkpoint (see also &lt;a href="https://learn.microsoft.com/en-us/azure/event-hubs/event-processor-balance-partition-load" rel="noopener noreferrer"&gt;Balance partition load across multiple instances&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We also discussed why &lt;a href="https://blog.techdominator.com/article/azure-event-hubs-checkpoints-and-rewinding.html#checkpoint-cost--best-practices" rel="noopener noreferrer"&gt;checkpointing at every event is not a good idea&lt;/a&gt;: each update has a cost, so checkpoints are typically batched.&lt;/p&gt;

&lt;p&gt;The consequence is that some events can be processed but not yet checkpointed, and those get processed again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbik3pgmp7u8cyfbcx8pt.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbik3pgmp7u8cyfbcx8pt.webp" alt="Idempotency Reprocessing" width="522" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Common situations include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Batched or interval checkpoints:&lt;/strong&gt; checkpointing every N events or every few seconds is cheaper, but widens the window of events that get replayed after a failure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No checkpoint on exit:&lt;/strong&gt; if the processor stops without writing a final checkpoint, the events handled since the last one are replayed on restart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crash:&lt;/strong&gt; the process dies between handling an event and checkpointing it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition ownership changes:&lt;/strong&gt; when instances join or leave (during auto-scale for example), partitions are rebalanced. The new owner starts from the last checkpoint, while the previous owner may still have been working on events past it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Does Checkpointing At Every Event Solve This?
&lt;/h2&gt;

&lt;p&gt;Checkpointing after each event narrows the replay window at a cost but doesn't close it. A crash can still occur in the event handler before the checkpoint operation.&lt;/p&gt;

&lt;p&gt;At runtime, the sequence looks like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The handler performs an operation.&lt;/li&gt;
&lt;li&gt;The process crashes before &lt;code&gt;UpdateCheckpointAsync&lt;/code&gt; completes.&lt;/li&gt;
&lt;li&gt;The next owner resumes from the previous checkpoint and handles the same event again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One might think about &lt;strong&gt;checkpointing before handling the event,&lt;/strong&gt; this drastically reduces duplicates but introduces a bigger risk of missing events.&lt;/p&gt;

&lt;p&gt;For most workloads, I would prefer duplicates over losses, and then make the handler tolerate them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Idempotent Processing Theory and State of The Art
&lt;/h1&gt;

&lt;p&gt;Idempotency is a widely discussed (dare I say classic?) subject in computer science and software engineering. Before looking at concrete techniques, let's look at some reference sources that discuss the matter.&lt;/p&gt;

&lt;p&gt;The term comes from mathematics, where an idempotent function produces the same result when applied to its own output: &lt;code&gt;f(x) = f(f(x))&lt;/code&gt;. In messaging, this translates into a message that has the same effect whether the receiver handles it once or several times.&lt;/p&gt;

&lt;p&gt;The following are the resources I found most useful, from the general pattern to the Azure specific guidance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enterprise Integration Patterns
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://martinfowler.com/books/eip.html" rel="noopener noreferrer"&gt;Enterprise Integration Patterns&lt;/a&gt; (EIP) is a classic messaging book. It describes the &lt;a href="https://www.enterpriseintegrationpatterns.com/patterns/messaging/IdempotentReceiver.html" rel="noopener noreferrer"&gt;Idempotent Receiver&lt;/a&gt; pattern, which starts from an observation: even when a sender sends a message once, the receiver may receive it more than once.&lt;/p&gt;

&lt;p&gt;The proposed solution is to design a receiver that can safely receive the same message multiple times. EIP identifies two primary means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Explicit deduplication:&lt;/strong&gt; remove duplicate messages, typically by remembering which ones were already handled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message semantics:&lt;/strong&gt; define messages so that applying them again is harmless, for example "set the quantity to 5" instead of "add 1 to the quantity".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each technique in the upcoming part 2 will indicate which of these two categories it belongs to. The pattern is also related to &lt;a href="https://www.enterpriseintegrationpatterns.com/patterns/messaging/GuaranteedMessaging.html" rel="noopener noreferrer"&gt;Guaranteed Delivery&lt;/a&gt;: a messaging system that persists messages to avoid losing them will, in some failure cases, deliver them again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Azure Architecture Center and Azure Functions
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/azure/architecture/patterns/idempotent-consumer" rel="noopener noreferrer"&gt;Idempotent Consumer pattern&lt;/a&gt; in the Azure Architecture Center is the same idea applied to brokers such as Event Hubs, Service Bus, Kafka and RabbitMQ. It states that exactly-once delivery is impractical across a distributed system, and that at-least-once delivery combined with a consumer that ignores duplicates gives &lt;strong&gt;effectively-once&lt;/strong&gt; processing.&lt;/p&gt;

&lt;p&gt;It also turns the deduplication option into an operational checklist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose a &lt;strong&gt;stable deduplication key&lt;/strong&gt; that survives redelivery, such as a producer-assigned message identifier or a business-level idempotency key.&lt;/li&gt;
&lt;li&gt;Decide where to store processed keys: a dedicated table (sometimes called an &lt;em&gt;inbox&lt;/em&gt;) or the business entity itself.&lt;/li&gt;
&lt;li&gt;Commit the key and the side effects in &lt;strong&gt;the same transaction&lt;/strong&gt;, otherwise a crash between the two leaves a failure window.&lt;/li&gt;
&lt;li&gt;Guard against concurrent duplicates with a &lt;strong&gt;uniqueness constraint&lt;/strong&gt;, since a check-then-process flow can race when competing consumers receive the same message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two other Microsoft Learn articles complement it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-idempotent" rel="noopener noreferrer"&gt;Designing Azure Functions for identical input&lt;/a&gt; lists defensive behaviors such as verifying that data exists before a delete, checking that it doesn't exist before a create, concurrency controls and data freshness validation. Most of these are semantic techniques in EIP terms.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/azure/architecture/serverless/event-hubs-functions/resilient-design#idempotency" rel="noopener noreferrer"&gt;Resilient Azure Event Hubs and Azure Functions design&lt;/a&gt; discusses idempotency specifically in the Event Hubs context, which is the same guidance linked earlier in this post.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Designing Data-Intensive Applications
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.oreilly.com/library/view/designing-data-intensive-applications/9781491903063/" rel="noopener noreferrer"&gt;Designing Data-Intensive Applications&lt;/a&gt; by Martin Kleppmann covers the design of data systems in general. Its chapter on stream processing asks a question that applies directly to Event Hubs consumers: if processing can be repeated after a failure, how do you get the effect of processing each event exactly once?&lt;/p&gt;

&lt;p&gt;The answer I found most practical is &lt;strong&gt;idempotence&lt;/strong&gt;: repeating an operation has the same effect as running it once, so a retry is harmless. Extra metadata can turn an operation that isn't naturally idempotent into one that is.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The chapter also discusses &lt;strong&gt;atomic commit protocol&lt;/strong&gt;, where the output of processing and the acknowledgment of the input (such as the offset) succeed or fail together in one transaction. In my opinion, it is &lt;strong&gt;more about implementing transactions&lt;/strong&gt; across systems than about making processing repeatable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In practice, this metadata is a marker stored with the state, written in the same atomic operation as the state change. Two kinds of markers are useful with Event Hubs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A position:&lt;/strong&gt; the &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/azure.messaging.eventhubs.eventdata.sequencenumber?view=azure-dotnet" rel="noopener noreferrer"&gt;sequence number&lt;/a&gt; assigned by Event Hubs when the event was enqueued in a partition. It tells you whether this event is newer than the last one applied.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An application specific key:&lt;/strong&gt; an identifier set by the producer, such as &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/azure.messaging.eventhubs.eventdata.messageid?view=azure-dotnet" rel="noopener noreferrer"&gt;&lt;code&gt;EventData.MessageId&lt;/code&gt;&lt;/a&gt;, that uniquely identifies the logical event. It tells you whether this event was already applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The position&lt;/em&gt; works well when events are processed in order: a replayed event carries a sequence number lower than or equal to the stored one and is ignored. It only makes sense within an ordered stream, and Event Hubs orders events per partition, not across the event hub.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The application specific key&lt;/em&gt; covers what the position can't. As discussed in &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#duplicates-while-sending" rel="noopener noreferrer"&gt;Duplicates While Sending&lt;/a&gt;, a producer retry creates a second event with its own sequence number, so the position looks new. The consumer instead records the keys it already processed, ideally in the same transaction as the side effect.&lt;/p&gt;

&lt;p&gt;Kleppmann also notes that idempotence may require &lt;a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html#making-the-lock-safe-with-fencing" rel="noopener noreferrer"&gt;&lt;strong&gt;fencing&lt;/strong&gt;&lt;/a&gt; when a partition moves from one node to another, so that the previous owner can't keep applying updates. In Event Hubs terms, this maps to the &lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-1.html#duplicates-while-receiving" rel="noopener noreferrer"&gt;partition ownership changes&lt;/a&gt; discussed earlier.&lt;/p&gt;

&lt;p&gt;The conclusion matches the Azure Architecture Center guidance: idempotent processing on top of at-least-once delivery achieves exactly-once &lt;em&gt;effects&lt;/em&gt; with a small overhead. The delivery stays at-least-once; only the observable outcome becomes exactly-once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resources
&lt;/h2&gt;

&lt;p&gt;For a more theoretical angle, Pat Helland's &lt;a href="https://doi.org/10.1145/2160718.2160734" rel="noopener noreferrer"&gt;Idempotence Is Not a Medical Condition&lt;/a&gt; (ACM Queue) discusses why at-least-once messaging pushes the responsibility of handling duplicates to the application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Closing Thoughts
&lt;/h1&gt;

&lt;p&gt;This post covered why Event Hubs consumers must tolerate duplicates, and what the literature recommends: at-least-once delivery combined with idempotent processing gives effectively-once outcomes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.techdominator.com/article/idempotent-event-processing-with-azure-event-hubs-part-2-remember-what-you-processed.html" rel="noopener noreferrer"&gt;In the upcoming part 2,&lt;/a&gt; I will walk through concrete techniques, from the most generic to the most domain specific, using a runnable sample. Each one comes with the failure scenarios it handles and the ones it doesn't.&lt;/p&gt;

</description>
      <category>azureeventhub</category>
      <category>eventdriven</category>
      <category>azure</category>
    </item>
  </channel>
</rss>
