Skip to content

ADR 0001: Transactional Configuration Edits and Deferred Event Publishing

Author: AirCommerce Architecture Team • Scope: service-configuration

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 MongoTransactionManager was not configured. This caused bulk edits to behave non-transactionally; if one edit failed (e.g., due to an OptimisticLockingFailureException), previous edits in the same request were not rolled back.
  • Leaking Events: The ConfigurationUpdatedEvent was being published synchronously. Since it extends RemoteApplicationEvent, 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.
  • 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.
  • 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

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.

The dynamic flow below visualizes the edit sequence, MongoDB transaction boundary, and deferred event dispatch to the message queue:

We explicitly registered a MongoTransactionManager bean. This allows the @Transactional annotation on bulkEditConfigurations to start a true MongoDB transaction (requires a Replica Set environment).

We introduced a two-step publishing mechanism to defer the RabbitMQ message dispatch until the database transaction is fully committed:

  1. The service publishes a local LocalConfigurationUpdatedEvent.
  2. A @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true) listens to this local event.
  3. Upon successful commit, this listener fires the actual ConfigurationUpdatedEvent (which inherits RemoteApplicationEvent), thereby dispatching the message to RabbitMQ via Configuration Events Queue.
// 1. Service triggers the local event
protected 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);
}

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).
  • 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.
  • 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.