| ▲ | ucarion 12 hours ago | ||||||||||||||||||||||
Do folks have any thoughts on ways of avoiding deadlocking access patterns? In a codebase where folks are sort of adding ad-hoc endpoints left and right, it's hard to avoid the case of two endpoints that more or less want to do:
Is there a "discipline" or practice that works well? Like, can you realistically, in a real-world messy business codebase, impose an "ordering" on your tables to avoid dining philosophers? | |||||||||||||||||||||||
| ▲ | rawgabbit 2 hours ago | parent | next [-] | ||||||||||||||||||||||
If you have different endpoints contending over the same rows, I would create “backend batching”. I would force endpoints to call a stored procedure. The stored procedure would append only to a “queue” table. Multiple workers would read from the queue and update the real tables in batches then update the queue with success and error codes. The batches are partitioned by the PK to minimize lock contention. | |||||||||||||||||||||||
| ▲ | malisper 11 hours ago | parent | prev | next [-] | ||||||||||||||||||||||
When I've dealt with this I've generally made sure the transactions are updating rows in a consistent order. You can do that by sorting the rows before you update them | |||||||||||||||||||||||
| ▲ | forgotmy_login 12 hours ago | parent | prev [-] | ||||||||||||||||||||||
Recalling from my previous studies here: I think you can use Serializable Isolation Level, the strictest level - this will cause one of the two to fail (that is; fail only when the two txns affected rows that would logically conflict). And then you build the expectation of such possible transaction failures into the code and treat retries as a first-class expectation. Does this get to what you're trying to solve at all? | |||||||||||||||||||||||
| |||||||||||||||||||||||