Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello,
- The following is why putting timeouts on a node js is a bad idea
- first let me clarify timeouts on node.js are fine but 45000 timeouts on each event (soccer game) on node.js is .
- Let's start first with what is node.js : is a JavaScript runtime built on Chrome's V8 JavaScript engine.
- 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
- 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
- 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 )
- 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
- promises : https://developers.google.com/web/fundamentals/primers/promises , https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
- js es6+ = https://medium.com/engineered-publicis-sapient/javascript-es6-es7-es10-where-are-we-8ac044dfd964
- everything on node.js runs on the event loop ( its the architecture that's used to process requests functions .... )
- if you are unfamiliar with the event loop i suggest reading the following article they explain everything in detail :
- https://nodejs.org/uk/docs/guides/dont-block-the-event-loop/ ,
- https://nodejs.org/uk/docs/guides/event-loop-timers-and-nexttick/ ,
- https://blog.risingstack.com/node-js-at-scale-understanding-node-js-event-loop/
- in a multi threaded stack running multiple functions that run after a delay is fine or you can use cron jobs
- 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/ )
- these while not breaking the even loop on large quantities they will slow it in a big way
- 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 :
- 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
- 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
- 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
- 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
- but with a huge base of users ( a lot of request ) it will simple not work on a single threaded web app
- so to do it in an efficient manner you need a multi threaded stack ( yours * java * )
- one idea that might help you already implemented the timeout in your payment page.
- 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
- 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
- Thank you
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement