Back

Question about functions: Wallets

  • 0
  • Databases
  • Functions
  • Cloud
Red
11 Apr, 2024, 09:11

In response to this: https://discord.com/channels/564160730845151244/1212740439438917652/1214247596046942218

Could the system reliably manage a scenario where it needs to check the balance before updating it? For example, if the current balance is $10, and a transaction requires verifying the balance before adding an amount, would the queued events guarantee that the second order waits for the completion of the first order before checking the balance? An example situation where the first order completes, resulting in a new balance of $15, and the second order should then consider this updated balance. "In situations of high traffic"

TL;DR
Developers are discussing implementing a locking mechanism to prevent concurrent actions. One developer shared a similar approach used in Appwrite for GitHub comments. The proposed locking mechanism involves creating a lock document in the database, checking for its existence, creating a new lock if it doesn't exist, proceeding with the function, and deleting the lock after execution. The system aims to manage scenarios like checking a balance before updating it, ensuring queued events consider updated balances in high-traffic situations. More details and examples can be found in the provided GitHub link.
Meldiron
11 Apr, 2024, 09:35

Hi 👋 You could implement a locking mechanism to prevent two executions from doing the same action. We even do this inside Appwrite for an edge case with GItHub comments - we need to get and then update - and we dont want 2 deployments to override at the same time. Here is how we do it: https://github.com/appwrite/appwrite/blob/7273d0e1177c70ee45fe8049459e70ef574515a6/src/Appwrite/Platform/Workers/Builds.php#L606-L633

Meldiron
11 Apr, 2024, 09:36
Red
11 Apr, 2024, 09:59

Thanks @Meldiron based on the similar topic if I'm correct it's basically:

  1. Create a lock collection in the appwrite database that holds lock documents
  2. Create the function
  3. The function checks to see if a lock document already exists
  4. If it does exist it has to retry after a certain time period let's say 1 second
  5. If it doesn't exist create a new lock document in the lock collection, and the function can proceed
  6. After the function execution is complete, delete the lock file that it created.
  7. Loop

Is this correct?

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