Activity
Mon
Wed
Fri
Sun
Nov
Dec
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
What is this?
Less
More
Amigoscode
2.1k
Free
1 contribution to Amigoscode
🚨🚨 Spring Boot developers I need your opinion on a production problem
🚨 **Spring Boot developers — I need your opinion on a production problem** I’m working on a Spring Boot application with **JPA + PostgreSQL + Kafka**, and I’ve hit a problem that took me way deeper than I expected. Imagine this: ```java @Transactional public void createOrder(Order order) { orderRepository.save(order); applicationEventPublisher.publishEvent( new OrderCreatedEvent(order.getId()) ); } ``` The event listener then publishes the event to Kafka: ```java @Async @TransactionalEventListener( phase = TransactionPhase.AFTER_COMMIT ) public void handle(OrderCreatedEvent event) { kafkaTemplate.send("orders", event.orderId()); } ``` At first glance, everything looks fine. But here's the problem: Sometimes the Kafka event **isn't published**, even though the order was successfully created. And if the application crashes at the wrong moment, we can end up with: Database ✅ GOOD Kafka ❌ NO Now I'm wondering: Would you use **Transactional Outbox** here? Is there another Spring-native approach you'd prefer? How would you guarantee that the database state and Kafka event don't get out of sync? I'm specifically interested in how you'd solve this in a **real production system**, not just theoretically.
2 likes • 12h
Why AFTER_COMMIT Fails in Production Using @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) with @Async still leaves a race condition gap. If your application crashes or restarts after the database transaction commits, but before kafkaTemplate.send() executes or gets acknowledged, the event is permanently lost. AFTER_COMMIT only ensures the event isn't sent if the DB transaction rolls back; it does not guarantee delivery to Kafka.
1 like • 12h
Instead of publishing directly to Kafka inside or immediately after the business method, you persist the event into an outbox table within the same DB transaction: @Transactional public void createOrder(Order order) { orderRepository.save(order); outboxRepository.save(new OutboxEvent("ORDER_CREATED", order.getId())) ; }
1-1 of 1
Dominic Mwaituka
1
3 points to level up
@dominic-mwaituka-4978
coding backend is what i love

Active 12h ago
Joined Jan 27, 2026
Powered by