4

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?

Amresh Kumar
  • 1,445
  • 17
  • 30
  • Depending on what database you are using, do you have (and use) indexes on the columns/fields that are getting searched? – Ash Jan 25 '16 at 10:46
  • @AshleyB My database response time is good b/w 40-100 ms. It's the http request to the third party server which is taking time as mentioned.. – Amresh Kumar Jan 25 '16 at 11:05
  • If your high response time is coming from the third party, you won't be able to do much. It's on there end, not yours. Depending on how up-to-date the response is, you could cache it locally with an expiration date using something like Redis. – Ash Jan 25 '16 at 12:41
  • @AshleyB If your read the question, you'll see that I've mentioned clearly when the same third party call is being made from browser, it takes 300-400 ms to respond. The same third party call is taking 900-1500 ms when being executed from the node server. – Amresh Kumar Jan 25 '16 at 13:01

2 Answers2

4

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) {

wrleskovec
  • 326
  • 1
  • 7
  • 1
    I was adding Keep-Alive in the header, but wasn't working. Added forever : true and it behaves as expected. Cheers – Amresh Kumar Jan 28 '16 at 12:10
0

Instead of using the async library, try calling the third part API in the request callback of your local DB search.

sam100rav
  • 3,733
  • 4
  • 27
  • 43