Skip to content
Init is here / May 19 - 23
Back

Incrementing/decrementing in Collections - avoiding race conditions

  • 0
  • Databases
  • General
ideclon
24 Aug, 2023, 17:16

I have a Collection with a list of items and our stock level for them. When someone makes an order, I need to decrement stock, and when new stock is added, I need to increment stock.

The simplest way would be to getDocument, then updateDocument with stock-1, but that would be an issue if there are two orders at the same time.

The only method I can think of to deal with this is: Create a new record in a collection order_updates. Check order_updates for any records created before the one I just created. If there are, wait 0.5 seconds and check again. Once there are no more records before my new one, get the current stock level and update it. Delete the order_updates record.

This seems a bit roundabout, though - does anyone have any better ways to deal with this? TIA!

TL;DR
The user is looking for a way to increment/decrement stock levels in a collection without facing race conditions. Currently, they are using a solution involving creating a new record in a separate collection and checking for any previous records before updating the stock level. However, they are seeking a more efficient approach. Solution: One possible solution is to implement a lock mechanism by creating a separate collection for locks. When a process wants to obtain a lock, it creates a document with the resource's ID. If the document is created successfully, the lock is obtained. If another process tries to obtain the same lock, it will receive an error. Once the process
Drake
25 Aug, 2023, 17:09

One way to create a lock is to create a locks collection. A process that wants to obtain a lock will create a document with id == id of the resource. If it can create the document successfully, it has obtained the lock. If any other document tries to obtain a lock, they'll get an error because the document already exists with the same id. When the process that obtained the lock is done, it can delete the lock document to release the lock

ideclon
25 Aug, 2023, 17:11

I’m not sure how that’s really any more efficient than my method (although it is tidier)

ideclon
25 Aug, 2023, 17:12

I’d really prefer if there were a way to just directly increment/decrement a number in a collection, but I don’t really expect that to happen

Drake
25 Aug, 2023, 17:13

You can πŸ‘ the related feature request: https://github.com/appwrite/appwrite/issues/3905

Drake
25 Aug, 2023, 17:13

It's on our roadmap, but I'm not sure when it'll be completed

ideclon
25 Aug, 2023, 17:15

Oh cool, and it’s got a PR already too!

Drake
25 Aug, 2023, 17:15

Eh I ran into complications πŸ˜…

Suiii
3 Nov, 2023, 16:20

Following

Meldiron
3 Nov, 2023, 16:54
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more