Files
nexus/wiki/concepts/Competing-Consumer-Pattern.md

40 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Competing Consumer Pattern"
type: concept
tags:
- EDA
- Architecture
- Messaging
- Cloud
last_updated: 2026-04-14
---
## Aliases
- Competing Consumers
- 竞争消费者模式
- 多消费者竞争模式
## Definition
竞争消费者模式Competing Consumer Pattern指多个消费者共享同一个消息队列但每条消息只被其中一个消费者处理。确保消息处理的负载均衡和故障容错。
## Implementation
- **AWS SQS**设置多个消费者从同一标准队列拉取消息SQS 自动将消息分配给可用的消费者
- 消费者之间的竞争通过 SQS 的隐式负载均衡机制实现
## Key Characteristics
- **Mutual Exclusion**:每条消息只被一个消费者处理
- **Load Balancing**:消息自动分配给空闲的消费者
- **Fault Tolerance**:某消费者失败,其获取的消息会重新入队供其他消费者处理
- **Ordering Not Guaranteed**:标准 SQS 不保证消息顺序
## Use Cases
- 并行处理大量独立任务(如图片处理、文件转换)
- 将工作负载分发到多个 Lambda 函数或 ECS 任务
- 实现工作线程池的消息分发
## Ordered Alternative
如需保证消息顺序,使用 **SQS FIFO 队列** + **单一消费者**,或在 Kinesis 中使用分片键保证同类型消息有序处理。
## Sources
- [[public-cloud-learning-sessions-opentext-event-driven-architecture-part-2-2024091]]