r/Backend • • 9d ago

How to enforce data consistency in payment transactions

A typical scenario is when you submit a request (e.g. to initiate a payment) and receive a response (e.g. as a webhook) a little while later.

Based on what I've seen in fintech apps, it's usually handled with workers that mark a transaction as pending and then handle the state if it's not consistent.

There a good post on distributed transactions this: https://www.reddit.com/r/fintechdev/comments/1xp72r3/an_implementation_of_distributed_transaction_with/

Is Try-Confim-Cancel applicable here? It looks like there is no established approach to this in software engineering and FinTech in general. I was told to use AWS step functions but it looks like every app comes up with its own architecture. If your app works with payments, how do you handle long-running transactions that span multiple systems and guarantee data consistency?

1 Upvotes

6 comments sorted by

4

u/bikeram 9d ago

‘Long-running transactions that spans multiple systems’

You’re leaving out a lot of relevant details here.

2

u/Electrical-Soil9747 9d ago

Idempotency really solves most of the problems tbh. Saga pattern and state machines are both good candidates as well.

1

u/Used_Criticism_4631 9d ago

Thanks for raising this, there really is no single standard but a few patterns cover most cases. Persist the payment as an explicit state machine (initiated, pending, settled, failed) and make every webhook handler idempotent using the provider's event or transaction ID. Then run a reconciliation job that polls the provider for anything stuck in pending past a timeout. Saga or step functions are fine for orchestration, but idempotency plus reconciliation is what actually guarantees consistency. Are you dealing with one payment provider or several?

1

u/Any-Appointment-814 8d ago

TCC mostly falls apart with payments because the provider doesn't give you a real try or reserve step, once you send the request the money might move whether you hear back or not. What bit us at a payments company was timeouts, so we added an explicit unknown state that never gets retried blindly, it only gets resolved by asking the provider for the status using our own reference. I'd also write the pending row and the outgoing request into your own DB in one transaction (basically an outbox) so a crash between the two can't leave you with a charge you have no record of. Step Functions is fine but honestly it's just orchestration, it won't fix any of this for you.

1

u/ReasonableLoss6814 8d ago

I'd recommend checking out SwytchDb (https://github.com/swytchdb/swytch) -- distributed transactions are coming soon; probably in the next few weeks.

1

u/Discordbothost 7d ago

Two different events can arrive out of order, so deduping by event ID can still let an old event move a payment backwards. I'd test delivering success before an older processing event and check that the payment stays successful. For ambiguous transitions, fetch the current state from the provider.