a guest Feb 18th, 2020 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Hello,
  3. The following is why putting timeouts on a node js is a bad idea
  5. first let me clarify timeouts on node.js are fine but 45000 timeouts on each event (soccer game) on node.js is .
  7. Let's start first with what is node.js : is a JavaScript runtime built on Chrome's V8 JavaScript engine.
  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
  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
  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 )
  15. async : , ,
  17. promises : ,
  19. js es6+ =
  21. everything on node.js runs on the event loop ( its the architecture that's used to process requests functions .... )
  23. if you are unfamiliar with the event loop i suggest reading the following article they explain everything in detail :
  25. ,
  26. ,
  30. in a multi threaded stack running multiple functions that run after a delay is fine  or you can use cron jobs
  32. but ins node the only things that won't interfere with the event loop is good old fashioned settimeoute functions ( )
  34. these while not breaking the even loop on large quantities they will slow it in a big way  
  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 :  
  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
  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
  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
  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
  46. but with a huge base of users (  a lot of request ) it will simple not work on a single threaded web app
  48. so to do it in an efficient manner you need a multi threaded stack ( yours * java * )
  50. one idea that might help you already implemented the timeout in your payment page.
  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
  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  
  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