What i have
- incoming single http request with ~10mb of data (i get hundreds of them in seconds)
- couple of servers.
loadBalancer
,server-1
,server-2
, ...availableServer
What i am currently doing
1- loadBalancer
gets request. Downloads data. Choose server. Send request to availableServer
2- Processed data send back to loadBalancer
4- Load balancer send the result to user.
Problem
loadBalancer
is downloading, uploading and handling big data as extra. Which slow downs the response time for user.
What i want
1 - Http/https request will come to my loadBalancer
server
2 - loadBalancer
choose destination server and redirect the request without downloading big data in request body.
3 - Main server will process the data and return the result to the user.
My Question
How to redirect the request without downloading data in body? Or what technique should i use to overwhelm this situation. I just want loadBalancer
to do a gentle touch to request to adjust it in the right direction.
Possible but unwanted solution
User can simply call
var bestServerUrl = get('loadBalancer/getAvailableServerUrl')
var result = post('bestserverUrl/processData',bigData)
But this way is unwanted. Becomes more complex for user. User might only use one server url. etc... It should be handled by me.
CodePudding user response:
See http-proxy-middleware, dynamic target. Your load balancer behaves like a proxy with dynamic target.
As the following code demonstrates, the load balancer (on port 8080) feeds the request body through to the available server (on port 8081) chunk by chunk, as it receives them. (The code simulates two chunks sent with a one second interval between them.)
express()
.use(function(req, res) {
req.on("data", function(data) {
console.log("Received " data);
});
})
.listen(8081, function() {
express()
.use(createProxyMiddleware({target: "http://localhost:8081"}))
.listen(8080, function() {
var req = http.request("http://localhost:8080", {method: "POST"});
req.write("A");
console.log("Sent A");
setTimeout(function() {
req.write("B");
console.log("Sent B");
}, 1000);
});
});
The load balancer does not fully download the big data before redirecting it. It opens an outgoing request to the available server and pipes the incoming request into that outgoing request. How big the chunks of transferred data are depends on the buffering settings in Node.js. In the end the entire big data passes through the load balancer, but it need not keep more than one chunk at a time in its memory.