question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

optimistic concurrency/row versioning support.

See original GitHub issue

I couldn’t find any issues created regarding optimistic concurrency so here we go…

Since Hasura handles DB transactions on its own, it’s a little bit more difficult to implement something like optimistic concurrency for db updates.

For those who don’t know, Optimistic concurrency is a way to solve concurrency issues when multiple parties attempt to update a value at the same time. Example: image

Another example: Inventory quantity count - we would want the inventory to always reflect the true inventory count as concurrent orders come in. Definitely don’t want updates to be lost.

I know Hasura isn’t really an ORM, but lots of existing ORMs support this feature out of the box where you can just “turn on” optimistic concurrency for any specified table.

It would be neat to also “turn on” this feature for a table in hasura as well.

I am not even sure if implementing this is currently possible in Hasura.

Update: Looks like there is an _inc param as part of update mutation that could be used if updating simple integer columns (like inventory count). This makes it so that we avoid the read-modify in read-modify-write cycle, and no need for complex locking/concurrency solution with a DB constraint (column > 0) for safety. Only works for simple integer use cases with simple business logic like (column > 0) though. Still the same issue exists for any none-integer column updates or complex business logic.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
tirumaraiselvancommented, Jan 12, 2021

Thanks for the explanation! Indeed, there is a problem if you are running some business logic outside of a transaction.

To solve this, we can either support a graphql open transaction protocol (something we have been exploring) or we can explore optimistic concurrency control mechanisms.

1reaction
smbleecommented, Jan 11, 2021

@tirumaraiselvan thanks for the reply!

I understand wrapping around transaction would solve this, and I also understand everything within a query/mutation block are wrapped around a transaction (which is good).

But imagine a case where you have to do the following:

  1. Get all the order items with all the products and their current inventory quantity using hasura query (transaction #1)
  2. Within the business logic, update the quantities and create a update_product_inventory_input[] for all the products to be updated.
  3. Call the update mutation with the created input objects using hasura mutation (transaction #2)

Imagine multiple calls are updating the same product quantities at around the same time.

Since #1 and #3 happen in different transaction blocks, it could be that during #2 (which could take some time), another call already could have updated the quantity which outdates the quantity fetched from #1, therefore whatever update that happened during #2 is will be based on old/outdated inventory quantities.

One solution to this (if you can create your own transactions), would be to wrap all 1~3 in a transaction block (the downside being you could potentially have a long lasting transaction)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Row Versioning Concurrency in SQL Server - Simple Talk
The optimistic concurrency model assumes that several concurrent transactions can usually complete without interfering with each other, ...
Read more >
Optimistic Concurrency - ODBC API Reference - Microsoft Learn
In optimistic concurrency, a row is left unlocked until the time comes to update or delete it. At that point, the row is...
Read more >
Optimistic Locking in SQL Server using the ROWVERSION ...
When I use optimistic concurrency, I implement it by adding a column of type ROWVERSION to my tables. Columns defined with a rowversion...
Read more >
SQL: Implementing Optimistic Concurrency in SQL Server with ...
SQL: Implementing Optimistic Concurrency in SQL Server with RowVersion ... It's common to need to have a way to read a row of...
Read more >
Optimistic Concurrency in SQL Server - SQLpassion
Optimistic concurrency was introduced back with SQL Server 2005 and is based on the principles of Row Versioning. The idea behind row ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found