1OrderService.placeOrder chạy trong một @Transactional: orders.save(Order.place(...)) rồi outbox.save(OutboxEvent.of("order", id, "OrderPlaced", toJson(new OrderPlaced(...)))). Cả hai dòng commit cùng nhau hoặc cùng rollback.OrderService.placeOrder runs inside one @Transactional: orders.save(Order.place(...)) then outbox.save(OutboxEvent.of("order", id, "OrderPlaced", toJson(new OrderPlaced(...)))). Both rows commit together or roll back together.
2OrderService không gọi kafkaTemplate.send ở đây vì Kafka không tham gia được transaction DB. Kafka đang ngừng nhưng đơn vẫn trả thành công; event nằm bền vững trong outbox chờ relay.OrderService never calls kafkaTemplate.send here because Kafka cannot join the DB transaction. Kafka is down, yet the order still returns successfully; the event waits durably in the outbox for the relay.
3Khi Kafka hoạt động lại, OutboxRelay.relay() (@Scheduled 500ms, @Transactional) SELECT các dòng published_at IS NULL với FOR UPDATE SKIP LOCKED và map thành Row. Khóa giữ tới cuối transaction để relay khác bỏ qua.Once Kafka recovers, OutboxRelay.relay() (@Scheduled 500ms, @Transactional) SELECTs rows where published_at IS NULL with FOR UPDATE SKIP LOCKED and maps them to Row. The lock lasts until the transaction ends so other relays skip them.
4Relay tạo ProducerRecord tới order.events, key ORD-1001, header eventId và eventType, rồi kafka.send(rec).join() chờ broker ack.The relay builds a ProducerRecord for order.events with key ORD-1001 and eventId / eventType headers, then kafka.send(rec).join() waits for the broker ack.
5Ngay sau ack, relay chết trước khi UPDATE published_at được commit. Transaction relay rollback và nhả khóa; đơn vẫn còn, còn dòng outbox vẫn chờ gửi.Right after the ack, the relay crashes before the published_at UPDATE commits. Its transaction rolls back and releases the lock; the order remains, and the outbox row is still pending.
6Lần chạy sau, relay lấy lại dòng đó và gửi lại cùng eventId. Đây là cửa sổ trùng của at-least-once; đánh dấu trước khi broker nhận còn tệ hơn vì có thể mất event.On the next run, the relay claims the row again and resends the same eventId. This is at-least-once's duplicate window; marking before the broker accepts would be worse because it could lose the event.
7Sau ack, relay chạy UPDATE outbox SET published_at = now() WHERE id = ? rồi commit; lượt poll sau bỏ qua dòng này. Go relayOnce làm y hệt cho cả batch bằng WHERE id = ANY($1), còn Cleanup xoá dòng đã gửi quá 7 ngày.After the ack, the relay runs UPDATE outbox SET published_at = now() WHERE id = ? and commits; later polls skip the row. Go's relayOnce does the same for the whole batch with WHERE id = ANY($1), and Cleanup deletes rows published over 7 days ago.
8Consumer nhận hai bản EVT-1001. Lần đầu ghi eventId cùng thay đổi nghiệp vụ trong một transaction; lần sau ràng buộc duy nhất trên eventId báo trùng nên bỏ qua.The consumer receives two copies of EVT-1001. The first records the eventId with the business change in one transaction; on the second, the unique eventId constraint flags the duplicate, so it is skipped.
9Cách thay thế không cần poll: Debezium PostgresConnector đọc WAL của public.outbox, EventRouter lấy aggregate_id làm key và route theo aggregate_type sang order.events, đặt type vào header eventType.The alternative without polling: Debezium's PostgresConnector reads the WAL of public.outbox, and EventRouter uses aggregate_id as the key, routes by aggregate_type to order.events, and puts type into the eventType header.