blob: 52d2730200bb96f0735605cc02d73c38c81f09bd [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<link rel="icon" type="image/png" href="images/favicon.png">
<title>Duplicate Message Detection</title>
<link rel="stylesheet" href="css/asciidoctor.css">
<link rel="stylesheet" href="css/font-awesome.css">
<link rel="stylesheet" href="css/rouge-github.css">
</head>
<body class="book toc2 toc-left">
<div id="header">
<h1>Duplicate Message Detection</h1>
<div id="toc" class="toc2">
<div id="toctitle"><a href="index.html">User Manual for 2.33.0</a></div>
<ul class="sectlevel1">
<li><a href="#using-duplicate-detection-for-message-sending">1. Using Duplicate Detection for Message Sending</a></li>
<li><a href="#configuring-the-duplicate-id-cache">2. Configuring the Duplicate ID Cache</a></li>
<li><a href="#duplicate-detection-and-bridges">3. Duplicate Detection and Bridges</a></li>
<li><a href="#duplicate-detection-and-cluster-connections">4. Duplicate Detection and Cluster Connections</a></li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Apache ActiveMQ Artemis includes powerful automatic duplicate message detection, filtering out duplicate messages without you having to code your own fiddly duplicate detection logic at the application level.
This chapter will explain what duplicate detection is, how Apache ActiveMQ Artemis uses it and how and where to configure it.</p>
</div>
<div class="paragraph">
<p>When sending messages from a client to a server, or indeed from a server to another server, if the target server or connection fails sometime after sending the message, but before the sender receives a response that the send (or commit) was processed successfully then the sender cannot know for sure if the message was sent successfully to the address.</p>
</div>
<div class="paragraph">
<p>If the target server or connection failed after the send was received and processed but before the response was sent back then the message will have been sent to the address successfully, but if the target server or connection failed before the send was received and finished processing then it will not have been sent to the address successfully.
From the senders point of view it&#8217;s not possible to distinguish these two cases.</p>
</div>
<div class="paragraph">
<p>When the server recovers this leaves the client in a difficult situation.
It knows the target server failed, but it does not know if the last message reached its destination ok.
If it decides to resend the last message, then that could result in a duplicate message being sent to the address.
If each message was an order or a trade then this could result in the order being fulfilled twice or the trade being double booked.
This is clearly not a desirable situation.</p>
</div>
<div class="paragraph">
<p>Sending the message(s) in a transaction does not help out either.
If the server or connection fails while the transaction commit is being processed it is also indeterminate whether the transaction was successfully committed or not!</p>
</div>
<div class="paragraph">
<p>To solve these issues Apache ActiveMQ Artemis provides automatic duplicate messages detection for messages sent to addresses.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="using-duplicate-detection-for-message-sending"><a class="anchor" href="#using-duplicate-detection-for-message-sending"></a><a class="link" href="#using-duplicate-detection-for-message-sending">1. Using Duplicate Detection for Message Sending</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Enabling duplicate message detection for sent messages is simple: you just need to set a special property on the message to a unique value.
You can create the value however you like, as long as it is unique.
When the target server receives the message it will check if that property is set, if it is, then it will check in its in memory cache if it has already received a message with that value of the header.
If it has received a message with the same value before then it will ignore the message.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>Using duplicate detection to move messages between nodes can give you the same <em>once and only once</em> delivery guarantees as if you were using an XA transaction to consume messages from source and send them to the target, but with less overhead and much easier configuration than using XA.</p>
</div>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>If you&#8217;re sending messages in a transaction then you don&#8217;t have to set the property for <em>every</em> message you send in that transaction, you only need to set it once in the transaction.
If the server detects a duplicate message for any message in the transaction, then it will ignore the entire transaction.</p>
</div>
<div class="paragraph">
<p>The name of the property that you set is given by the value of <code>org.apache.activemq.artemis.api.core.Message.HDR_DUPLICATE_DETECTION_ID</code>, which is <code>_AMQ_DUPL_ID</code></p>
</div>
<div class="paragraph">
<p>The value of the property can be of type <code>byte[]</code> or <code>SimpleString</code> if you&#8217;re using the core API.
If you&#8217;re using JMS it must be a <code>String</code>, and its value should be unique.
An easy way of generating a unique id is by generating a UUID.</p>
</div>
<div class="paragraph">
<p>Here&#8217;s an example of setting the property using the core API:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight nowrap"><code data-lang="java"><span class="o">...</span>
<span class="nc">ClientMessage</span> <span class="n">message</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="na">createMessage</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="nc">SimpleString</span> <span class="n">myUniqueID</span> <span class="o">=</span> <span class="s">"This is my unique id"</span><span class="o">;</span> <span class="c1">// Could use a UUID for this</span>
<span class="n">message</span><span class="o">.</span><span class="na">setStringProperty</span><span class="o">(</span><span class="no">HDR_DUPLICATE_DETECTION_ID</span><span class="o">,</span> <span class="n">myUniqueID</span><span class="o">);</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>And here&#8217;s an example using the JMS API:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight nowrap"><code data-lang="java"><span class="o">...</span>
<span class="nc">Message</span> <span class="n">jmsMessage</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="na">createMessage</span><span class="o">();</span>
<span class="nc">String</span> <span class="n">myUniqueID</span> <span class="o">=</span> <span class="s">"This is my unique id"</span><span class="o">;</span> <span class="c1">// Could use a UUID for this</span>
<span class="n">message</span><span class="o">.</span><span class="na">setStringProperty</span><span class="o">(</span><span class="no">HDR_DUPLICATE_DETECTION_ID</span><span class="o">.</span><span class="na">toString</span><span class="o">(),</span> <span class="n">myUniqueID</span><span class="o">);</span>
<span class="o">...</span></code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="configuring-the-duplicate-id-cache"><a class="anchor" href="#configuring-the-duplicate-id-cache"></a><a class="link" href="#configuring-the-duplicate-id-cache">2. Configuring the Duplicate ID Cache</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The server maintains caches of received values of the <code>org.apache.activemq.artemis.core.message.impl.HDR_DUPLICATE_DETECTION_ID</code> property sent to each address.
Each address has its own distinct cache.</p>
</div>
<div class="paragraph">
<p>The cache is a circular fixed size cache.
If the cache has a maximum size of <code>n</code> elements, then the <code>n + 1</code>th id stored will overwrite the <code>0</code>th element in the cache.</p>
</div>
<div class="paragraph">
<p>The maximum size of the cache is configured by the parameter <code>id-cache-size</code> in <code>broker.xml</code>, the default value is <code>20000</code> elements.</p>
</div>
<div class="paragraph">
<p>To implement an address-specific <code>id-cache-size</code>, you can add to the
corresponding address-settings section in <code>broker.xml</code>. Specify the
desired <code>id-cache-size</code> value for the particular address. When a message
is sent to an address with a specific <code>id-cache-size</code> configured, it
will take precedence over the global <code>id-cache-size</code> value, allowing
for greater flexibility and optimization of duplicate ID caching.</p>
</div>
<div class="paragraph">
<p>The caches can also be configured to persist to disk or not.
This is configured by the parameter <code>persist-id-cache</code>, also in <code>broker.xml</code>.
If this is set to <code>true</code> then each id will be persisted to permanent storage as they are received.
The default value for this parameter is <code>true</code>.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<div class="paragraph">
<p>When choosing a size of the duplicate id cache be sure to set it to a larger enough size so if you resend messages all the previously sent ones are in the cache not having been overwritten.</p>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="duplicate-detection-and-bridges"><a class="anchor" href="#duplicate-detection-and-bridges"></a><a class="link" href="#duplicate-detection-and-bridges">3. Duplicate Detection and Bridges</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Core bridges can be configured to automatically add a unique duplicate id value (if there isn&#8217;t already one in the message) before forwarding the message to its target.
This ensures that if the target server crashes or the connection is interrupted and the bridge resends the message, then if it has already been received by the target server, it will be ignored.</p>
</div>
<div class="paragraph">
<p>To configure a core bridge to add the duplicate id header, simply set the <code>use-duplicate-detection</code> to <code>true</code> when configuring a bridge in <code>broker.xml</code>.</p>
</div>
<div class="paragraph">
<p>The default value for this parameter is <code>true</code>.</p>
</div>
<div class="paragraph">
<p>For more information on core bridges and how to configure them, please see <a href="core-bridges.html#core-bridges">Core Bridges</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="duplicate-detection-and-cluster-connections"><a class="anchor" href="#duplicate-detection-and-cluster-connections"></a><a class="link" href="#duplicate-detection-and-cluster-connections">4. Duplicate Detection and Cluster Connections</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>Cluster connections internally use core bridges to move messages reliable between nodes of the cluster.
Consequently they can also be configured to insert the duplicate id header for each message they move using their internal bridges.</p>
</div>
<div class="paragraph">
<p>To configure a cluster connection to add the duplicate id header, simply set the <code>use-duplicate-detection</code> to <code>true</code> when configuring a cluster connection in <code>broker.xml</code>.</p>
</div>
<div class="paragraph">
<p>The default value for this parameter is <code>true</code>.</p>
</div>
<div class="paragraph">
<p>For more information on cluster connections and how to configure them, please see <a href="clusters.html#clusters">Clusters</a>.</p>
</div>
</div>
</div>
</div>
</body>
</html>