SHARE
TWEET

ENDMENOW

a guest Feb 18th, 2020 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Hello,
  2.  
  3. The following is why putting timeouts on a node js is a bad idea
  4.  
  5. first let me clarify timeouts on node.js are fine but 45000 timeouts on each event (soccer game) on node.js is .
  6.  
  7. Let's start first with what is node.js : is a JavaScript runtime built on Chrome's V8 JavaScript engine.
  8.  
  9. node js is unlike java ( your back end stack ) is a single threaded application  which means that all the requests are processed in a single thread
  10.  
  11. meaning that node can only process one request at a time before it goes to the next one on the event queue there for any code that block I/O is highly not risky because it might block your queue of incoming requests
  12.  
  13. to solve that js ES6+ uses promises and async functions ( in case you are unfamiliar with ES6+ read up on the following links before you continue )
  14.  
  15. async : https://javascript.info/async-await , https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function , https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
  16.  
  17. promises : https://developers.google.com/web/fundamentals/primers/promises , https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
  18.  
  19. js es6+ = https://medium.com/engineered-publicis-sapient/javascript-es6-es7-es10-where-are-we-8ac044dfd964
  20.  
  21. everything on node.js runs on the event loop ( its the architecture that's used to process requests functions .... )
  22.  
  23. if you are unfamiliar with the event loop i suggest reading the following article they explain everything in detail :
  24.  
  25. https://nodejs.org/uk/docs/guides/dont-block-the-event-loop/ ,
  26. https://nodejs.org/uk/docs/guides/event-loop-timers-and-nexttick/ ,
  27. https://blog.risingstack.com/node-js-at-scale-understanding-node-js-event-loop/
  28.  
  29.  
  30. in a multi threaded stack running multiple functions that run after a delay is fine  or you can use cron jobs
  31.  
  32. but ins node the only things that won't interfere with the event loop is good old fashioned settimeoute functions ( https://nodejs.org/de/docs/guides/timers-in-node/ )
  33.  
  34. these while not breaking the even loop on large quantities they will slow it in a big way  
  35.  
  36. presuming that you read most if not all the reading material or that you are already familiar with all of the above here is an example :  
  37.  
  38. if we have for example 5000 users ( very low number of users )  now for each on of them when they go to the payment platform a setimeout function will initiate ( an anonymous function so no way for clearsetimeout )  the function will start after for example lets say 10 minutes . if you are used to synchronous code you might think that after the 10 min it will execute but in reality it won't
  39.  
  40. so as a recap now we have 5000 users * ( 10 min settimeout function )  when the time of each one finishes it won't execute instead it will go go on the stack to execute on the next tick
  41.  
  42. meaning after the current even loop end node checks for timers so it will find all those settimeout functions and it will start processing them each on at a time and it won't stop until there is nothing left on the timer stack
  43.  
  44. meaning that all the incoming requests ( login , logout , add to cart ) are going to be waiting for the timeout functions to end now if we had a small number of timeout functions it would be simple and fast
  45.  
  46. but with a huge base of users (  a lot of request ) it will simple not work on a single threaded web app
  47.  
  48. so to do it in an efficient manner you need a multi threaded stack ( yours * java * )
  49.  
  50. one idea that might help you already implemented the timeout in your payment page.
  51.  
  52. a possibility is that when that happens you send a request there to the fail url  since a timeout is going to be considered the same as a failure in payment on our end it might as well be sent on the same link
  53.  
  54. in case you are not familiar with node and es6+  and you have any questions regarding it feel free to send an email or call i would be more than happy to help  
  55.  
  56. Thank you
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top