fix(sqs): scope FIFO dedup per MessageGroupId when DeduplicationScope=messageGroup (#908)

* fix(sqs): scope FIFO dedup per MessageGroupId when DeduplicationScope=messageGroup

FIFO queues default to queue-wide deduplication, but the high-throughput
configuration (DeduplicationScope=messageGroup,
FifoThroughputLimit=perMessageGroupId) scopes dedup to each
MessageGroupId. Floci was always keying the dedup cache by
MessageDeduplicationId alone, so two SendMessage calls that shared a
dedupId across different message groups would have the second silently
swallowed even when the queue's DeduplicationScope said they were
distinct.

When DeduplicationScope=messageGroup, key the dedup cache by
(messageGroupId, dedupId) and constrain the duplicate-lookup helper
to the same group. Queue-scoped behavior is unchanged (still keyed
on dedupId alone, lookup spans all groups), and a regression test
covers both paths.

* fix(sqs): use NUL delimiter for group-scoped FIFO dedup key

'|' is in the allowed character set for both MessageGroupId and
MessageDeduplicationId, so the composite key collides — e.g.
(group="a", dedup="b|c") and (group="a|b", dedup="c") both produce
"a|b|c". Use NUL (which is not allowed in either field) so the
composite key is unambiguous, and add a regression test.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-18 20:53:33 -05:00
1 parent 92c5c26763
commit 337b35c938
3 files changed
+58 -4

No files matched your search

@@ -217,8 +217,15 @@ class GuardedMessageQueue {
}
Message findByDeduplicationId(String dedupId) {
return findByDeduplicationId(dedupId, null);
}
Message findByDeduplicationId(String dedupId, String messageGroupId) {
try (var _ = hold()) {
return messages.stream().filter(msg -> dedupId.equals(msg.getMessageDeduplicationId()))
return messages.stream()
.filter(msg -> dedupId.equals(msg.getMessageDeduplicationId()))
.filter(msg -> messageGroupId == null
|| messageGroupId.equals(msg.getMessageGroupId()))
.findFirst().orElse(null);
}
}
@@ -400,18 +400,27 @@ public class SqsService {
}
}
// Check deduplication window — atomic putIfAbsent to avoid race condition
// Check deduplication window — atomic putIfAbsent to avoid race condition.
// When DeduplicationScope=messageGroup, dedup is scoped per MessageGroupId,
// so two messages with the same MessageDeduplicationId in different groups
// are not duplicates and must both be accepted.
boolean groupScoped = "messageGroup".equalsIgnoreCase(
queue.getAttributes().get("DeduplicationScope"));
// Use NUL as the delimiter — it is outside the SQS-allowed character set for
// MessageGroupId/MessageDeduplicationId, so the composite key is unambiguous.
String dedupCacheKey = groupScoped ? messageGroupId + "\0" + dedupId : dedupId;
cleanupDeduplicationCache(storageKey);
var dedupMap = deduplicationCache.computeIfAbsent(storageKey, k -> new ConcurrentHashMap<>());
Instant expiry = Instant.now().plusSeconds(DEDUP_WINDOW_SECONDS);
Instant previous = dedupMap.putIfAbsent(dedupId, expiry);
Instant previous = dedupMap.putIfAbsent(dedupCacheKey, expiry);
persistDedup(storageKey);
if (previous != null && Instant.now().isBefore(previous)) {
// Duplicate within window — keep the original messageId and
// sequenceNumber but compute response MD5s from this request's
// body and attributes, otherwise SDK clients (which validate
// MD5 against what they sent) reject the response.
Message existing = getOrCreateQueue(storageKey).findByDeduplicationId(dedupId);
Message existing = getOrCreateQueue(storageKey).findByDeduplicationId(
dedupId, groupScoped ? messageGroupId : null);
if (existing != null) {
Message response = new Message(body);
response.setMessageId(existing.getMessageId());