In my application, I have to create an API for searching n items. The server logic is to find for items in it's own database and if the result count is less than n, then use search results from a third party service to fill in the remaining results.
The problem I'm facing over here is, the time taken to execute the third party request.
On an average it takes around 900-1500 ms to execute the third party request from the server, however if I execute the same request from browser or any other rest clients, the same takes around 300-400 ms.
This is what I'm doing
router.get('/search/:searchParam', function (req, res, next) {
async.parallel({
local : function(callback){
//Search for object in DB
callback(null,localResults);
},
fallback : function(callback) {
var url = <searchURL>+<queryParam>;
//This takes 900-1500 ms
request(url, function (error, response, body) {
if(error){
return callback(err);
}
callback(null,JSON.parse(body));
});
}
}, function(err, results){
if(err){
return reject(err);
}
if(results.local.length < searchLimit) {
results.local.push.apply(results.local,_.first(results.fallback,searchLimit-results.local.length));
results.local = results.local.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]);
}
res.json(results.local);
})
})
The avg time taken is :
A. 40-100 ms for local search
B. 900-1500 ms for fallback search
A+B. 1000-1500ms Total time
I'm not getting how to improve the overall response time.
Any suggestions?
Its probably because you aren't using keepalive with your requests. I think this will probably fix it, but I could be wrong.
request({url: url, forever: true}, function (error, response, body) {