node.js - NodeJS Event Loop Fundamendals -


i'm sure it's commonly asked question didn't find concrete answer.

i kind of understand basic concept of nodejs , it's asynchronous/non-blocking nature of processing i/o.

for argument sake, let's take simple example of http server written in node executes unix command 'find /' , writes result http response (therefore displaying result of command on user's browser). let's assume takes 3 seconds.

let's assume there 2 users 'a' , 'b' requesting through browsers @ same time.

as understand user's requests queued in event queue (message a, message b). message has reference it's associated callback executed once processing done.

since, event loop single threaded , processes events 1 one,

in above example, take 6 seconds callback of "user b" triggered? [3 "user a"s event processing , 3 it's own event processing]

this sounds i'm missing here?

the worst if 100 users requesting @ same millisecond? 100th event owner going unfortunate user , has wait eternity.

as understand, there 1 event queue in runtime, above problem can applicable user in part of application. example, slow database query in web page x slow down different user in web page y?

fundamentally, see problem in serial processing of events , serial execution of associated callbacks.

am missing here?

a written node.js server use async i/o , communication networking, disk i/o, timers or communication other processes. when written way, multiple http requests can worked on in parallel. though node.js code processes given request run 1 @ time, anytime 1 request waiting i/o (which typically of time of request), other requests can run.

the end result requests appear progress @ same time (though in reality, work on them interwoven). javascript event queue mechanism serializing work among various requests. whenever async operation finishes it's work or wishes notify main js thread of event, puts in event queue. when current thread of js execution finishes (even if has own async operations in progress), js engine looks in event queue , executes next item in queue (usually form of callback) and, in way, next queued operation proceeds.

in specific example, when fire process , asynchronously wait result, current thread of execution finishes , next item in event queue gets run. if next item http request, request starts processing. when second request, hits async point, it's thread of execution finishes , again next item in event queue runs. in way, new http requests started , asynchronous callbacks async operations have finished run. things happen in fifo (first-in, first-out) order how put in event queue. "roughly" because there different types of events , not serialized equally, purpose of discussion implementation detail can ignored.

so, if 3 http requests arrive @ exact same time, 1 run until hits async point. then, next run until hits async point. then, third run until hits async point. then, whichever request finishes first async operation callback async operation , run until done or hits async point. and, on...

since of causes web server take time respond sort of i/o operation (disk or networking) can programmed asynchronously in node.js, whole process works quite , lot more efficient server resources using separate thread per request. 1 time doesn't work if there's heavy compute-intensive or long running, not asynchronous operation ties main node.js thread long periods of time. because node.js system cooperative cpu sharing system, if have long running operation ties main node.js thread, hog system (there no pre-emptive sharing @ other operations there mutli-threaded system). hogging system makes other requests wait until first 1 done. node.js answer cpu hogging computation move 1 operation process , communicate asynchronously other process node.js thread - preserving async model single node.js thread.


for node.js database operations, database provide async interface node.js programming use database in async fashion , implementation of database interface implement interface in async fashion. done communicating other process actual database logic implemented (probably communicating via tcp). actual database logic may use actual threads or not - that's implementation detail database itself. important node.js computation , database work out of node.js thread in other process, perhaps on host not block node.js thread.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -