Part 08 · RabbitMQ · 8.1.01

Broker architecture và routing model

Producer không publish trực tiếp “đến consumer”. Nó publish vào exchange; exchange dùng binding để route một delivery tới không, một hoặc nhiều queues, rồi consumer nhận từ queue.

Mental model: exchange quyết định routing, queue sở hữu backlog, consumer xử lý delivery. Routing thành công không đồng nghĩa message đã durable, consumer đã nhận hay business side effect đã commit.

1. Connection, channel, vhost và topology

TCP connection là transport dài hạn giữa client và node. Channel multiplex nhiều logical sessions trên một connection để tránh chi phí socket/TLS/auth cho mỗi operation. Virtual host cô lập namespace exchanges/queues/bindings và permission; nó không phải security boundary thay network isolation.

Thành phầnVai tròFailure/scale concern
ConnectionTransport, authentication, heartbeatChurn và reconnect storm
ChannelAMQP session cho publish/consume/ackProtocol error đóng channel
ExchangeNhận publish và evaluate bindingsUnroutable hoặc fan-out ngoài ý muốn
QueueGiữ delivery và dispatch consumerBacklog, hot queue, leader locality
BindingRule exchange → queue/exchangePattern/metadata sai
VhostNamespace + permission scopeQuota/policy/connection isolation

2. Exchange types và routing semantics

Direct match routing key chính xác; topic match dot-separated pattern với *#; fanout bỏ qua routing key và gửi tới mọi binding; headers match header arguments. Default exchange là direct exchange đặc biệt có binding theo queue name.

Một publish có thể được copy vào nhiều queues nếu nhiều bindings match. Mỗi queue có delivery lifecycle riêng; một consumer ack ở queue A không ảnh hưởng bản sao trong queue B. Exchange thường không giữ backlog; nếu không có route, delivery có thể biến mất tùy publisher options/topology.

3. Publish call flow và unroutable messages

producer
  -> TCP connection / AMQP channel
  -> basic.publish(exchange, routingKey, properties, body)
  -> exchange evaluates bindings
  -> zero, one, or many queues
  -> queue dispatches according to consumer credit/prefetch

Với mandatory=true, broker return unroutable publish cho producer; client phải đăng ký return handler và metrics. Alternate exchange có thể route message không match sang fallback topology. Cả hai chỉ giải quyết routing failure, không thay publisher confirm hoặc persistence.

Silent loss: publish call trả về ở client thường chỉ cho biết frame đã được ghi/accepted bởi client library. Nếu không dùng publisher confirms và unroutable handling, producer thiếu bằng chứng broker đã route/accept delivery.

4. Topology declaration và lifecycle

Exchange/queue declaration là idempotent khi tên và attributes tương thích. Redeclare cùng tên với type, durable, exclusive hoặc arguments khác gây channel-level exception. Deployment cần một topology owner hoặc contract để nhiều service không tranh cấu hình.

Exclusive queue gắn với connection; auto-delete thường xóa khi consumer cuối biến mất sau khi queue từng có consumer. Resource tạm có server-generated name giúp tránh reconnect race. Cluster policies phù hợp hơn hard-code operational arguments như dead lettering, max length hoặc queue type vào từng application.

5. Connection/channel ownership

Tái sử dụng một số connection dài hạn theo client guidance, mở channels theo workload/thread ownership. Channel không nên được share concurrent nếu client library không bảo đảm thread safety; frame interleaving, confirm sequence và ack ownership có thể sai. Consumer callback cũng cần tránh block I/O loop của client.

6. Failure modes và evidence

Red flags gồm binding topic quá rộng, queue-per-user/entity làm topology bùng nổ, unroutable publish không quan sát, connection churn, shared channel concurrency và topology drift giữa environments. Theo dõi connection/channel count, publish/return/confirm rate, queue depth, routing drop, consumer count và recovery attempts.

Review checklist: exchange/queue ownership rõ; routing key contract versioned; mandatory/alternate-exchange policy có test; declarations tương thích; connections dài hạn; channels có owner; reconnect không tạo storm; permission tách configure/write/read.
Tài liệu: AMQP 0-9-1 Model Explained · RabbitMQ Channels · Virtual Hosts · Exchanges · Queues