Notifications have become part of our daily life we receive them almost every
minute on our phones tablets computers as software engineers it is good to
understand how notifications how this system of notifications work under the
hood and what different type of notifications there are pros and cons of
each what architecture how they are architectural II designed at the back
end and this is what we will discuss in this video coming up what's up y'all
this is Hussein Nasser from IGeometry where we discuss software engineering by
example today's topic is Paul versus push notifications so in this video we
will go through the differences between pull and push notifications how each
work and what is the pros and cons advantages and disadvantage of both so
let's get started so what is the pull notification the bullet vacation is and
let's take an example here and chatting application like whatsapp or or iMessage
or any other chatting application but we're gonna use the poll approach to
configure this system so it's a chat app so we have clients like a mobile phone
or a web browser and that browser or that application have this application
that talks to a server and says hey I want to send a message to Bob or Alice
and then Alice receives the message somehow so let's see how that works okay
so in this in the poll approach Alice will send a message to Bob will set will
talk to the server and usually through an HTTP or rest endpoint that says hey
server this server could be behind load balancer we don't really care okay and I
say hey I know what I want to send a message to Bob this is there has address
and just go ahead and send this message to them okay we cannot take care of
anchored encryption if possible if there is any and then you're gonna send the
so that message goes to the server and the server stores that message okay
it's just it just remains there okay and it's the responsibility of Bob or any
other client really that has the application to check for his own
messages like our email work right so it's just like you check Oh check do I
have a new message do I have a new message do you have a new message like
like and a fresh button or something like think about it this way it could be
user based like a user will refresh I click a button to refresh okay or could
the app itself does a poll Pete Oh ll right it's a different so so there's a
pulse is every I don't know one minute maybe so every one minute hey do I have
a new messages Bob will check do I have a message it do
I have a new message do I have a new message have a new message that's that's
what it does it's like we will check your M box every now and then okay Alice
does the same and in fact any other client here does the exact same thing
okay so it's very very simple architecture as you can see so okay
what's wrong with this all right so what is what is really wrong with
this so were there I thought were the good things I pros okay pros 20 yes pros
what is the advantages for this for at first the advantages of this the serve
the client doesn't really need to keep knowledge of the server and the server
doesn't need to keep knowledge of the clients witness that me really so in
other word those two nodes are stateless so a client says can talk to a server
ask it for for to post a message and it stores invest server and Bob can talk to
the same server or another server if we have a because we're gonna store it in a
database Bob can talk to that another server right
and we'll check will do a check and read the database and then pull the
information so we don't really need to call to talk to the
server over and over again so this this is a stateless architecture so that by
design it's it can be optionally if you want it to be it's a stateless so you
can connect to an environment guys I I'm gonna put a link here to a video that I
made to do a prophetic four-pillar video that to make a difference between what
is the difference between stateless and stateful I go there in detail between
the two but but this architecture could be stateless as is I guess it's possible
to design it in a state in this fashion this way I keeper can put it as a load
balancer and the server doesn't really need to know this the client as you can
see right it's just someone in the client asked his question and then the
client gets an answer from the server the server doesn't keep track hey oh you
you're Alice oh yeah yeah I know you yeah you got an error message yeah there
now it doesn't do that it's not is there's no awareness between the two
okay so that's stateless all right another thing here if you notice because
it is it is possible to be stateless weak the server doesn't have to keep
connections a list of connections open for each single client right for each
request that is made the server can create a connection and just destroy it
if it's not if it's not used right so kinda uses less memory at the back end
so this is like I'm gonna put an asterisk on this who will explain that
more when we go to the push notification and see how it goes
cons first thing right I receive our almost not real-time right so I checked
my messages every if I check my messages I don't know if the application decided
to check my message or to pull a message like Bob here every I don't know if a
minute we will receive we could have like received a message is very too late
we won't read the message in real time so the moment I send a message else in
the message Bob takes some time to refresh
unless he's constantly refreshing right so another bad thing about this is
bandwidth so what does this kills bandwidth right because imagine with
this flood of poles especially in the check messages right so it's like every
kind they're like hundred clients or mil
million client evening every client is always like hey do I have any message
hey do I have any message hey do you have any message yeah you have a new
message hey you got it any other idea right and with each Pole
there is resources that are being consumed right you're opening it a TCP
connection your HTTP connection which is TCP connection then having memory and
then doing it a handshake and doing all that stuff which obviously takes CPU
cycles both server and client and then after that you do that is send your
stuff and then you get back server hey by the way you don't have any messages
right and then then you do it again and the server's say hey still no messages
they'll know my such a stellar message so they're just like you're almost like
occupying the bandwidth for nothing right so that's kind of like in a
nutshell there Paul notifications in general I maintain a stateful connection
between the client and the server and this way not only the kind can talk to
the server it's the other way around a silver can keep track of his coin Hey
hey I know you Alice yeah you have a new message get yeah
good just take it right so let's go through what what happens here so Bob
that way but yeah Bob post a message to the server to Alice let's say right so
Bob says hey I have a connection it's open already so he doesn't really need
to open a connection but so that's that's a plus right you say so you know
what server hey I want to send a message to Alice and he posts the message the
server immediately just stores that server I mean the connection I mean the
message right also it decides it doesn't really need to store it if you think
about it like and that's what whatsapp claims katenka
claims that don't store messages because they use this right because they take
that message and immediately could be storing it could be encrypting we could
be it could do a lot of things but in general take that message and then you
know what oh you're going to Alice hey Alice is there that's the connection for
Alice proof take it right immediately it's like it's like acting like staking
something from one place to another it's like a broker you know just like hey
give me a message hey Alice is there that where is that channel for Alice
yeah just it's like a bunch of pipes that are open it's just like the server
delivers the message actually delivers actually pushes the message to the
clients throat directly wow that was vivid yeah so server directly can push
messages immediately to their client because it maintains a stateful full
connection I don't know is a full twice there but yeah it's it's a sustainable
connection the server knows every single client and it can talk to the client
directly in a bi-directional way that's why Bob sends a message and this
immediately gets it right long as she is online right so if Alice is not only
that means that connection is severed so that that's the the meaning of if Alice
is not online usually what what's up do here is like they don't allow you to
sign in and signing they you're always signed in and you they maintain this
connection they make sure that your connection is always up and running
right and they make sure that it's always going through this pipe it's like
huge pipe that is always known right so as good let's start with posing cons of
this so one of the pros and cons of this pros what are the pros obviously we have
real time stuff and that's like the best thing ever right because I have real
time I immediately get results right and no bandwidth so that's again there
is a like let's say less bad that's just weird
yeah so let's bandwidth because I don't have that poll messages anymore the
client doesn't pull that the network is only busy when there is really something
important right there is no messages that are almost I didn't write there's
no messages that don't do anything like check messages check messages check my
output there's no appalled right whatever I there if you see something in
the network it's a legit thing it's something that needs to be delivered
essentially right so let's band was right again it depends on you
but let's bandwidth compared to the port method right and yeah I would say even
could be a less CPU utilization because I don't need to open and close
connections and just like the stateless approach because I have the connection
open and the connection is always open and I don't need to spend time and
resources spin acknowledging the connection authenticating and doing all
that's the stuff right colons bad things about this what is bad what could be bad
about this right the fact that is a state fall it's kinda problematic
because scaling becomes really hard now because Alice have to talk to this
particular server right it has to talk to this server if this server is down
that's it Alice to have to re-establish the connection herself to another server
right it's it's it is responsible to make that connection again right unlike
the pull method where hey I know the load balancer I'm gonna talk to this
load balancer on this cluster and then I don't care if
that server goes down it will immediately refer me to another server
shit this was like so it will immediately do that load balancing right
so that's that's a stateful approach can can be like it won't be easy not easy to
scale horizontally right what else could be a problem
memory on the server because now a server let's say I have hundred say I
have a thousand connections on the server or a thousand clients connecting
to this server so I have to maintain a thousand TCP connection opened on own
port 777 right so I have to keep those open and make sure they're always
running and if it's there now I have to reconnect them again so those consume
memory versus the pol approach you only need to keep connections open when you
are doing something right so you can actually because it's stateless right
plus plus the the load is distributed among multiple servers in the stateless
approach again watch that video a stateless versus stateful to understand
what I mean here and I think I think that's that's it for
today sorry for the long with you guys but
hope you enjoy it if you new to this channel consider subscribing we have a
lot of other software engineering concepts and videos to teach you more
about solar energy become a better software engineer that so you can get
you can get a better better jobs and and become a better software engineer in
generals all right I like like if it's this video if you like it come and ask
any questions and I'm gonna ask you announced this week question time
what do you think what what what method of notification as a software engineer
you would prefer to use push or pull right nothing wrong with both there are
advantages as you can see and disadvantages maybe it's an overkill to
use Bush Bush is always good what the real-time thing it's a really big thing
but could be can I swear to use the poll approach to maybe save
in memory and do other things and do stateless architecture right so guys
that's as for us today I'm gonna see you in the next one
you guys stay awesome bye bye