Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Interesting. I use Postgres and can't find any mention of this in the docs: https://www.postgresql.org/docs/current/static/mvcc.html talks about the usual row and table locks, and https://www.postgresql.org/search/?u=%2Fdocs%2F9.6%2F&q=Opti... is empty. Is it known by some other name in the Postgres land?

Also, I found this and similar on SO: https://stackoverflow.com/questions/17431338/optimistic-lock...

So if this technique actually requires me to write my database access code at this level, it means that using something like an ORM is nearly impossible in a way that makes an ORM useful.



That stackoverflow answer is wrong - or at least, only applies to MySQL. The author is apparently unaware of other databases.

If you put Postgres in repeatable read or serializable[1] mode, it manages optimistic concurrency for you. Collisions will produce an error in the second transaction attempting to commit. The only "trick" is that your client code should be prepared to retry transactions that have concurrency failures, and this is very easily abstracted by a library. Your SQL or ORM code does not change (I use Hibernate, fwiw).

Some good reading: https://www.postgresql.org/docs/current/static/transaction-i...

[1] Repeatable Read won't detect collisions caused by phantom rows, so pick carefully. Most CRUD apps don't care about phantom rows.


How does this work if your transactions also do external things? So if have two concurrent transactions that charge a credit card via an external API, as well as mark the order as paid in your DB? Retrying that transaction would be disastrous, no?


This is a problem that affects all distributed systems irrespective of database isolation levels. For example, your call to the external API might timeout; did the charge succeed or not? Database locking doesn't help you.

The solution comes in two parts:

1) Ensure your call to the card processor is idempotent and retry it. For example, grep Stripe's documentation for the "Idempotency-Key" header. Every processor should have a similar mechanism. Conveniently, if your database transaction unit-of-work includes the remote call, it will retry automatically in an optimistic scenario.

2) Get a webhook call from the credit card processor. Even though you are retrying your transaction, you can't guarantee that your server didn't crash without a successful commit. This potentially leaves a charged card without recording the purchase; the webhook will sync it up afterwards.

Distributed systems are hard if you think past the happy path. I wish every REST API would have an equivalent of Idempotency-Key.


Yup. I'm familiar with Strip's solution to this and am really happy with it. I also wish more API's had that.

But this brings me back to my original point: having a library that blindly re-tries POST requests that result database transactions on every conflict is not going to work. Until every external API, from printers to credit card processors, has an Idempotency Key equivalent you just can't do that generically, and you will have to do that with a custom bit of code for every situation.

Isn't it easier to just acquire an advisory lock for the critical bit and take your own database's concurrency contention out of the equation?


A lock still doesn't give you a transaction across two systems that aren't transactionally connected. If you're specifically worried about retries you can create a "task" (row in a table) in the transaction and have a consumer read tasks and propagate the message.

A lock is seductive because in the happy path it looks like you have a distributed transaction. In practice making reliable calls across distributed systems involves more complexity. The hard part is figuring out what to do when your remote call times out and you aren't sure if it succeeded; once you resolve that, you can usually work with any concurrency scenario.


That's how isolation is implemented. MVCC is not compatible with any standard SQL level, but it is used for the lower ones.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: