Over a million developers have joined DZone.
Silver Partner

The Taming of the Queue: Measuring the Impact of Request Queueing

· Performance Zone

The Performance Zone is brought to you in partnership with Smartbear. Learn more about how the performance of your API's, web, and mobile applications have a direct impact on your bottom line with the Guide to Synthetic Monitoring.

A few weeks back, webserver request queueing came under heightened scrutiny as rapgenius blasted Heroku for not using as much autotune as promised in their “intelligent load balancing”. If you somehow missed the write-up (or response), check it out for its great simulations of load balancing strategies on Heroku.

What if you’re not running on Heroku? Well, the same wisdom still applies – know your application’s load balancing and concurrency and measure its performance   Let’s explore how request queueing affects applications in the non-PaaS world and what you can do about it.

Full-stack apps have full-stack problems

Rapgenius had been monitoring server-side request latency as only the time the request spent being processed in the app layer – leading to large discrepancies between what their APM tools were reporting and what the actual user experience was. The missing latency was attributable to queueing happening just before the application processed each request, which was outside the visibility of the tools being used to monitor the site.

If your application processes requests at a constant speed but receives an increasing volume of requests (generally a good problem to have), you’ll start to face request queueing.

What does this queueing look like?

(I’ll be using nginx and gunicorn as examples here because that’s what we use, but the same reasoning and analysis principles apply no matter what stack you’re running.)

To visualize this problem, let’s look at a simple test stack running nginx in front of a Python app with eight worker processes. In our case, it’s actually intelligently load balanced by gunicorn because there’s a single queue that knows which workers are busy (unlike Heroku at scale). However, we can still run into plenty of problems.

I’ve instrumented it so we watch the latency of requests moving through the full stack, starting at the load balancer:


In this image, orange represents time spent queued in a webserver, while the other colors represent the components of the application (app, DB, cache).

As you can see, the application performs admirably, slowing a bit under load but never getting slower than 150 ms to process a response. If that’s all you were looking at, you’d be delighted! But the slow buildup of queue depth results in and increased amount of time spent in each request, which is shown in orange. Yikes!

Mind your Ps and Queues

In your application, there’s likely to be queueing anywhere you distribute request load over multiple backends. In the simplest app, this might be happening between your webserver and application layer, as above. Dynamic requests must be handled by the app, and if all the app workers are busy, requests will have to wait. Here’s what that might look like for a single Heroku dyno, or an app you stand up on a development server:


In fact, a common problem we see is that an app is underprovisioning app workers in production, even if the nodes they’re running on aren’t working very hard. If you see request queueing with low server load, consider running more app worker processes:queueing

This has the great property of helping you get the most out of your frontend node, but assuming that your local app server can do intelligent load balancing like gunicorn, it also has some beneficial load distribution properties. We’ll get to those in a second.

Third scenario: you’re running single application workers on multiple frontend nodes. This is your Thin app running on a number of Heroku dynos. It will look more like this:queueing

The challenge now is that unless the remote load balancer is keeping track of which workers are busy, it will have to distribute load less intelligently.

What’s wrong with random balancing?

Random assignment sounds pretty good intuitively. Let’s say I’m going to route 100 requests to two app workers, with a 50% probability of choosing each worker each time. At the end, you’d expect me to have around 50 processed by each. Sounds fair, right?

The problem is that at any given time during the handling of those 50 requests, one node might be two or three deep while the other is empty, which is a problem for latency. Compounding this is the possibility that different requests take different amounts of time to process.

For a mathematical analysis, check out this blog post. For a simulation, I’ll cite this cool animated gif from the rapgenius analysis:

So, it seems like we want to have at least some level of intelligence in our load balancing.

Alleviating the pain of scale

Heroku’s response is that it can be difficult to keep track of which workers are busy and which are free when you’re at scale – that’s why their routing mesh degrades to semi-random behavior.  This is definitely not an easy problem, because their “load balancer” isactually a distributed system.  However, even without tackling this omnipotence problem at the top level, local intelligence under a random umbrella can be very effective.

There’s a lot of app servers that support this. For instance, if you’re running Unicorn for Ruby, or gunicorn for Python, each app server has a pool of workers which have a local queue and are routed to intelligently. So, your setup looks more like this:


This actually makes a big impact on performance. If you replace each single-worker dyno with a two-worker intelligently-routed app server, you get much-improved performance.

However, that assumes evented workers, where the cost of adding a second worker to a node is minimal. What if you’re using non-evented threads or processes, so you care about the total CPU and memory consumption of your workers?

To answer that question, and to try out R for the first time, I modified the rapgenius simulations to look at the effects of scaling the overall number of workers and workers-per-node, on request queueing:queueing

(source on github)

Queueing performance improves quite well with the number of workers on each naively-balanced endpoint. (The shelf in the eight-worker line is due to the fact that 10 and 15 are both < 16). You can see that, in fact, two naively-routed pairs of eight-worker (intelligently-routed) nodes are better than 100 naively-routed one-worker nodes. See the pattern? The lines are converging on a single, fully intelligently-routed cluster.

This is possible with app worker processes or threads on each node, but if you’re running evented workers, each individual worker is capable of handling quite a number of requests simultaneously!

How do I know if I have this problem?

OK, so it’s an interesting problem to think about, but really the practical question is, “Is queueing affecting my application’s responsiveness?” Monitoring the full stack  is the best way to stay on top of performance problems–webserver queueing among many others.

You can usually get an isolated look at webserver queueing from your load balancer and/or app server. For instance, if you’re running FCGI on lighttpd, you can check the queue depth of each worker.

But the ultimate determinant of the success or failure of your load balancing is the impact on latency and concurrency. Check out this 3-minute video on understanding webserver queueing.

Related Articles


The Performance Zone is brought to you in partnership with Smartbear. Discover how important it is to recognize the inherent power of APIs and bulding the appropriate safeguards to protect it with the Essential Guide to API Monitoring.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}