Welcome!

Python Authors: Matt Davis, Jyoti Bansal, Pat Romanski, Donald Meyer, Liz McMillan

Related Topics: Java IoT, Microservices Expo, Open Source Cloud, Machine Learning , Ruby-On-Rails, Python

Java IoT: Article

The Taming of the Queue

Measuring the Impact of Request Queueing

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:queueing

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:

queueing

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” is actually 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:

queueing

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?
Okay, so it’s an interesting problem to think about, but really the practical question is, “Is queuing 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

More Stories By Dan Kuebrich

Dan Kuebrich is a web performance geek, currently working on Application Performance Management at AppNeta. He was previously a founder of Tracelytics (acquired by AppNeta), and before that worked on AmieStreet/Songza.com.

@ThingsExpo Stories
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
Elon Musk is among the notable industry figures who worries about the power of AI to destroy rather than help society. Mark Zuckerberg, on the other hand, embraces all that is going on. AI is most powerful when deployed across the vast networks being built for Internets of Things in the manufacturing, transportation and logistics, retail, healthcare, government and other sectors. Is AI transforming IoT for the good or the bad? Do we need to worry about its potential destructive power? Or will we...
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
SYS-CON Events announced today that Suzuki Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Suzuki Inc. is a semiconductor-related business, including sales of consuming parts, parts repair, and maintenance for semiconductor manufacturing machines, etc. It is also a health care business providing experimental research for...
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a mu...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that SourceForge has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. SourceForge is the largest, most trusted destination for Open Source Software development, collaboration, discovery and download on the web serving over 32 million viewers, 150 million downloads and over 460,000 active development projects each and every month.
SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...