Skip to content

Instantly share code, notes, and snippets.

@ORESoftware ORESoftware/

Last active Jan 28, 2019
What would you like to do?
Readers-writer lock for payment processing

No official CS education but I learned about readers-writer locks after implementing a mutex library. I happened to come across a real-world use case that I would need this for.

Say we have many people signing up to take a class, their payment (through Stripe, or whatever) might be in flight. And imagine the teacher cancels the class, which would trigger a refund event.

Without proper locking, the refund might fail to execute for those students whose payments are in flight at the time of the class cancelation.

So what we probably need to do is:

  1. Payments don't block each other, but they do block the cancelation/refund routine.
  2. Refund routine blocks all payments (and other refund routines).

That appears to be a classic readers-writer lock thing, where the payments are the readers and the refund is the writer? The only difference is that once a refund occurs, a flag is flipped never to be flipped again (no uncancellation is possible).

How can I implement an evented readers-writer lock using a relational database and multiple application servers?

Once I have more than one application server, going to have to use a lock in the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.