diff --git a/activemq-client/pom.xml b/activemq-client/pom.xml
index 57685b2af13..614c41ef880 100644
--- a/activemq-client/pom.xml
+++ b/activemq-client/pom.xml
@@ -66,6 +66,12 @@
provided
+
+ com.github.ben-manes.caffeine
+ caffeine
+ 3.2.4
+
+
diff --git a/activemq-client/src/main/java/org/apache/activemq/CaffeineMessageAudit.java b/activemq-client/src/main/java/org/apache/activemq/CaffeineMessageAudit.java
new file mode 100644
index 00000000000..fb9dd4158e7
--- /dev/null
+++ b/activemq-client/src/main/java/org/apache/activemq/CaffeineMessageAudit.java
@@ -0,0 +1,182 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+
+import org.apache.activemq.command.MessageId;
+import org.apache.activemq.command.ProducerId;
+import org.apache.activemq.util.AtomicBitArrayBin;
+import org.apache.activemq.util.IdGenerator;
+
+/**
+ * A message audit backed by Caffeine cache and {@link AtomicBitArrayBin}.
+ *
+ *
Uses Caffeine's {@link Cache} with size-based eviction (TinyLfu policy)
+ * for bounded producer tracking, paired with lock-free
+ * {@link AtomicBitArrayBin} for per-producer bit operations.
+ *
+ *
Producer count is bounded by {@link #getMaximumNumberOfProducersToTrack()}.
+ * Caffeine's TinyLfu admission policy evicts entries based on frequency and
+ * recency, providing near-optimal hit rates. The per-producer audit window is
+ * controlled by {@link #getAuditDepth()}.
+ *
+ *
All per-producer bit operations are lock-free via {@link AtomicBitArrayBin}.
+ * The only synchronization points are within Caffeine's internal structures
+ * for cache management.
+ */
+public class CaffeineMessageAudit {
+
+ public static final int DEFAULT_WINDOW_SIZE = 2048;
+ public static final int MAXIMUM_PRODUCER_COUNT = 64;
+
+ private volatile int auditDepth;
+ private volatile int maximumNumberOfProducersToTrack;
+ private volatile Cache cache;
+
+ public CaffeineMessageAudit() {
+ this(DEFAULT_WINDOW_SIZE, MAXIMUM_PRODUCER_COUNT);
+ }
+
+ public CaffeineMessageAudit(int auditDepth, int maximumNumberOfProducersToTrack) {
+ this.auditDepth = auditDepth;
+ this.maximumNumberOfProducersToTrack = maximumNumberOfProducersToTrack;
+ this.cache = buildCache(maximumNumberOfProducersToTrack);
+ }
+
+ public int getAuditDepth() {
+ return auditDepth;
+ }
+
+ public void setAuditDepth(int auditDepth) {
+ this.auditDepth = auditDepth;
+ }
+
+ public int getMaximumNumberOfProducersToTrack() {
+ return maximumNumberOfProducersToTrack;
+ }
+
+ public void setMaximumNumberOfProducersToTrack(int maximumNumberOfProducersToTrack) {
+ this.maximumNumberOfProducersToTrack = maximumNumberOfProducersToTrack;
+ var newCache = buildCache(maximumNumberOfProducersToTrack);
+ newCache.putAll(this.cache.asMap());
+ this.cache = newCache;
+ }
+
+ public boolean isDuplicate(String id) {
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return false;
+ }
+ var bab = cache.get(seed, k -> new AtomicBitArrayBin(auditDepth));
+ var index = IdGenerator.getSequenceFromId(id);
+ if (index >= 0) {
+ return bab.setBit(index, true);
+ }
+ return false;
+ }
+
+ public boolean isDuplicate(final MessageId id) {
+ if (id == null) {
+ return false;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return false;
+ }
+ var bab = cache.get(pid.toString(), k -> new AtomicBitArrayBin(auditDepth));
+ return bab.setBit(id.getProducerSequenceId(), true);
+ }
+
+ public void rollback(final MessageId id) {
+ if (id == null) {
+ return;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return;
+ }
+ var bab = cache.getIfPresent(pid.toString());
+ if (bab != null) {
+ bab.setBit(id.getProducerSequenceId(), false);
+ }
+ }
+
+ public void rollback(final String id) {
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return;
+ }
+ var bab = cache.getIfPresent(seed);
+ if (bab != null) {
+ var index = IdGenerator.getSequenceFromId(id);
+ bab.setBit(index, false);
+ }
+ }
+
+ public boolean isInOrder(final String id) {
+ if (id == null) {
+ return true;
+ }
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return true;
+ }
+ var bab = cache.getIfPresent(seed);
+ if (bab != null) {
+ var index = IdGenerator.getSequenceFromId(id);
+ return bab.isInOrder(index);
+ }
+ return true;
+ }
+
+ public boolean isInOrder(final MessageId id) {
+ if (id == null) {
+ return false;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return false;
+ }
+ var bab = cache.get(pid.toString(), k -> new AtomicBitArrayBin(auditDepth));
+ return bab.isInOrder(id.getProducerSequenceId());
+ }
+
+ public long getLastSeqId(ProducerId id) {
+ var bab = cache.getIfPresent(id.toString());
+ if (bab != null) {
+ return bab.getLastSetIndex();
+ }
+ return -1;
+ }
+
+ public void clear() {
+ cache.invalidateAll();
+ }
+
+ public int getProducerCount() {
+ cache.cleanUp();
+ return (int) cache.estimatedSize();
+ }
+
+ private static Cache buildCache(int maxSize) {
+ return Caffeine.newBuilder()
+ .maximumSize(maxSize)
+ .build();
+ }
+}
diff --git a/activemq-client/src/main/java/org/apache/activemq/ConcurrentMessageAudit.java b/activemq-client/src/main/java/org/apache/activemq/ConcurrentMessageAudit.java
new file mode 100644
index 00000000000..11c9eeeba61
--- /dev/null
+++ b/activemq-client/src/main/java/org/apache/activemq/ConcurrentMessageAudit.java
@@ -0,0 +1,200 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq;
+
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.activemq.command.MessageId;
+import org.apache.activemq.command.ProducerId;
+import org.apache.activemq.util.AtomicBitArrayBin;
+import org.apache.activemq.util.IdGenerator;
+
+/**
+ * A lock-free message audit backed by {@link ConcurrentHashMap} and
+ * {@link AtomicBitArrayBin}.
+ *
+ *
The upstream {@code ActiveMQMessageAudit} wraps every operation in a
+ * single {@code synchronized(this)} block, serializing all threads regardless
+ * of which producer they are working with. This class eliminates all
+ * synchronization on the hot path:
+ *
+ *
{@link ConcurrentHashMap#computeIfAbsent} for lock-free producer
+ * lookup and atomic insertion
+ *
{@link AtomicBitArrayBin} for CAS-based bit-level mutations —
+ * no {@code synchronized} blocks anywhere in the hot path
+ *
+ *
+ *
Producer count is bounded by {@link #getMaximumNumberOfProducersToTrack()}.
+ * When the limit is exceeded, entries are evicted in iteration order (approximate
+ * FIFO). The per-producer audit window is controlled by {@link #getAuditDepth()}.
+ */
+public class ConcurrentMessageAudit {
+
+ public static final int DEFAULT_WINDOW_SIZE = 2048;
+ public static final int MAXIMUM_PRODUCER_COUNT = 64;
+
+ private volatile int auditDepth;
+ private volatile int maximumNumberOfProducersToTrack;
+ private final ConcurrentHashMap map;
+
+ public ConcurrentMessageAudit() {
+ this(DEFAULT_WINDOW_SIZE, MAXIMUM_PRODUCER_COUNT);
+ }
+
+ public ConcurrentMessageAudit(int auditDepth, int maximumNumberOfProducersToTrack) {
+ this.auditDepth = auditDepth;
+ this.maximumNumberOfProducersToTrack = maximumNumberOfProducersToTrack;
+ this.map = new ConcurrentHashMap<>(maximumNumberOfProducersToTrack);
+ }
+
+ public int getAuditDepth() {
+ return auditDepth;
+ }
+
+ public void setAuditDepth(int auditDepth) {
+ this.auditDepth = auditDepth;
+ }
+
+ public int getMaximumNumberOfProducersToTrack() {
+ return maximumNumberOfProducersToTrack;
+ }
+
+ public void setMaximumNumberOfProducersToTrack(int maximumNumberOfProducersToTrack) {
+ this.maximumNumberOfProducersToTrack = maximumNumberOfProducersToTrack;
+ evictExcess();
+ }
+
+ public boolean isDuplicate(String id) {
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return false;
+ }
+ var bab = getOrCreate(seed);
+ var index = IdGenerator.getSequenceFromId(id);
+ if (index >= 0) {
+ return bab.setBit(index, true);
+ }
+ return false;
+ }
+
+ public boolean isDuplicate(final MessageId id) {
+ if (id == null) {
+ return false;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return false;
+ }
+ var bab = getOrCreate(pid.toString());
+ return bab.setBit(id.getProducerSequenceId(), true);
+ }
+
+ public void rollback(final MessageId id) {
+ if (id == null) {
+ return;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return;
+ }
+ var bab = map.get(pid.toString());
+ if (bab != null) {
+ bab.setBit(id.getProducerSequenceId(), false);
+ }
+ }
+
+ public void rollback(final String id) {
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return;
+ }
+ var bab = map.get(seed);
+ if (bab != null) {
+ long index = IdGenerator.getSequenceFromId(id);
+ bab.setBit(index, false);
+ }
+ }
+
+ public boolean isInOrder(final String id) {
+ if (id == null) {
+ return true;
+ }
+ var seed = IdGenerator.getSeedFromId(id);
+ if (seed == null) {
+ return true;
+ }
+ var bab = map.get(seed);
+ if (bab != null) {
+ var index = IdGenerator.getSequenceFromId(id);
+ return bab.isInOrder(index);
+ }
+ return true;
+ }
+
+ public boolean isInOrder(final MessageId id) {
+ if (id == null) {
+ return false;
+ }
+ var pid = id.getProducerId();
+ if (pid == null) {
+ return false;
+ }
+ var bab = getOrCreate(pid.toString());
+ return bab.isInOrder(id.getProducerSequenceId());
+ }
+
+ public long getLastSeqId(ProducerId id) {
+ var bab = map.get(id.toString());
+ if (bab != null) {
+ return bab.getLastSetIndex();
+ }
+ return -1;
+ }
+
+ public void clear() {
+ map.clear();
+ }
+
+ public int getProducerCount() {
+ return map.size();
+ }
+
+ private AtomicBitArrayBin getOrCreate(String key) {
+ var bab = map.get(key);
+ if (bab != null) {
+ return bab;
+ }
+ bab = map.computeIfAbsent(key, k -> new AtomicBitArrayBin(auditDepth));
+ evictExcess();
+ return bab;
+ }
+
+ private void evictExcess() {
+ var max = maximumNumberOfProducersToTrack;
+ while (map.size() > max) {
+ var it = map.keySet().iterator();
+ if (it.hasNext()) {
+ it.next();
+ it.remove();
+ } else {
+ break;
+ }
+ }
+ }
+}
diff --git a/activemq-client/src/main/java/org/apache/activemq/util/AtomicBitArrayBin.java b/activemq-client/src/main/java/org/apache/activemq/util/AtomicBitArrayBin.java
new file mode 100644
index 00000000000..46354249a88
--- /dev/null
+++ b/activemq-client/src/main/java/org/apache/activemq/util/AtomicBitArrayBin.java
@@ -0,0 +1,230 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.util;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicLongArray;
+
+/**
+ * A lock-free replacement for {@link BitArrayBin} that uses {@link AtomicLongArray}
+ * as a ring buffer with CAS-based bit operations.
+ *
+ *
The upstream {@code BitArrayBin} stores bits in a {@code LinkedList}
+ * and requires external synchronization for all access. This class replaces that
+ * with a fixed-size {@code AtomicLongArray} ring buffer where each slot holds 64 bits.
+ * All operations use CAS (compare-and-swap) instead of locks.
+ *
+ *
Ring buffer design
+ *
+ *
Slots are addressed by absolute position: {@code ringPos = (index / 64) % capacity}.
+ * This mapping is independent of the window origin, so when the window advances,
+ * slots that remain in the window keep their data without copying. Only evicted
+ * slots need to be reclaimed, which happens lazily on first access.
+ *
+ *
Each slot carries an epoch ({@code slotEpoch[ringPos]}) identifying which
+ * absolute 64-bit block it holds. On access, if the slot's epoch doesn't match
+ * the expected epoch, the slot is reclaimed via a brief CAS protocol: the epoch
+ * is set to a {@code CLEARING} sentinel, bits are zeroed, and the epoch is
+ * published to the new value. Concurrent readers that see {@code CLEARING}
+ * retry until the transition completes.
+ *
+ *
Concurrency guarantees
+ *
+ *
Common path (bit set/get within current window): fully lock-free,
+ * single CAS on the bits slot
+ *
Window advance (rare, only on sequence jumps): single CAS on
+ * the origin; slot reclamation uses a brief per-slot CAS protocol
+ *
No global lock: threads operating on different bit indices within
+ * the same bin only contend if they hash to the same 64-bit slot
+ *
+ */
+public class AtomicBitArrayBin {
+
+ static final int LONG_SIZE = 64;
+
+ private static final long UNINITIALIZED = -1L;
+ private static final long CLEARING = Long.MAX_VALUE;
+
+ private final int capacity;
+ private final AtomicLongArray bits;
+ private final AtomicLongArray slotEpoch;
+ private final AtomicLong origin;
+ private final AtomicLong lastInOrderBit;
+
+ public AtomicBitArrayBin(int windowSize) {
+ capacity = Math.max(1, ((windowSize + 1) / LONG_SIZE) + 1);
+ bits = new AtomicLongArray(capacity);
+ slotEpoch = new AtomicLongArray(capacity);
+ for (var i = 0; i < capacity; i++) {
+ slotEpoch.set(i, UNINITIALIZED);
+ }
+ origin = new AtomicLong(0);
+ lastInOrderBit = new AtomicLong(UNINITIALIZED);
+ }
+
+ /**
+ * Set or clear a bit at the given index.
+ *
+ * @param index the absolute bit index (message sequence number)
+ * @param value true to set, false to clear
+ * @return the previous value of the bit (true if it was already set)
+ */
+ public boolean setBit(long index, boolean value) {
+ if (index < 0) return false;
+
+ while (true) {
+ var orig = origin.get();
+ var epoch = index / LONG_SIZE;
+ var originEpoch = orig / LONG_SIZE;
+
+ if (epoch < originEpoch) {
+ return true;
+ }
+
+ if (epoch >= originEpoch + capacity) {
+ advanceOrigin(orig, epoch);
+ continue;
+ }
+
+ var ringPos = (int)(epoch % capacity);
+ var bitOffset = (int)(index % LONG_SIZE);
+ long mask = 1L << bitOffset;
+
+ if (!ensureSlotEpoch(ringPos, epoch)) {
+ continue;
+ }
+
+ while (true) {
+ if (slotEpoch.get(ringPos) != epoch) {
+ break;
+ }
+
+ var oldBits = bits.get(ringPos);
+ var wasSet = (oldBits & mask) != 0;
+
+ if (value) {
+ if (wasSet) return true;
+ if (bits.compareAndSet(ringPos, oldBits, oldBits | mask)) return false;
+ } else {
+ if (!wasSet) return false;
+ if (bits.compareAndSet(ringPos, oldBits, oldBits & ~mask)) return true;
+ }
+ }
+ }
+ }
+
+ /**
+ * Get the boolean value at the index.
+ *
+ * @param index the absolute bit index
+ * @return true if the bit is set, or if the index is behind the window
+ */
+ public boolean getBit(long index) {
+ if (index < 0) return false;
+
+ var orig = origin.get();
+ var epoch = index / LONG_SIZE;
+ var originEpoch = orig / LONG_SIZE;
+
+ if (epoch < originEpoch) return true;
+ if (epoch >= originEpoch + capacity) return false;
+
+ var ringPos = (int)(epoch % capacity);
+ var curEpoch = slotEpoch.get(ringPos);
+
+ if (curEpoch != epoch) {
+ return curEpoch > epoch && curEpoch != UNINITIALIZED;
+ }
+
+ var bitOffset = (int)(index % LONG_SIZE);
+ return (bits.get(ringPos) & (1L << bitOffset)) != 0;
+ }
+
+ /**
+ * Test if the index is the next expected in-order sequence.
+ *
+ * @param index the absolute bit index
+ * @return true if this is the next in-order message
+ */
+ public boolean isInOrder(long index) {
+ var prev = lastInOrderBit.getAndSet(index);
+ return prev == UNINITIALIZED || prev + 1 == index;
+ }
+
+ /**
+ * Get the index of the highest set bit across all valid slots.
+ *
+ * @return the highest set bit index, or -1 if no bits are set
+ */
+ public long getLastSetIndex() {
+ var orig = origin.get();
+ var originEpoch = orig / LONG_SIZE;
+
+ for (int offset = capacity - 1; offset >= 0; offset--) {
+ var epoch = originEpoch + offset;
+ var ringPos = (int)(epoch % capacity);
+
+ var curEpoch = slotEpoch.get(ringPos);
+ if (curEpoch != epoch || curEpoch == CLEARING) continue;
+
+ var slotBits = bits.get(ringPos);
+ if (slotBits != 0) {
+ var highBit = LONG_SIZE - 1 - Long.numberOfLeadingZeros(slotBits);
+ return epoch * LONG_SIZE + highBit;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * @return the number of 64-bit slots in the ring buffer
+ */
+ public int getCapacity() {
+ return capacity;
+ }
+
+ private void advanceOrigin(long currentOrigin, long targetEpoch) {
+ var newOriginEpoch = targetEpoch - capacity + 1;
+ var newOrigin = Math.max(0, newOriginEpoch * LONG_SIZE);
+ if (newOrigin > currentOrigin) {
+ origin.compareAndSet(currentOrigin, newOrigin);
+ }
+ }
+
+ private boolean ensureSlotEpoch(int ringPos, long expectedEpoch) {
+ var curEpoch = slotEpoch.get(ringPos);
+
+ if (curEpoch == expectedEpoch) return true;
+
+ if (curEpoch == CLEARING) {
+ Thread.yield();
+ return false;
+ }
+
+ if (curEpoch > expectedEpoch && curEpoch != UNINITIALIZED) {
+ return false;
+ }
+
+ if (slotEpoch.compareAndSet(ringPos, curEpoch, CLEARING)) {
+ bits.set(ringPos, 0);
+ slotEpoch.set(ringPos, expectedEpoch);
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/activemq-client/src/test/java/org/apache/activemq/MessageAuditPerformanceTest.java b/activemq-client/src/test/java/org/apache/activemq/MessageAuditPerformanceTest.java
new file mode 100644
index 00000000000..f8b81f2de49
--- /dev/null
+++ b/activemq-client/src/test/java/org/apache/activemq/MessageAuditPerformanceTest.java
@@ -0,0 +1,794 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.concurrent.locks.StampedLock;
+
+import org.apache.activemq.util.AtomicBitArrayBin;
+import org.apache.activemq.util.BitArrayBin;
+import org.apache.activemq.util.IdGenerator;
+import org.junit.Test;
+
+/**
+ * Compares the throughput of message audit implementations under three
+ * concurrency patterns:
+ *
+ *
+ *
Partitioned — each thread owns a disjoint set of
+ * producers (no map or bin contention between threads)
+ *
Shared — all threads access all producers, each writing
+ * a distinct message slice (map read contention + per-bin lock
+ * contention)
+ *
Mixed R/W — audit is pre-populated, then all threads
+ * interleave duplicate re-checks (reads) with new inserts (writes)
+ * across all producers (the most realistic broker workload)