News for hackers. Stuff that matters.
Re:Transactions Are Not Locks (2022) by JoshGlazebrook on Saturday October 21, 2023 @10:25PM (#37972311)
This is a major pain point with dealing with Stripe. If you want to write a record in your local db (to record a cancellation), then call the stripe api to say cancel a subscription, if the api call fails, you can rollback your db changes by failing the transaction.

If the api call succeeds, but your transaction fails to write to the db and rollsback, then what?

You then have to rely on the Stripe webhooks to "sync" your local db state with what stripe has, oh but don't forget stripe webhooks don't have any guaranteed order in which they are delivered, so just ignore all of the event data, you have to fetch the current state of the stripe object your event is associated with and make sure the handling of these events are idempotent.

It's not wise to rely solely on Stripe being the source of truth, but there is no way to really implement 2 phase commit with external services like this. Maybe you fire off an event into a pubsub or queue to handle the actual writing to your db, but what if the webhook gets to your service first?

Shit is hard.

Re:Transactions Are Not Locks (2022) by tynorf on Saturday October 21, 2023 @10:56PM (#37972438)
You might be interested in this: https://brandur.org/idempotency-keys
Re:Transactions Are Not Locks (2022) by yencabulator on Saturday October 21, 2023 @11:21PM (#37972545)
You record a want-to-cancel state, and then only after Stripe acknowledges the cancellation you transition from want-to-cancel to cancelled. State machines.
Re:Transactions Are Not Locks (2022) by withinboredom on Sunday October 22, 2023 @04:52AM (#37973861)
Any sync needs two phases:

- active state transitions (webhooks)

- reconciliation (api)

You can do reconciliation without active state transitions, but it usually takes awhile (slow and scheduled). However, it’s a requirement with webhooks because you don’t know if they are in-order or delayed or just broken. Thus, you need to reconcile your state every so often.