node.js – Need help optimizing request forwarding for Node JS API
I am trying to write a front facing API route that will receive an incoming HTTP post request and forward it to a separate server of mine for processing. The reason I have this front facing API is because I do not want my processing server to be publicly accessible. The problem that I am having with my front facing API is that when I am testing this route under load, it generates high response times and blocks other routes from completing their request.
To keep things simple for testing, here is how I have it implemented currently
const axios = require("axios");
const HttpsAgent = require("agentkeepalive").HttpsAgent;
const httpsAgent = new HttpsAgent({
maxSockets: 16,
maxFreeSockets: 16,
timeout: 60000,
freeSocketTimeout: 30000
})
const axiosAgent = axios.create({
httpsAgent: httpsAgent
})
app.post('/request-forwarding', async (req, res) => {
res.status(200).send('success')
let config = {
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
};
await axiosAgent.post('https://myserver.com/receive-request', req.body, config)
.then(response => {
console.log(response.data)
})
.catch(err => {
console.log(err.message)
})
})
app.get('/blocked', (req, res) => {
res.status(200).send('success')
})
I have this code running on an old Azure S2 plan. The processing server in question only has 1 route that only returns a success response so I can be certain that request to the processing server is not being held up by anything. I am using autocannon via commandline to test the route under load. I have the benchmark setup to use 200 connections and run for a total of 10 seconds. The results from autocannon is telling me that the response times are averaging from 4.5 to 6 seconds during the benchmark. While the benchmark is running, I also have Postman making a get request to the second route that I have. The response times for this route averages from 3 to 4.5 seconds while the benchmark is active.
Here is what else I’ve tried to see if I could help alleviate this problem.
- Using a simple res.redirect(307). While this does for with single requests, when benchmarking via autocannon, the processing server is not actually receiving any requests. Any end user would also be spammed with 307 HTTP responses which I feel is not good.
- Removed the async/await from the route. This did not impact performance in any way.
- http-proxy-middleware. This was promising due to how simple it was, but its performance was worse than my current method.
- In-house fetch(). The results were generally the same.
I am out of ideas on how I can improve performance for this API and do not know if this is a scenario where I should rethink how to handle outside requests or if this is a scenario where I just need to throw more hardware at it. When running a benchmark with the front facing API running locally but still have it posting to the processing server on azure, I am experiencing low response times that don’t exceed 400ms. I would appreciate any help for this problem.
Read more here: Source link
