CA 34 - Atomicity - Design a Reliable Wallet Transfer System with ACID Guarantees

 Building a (similar to PhonePe / GPay / Paytm).


Users can,
  • Store money in their wallet
  • Transfer money to other users
  • View transaction history
This system must strictly follow , because even a small inconsistency can lead to,
  • Money loss
  • Duplicate transactions
  • Incorrect balances
TABLE :

    Accounts table: 

        QUERY - CREATE TABLE accounts (
                            id SERIAL PRIMARY KEY,
                            name TEXT NOT NULL,
                            balance INT NOT NULL CHECK (balance >= 0),
                            last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                        );


    The table is created having id as the Primary Key which is serial, thus the id will be in a sequence. The name and the balance is set to NOT nULL, thus they cannot have null values.
    Also a check condition is given for the balance that the balance column cannot have negative values.
And a default is used for the last_updated column to set the current date.

Entering a dummy value : 

    

In order to show the Atomicity of postgres we will open a transcation and explain what Atomicity is. Atomicity is nothing but a property of a DBMS where the trascation is either successful or completly failed and rolledback. 

    Thus this either successful transcation or Rollback of complete transcation comes in handly for our App which handles online transcation. In case if our user 'Alice' sends money to 'Bob' , the amount is debited from Alice account but before crediting into Bob account there were some interruptions, this causes partial transcation, thus to avoid this the Atomicity features comes into the play. So the transcation is rolledback.

    We will demonstrate it here.


    Here we have started a transcation and have debited amount from the alice account, let us make some interruptions like syntax error in next step


    After the interruption, error occurs saying that the transcation is aborted and command is ignored. Let's check if the changes made to alice account is there.


    As we can see that the changes made could not be commited rather they are rolledback. And the changes made to the Alice account is reverted.

        Thus no partial transcation can be taken place, this is the Atomicity feature.

Another Example:


Here a transcation was begun and there a money transfere between Alice account and bOb account which was successful. So the changes made can be seen in the transcation, but now there was a check constraint error for the Alice account due to the transfer of 1000 from here acutal balance 500. Thus this will aslo be considered as partial transcation and the changes made will be reverted


So now we want the first successful transfer to be saved, thus we make use of Savepoint, even if there are some interruption we can roll back to the previous save and completed the transcation .


    Here as we can see the savepoint is used after the first debit and credit process thus even if there are any errors after this savepoint and then commit. Thus the transfer between the Alice and Bob account has been successfully completed. This can also be used to rollback and redo if any syntax errors are made. 



Comments

Popular posts from this blog

CA 04 - Two Sum & Sorted Two Sum

CA 05 - Reverse the array

CA 21 - Basic Select SQL Queries