ADR 0001: Transactional Configuration Edits and Deferred Event Publishing
Author: AirCommerce Architecture Team • Scope: service-configuration
1. Context and Problem Statement
Section titled “1. Context and Problem Statement”The Configuration Service provides a bulkEditConfigurations endpoint to update multiple dynamic configuration entities in a single request.
Currently, the system uses Spring Data MongoDB for persistence in Configuration Database and Spring Cloud Bus (RabbitMQ) to broadcast ConfigurationUpdatedEvents to other microservices so they can invalidate their caches.
The problems identified were:
- Lack of Atomicity: MongoDB transactions were not enabled by default because
MongoTransactionManagerwas not configured. This caused bulk edits to behave non-transactionally; if one edit failed (e.g., due to anOptimisticLockingFailureException), previous edits in the same request were not rolled back. - Leaking Events: The
ConfigurationUpdatedEventwas being published synchronously. Since it extendsRemoteApplicationEvent, Spring Cloud Bus immediately routed it to RabbitMQ. If a subsequent item in a bulk edit failed, the events for previously processed items were already dispatched, leading to cache inconsistencies across downstream services.
2. Decision Drivers
Section titled “2. Decision Drivers”- Data Integrity: Bulk operations must be atomic (all-or-nothing).
- Consistency: Cache invalidation events must perfectly reflect the final committed state of the database.
- Scalability: Avoiding polling mechanisms for configuration changes.
3. Considered Options
Section titled “3. Considered Options”- Option 1: Continue with synchronous event publishing without transactions
- Option 2: Accumulate events in a list and publish at the end of the controller method
- Option 3 (Chosen): Enable MongoDB Transactions and use
@TransactionalEventListener
4. Decision Outcome
Section titled “4. Decision Outcome”We chose Option 3, because it leverages standard Spring framework capabilities to elegantly handle both the database atomicity and the event dispatch lifecycle without polluting business logic with transaction management code.
4.1. Visual Architecture Slice
Section titled “4.1. Visual Architecture Slice”The dynamic flow below visualizes the edit sequence, MongoDB transaction boundary, and deferred event dispatch to the message queue:
4.2. Implementation Mechanics
Section titled “4.2. Implementation Mechanics”Enabling MongoDB Transactions
Section titled “Enabling MongoDB Transactions”We explicitly registered a MongoTransactionManager bean. This allows the @Transactional annotation on bulkEditConfigurations to start a true MongoDB transaction (requires a Replica Set environment).
Decoupling Event Publishing
Section titled “Decoupling Event Publishing”We introduced a two-step publishing mechanism to defer the RabbitMQ message dispatch until the database transaction is fully committed:
- The service publishes a local
LocalConfigurationUpdatedEvent. - A
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)listens to this local event. - Upon successful commit, this listener fires the actual
ConfigurationUpdatedEvent(which inheritsRemoteApplicationEvent), thereby dispatching the message to RabbitMQ via Configuration Events Queue.
// 1. Service triggers the local eventprotected void publishConfigurationChange(String namespace, String key) { applicationEventPublisher.publishEvent(new LocalConfigurationUpdatedEvent(namespace, key));}
// 2. Spring holds the event until the current transaction commits@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)public void onLocalConfigurationUpdatedEvent(LocalConfigurationUpdatedEvent event) { ConfigurationUpdatedEvent cloudEvent = new ConfigurationUpdatedEvent( this, busProperties.getId(), String.format("%s:**", event.getNamespace()), event.getKey() ); applicationEventPublisher.publishEvent(cloudEvent);}5. Pros and Cons of the Options
Section titled “5. Pros and Cons of the Options”Option 1: Continue with synchronous event publishing without transactions
Section titled “Option 1: Continue with synchronous event publishing without transactions”- Good: Simple code, no changes required.
- Bad: Violates atomicity. Causes cache inconsistencies when partial failures occur in bulk edits.
Option 2: Accumulate events in a list and publish at the end of the controller method
Section titled “Option 2: Accumulate events in a list and publish at the end of the controller method”- Good: Fixes the leaking events issue without complex Spring configurations.
- Bad: Still lacks database atomicity without a transaction manager. Requires manual boilerplate code in every service method to collect and return events to the controller.
Option 3 (Chosen): Enable MongoDB Transactions and use @TransactionalEventListener
Section titled “Option 3 (Chosen): Enable MongoDB Transactions and use @TransactionalEventListener”- Good: Guarantees database atomicity. Guarantees events are only fired if changes are persisted. Clean separation of concerns using standard Spring mechanisms.
- Bad: Requires MongoDB to be deployed as a Replica Set (even for single-node setups like local testing).
6. Validation and Verification Strategy
Section titled “6. Validation and Verification Strategy”- Automated Integration Tests: Verified via Spring Boot integration tests with Testcontainers MongoDB running in single-node Replica Set mode (
rs0). - Rollback Verification: Test cases verify that throwing an unchecked exception mid-way through a multi-item bulk edit rolls back all documents in MongoDB.
- Event Suppression Verification: Test assertions confirm that no RabbitMQ messages are dispatched if the transaction rolls back, while verifying dispatch occurs immediately after successful commit.
7. Migration and Rollout Consequences
Section titled “7. Migration and Rollout Consequences”- Operational Impact: Requires all MongoDB environments (local development Docker compose, staging, and production clusters) to run with Replica Set support enabled to satisfy multi-document transaction prerequisites.
- Consumer Impact: Transparent to consumer microservices; downstream services continue to consume standard Spring Cloud Bus events.