- Download Postgres, an API testing tool (I used Insomnia). Install them
git clone
this repo. Create a virtual environment. Install the packages inrequirements.txt
file- Spin up a new server and a new postgres database. Replace the database configuration present in the
app.py
file with that of your database - Run the instructions present in the
setup.py
in psql. It has the configuration to create database tables, add checks and constraints. Deposit some money at/deposit
path to facilitate transfers. - Run
flask run
after exporting the env variables. Check the official flask docs if necessary. - Send a post request to
localhost:5000
(flask port) in json format with the input format mentioned below. - You can see the output in the insomnia/ postman terminal.
A Dockerfile
and docker-compose
is present in the root directory.
- Download and install Docker
- Run
docker-compose up -d --build
which pulls the official python image, installs all the necessary packages, spins up 2 containers runningflask
andpostgres
. - Using
Insomnia
orpostman
(api testing tool), send requests in below input format athttp://192.168.99.100:5000/deposit
andhttp://192.168.99.100:5000/transfer
. - Happy Testing!!
{ "from": "account_no", "to": "account_no", "amount": "money"}
{ "from": "account_no", "to": "account_no", "amount": "money"}
- ACID compliance (RDBMS)
- Indices on accounts to make lookups faster
- Preventing Race conditions and deadlocks
- Parameterized SQL queries to prevent SQL Injection
- No float operations since they are incorrect. Numeric or decimal data types should be used.
- Consistency and Availability
TABLE transactions
- id (unique)
- amount
- account_no (foreignkey)
- created_datetime
TABLE balances
- account_no (unique)
- balance
The variations.py
contains some interesting trivia. (*Not a part of the project)
I tried to implement most of the logic in SQL as stored procedures. This is due to the fact/opinion that database engines are extremely fast than python and the presence of begin commit rollback
is very valuable
Other Technical details are present in considerations.txt
file in the repository
CREATE or replace PROCEDURE transact(fromacc varchar, toacc varchar, amt numeric, out transactionid uuid)
LANGUAGE plpgsql
AS $$
BEGIN
update balances
set balance = balance - amt
where account_no = fromacc;
update balances
set balance = balance + amt
where account_no = toacc;
INSERT INTO transactions(amount, credit_account_no, debit_account_no) VALUES (amt, toacc, fromacc)
returning id into transactionid;
END;
$$;
NOTE: Ideally, you want to check the database if the changes are being committed or not. It is in this project.