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.
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ần | Vai trò | Failure/scale concern |
|---|---|---|
| Connection | Transport, authentication, heartbeat | Churn và reconnect storm |
| Channel | AMQP session cho publish/consume/ack | Protocol error đóng channel |
| Exchange | Nhận publish và evaluate bindings | Unroutable hoặc fan-out ngoài ý muốn |
| Queue | Giữ delivery và dispatch consumer | Backlog, hot queue, leader locality |
| Binding | Rule exchange → queue/exchange | Pattern/metadata sai |
| Vhost | Namespace + permission scope | Quota/policy/connection isolation |
2. Exchange types và routing semantics
Direct match routing key chính xác; topic match dot-separated pattern với * và #; 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.
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.
- Heartbeat phát hiện dead peer nhưng timeout quá thấp tạo false positive khi pause/network jitter.
- Automatic recovery phải redeclare topology theo thứ tự và có exponential backoff/jitter.
- Connection/channel pool không được che resource leak hoặc tạo unbounded channel count.
- Credential/vhost permission dùng least privilege cho configure/write/read.
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.