概念概述
消息代理(Message Broker)指的是:消息代理中介异步通信,在生产者和消费者之间提供持久化、路由与投递保证,并承担缓冲与背压职责。
从更严格的定义看,A message broker is middleware that receives messages from producers, stores and routes them according to rules, and delivers them to consumers, decoupling the two sides in time and knowledge. 该概念在工程实践中的价值,在于它能帮助团队用统一的语言描述一个具体的质量属性或设计约束,从而在评审与协作中减少歧义。
为什么需要它
It is used for durable work queues, event distribution, and buffering between services with different throughput. 它与 Layered Architecture、Onion Architecture、Clean Architecture 等概念经常一起出现,用于解决相近的工程问题。理解它的适用场景,有助于在多种备选方案之间做出有依据的取舍,而不是凭直觉选择技术方案。
常见误解
- a broker removes the need to think about delivery semantics
- queueing guarantees messages are processed in publish order
需要避免的反模式
- ignoring the broker’s persistence settings and assuming durability
- letting an unbounded queue grow without monitoring or a dead letter path
实践要点
- Brokers add value through durability, routing, and back-pressure, but each has its own semantics for acknowledgement, ordering, and redelivery that applications must understand.
- A queue that accumulates because consumers lag is an operational signal, so depth and age should be alerted rather than discovered after an outage.
- A dead letter queue captures messages that cannot be processed, preventing a poison message from blocking progress while preserving it for inspection.