Skip to content

Instantly share code, notes, and snippets.

@ORESoftware
Last active January 28, 2019 07:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ORESoftware/490841ca6fef67803bdae1999900fe3a to your computer and use it in GitHub Desktop.
Save ORESoftware/490841ca6fef67803bdae1999900fe3a to your computer and use it in GitHub Desktop.
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