node.js - blocking code in non-blocking http server -


for example, have http server, node.js, non-blocking.

for each request db operation.

what cannot understand difference between blocking db operation , non-blocking db operation?

since web server non-blocking, blocking db operation inside request makes no difference?

here's analogy might understand. let's suppose you're in sales , have 50 phone calls make today.

in blocking model, place call and, if person isn't ready talk you, have sit on line , wait , wait until ready talk you. when ready talk you, have conversation, hang , can make next call , repeat process. so, time phone busy , not able make calls sum of time waiting client ready talk plus time talked.

in non-blocking model, place call , if person not available, leave quick message , call when ready talk. hang leaving message , call next client. total time phone busy time leave quick message , time talk when call back. doesn't matter if call in 1 minute or 3 hours - not affect overall efficiency @ making calls. can place , complete lot more calls non-blocking model because aren't wasting lot of time when doing else waiting client ready talk.

node.js gets efficiency using non-blocking i/o model , not using threads. implemented appropriately, non-blocking model more efficient having lots of individual threads waiting on blocking requests.

so, in node.js if have blocking database request, entire time database request blocking, node.js web server cannot else. has 1 thread if thread busy blocking request takes awhile complete server rendered useless significant period of time. broken implementation of node.js server process , there little point in using node.js way.

with non-blocking database request, node.js server starts processing http request. runs code, gets non-blocking database request. request started , callback registered completion, because it's non-blocking, database call returns immediately. node.js server returns event loop , can start working on other http requests or other events server process (e.g. completion of other http requests). then, sometime later, database calls finishes , puts entry in event queue trigger callback. when nodejs server gets event in event queue, callback called , original request has answer database server , can finish original request. meanwhile, time waiting database request finish, busy processing other things - it's 1 single thread.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -