This package is a simpler install without native dependencies, but requires node >= 0.9.1.
What happens when your service is overwhelmed with traffic? Your server can do one of two things:
This library helps you do the latter.
toobusy polls the node.js event loop and keeps track of "lag",
which is long requests wait in node's event queue to be processed.
When lag crosses a threshold,
toobusy tells you that you're too busy.
At this point you can stop request processing early
(before you spend too much time on them and compound the problem),
and return a "Server Too Busy" response.
This allows your server to stay responsive under extreme load,
and continue serving as many requests as possible.
npm install toobusy-js
var toobusy =express = ;var app = ;// middleware which blocks requests when we're too busyapp;app;var server = app;process;
The library exposes a few knobs:
maxLag - This number represents the maximum amount of time in milliseconds that the event queue is behind,
before we consider the process too busy.
interval - The check interval for measuring event loop lag, in ms.
var toobusy = ;// Set maximum lag to an aggressive value.toobusy;// Set check interval to a faster value. This will catch more latency spikes// but may cause the check to be too sensitive.toobusy;// Get current maxLag or interval setting by calling without parameters.var currentMaxLag = toobusy interval = toobusy;toobusy;
The default maxLag value is 70ms, and the default check interval is 500ms. This allows an "average" server to run at 90-100% CPU and keeps request latency at around 200ms. For comparison, a maxLag value of 10ms results in 60-70% CPU usage, while latency for "average" requests stays at about 40ms.
These numbers are only examples, and the specifics of your hardware and application can change them drastically, so experiment! The default of 70 should get you started.
toobusy-js exposes an
onLag method. Pass it a callback to be notified when
a slow event loop tick has been detected.
There is nothing new under the sun. (Ecclesiastes 1:9)
Though applying "event loop latency" to node.js was not directly inspired by anyone else's work, this concept is not new. Here are references to others who apply the same technique: