so that's my head Twitter and all add
materially nough please ask me questions
whatever I replied most of the time so
I'm going to
to talk about the no dial you might ask
why not' well I am part of the nodejs
technical steering committee so whatever
a lot of things no J yes if you have
questions about north core thing me so
I'm also a principal ocular the near
forum we are a professional service
company
distributed all around the globe but
business in Ireland whatever if you need
us we are there check it out check us
out also another little bit of intro
there is these lights if you have a
laptop there are some interactive
visualizations so if you have a laptop
out and you want to play with them these
are these lights you can take a picture
whatever whatever you want so let's get
started when everybody starts with note
they start with a very simple question
how to debug a synchronous activity you
know you have a socket coming in on an
HTTP request coming in and you want to
react to to those activity and you know
what everybody does oh well I can use
the inspector protocol the expector
chrome inspector and I just laughs yeah
more or less
but what if we have 10 concurrent
requests a hundred how can i debug an
application running in production where
i have a lot of concurrency in place in
other I came from the Java world
whatever I've done this and I've said it
you could stop a thread single try than
just debug that you can't do that in
node so most of the stuff is because no
js' is based on a concept called the
ever loop and you'll probably are
familiar with the node event loop okay
how many of you think you are familiar
with a node event loop okay how many of
you I've seen this less people this is
this the secret sauce okay that's the
most Aiden piece of node lore that you
can find took six months to write that
guide please read it if you want to know
more about our node works and this is on
that node event loop works probably not
really easy to understand so let's
switch to a more easy to understand
version because Pro
a little bit more clear to everybody so
when the JavaScript is execute the only
thing that a node application does when
at least two i/o it talked to the kernel
a single barrier or the thread pool and
says when this event happens when this
condition happens please call back call
a function when these finishes that's
the only thing that that node does and
it is specified in passing a JavaScript
function and you know some event happens
like you are receiving some data from a
socket and that it calls back and then
wants to call that function how does
that happen that is let's see whoa there
is an event queue here and the event
queue means that all the functions are
processed one at a time so just to
repeat the dusky primetime specify a
fare other function as a listener to
some IO event and then the yo event
happens and the function is called but
you know that is one caller ID to all of
this we have only one function executing
again a given time and this is one of
the key tenants of any nodejs
and in ojs function now you might ask
why all of this matter when doing a
synchronous activity would it make these
things simpler yes and now it makes
things simpler to code however there is
one key piece that is a little bit
obscure which knowing which code is
running relative to one another also
what about next ik promises and set
immediate now which one will run first
if you schedule a promise if you run a
promise if you do next ik or if you do
set immediate you know I'm not throwing
set timeout zero in there because that's
even trickier so next it are always
executed before
promises are resolved and before any
other are your events promises are if
you do new promise the function is
executed synchronously but then resolve
are synchronously
and before any other are your events set
immediate instead exercise the full
event loop that we told you that we've
told you you know and I've not thrown in
setting itself time Abdera because
that's another level of complication so
again the artist piece of information
that the thing that when a newbie starts
working with node is to know when a
chunk of code is executed relative to
one another so going back to the
question how to debug multiple
asynchronous activity how do we do this
because you know we have multiple things
happening how do we we have a bad bug
and that is happening only when I'm
calling three different routes at the
same time and the moon is aligning and
Mars is over there I don't know
it really gets complicated and I don't
understand what what is happening it
here it's very hard so it goes back to
to a problem and something that we're
missing in JavaScript which is the
concept of the synchronous context so
let's look at a bad very basic HTTP
server written in ojs and you can see
that we do our HTTP create server and we
pass in a function this function in fact
when we receive an HTTP request from a
browser you have a new logical a
synchronous context that is being
created which means you know this is
your transaction this is your HTTP
request and response this is a logical
concept that is living in there okay all
the things that the Sun from this one
will be you know linked one with another
mainly because they are part of the same
closure you see it's a closure created
here so they are part of the same
closure and then when I create another
closure you know it's in here okay and
and the context is being the response is
being propagated just by using that so
it's really powerful right however there
is no concept of a synchronous context
in the JavaScript language so you know
it's something that is built in into our
browser works and how the event loop
works but there is no way to control in
any
reflection oriented way this type of
things so it's completely illogical
contest construct but is not a language
not a language feature there is some
work in making this a reality where
there is actually a concept in the
language pack and some of the wall you
can find some of the work done by the
Diagnostics working group in no js'
check it out and this goes a little bit
into the deep depth of this and it's
really really interesting to read then
well if there is no concept of seamless
context how can we track IO events so
how can we know when things happen you
know this is a thing called a Singh
cooks you're probably heard about a
Singh cooks in the past and it's one of
those you know new features of not Jas
that do that can do great things also
you know there are somewhat hard to use
this is an example of using them this
actually real real code can you
understand what it does
well it's complex and it also generates
a lot of data and I'm not going to
explain it because it's a really big
that conversation so myself and my team
we have work force each month trying to
make sense about all the stuff and how
to provide a way to reason about the
synchronous context and their
synchronous activity in node J yes so
you know and when you have this type of
question the best way to attack these
problems you know I am a software
engineer okay
by trade whatever so you know you go oh
well you ask a designer sorry we can't
do this so I ask we ask I ask a designer
hey can you please sit down with a
little bit of a design workshop whatever
to figure this out turns out that they
they answer to the question on how to
visualize the synchronous activity is
bubbles so bubbles yeah that's it so
let's make an example we have we have a
nice very tiny piece of ass off code
here okay it's the same server that we
were talking about before you so we go
to all our lights so hey so we go to our
so we are here and we have that code now
we can use this tool called bubble probe
and this part of a sweet code clinic you
might check it out late this is critter
linked and autocannon is a tool to
generate some load so we are putting in
a thousand requests just to spice things
up to know where things behave in
production and basically you run on your
code not probably I've typed yeah you we
run our code now you can see that
autocannon is pinning things up a little
bit and running a little bit of those
requests and it's basically now
generating our visualization take some
time you know this machine is not
powerful at all and this is a bubble
prof diagram now I'm zooming in and so
what we can see on on the right side is
the number of a synchronous operation
being done of by this system and you can
see that it's doing you know we have
twelve thousand a synchronous resource
being created by running a thousand
requests in no Jaya's does this app help
anybody maybe yes maybe no the key
important part are blue a further down
we can start from the root so when the
root this is the root node and represent
the starting point of your application
yeah then we can go deep and you know
see the first part and the first part in
fact it's on line 20 of our application
basic dodge is whatever it is and it's
alcohol to server dot listen our server
is starting to listen on stuff still
this is not helping me and again knowing
what is happening in my node application
so the next part it's this big block
here which is a bunch of and network
activity which is you can see it in in
green and this bunch of network activity
it's there's some bunch of antifreeze
these are not core stuff but then we
have a bunch of another node core stuff
which come from how we receive a TCP
connection in node.js itself so you know
that you are receiving a TCP connection
on J yes when you receive an HTTP
request you got an HD a TCP connection
being open after this you after this you
actually the key part of our application
which is our
set time out now you know you see that
is a set time out happening here and you
see a fork and you can ask wise is there
a fork now you can see two things first
of all the execution continued on the
left because there's a big Harrow that
will give you a hint that the execution
continued on the left but our execution
force white force well on the right you
can see that you know it start a set
timeout okay on the right but then on on
the left its when it's our promise dot
then okay which the resolution of the
promise itself so it it is and there is
a tiny bit of thing in the middle of
North core bits and bobs where things
are you know executed so so you know
these things is clear enough and we have
a bunch of stuff that not core does when
it finishes and you know it's for
example in here we are calling a
responsible end which is where we send
our response back to the user so we were
talking this okay no these I understand
is this is matching which matches my
understanding
now let's pile things up a little bit
now let's do a little quick refactoring
on this system and well if you are
following there is the full name of this
thing the generator one let's switch to
the sink oh wait when I think about
version this is function is that
function J functionally equivalent to
the previous one note that we are using
dot catch always put a dot catch when
you're doing these type of things
because otherwise you will leak file
descriptor and you know make popcorn of
your servers so I don't do that
put a catch here and when you're calling
promises so you need you know we are
doing this so let's try let's see if
it's the same as before
how many people think is the same how
many people 60s there different somebody
knows
somebody knows so this is this is that's
my zoom in okay so you can see that this
is actually different from the previous
one you generate a different diagram why
well first of all we can note that we
are actually using more synchronous
resources
note that promises are a synchronous
resources because they are scheduled in
they're going to be a synchronous
activity happening in the future so you
see that we are scheduling 2000 more
promises more or less than before why is
that
and the reason comes in the way the a
sink await I think a way to specified
and you know for every way that you do
you are locate a bunch of promises that
are not necessarily needed and they're
going to be removed and you know fixed
in later version of the spec I can send
you some links if you are interested so
the Reedy and you can see the difference
here that you know we have our handle
function and then we our a sink await
function and it gets split in two and
there is a bunch of tiny thing in here
which says one frame with no file and
this is you know there are some
throwaway called throwaway promises in
its gets you see some artifacts due to
our promises work and you know and this
is a completing of the hallway promise
that goes nowhere this promise is
completely like generated to just
fulfill some spec need whatever anyway
this is with the sink await and you know
then another step interesting it's just
just use callbacks you know why we are
using this type of thing why can't we
just use callbacks and let's do another
example that's calling FS read file one
of the parameters of nodejs so we go
back here and we are running our files
example and I think loosing tab but it's
not a really good idea so a lot of files
in there so we are running it and
generating some bunch of things and here
we go
some magic sprinkling I don't know here
we go
now you can see that is another a
slightly different one this is fully
linear let me zoom in okay you can see
in this blah blah
there's a lot of brown activity which is
a reading of files a bunch of scheduling
activity which is the purple and then
some green one which is data so we have
these blocks of brown activity in here
that you can see and you know the third
let's click on the first one and the
first one is our f1 function and you can
see oh maybe as we are reading a file so
this is scheduling our rid of the file
then we have this next block afterwards
which is in fact a combination of three
different a synchronous activities
because no J yes in order to read that
file is actually accessing the file
system three times and is completely in
it for you you just call a fast read
file but it's segmented and it makes
things interleaved so that it you can
use multiple signals activity at the
same time and he's doing treat and is
doing three things and it's calling this
function read file after open which is
totally an old call thing and we are not
should not really worry too much about
it
and now we have the first we have the
first file and then the second file and
then the third file okay this seems I
can understand this so let's switch to a
sink await okay now you should have
learned we should have seen that it
changes significantly the structure of
my application and it's it's actually
very it's actually very useful because
it moved from something that is very
hard to read and very hard to maintain
or something this is way better to read
and maintain so yeah I like it right
I'm a fan by the way and so what is
happening it's now you can see that the
the actual bubble prof art as we call it
it is slightly different than them
before because you can see that there is
way more scheduling activity happening
and you can see where the throw away
promises are so there is at Rahway
promise here via KHOU we are actually
calling a sink await three times as I
wait three times so we have three throw
away promises and we have one here one
here and one here now there is and we
can now understand how things are
getting interleaved between each other
and how we can use this information to
write better code and maybe fix problems
one more thing it's we have written our
code in this way now we can also very
quickly write our code to be Impa in
parallel note that changing a bunch of
code back he'll functions to be parallel
it's hard doing it with the sink away it
is a matter of changing three lines of
code so benefit there note that I'm not
using a weight and let's see how it goes
take some time here we go I'm zoom in
and now now you can see that in fact the
truth we throw away promises that we
were talking before I have gone I have
disappeared and we now have our only
three blocks of reading our files and we
can unpack this web and you can see that
you know there is all the the things
will get generated properly and stuff
like that and there is a lot of
information that you can that you can
gather from this tool and to visualize
your synchronous activity and understand
how human your node process work in fact
we've done some analysis on some some
open source modules this is from a
library called ipfs and it's a
distributed web platform and we have
done some optimizations in there and
there was some some there was things
that could be improved and you can see
there is a lot of activity in here a lot
of things that are you know chained
together and so on and so forth and you
can read all of this in this issue if
you are interested it's long it's a long
long long paper of explanation on what
was the problem
how we fix it in the end we fix it we
got a 30% performance improvements so
yeah
what's nice what was a nice one okay so
let's go a little bit into some
performance consideration on how you
write your node application and how to
improve them so you know we've talked a
little bit about the event loop and now
the fact that your i/o
runs one at a time right and we schedule
functions so when we receive a request
and it really is we schedule some
activity to happen in the future
we set a function into our event loop
machinery right and they will call me
back when it's finished so when this is
low are your operation that function
will stay alive longer okay which means
that they can accept more stuff from I
can accept more requests before
returning right that's what everybody
would do well I have capacity I'm not
doing anything I'm haidle so I will get
more data from you know I let's add more
HTTP requests so yeah it's increased the
amount of concurrent tasks because I
have more CPU bandwidth now I'm waiting
for stuff to happen so I can execute
stuff good however the more increased
the concurrency the more you increase
the memory consumption why well we have
a for each one that we received we are
allocating new closures so new closures
gets allocated new objects and all of
these occupy memory which is you know
precious in most systems the moment you
increase memory consumption you increase
you make the word of the nice garbage
collector harder why well because you
have es more stuff to clean up and you
will is worth even the performance of
the garbage collector is getting better
and better and better and better better
better and better every year we are
scheduling more stuff every year to
every moment to do more arise so it
increased the pressure on the garbage
collector to to do work and these the
garbage collector where does it run is
it magic no it's not magic it runs on a
CPU and you know when you are working on
a container system or are deploying
nodejs in production you are doing you
probably have one or two or three
maybe course I don't know Italy most of
the time once one Corps
so if regard watch collectors running
maybe my note process cannot run so we
have a problem there okay there is
contention so in the end and so this
means that the garbage collector will
steal my CPU cycles perfect and will
steal my CPU cycle from the JavaScript
critical path which is which is a little
bit a tricky thing II so the net result
is that late in a node.js application
latency and throughput are tightly
connected so the moment you see an
increase in latency you will see also
likely a decrease in in throughput
mainly because they are linked together
by the way the node works and the fact
that typically it can take a huge amount
of high concurrency compared to other
platforms and and this is one of the
things that you know having doing it as
a job to go through and putting node.js
application in production it's one of
the most misunderstood concepts about
about no GS and think thinking that this
true concept latency and throughput are
not connected to each other and you
should always remember this so that this
tool is part of the clinic J's suite you
can find it there it there is also a
video of me speaking whatever so if you
need some help in improving the
performance of your node.js application
reach out eh say hi and yeah that's it
and again these lights you probably want
to check them out I don't know hopefully
hopefully yes hopefully now I will tweet
about them sooner rather than later
somebody taking a pictures thank you I
put it right they put it there for you
so yes and thank you
you