Stop Losing Events: Implementing the Transactional Outbox Pattern on AWS

Stop Losing Events: Implementing the Transactional Outbox Pattern on AWS

We’ve all been there. Your microservice needs to do two things: save a record to the database and publish an event to a message broker. You save the data to PostgreSQL. Then you try to publish the event to SQS or EventBridge and a network blip happens. The publish fails. Now your database has the new record, but the rest of your system has no idea it exists. Your data is officially inconsistent. Let’s look at how to solve this by implementing the Transactional Outbox pattern with Java, Spring Boot, and AWS.

Desarrolladora

The Architecture: Why Outbox?

Instead of trying to sync the database and the message broker from your application code, the Outbox pattern keeps it simple: you save the business entity and the event in the exact same database transaction.

It’s all or nothing. If the transaction commits, both are saved. If it fails, neither is.
Then, a separate asynchronous process (the Relay) reads those events from the database and pushes them to AWS. For this setup, we'll use:

  • Amazon RDS (PostgreSQL): To store both our domain tables and the  outbox_events  table.
  • AWS DMS (Database Migration Service): To act as our Relay using Change Data Capture (CDC).
  • Amazon Kinesis / EventBridge: To route the events.
  • Amazon SQS: As the destination for our consumers.


Writing the Code (Spring Boot)

Let’s use a real scenario from posData, a digital legacy platform I've been working on. When a new user registers, we save their profile and emit a  UserRegistered  event.
First, we need an outbox table in our PostgreSQL database:
SQL

CREATE TABLE outbox_events (
id UUID PRIMARY KEY,
aggregate_type VARCHAR(255) NOT NULL,
aggregate_id VARCHAR(255) NOT NULL,
event_type VARCHAR(255) NOT NULL,
payload JSONB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Next, the Spring Boot service. We use  @Transactional  to bind both inserts together.
Java
@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final OutboxEventRepository outboxEventRepository;
private final ObjectMapper objectMapper;

@Transactional
public void registerUser(User user) {
// 1. Save the actual business entity
userRepository.save(user);

// 2. Build the event payload
JsonNode payload = objectMapper.valueToTree(user);
// 3. Save the event to the outbox table in the same transaction
OutboxEvent event = OutboxEvent.builder()
.id(UUID.randomUUID())
.aggregateType("USER")
.aggregateId(user.getId().toString())
.eventType("UserRegistered")
.payload(payload.toString())
.build();
outboxEventRepository.save(event);
}
}

If the database crashes right after saving the user, the  @Transactional  context rolls everything back. No orphan records, no missing events.

The Relay: AWS DMS to the Rescue

You might be thinking: "Can't I just write a  @Scheduled  cron job in Java to poll the outbox table every 5 seconds?"
You could, but polling a database to death doesn't scale well and adds unnecessary load to your RDS instance.

Instead, we use AWS DMS with Change Data Capture (CDC). DMS reads the PostgreSQL transaction logs (WAL) natively.
  1. Set up a Source Endpoint pointing to your RDS instance.
  1. Set up a Target Endpoint pointing to Kinesis Data Streams.
  1. Create a Migration Task mapping rule to watch only the  outbox_events  table.
JSON
{
"rules": [
{
"rule-type": "selection",
"rule-id": "1",
"rule-name": "capture-outbox",
"object-locator": {
"schema-name": "public",
"table-name": "outbox_events"
},
"rule-action": "include"
}
]
}
Whenever Spring Boot commits a transaction to the outbox table, DMS instantly detects the change in the logs and streams the event. Your application doesn't lift a finger.

The Catch: You Must Be Idempotent

This architecture guarantees at-least-once delivery. That’s a fancy way of saying: your consumer microservices will eventually receive duplicate messages. Network retries happen.
When your AWS Lambda or Spring Cloud AWS  @SqsListener  picks up the message, you must check the event ID. If you already sent the welcome email for event  id: 12345 , just drop the duplicate and move on. Idempotency is non-negotiable here.

Don't Forget Housekeeping

If you push this to production as-is, your  outbox_events  table will grow forever. Eventually, your DBA will hunt you down.
Set up an EventBridge rule that triggers a Lambda function during off-peak hours to clean up old processed events ( DELETE FROM outbox_events WHERE created_at < NOW() - INTERVAL '7 days' ).

The Cost Factor: What About the AWS Bill?

Let’s talk about money, because architectural purity shouldn't bankrupt your project. Does this setup cost a lot?
  • Amazon RDS: You’re already paying for your database. The only extra cost here is the minimal storage for the  outbox_events  table, which is negligible if you actually implement the 7-day cleanup rule.
  • AWS DMS: This will be your main expense. Unlike SQS or Lambda, standard DMS isn't scale-to-zero serverless. You need a Replication Instance running 24/7 to read the transaction logs. A  dms.t3.small  instance will cost you around $15–$20 a month (Single-AZ) depending on your region.
  • Amazon Kinesis: A single provisioned shard costs about $11 a month and gives you plenty of throughput (1MB/s in, 2MB/s out) to handle thousands of events per day.
  • Amazon SQS & AWS Lambda: Practically free for small to medium workloads. SQS gives you 1 million free requests per month, and Lambda's free tier is famously generous.

The verdict: You are looking at a baseline of roughly $30 a month for the event routing infrastructure (DMS + Kinesis).
For a weekend hobby project, that might sting a bit (if that's the case, a simple Spring  @Scheduled  poller might be your MVP). But for a production environment, where dropping a registration event means a broken onboarding flow and a frustrated user, $30 a month for guaranteed data consistency is an absolute steal.

Wrapping up

Yes, the Transactional Outbox pattern requires a bit more infrastructure heavy-lifting upfront. But the peace of mind you get from knowing your data will always remain eventually consistent across your microservices is absolutely worth it. By offloading the event relay to AWS DMS, you keep your application code clean and strictly focused on business logic.
I’m curious about your setups—are you using CDC tools like DMS or Debezium for this, or relying on other patterns? Let’s chat in the comments!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article