today we are going to talk about system design and in this I'm going to discuss
about how QR code is used in payment transactions you would have seen lot of
examples like paytm, paypal googlepay amazon pay phone pay and similar
apps. let's understand how it is designed for your interview and also if you want
to do any other use case on system designs.
so here are the key discussion points so what's QR code what's the QR payment
key things to know to understand the system design and QR payment system
design in detail how to use micro services with distributed database and
scaling the system so what's QR code it's a QR is quick response code which
is a two-dimensional barcode and it is machine readable and it consists of
array of black and white squares typically used for storing URLs or any
other text information where you can use a camera and the scanner to read the
informations right?
so you see a lot of pictures about the QR code and they are used in lot of stores
and he was the terminals etc if you want to do any payment transactions right so
what's QR code payment so it's basically a contactless payment that means you
don't involve any physical cash when you are doing a transaction between you and
the seller right? so how does the transaction happens so let's say you go
into the shop you buy something and you want to pay a cash to the seller so
there is a QR code in front of the shop what you do is you scan that and you see
the amount which you have to enter and you give the amount amount information
and you say is and the money gets transferred into the sailors wallet
right that's how it happens so it is like
money transfers from buyers wallet to the sellers wallet or any account right
so let's understand what are the key things to know even before you go into
detail about the system design so you should know what is QR code generator
you should know what is a payment gateway the micro-services
load balancers and even the MySQL irides so what is QR code generator it is
nothing but a software which is a less set of VPS where it converts the text
into the QR code or where you can convert the QR code into the text so the
most popular I have seen as is easing which is done by Google and it is
available in the internet to use to see how it can be implemented in your
application right so what is payment gateway
so payment get me is nothing but instead of software components which helps you
to transfer the money from one location to other let's say you want to transfer
the money from your account to the bank account so it involves a payment gateway
let's say in the e-commerce business let's say you want to transfer a money
from yourself to the merchant it goes why our payment gateway the money gets
transferred into the merchant account right so what are micro services micro
services are the fine-grained services where you can deploy a test and do what
operations whatever you want independently right it is comparatively
much much better than the monolithic service and it can be installed
separately right so what is load balancers so we have seen a lot of
details about the load balance in a previous video let me give a high level
high level of what load balancers are so let's say you have tons of requests
coming into the server the server cannot handle on its own so you have a load
balancer in between and it gets all the requests and it transfers or distributes
the number of requesters the multiple servers that means their load gets
balanced between the client and the server
that's what load balancer is called I will also put the description or the
details about the other video about the road balancer in the description section
so you can go and learn understand more about that so what is MySQL database
MySQL is the relational database where you can store the records in the same
form of the field right so you also have a structured query language where you
can update or delete the record site so
these are the basic use cases which you are going to see in the system design so
you're going to see how to create a QR code read the QR code register buyer and
seller transfer money from bio to seller and about the database schema used in
this system design if you need more use cases please comment and subscribe to
get in future videos so what you see on the screen is the database schema which
is used in the system design right so this is a minimal version of the
database and there is a lot of option to improve or increase the number of table
if this if the scope of the application is more so if you look at the screen you
have two schemas one is seller schema the other one is the buyer schema right
so each has three different tables so Saylor schema if you look into that you
have a salad table which has seller ID seller name tax ID phone email account
number bank name and bank details and the second table is seller wallet which
has the sellers wallet ID seller ID and you have the balance and the third one
is a seller's transaction table so which has ID by your ID purchased item and mo
so this stores all the transactions when the buyer goes to the seller and buy
certain products similarly you have a bias schema which has buyer ID by a name
for email bank name and bank details and there is the buyer wallet where the
buyer can store his money which has wallet ID buyer ID and the balance and
the third table is by a transaction it has ID a seller ID buyer ID purchase
item a mode and status so this has all the transaction where the buyer is doing
with the shopkeeper right so you have to understand there is one field which is
called status let's talk more about the status in the different sections but
this is primarily used when you have a micro service based transactions and or
when you have a distributed transaction using
microservices let's see more detail in the later section okay before going into
the system design let's understand the sequence of operations there are
multiple ways you can do this operation so let's go into the type form in this
what happens is buyer and seller registers in the system buyer goes to
the store and purchases an item that information is stored in the buy a
transaction table sarah has transaction stored as well seller shows QR code to
the buyer generated by the POS device buyer scans the QR code through his
phone app by air enters the amount and clicks ok now the money gets transferred
from the buyer to the seller and the recipient rated and the transaction
completes this is a type 1 which is typically followed when you have a
complete software infrastructure and this is the second sequence of operation
or you can call it as a type 2 right buyer and seller registers in the system
and the second thing is seller downloads the QR code and displays in front of his
store the seller can also download through the email so here what happens
is buyer goes to the store and purchase an item and the store and whatever the
information is stored in the buy a transaction table now the buyer scans
the QR code displayed in the store through his phone app now buyer enters
the amount and clicks ok the money transferred from the buyer to seller
here the only way of confirmation is SMS notification that is a service
notification is sent to the seller and buyer for confirmation that so the
transaction completes this is one of the common practice where a lot of street
vendors or you know the cab drivers who have where Paytm is predominantly doing
lot of operations and transactions with this this kind of a sequence of
operation right
so what you see on the screen is a very minimal version of system design diagram
which is used to transfer the money from one account to another using the QR code
so let's go a little bit deep into that so now if you see there are two shake
sections one is the seller site and other one is a biased site so you have
two apps seller app and the buyer app so you can also see the number of services
used for both seller as well as the buyer and you see a database for seller
and buyer which are separately separate and there is a payment gateway in the
bank right so let's go to the services and see what it is what it is for and
how it is used so if you look under the seller app site you have a service
called registered since register seller business where you go and register the
seller information and those information gets stored into the seller table right
and you have a gate QR code for this so basically what it does is it connects to
the database and based on all the seller information it can extract or it can
generate the QR code and give us an output so the seller can print that QR
code and he can place in front of his people c-terminal or it can place in
front of his store so that it can be used for transaction and the third one
is create seller wallet so if a seller wants to create a wallet he can use the
service to create a wallet and the quality information is stored in the
database so the information like you know create wallet whatever the data we
collected to the create wallet is stored the seller wallet which is a second
table and the fourth one is transfer money from the buyer wallet to the bank
let's say seller or seller a certain information or the seller as a serena
mode which has to be transferred to the bank he can use the service way it
connects to the payment gateway and the payment gateway transfer the money's to
the back transfer the money to the bank and it is you if you see here for gate
QR code for this we are using as easing as easing EP where it can use to convert
the text to the QR code so now let's go into the buyer app
so by our app interact with certain services which are over here so which
has a gate data get data from QR code let's say you you scan your QR code it
gets the text which is encrypted one which we don't which I have not
discussed much over here but normally it is an encrypted text which you can see
in your phone and which can be used for for the transaction and this is a
registered buyer so if you see the first table whatever information you are
giving through the registered buyer can be stored in this table which is served
by a table and this is create buyer wallet so let's say you want to create a
wallet if you are a buyer or if you're a customer you can use the service to
create the buyer wallet you give informations like your information like
phone numbers email account number bank details and those information gets
stored as a sorry glue this information gets stored into the buyer wallet
database right the fourth service you have is transfer money to buy your
wallet so what it happens here is you have if you have certain money in the
bank if you want to transfer money from the bank to the wallet this service is
used what it does is it connects the payment gateway and interconnects the
bank and it transfers money to the through the payment gateway to the
wallet right so that is primarily the money is stored into the buyer wallet
section or buy it warrant by our wall and table so the final
as you see on the screen is transfer money from by a to seller and it is a
very important service and it is one of the frequently asked question when there
is a distributed transaction between multiple schemas and when we are using a
microservices so let's follow the pink line so when there is a transfer money
from by a to seller there are certain set of activities which happens and
that's what they picked it in the screen so as soon as this service is called
what it happens is it calls the sorry as soon as the service is called
it marks a spending in the bias schema right the status gets changed into
pending parallely what happens is it calls the function called creat saga
this function goes into the seller schema and tries to insert the data
ought and tries to insert the money into the seller schema right so once it
understands it is siliceous schema has been updated and says it gives it is
approve it comes back over here and it says the pending status is confirm and
the money gets transferred from the bias schema to the seller schema right so
let's say in other case if it is not able to update in the Salaf schema you
get a disapproval message what happens over here is it goes and try and
transfer it changes the pending status to cancel that so the whole transaction
between the self schema and the bias scheme of works right so I put more
details step by step into the screen and if you want you can read and understand
very clearly so here I talked about what is the saga pattern saga is a sequence
of local transactions where each transaction updates data within a single
service right so I put the steps like you know how different sequences of
operations are happened so let us read and understand more it mote really so
here we talk about transfer money from buyer to seller which creates a pending
status in the buy a transaction table in the buyer schema and it creates a
function called create saga right so create saga sends a command to the
seller wallet table to update the wallet table which is a sale Simo
so create saga at the same thing gets messy just an updated as an update pass
or update fail let's say if the update passed then the buyer transaction table
in the bias schema is updated is decremented and finally pending
status is remote if update failed in the bio transaction table in the bio schema
is updated with canceled stated so this is how the whole distribution transistor
beta transaction works nice so let's talk about scaling of the system so we
can do or apply the scaling in multiple locations right so let's say this is a
system design we can apply scaling at multiple locations
the first one is CDN right so if your CDN is nothing but a content delivery
network if you have an application and if you have a lot of static content or
images or JavaScript we can directly put on the CDN and the client application
instead of going to the server to fetch that every time it can go to the CDN
which is which might be the closest location and it can fetch easily that's
where the CDN comes into the picture the second one is the darker and the pots
right so you have lot of services typically they are deployed in Tomcat
server our logic server and they are put into the docker containers and they are
put into the pot right so now if you see there a number of requests coming into
the service is gradually growing based on the number of number of years or
number of number of months weight of the your application is having right so this
is going to grow up right so what we can do is we can put all those services in
the darker containers and the darker containers can be deployed into the pot
and you can have an auto configuration or manual configuration in such a way
right when number of requesters coming to the services increases or the CPU
goes beyond a pressure limit it's a 60 to 70% you can add one more docker and
you can add one more pot right so in this way you know instead of relying on
one machine you can have multiple mental containers where it can grow gradually
right so that you can improve the performance at the app server level and
the third one is the load balancers so we are talk and detail about the load
load balancer earlier right let's say if you want to scale the system you can
have a load balancer accessibility so let's say you have a number of requests
coming from the client and hitting the services right the services cannot
handle all the requests coming at coming to the services right so what we can
have is it can have a load balancer and what it does is it diverse divorces
or it splits the request and use back to different services so that you know all
the requests dozen will not go into one server or once one server and it will be
split into multiple servers in that way the load gets balanced right that's the
purpose of load balancer the last one is the sharding of the database so you have
a you have a database scheme over here gradually if you have a lot of buyers or
if you have a lot of sellers it is going to keep growing and one database cannot
withstand so instead we'll go over mechanism called shaadi so I'm going to
give a detailed video but a sharding in maybe in couple of weeks keep watching
that I will give more details on that and to understand what shorting is
instead of having all the data in one schema you can split that into multiple
schemas and you can have a lookup to say let's say customer from A to B A to D
the customer name was starting from A to D wants to key to the server you can put
and put those information in one database schema and the customer from E
to W or whatever right you can put into the another schema so in that way you
know instead of putting all the content in one schema we can spit that schema
into multiple schemas and you can have a lookup where you can identify who the
customer or the what is his first name and based on that you can go into the
different schema to get those information so that way in the load and
the database level is also getting split into multiple schema and you know you
can you and you know you can gradually you can scale the system so this is one
of the common we've just been used in the industry right and if you have any
questions you can put in the comments so that you know I'm very happy to answer
and and if you look under this picture it's a very simplified version and you
have you can have a lot of services gradually but just for your
understanding and make it simple I put a minimum number of minimum number of
services to understand this understand the system right
thank you very much hope you have enjoyed this video and if you have any
comments put in the comment box and please make sure of subscribing liking
and sharing so that others can also read and understand about this and you can
find me at the LinkedIn thank you for watching this video
have a great day bye