AWS Builder Center

When ONDC Events Arrive Late, Twice, or Out of Order: Building a Deterministic Transaction Engine on AWS

ONDC transactions are asynchronous, distributed, and made up of multiple protocol messages. For an observability system, storing those messages is only the beginning. The harder problem is reconstructing the actual order lifecycle when events are duplicated, delayed, or processed more than once. This article explains how we designed a deterministic transaction engine using Amazon SQS, AWS Lambda, Amazon S3, and Aurora PostgreSQL.

An ONDC transaction is not a single API request.
A typical order can move through multiple interactions such as select, init, confirm, on_confirm, and status callbacks. ONDC's protocol uses transaction_id across the transaction and message_id to correlate a request/callback cycle. The latter can also be used to identify duplicate messages.
That creates a simple but important engineering question:
How do we reconstruct one reliable order timeline from an unreliable event stream?
For ONDC Pulse, we solved this with a deterministic event-processing pipeline.

The important part is the separation of responsibilities.
-S3 keeps the original payload.
-SQS provides a durable processing boundary.
-Lambda performs the business logic.
-Aurora stores the operational state.
The dashboard only reads the resulting state.
Our research follows the same separation: raw ONDC messages are archived while normalized events are used for processing and order-level state reconstruction.

Raw events and canonical events are different things

We don't put the original ONDC payload directly into our state machine.
First, we preserve the exact payload:
{
"context": {
"action": "on_confirm",
"transaction_id": "tx-123",
"message_id": "msg-456",
"timestamp": "2026-09-20T16:10:00Z"
},
"message": {
"order": {
"id": "order-789"
}
}
}
Then we create an internal event:
{
"eventId": "evt-001",
"transactionId": "tx-123",
"messageId": "msg-456",
"orderId": "order-789",
"eventType": "CONFIRM",
"eventTime": "2026-09-20T16:10:00Z",
"participantRole": "SELLER",
"rawPayloadUri": "s3://..."
}
The difficult part: ordering Suppose the actual protocol flow is:
/confirm
/on_confirm
/on_status
Our consumer may observe:
/on_confirm
/on_status
/confirm
Or it may receive the same message twice.
AWS Lambda event source mappings with SQS are at-least-once, so duplicate processing can happen. AWS therefore recommends that Lambda consumers be idempotent. 
That means queue delivery order cannot be treated as business truth.
We separate three concepts:

This is why the processor considers the event identity, protocol timestamp, current order state, and transition rules instead of simply trusting arrival order.

Idempotency belongs in the database

The easiest way to make retries safe is to enforce uniqueness at the database boundary.
For example:
tenant_id + message_id
can represent one unique protocol message for a tenant.
Then processing becomes conceptually:
Receive event
↓
Insert event
↓
Already exists?
/ \
yes no
↓ ↓
no-op lock order
↓
validate transition
↓
update state
↓
commit

Now imagine Lambda successfully commits the transaction but fails before acknowledging the SQS message.
SQS can deliver the message again.
The second execution hits the uniqueness constraint and becomes a no-op instead of creating another state transition. That is exactly the kind of failure mode a distributed event processor has to handle.

State reconstruction

Once events are deduplicated, we can reconstruct the order state.
A simplified state machine looks like:
SEARCHED
↓
SELECTED
↓
INITIATED
↓
CONFIRMED
↓
ASSIGNED
↓
PICKED_UP
↓
OUT_FOR_DELIVERY
↓
DELIVERED
The state machine does not ask an AI model whether a transition is valid.
It simply evaluates:
(current_state, incoming_event)
and returns:
next_state
or:
invalid transition
For example:
CONFIRMED + ASSIGNED
→ valid

CONFIRMED + DELIVERED
→ invalid

CONFIRMED + no next event
→ unresolved
That last case is important.
Some failures are not invalid events.
They are missing events.
Our research specifically treats invalid transitions and missing/delayed callbacks as deterministic failure signals. 

Detecting failures that never send an error

Suppose an order reaches  CONFIRMED .
We expect  ASSIGNED  within 120 seconds.
But nothing arrives.
There is no message saying:
"This order has failed."
We have to detect the absence itself.

A scheduled Lambda can periodically evaluate active orders against their SLA rules.
For example:
{
"fromState": "CONFIRMED",
"expectedEvent": "ASSIGNED",
"thresholdSeconds": 120,
"severity": "SEV_2"
}
The evaluator calculates:
elapsed = current_time - last_event_time
If:
elapsed > threshold
the order becomes a candidate for an SLA breach.
No LLM is needed and it's just deterministic temporal logic. 

The SLA engine must also be idempotent

A one-minute scheduler may evaluate the same order repeatedly.
Without protection, this could happen:
16:03 → Incident #1
16:04 → Incident #2
16:05 → Incident #3
Instead, the incident should be identified by something like:
tenant + order + SLA rule + active breach
The first evaluation creates the incident.
Later evaluations update the existing incident.
This gives us a useful distributed-systems property:
Retries create additional evaluations, not additional business effects.

Why this design matters

The result is more than a collection of logs.
For one order, ONDC Pulse can reconstruct:
10:01:02 /confirm
10:01:08 /on_confirm
10:01:08 waiting for /on_status
10:03:08 SLA boundary crossed
10:04:00 incident detected
An operator can then see exactly where the transaction stopped, which event was expected, how long it waited, and what evidence caused the incident.
And that is the core idea behind ONDC Pulse: turning an asynchronous ONDC event stream into a deterministic, queryable transaction timeline.
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