what is going on guys my name is russain
and in this video I want to talk about
three four four things yes
synchronized operations asynchronous
operations multi-threading and multi
processing I hope I nail all of them if
you are interested stay tuned so the
first one guys synchronous operation
synchronicity is when you make a request
whether this is a network call or a
request to read from disk or a request
to send to a printer to print does
anybody print these days I don't think
so
right but yeah if you're doing that then
there is this magic thing that's what we
call the process and there's in this
process there's always by default one
thread that does the work actually does
the actual work now think about it guys
if you're calculating a prime number
then you that thread is just busy in the
CPU right this is using the resource of
the CPU to calculate things 1 plus 1
divided by 3 how do you calculate a
prime number alright so now it's busy
but think about it if you're reading
from disk you ask your thread is asking
the operating system to read a file from
disk the thread is not doing actually
any work it's basically blocked and
waiting that's it it's just waiting so
if you reading then who who is doing the
actual work from reading the disk that's
the disk controller
it's called IO controller it's it's
something on the motherboard that I
forgot how it actually works it's just
some driver on the battle board
maybe the operating system is
responsible that so the thread here is
just sitting here like that it's just
yeah alright I'm just waiting here not
doing nothing so the rest of your code
guess what it's also blocked and that's
called synchronous execution because
you're sending a request and you're
waiting and blocked despite you not
doing any work really
is just blocked so that's synchronous
exclusion same thing applies to
ascending a rest request to the endpoint
to the to the server right you're not
doing any work the server is doing the
work you're just waiting for result
right the TCP just file descriptor is
waiting for something to wake it up and
receive them for mayhem well until that
is you also block so if there are code
here you cannot execute that code
unfortunately if there is a UI thread
right no if you if there is a user
experience or UI and peep the user is
actually pressing assuming you're a
single threaded that button doesn't do
anything you cannot do anything because
that thread cannot leave that state to
actually go and serve and animate the
button as if it was clicked because
guess what it's blocked assuming a
single threaded right most application
has a UI thread in but let's assume just
for simplicity if you're asking a visual
basic 5 application like 1998 kind of a
things right but yeah one application is
blocked right now people say this is a
ridiculous we cannot do this this is
dumb how about we introduce some other
thread let the let the process execute
another thread that does more work
sounds cool so what is a thread a thread
is we have the process think of it as
like a container but think of it like
almost like a container that has 1/3 of
execution all the time right if you as
the process owner a writer of the
application can spin up another threat
to do some other work another third can
do some other work all these threads
have in common is they share the same
resources that the process has that is
big big big big big problem because they
can start tracing for these memory
locations and you can get into the same
situation that you get with a database
which is locking and race conditions and
all that jazz right so
what do you do guys what do you do right
you don't do multi-threading because
multi-threading is evil right
check out this white paper that people
just say stay away from multi-threading
so people says okay some people stayed
away from multi-threading because they
cannot get it right because it's very
hard to build your application so it's a
thread safe right that's what calls
thread safety is essentially having
mutexes on your resources so no two
threads can actually access the same
process resources at the same time
what's the problem right that's the
that's the big problem right so people
shied away from multi-threading some
people like there's still a thing some
people still doing here and they are
bragging that their applications through
itself definitely sequel light per hour
cycle light does not like
multi-threading check out check that out
he despised it so yeah so
multi-threading is essentially evil
that's my personal opinion you might
have different let me know in the
comment section but essentially
multi-threading is yeah it's very hard
to write it's very very difficult to get
right you need to have a very very
skillful understanding of how operating
system works and your CPU because your
CPU can have multi cores and your
threads can execute in parallel
literally important not time slice in
parallel like that's that's good but
doesn't give you much in for much
benefit to be honest some smart people
came and says you know what we're gonna
execute something called asynchronous
accusin and nodejs is a very elegant
example of this it's a single threaded
non blocking asynchronous framework
that's that's the title of this so let's
talk about asynchronous ctz right i
cannot spell guys and i cannot talk
about asynchronous cities asynchronicity
is the idea of having a single threaded
single process single thread that does
the work the process cannot do work by
itself well
it needs a thread but well that is also
based on the operating system Linux
treat threads as a sub process versus
windows actually just three stories
either actual thread of thing which
would kind of complicate things but
never Maya back to the point single
thread single process if you're reading
from desk we're gonna do it
asynchronously here's what I'm gonna do
because the thread doesn't literally it
is not doing anything it's just waiting
and it's being blocked and the rest of
the code cannot be executed it's blocked
here right and the rest of the code
can't get executed right so what do we
do what do we do guys and I'm sending a
request to the i/o controller to read
from disk and I'm sending a request a
fetch command for example JavaScript and
I'm reading from network resources
breast endpoint G RPC right and I'm
waiting I'm just just sitting here
waiting doing nothing right the
application knows this and you say oh
okay this thread is not really doing
anything so what we're gonna do is let's
send that request that is essentially
that a quiz that is actually was
blocking and here's what I'm gonna do
we're gonna tell whoever owns that other
end
io control network says hey here's a
request I want to read this resource I
want to read this rest endpoint I want
to read this block of desc
and here's the thing I'm gonna go away
and here's a function it's called a
callback call me back all right
whenever you're done I'm gonna do my own
thing here so the the thread immediately
unblocked and start executing other
codes until that function finishes right
that actual reading is done and the
reader will call back that function
which will trigger some sort of an event
which the thread oh there's something
here let me go and execute and this is
in JavaScript the implementation is that
literally an event loop the thread goes
is okay do I have jobs to execute do I
have actual code that I need to execute
is there something coming from a
callback is there something going a lot
yeah so that's how work async
honestly beautiful beautiful design one
slight problem
and I don't know if you feel coded and
callbacks in Ajax back in the 2000s 2004
2003 that's when I started coding with
Ajax that cord is ugly as f you cannot
read it because most of the time we need
go a callback you need some information
from the callback to do something else
so you start doing ok callback and then
is okay this function and then you take
this function and then let me go do all
this other thing and then that thing
needs a callback so you end up doing a
curly braces in the cooler braces in
color braces and color braces and it's
completely unreadable right so people
when they invented this meanwhile some
people didn't like it
the multi-threaded people says yeah
that's just ugly code like multi so it
is a beautiful code huh right but some
people defer to from this because it's
just a cleanest the JavaScript community
came in and says you know what we'll fix
this we still like I synchronous but
we're gonna bring promises they promises
and the picture and now the code is
better with that vendor then the then
the then the catch some people even did
it even better and that's pretty much a
standard I think async await you write
the function you code this is
asynchronous function and every code
that is essentially blocking you write
and await on top of it and that's it it
will act asynchronously but look
synchronous to you and it's all sheesh
and syntactical sugar shin tack she can
you think syntactical sugar three times
in a row I dare you guys you cannot
syntactical sugar I cannot think damn it
okay all right go back all right so
that's that's a a syncretic sujin that's
my favorite thing nor I like my process
to have single threaded and I don't care
about multi-threaded I'm gonna use
asynchronous execution
all this mess of synchronous
asynchronous and multi-threaded some
people said if all that we are multi
processing guys multi processing I'm
also with this team multi processing and
I'm gonna define it the way I understand
it if if that doesn't make sense let me
know or if he if you disagree let me
know because I would love to learn how
do you perceive this so multi processing
the idea of spin up instead of splitting
up threads in a single process which
shares the same resource of the process
right what the heck is that
okay so instead of spinning up multiple
threads in a process spit up just unique
processes with their own beautiful
memory structure with their own resource
with their own everything and then what
do you do you just communicate between
them use inter process communication you
can use a centralized Redis database and
yeah there are many ways to communicate
between processes you can use sockets
between them I know it's a little bit
overkill but you can list literally
listen on a server here localhost
localhost localhost and ports and
communicate with port and all the
communication will happen with ccp's
right that's fine it's all localhost
anyway who cares you can do that people
do this absolutely fine right there are
some limitations but here's one example
where multi process is actually good
let's say you want write a function that
brute-force a password that example I
know but you want to write it so that
multi-threaded application
multiprocessors and asynchronous write
the best way to write it in my opinion
is use a multi processor because multi
process can be spin up on multiple not
just judgin the same machine could be
scaled to run on multiple machines as
well alright and i believe this is
essentially how containers work in a
nutshell they're more isolated than a
process but sure but here's a here is a
good problem I want
- this is a hash right md5 hash and I
want to know I want to reverse it which
is an almost an impossible problem
impossible it is a very hard problem to
solve so what do you do well you
basically come up with garbage strings
and try to hash it and see if they match
it and the string matches the hash of
this thing matches the hash that you
were given that means that's an input is
a candidate input right well one machine
can just take a string and then just
loop through and take a dictionary a
rainbow table and just loop through all
of them and then generally start
generating the hash that's absolutely
fun but here's the thing you can have
multiple processes throw multiple
processes at the problem and you say
okay you process one take this rainbow
table you process to take this
dictionary from A to M and this process
take from n to Z right and so on
you can stop splitting a problem and
multiple process so your multi process
sink right and if you're kind of hip
right and it's like yeah if you're hip
you can use a reverse proxy send that
problem to reverse proxy and let the
reverse proxy actually divide the
problem you can write logic in the
reverse proxy I believe it's a proxy
Ingenix allow you to write some checks
at least lower code to actually split
Hey oh you yours
this server spin up a container or
multiple containers and throw some
problems at it it's like between this
and this and you server between this and
this and you between this and this and
so you're kinda multi-processing twindom
hadoop kinda work the same way as well
but yeah so that's essentially a multi
processing that's in the way I see it
might be wrong but that one's I can
brief discussion between synchronous
asynchronous multi-threading and multi
processing guys I hope you enjoyed this
video you guys see you in the next one
keep up those good good questions coming
guys I love them so much get see in the
next one you guys stay safe out there