0

I have unpleasant situation when one of my "long" response in some way blocks another AJAX requests. I call simultaneously 3 different resources:

var list = ['/api/filters','/api/criteria/brands','/api/criteria/genders']  
list.forEach(function(item){$.post(item)})

On server side I could see the following times in logfile:

GET /api/filters 304 51ms
GET /api/criteria/genders 200 1ms
GET /api/criteria/brands 200 0ms

Thats look cool for me, but in browser the picture is absolutely different.

picture with google chrome network tab

So it looks like browser wait for answer on first ( long request ) and only afterwards receive last 2 results.

What could be the reason for this behavior?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Alber
  • 157
  • 1
  • 1
  • 8
  • Under 50ms is pretty dam fast, what's wrong with that, it could just be your network – adrian Jan 23 '13 at 09:22
  • This on local macine with SSD, so in real time delays will be "a bit" longer. And really 51ms is just output to response about 300kb of JSON previously cached in memory ( no any calculations, DB request etc ) – Alber Jan 23 '13 at 09:26
  • Why don't you put it in a real web environment, (AWS, Heroku), to be sure you're not optimizing for ghosts – adrian Jan 23 '13 at 09:27

2 Answers2

0

Every browser just handles a specific amount of simultaneous requests at a time. If you fire 10 ajax requests at the same time, the browser put them on a stack and handles one after the other.

You can find more information about the concurrent requests (because that includes images, javascript, etc as well) in browsern in this question.

Community
  • 1
  • 1
Marc
  • 6,749
  • 9
  • 47
  • 78
  • But i fire only 3 call, even from console to make this situation as clear as it possible – Alber Jan 23 '13 at 09:27
0

The node server runs is single threaded and any piece of code that uses CPU cycles blocks the entire process.

As a result, if GET /api/filters does a lot of CPU intensive computations, it will block any other requests till it completes. Adding some more information about what it actually does can help in putting together a better answer.

In case you have IO operations in there, then try to make them asynchronous. That will allow node to serve the other URLs while the first one is doing IO.

Akhil Raina
  • 379
  • 2
  • 9