#How do I do an atomic transactions to a database?

1 messages · Page 1 of 1 (latest)

orchid ridge
hybrid grail
viral wasp
#

May i know when it will be implemented?

#

We have a live app running...but randomly some query fails and data integrity is lost.Please let me know soon abt this

green sedge
viral wasp
#

When using for loop in appwrite functions...some requests randomly doesnt get executed

green sedge
#

Also, I think that got solved in the latest versions

viral wasp
viral wasp
green sedge
viral wasp
#

Ok sure...if this time any one operation fails...I will create a post...thank u...

#

But is there any timeline for transactions feature to be implemented in future?

terse pendant
#

I'm also waiting for this feature in the Appwrite Cloud.

green sedge
terse pendant
#

I've already developed a production level project and almost ready to deploy it. But my major concern is the transactions feature. My project deals with in-app currencies and in case of any failure, users could get free money or lose their money for nothing. This is a big issue.

green sedge
terse pendant
terse pendant
#

Could you provide me a link to a code snippet or tutorial where transactions have been implemented using Cloud functions?

naive field
#

Atomic transactions can not be solved with functions. It's error prone and not reliable at all. We need transaction feature built in Appwrite there is no way around it

viral wasp
naive field
viral wasp
#

Yes....forgot to mention abt for loop with one request at a time...instead of sending multiple async requests at a single time from that function

sick terrace
# naive field Atomic transactions can not be solved with functions. It's error prone and not r...

I was also requiring that feature but got a logic to do it by ourselves,
create a collection "transactions" then add a document to this collection to increament,
and create another function with cron timings set by you that checks for any docs present in transactions and do the process you asked and then delete the document in transactions when the operation is done,
For multi purpose operations use the same transaction document with type:"increment","collection","document" values set while creating makes it useable across whole project

naive field
sick terrace
#

also don't foget to add a document that takes care of the current cron running status avoiding the race condition

naive field
#

Creating document in a collection is a good solution because it does not depends on previous data

sick terrace
#

ya true

#

is there a way too get the number of documents present in a collection if there were in lakhs for a query?

naive field
#

lakhs? You can use listDocuments and set Query.limit to a high number

latent fable
pseudo wagon
lyric pasture
#

@pseudo wagon To ensure atomic operations and prevent concurrent updates on documents in Appwrite, you can use cloud funcitons using the following concepts:

  1. Locking Documents
    Use an additional collection to create "lock documents." For every document you want to lock, create a document with the same ID in the lock collection. If this succeeds, the lock is confirmed.
    Add a timestamp to the lock to handle potential failures in releasing the lock. If the timestamp is older than X seconds, you can safely overwrite the lock.
    Release the lock by deleting the corresponding lock document after the operation is complete.

  2. Rollback Mechanism
    Implement a rollback method for each individual document update. This allows you to revert all successful operations if one fails.
    Execute all operations, check their success, and roll back if needed.

  3. Retry Wrapper
    Wrap each individual method in a retry mechanism with exponential backoff. This greatly reduces the likelihood of failure for transient issues.

  4. Transaction Documents
    Create a transaction document before triggering a cloud function with a pending = true flag. If the function succeeds, update the transaction document to pending = false and add a success = true field.
    This approach ensures client feedback, even if the client loses internet during execution.

This method isn't perfect but significantly reduces errors and ensures more reliable operations.

Let me know if i missed something 😉

green sedge
#

Hmmm yes, the 4th could be a good way to solve that