1Go main: b := pubsub.NewBroker() rồi emails := b.Subscribe("order.placed", 16) và points := b.Subscribe("order.placed", 16). Mỗi lần Subscribe tạo một channel riêng có buffer 16 và thêm vào subs[topic] dưới mu.Lock().Go main: b := pubsub.NewBroker(), then emails := b.Subscribe("order.placed", 16) and points := b.Subscribe("order.placed", 16). Each Subscribe creates its own buffer-16 channel and appends it to subs[topic] under mu.Lock().
2wg.Add(2) và hai goroutine: email worker chạy for ev := range emails (giả lập SMTP chậm bằng time.Sleep(50ms)), loyalty worker chạy for ev := range points.wg.Add(2) and two goroutines: the email worker runs for ev := range emails (simulating slow SMTP with time.Sleep(50ms)), the loyalty worker runs for ev := range points.
3main gọi b.Publish("order.placed", id) cho A1, A2, A3. Publish giữ RLock và với MỖI channel trong subs[topic] thử ch <- Event{Topic, Payload}; buffer đầy thì nhánh default bỏ sự kiện thay vì chặn publisher. Đây là fan-out: mỗi subscriber nhận một bản.main calls b.Publish("order.placed", id) for A1, A2 and A3. Publish holds RLock and for EACH channel in subs[topic] tries ch <- Event{Topic, Payload}; if a buffer is full the default case drops the event instead of blocking the publisher. This is fan-out: every subscriber gets a copy.
4loyalty worker nhận ngay bản A1 của nó và in ra, không phải chờ email worker đang ngủ 50ms. Sau đó email worker mới in. Thứ tự giữa hai worker không cố định.The loyalty worker receives its copy of A1 at once and prints, without waiting for the email worker's 50ms sleep. The email worker prints afterwards. Ordering between the two workers is not fixed.
5b.Close() đóng mọi channel subscriber và xoá topic. Các sự kiện còn trong buffer vẫn được đọc hết trước khi range kết thúc; wg.Wait() chờ cả hai worker xong rồi main thoát.b.Close() closes every subscriber channel and deletes the topic. Events still buffered are drained before the range loops end; wg.Wait() waits for both workers and main exits.
6Java giữa các service dùng Kafka: OrderService.place lưu đơn rồi kafka.send(TOPIC, order.id(), event) với key = orderId để mọi sự kiện của một đơn vào cùng partition. Lỗi gửi chỉ được log trong whenComplete.Java between services uses Kafka: OrderService.place saves the order and calls kafka.send(TOPIC, order.id(), event) with key = orderId so all events of one order land in the same partition. Send failures are logged in whenComplete.
7OrderSubscribers có hai @KafkaListener với groupId khác nhau: email-service gọi sendConfirmation → email.sendOrderConfirmation, loyalty-service gọi addPoints → loyalty.addPointsOnce(orderId, userId, total / 1000). Kafka giao ít nhất một lần nên handler phải idempotent.OrderSubscribers has two @KafkaListeners with different groupIds: email-service calls sendConfirmation → email.sendOrderConfirmation, loyalty-service calls addPoints → loyalty.addPointsOnce(orderId, userId, total / 1000). Kafka delivers at least once, so handlers must be idempotent.
8CacheInvalidation.productChanged gọi redis.convertAndSend("cache.product.evict", productId). evictListener (RedisMessageListenerContainer) trên MỌI instance nhận và gọi cache.evict(productId). Redis pub/sub không lưu lại: instance đang offline sẽ lỡ tin.CacheInvalidation.productChanged calls redis.convertAndSend("cache.product.evict", productId). The evictListener (RedisMessageListenerContainer) on EVERY instance receives it and calls cache.evict(productId). Redis pub/sub stores nothing: an offline instance misses the message.
9Go giữa các process dùng NATS: PublishOrder json.Marshal rồi nc.Publish("order.placed", data); SubscribeOrders đăng ký nc.Subscribe, callback json.Unmarshal rồi sendEmail(ev). Subscribe thường là fan-out; QueueSubscribe sẽ chia tải thay vì phát cho tất cả.Go across processes uses NATS: PublishOrder does json.Marshal then nc.Publish("order.placed", data); SubscribeOrders registers nc.Subscribe, whose callback does json.Unmarshal then sendEmail(ev). Plain Subscribe is fan-out; QueueSubscribe would load-balance instead.