Everything You Know About Latency Is Wrong
Everything You Know About Latency Is Wrong
Everything you think you know about latency is wrong, so correct that by discovering latency: what it is, measurements, and more!
Join the DZone community and get the full member experience.Join For Free
okay, maybe not everything you know about latency is wrong. but now that i have your attention, we can talk about why the tools and methodologies you use to measure and reason about latency are likely horribly flawed. in fact, they’re not just flawed, they’re probably lying to your face.
when i went to strange loop in september, i attended a workshop called “understanding latency and application responsiveness” by gil tene. gil is the cto of azul systems, which is most renowned for its c4 pauseless garbage collector and associated zing java runtime. while the workshop was four and a half hours long, gil also gave a 40-minute talk called “how not to measure latency” which was basically an abbreviated, less interactive version of the workshop. if you ever get the opportunity to see gil speak or attend his workshop, i recommend you do. at the very least, do yourself a favor and watch one of his recorded talks or find his slide decks online.
the remainder of this post is primarily a summarization of that talk. you may not get anything out of it that you wouldn’t get out of the talk, but i think it can be helpful to absorb some of these ideas in written form. plus, for my own benefit, writing about them helps solidify it in my head.
what is latency?
latency is defined as the time it took one operation to happen. this means every operation has its own latency—with one million operations there are one million latencies. as a result, latency cannot be measured as work units / time . what we’re interested in is how latency behaves . to do this meaningfully, we must describe the complete distribution of latencies. latency almost never follows a normal, gaussian, or poisson distribution, so looking at averages, medians, and even standard deviations is useless.
latency tends to be heavily multi-modal, and part of this is attributed to “hiccups” in response time. hiccups resemble periodic freezes and can be due to any number of reasons—gc pauses, hypervisor pauses, context switches, interrupts, database reindexing, cache buffer flushes to disk, etc. these hiccups never resemble normal distributions and the shift between modes is often rapid and eclectic.
how do we meaningfully describe the distribution of latencies? we have to look at percentiles, but it’s even more nuanced than this. a trap that many people fall into is fixating on “the common case.” the problem with this is that there is a lot more to latency behavior than the common case. not only that, but the “common” case is likely not as common as you think.
this is partly a tooling problem. many of the tools we use do not do a good job of capturing and representing this data. for example, the majority of latency graphs produced by grafana, such as the one below, are basically worthless. we like to look at pretty charts, and by plotting what’s convenient we get a nice colorful graph which is quite readable. only looking at the 95th percentile is what you do when you want to hide all the bad stuff. as gil describes, it’s a “marketing system.” whether it’s the cto, potential customers, or engineers—someone’s getting duped. furthermore, averaging percentiles is mathematically absurd. to conserve space, we often keep the summaries and throw away the data, but the “average of the 95th percentile” is a meaningless statement. you cannot average percentiles , yet note the labels in most of your grafana charts. unfortunately, it only gets worse from here.
gil says, “the number one indicator you should never get rid of is the maximum value. that is not noise, that is the signal. the rest of it is noise.” to this point, someone in the workshop naturally responded with “but what if the max is just something like a vm restarting? that doesn’t describe the behavior of the system. it’s just an unfortunate, unlikely occurrence.” by ignoring the maximum, you’re effectively saying “this doesn’t happen.” if you can identify the cause as noise, you’re okay, but if you’re not capturing that data, you have no idea of what’s actually happening.
how many nines?
but how many “nines” do i really need to look at? the 99th percentile, by definition, is the latency below which 99% of the observations may be found. is the 99th percentile rare ? if we have a single search engine node, a single key-value store node, a single database node, or a single cdn node, what is the chance we actually hit the 99th percentile?
gil describes some real-world data he collected which shows how many of the web pages we go to actually experience the 99th percentile, displayed in table below. the second column counts the number of http requests generated by a single access of the web page. the third column shows the likelihood of one access experiencing the 99th percentile. with the exception of google.com, every page has a probability of 50% or higher of seeing the 99th percentile.
the point gil makes is that the 99th percentile is what most of your web pages will see. it’s not “rare.”
what metric is more representative of user experience? we know it’s not the average or the median. 95th percentile? 99.9th percentile? gil walks through a simple, hypothetical example: a typical user session involves five page loads, averaging 40 resources per page. how many users will not experience something worse than the 95th percentile? 0.003%. by looking at the 95th percentile, you’re looking at a number which is relevant to 0.003% of your users. this means 99.997% of your users are going to see worse than this number, so why are you even looking at it?
on the flip side, 18% of your users are going to experience a response time worse than the 99.9th percentile, meaning 82% of users will experience the 99.9th percentile or better. going further, more than 95% of users will experience the 99.97th percentile and more than 99% of users will experience the 99.995th percentile.
the median is the number that 99.9999999999% of response times will be worse than. this is why median latency is irrelevant. people often describe “typical” response time using a median, but the median just describes what everything will be worse than. it’s also the most commonly used metric.
if it’s so critical that we look at a lot of nines (and it is), why do most monitoring systems stop at the 95th or 99th percentile? the answer is simply because “it’s hard!” the data collected by most monitoring systems is usually summarized in small, five or ten second windows. this, combined with the fact that we can’t average percentiles or derive five nines from a bunch of small samples of percentiles means there’s no way to know what the 99.999th percentile for the minute or hour was. we end up throwing away a lot of good data and losing fidelity.
a coordinated conspiracy
benchmarking is hard . almost all latency benchmarks are broken because almost all benchmarking tools are broken. the number one cause of problems in benchmarks is something called “coordinated omission,” which gil refers to as “a conspiracy we’re all a part of” because it’s everywhere. almost all load generators have this problem.
we can look at a common load-testing example to see how this problem manifests. with this type of test, a client generally issues requests at a certain rate, measures the response time for each request, and puts them in buckets from which we can study percentiles later.
the problem is what if the thing being measured took longer than the time it would have taken before sending the next thing? what if you’re sending something every second, but this particular thing took 1.5 seconds? you wait before you send the next one, but by doing this, you avoided measuring something when the system was problematic. you’ve coordinated with it by backing off and not measuring when things were bad. to remain accurate, this method of measuring only works if all responses fit within an expected interval.
coordinated omission also occurs in monitoring code. the way we typically measure something is by recording the time before, running the thing, then recording the time after and looking at the delta. we put the deltas in stats buckets and calculate percentiles from that. the code below is taken from a cassandra benchmark.
however, if the system experiences one of the “hiccups” described earlier, you will only have one bad operation and 10,000 other operations waiting in line. when those 10,000 other things go through, they will look really good when in reality the experience was really bad . long operations only get measured once, and delays outside the timing window don’t get measured at all.
in both of these examples, we’re omitting data that looks bad on a very selective basis, but just how much of an impact can this have on benchmark results? it turns out the impact is huge .
imagine a “perfect” system which processes 100 requests/second at exactly 1 ms per request. now consider what happens when we freeze the system (for example, using ctrl+z) after 100 seconds of perfect operation for 100 seconds and repeat. we can intuitively characterize this system:
- the average over the first 100 seconds is 1 ms.
- the average over the next 100 seconds is 50 seconds.
- the average over the 200 seconds is 25 seconds.
- the 50th percentile is 1 ms.
- the 75th percentile is 50 seconds.
- the 99.99th percentile is 100 seconds.
now we try measuring the system using a load generator. before freezing, we run 100 seconds at 100 requests/second for a total of 10,000 requests at 1 ms each. after the stall, we get one result of 100 seconds. this is the entirety of our data, and when we do the math, we get these results:
- the average over the 200 seconds is 10.9 ms (should be 25 seconds).
- the 50th percentile is 1 ms.
- the 75th percentile is 1 ms (should be 50 seconds).
- the 99.99th percentile is 1 ms (should be 100 seconds).
basically, your load generator and monitoring code tell you the system is ready for production, when in fact it’s lying to you! a simple “ctrl+z” test can catch coordinated omission, but people rarely do it. it’s critical to calibrate your system this way. if you find it giving you these kind of results, throw away all the numbers—they’re worthless.
you have to measure at random or “fair” rates. if you measure 10,000 things in the first 100 seconds, you have to measure 10,000 things in the second 100 seconds during the stall. if you do this, you’ll get the correct numbers, but they won’t be as pretty. coordinated omission is the simple act of erasing, ignoring, or missing all the “bad” stuff, but the data is good.
surely this data can still be useful though, even if it doesn’t accurately represent the system? for example, we can still use it to identify performance regressions or validate improvements, right? sadly, this couldn’t be further from the truth. to see why, imagine we improve our system. instead of pausing for 100 seconds after 100 seconds of perfect operation, it handles all requests at 5 ms each after 100 seconds. doing the math, we get the following:
- the 50th percentile is 1 ms
- the 75th percentile is 2.5 ms (stall showed 1 ms)
- the 99.99th percentile is 5 ms (stall showed 1 ms)
this data tells us we hurt the four nines and made the system 5x worse ! this would tell us to revert the change and go back to the way it was before, which is clearly the wrong decision. with bad data, better can look worse . this shows that you cannot have any intuition based on any of these numbers. the data is garbage.
with many load generators, the situation is actually much worse than this. these systems work by generating a constant load. if our test is generating 100 requests/second, we run 10,000 requests in the first 100 seconds. when we stall, we process just one request. after the stall, the load generator sees that it’s 9,999 requests behind and issues those requests to catch back up. not only did it get rid of the bad requests, it replaced them with good requests. now the data is twice as wrong as just dropping the bad requests.
what coordinated omission is really showing you is service time , not response time. if we imagine a cashier ringing up customers, the service time is the time it takes the cashier to do the work. the response time is the time a customer waits before they reach the register. if the rate of arrival is higher than the service rate, the response time will continue to grow. because hiccups and other phenomena happen, response times often bounce around. however, coordinated omission lies to you about response time by actually telling you the service time and hiding the fact that things stalled or waited in line.
latency doesn’t live in a vacuum. measuring response time is important, but you need to look at it in the context of load. but how do we properly measure this? when you’re nearly idle, things are nearly perfect, so obviously that’s not very useful. when you’re pedal to the metal, things fall apart. this is somewhat useful because it tells us how “fast” we can go before we start getting angry phone calls.
however, studying the behavior of latency at saturation is like looking at the shape of your car’s bumper after wrapping it around a pole. the only thing that matters when you hit the pole is that you hit the pole . there’s no point in trying to engineer a better bumper, but we can engineer for the speed at which we lose control. everything is going to suck at saturation, so it’s not super useful to look at beyond determining your operating range.
what’s more important is testing the speeds in between idle and hitting the pole. define your slas and plot those requirements, then run different scenarios using different loads and different configurations. this tells us if we’re meeting our slas but also how many machines we need to provision to do so. if you don’t do this, you don’t know how many machines you need.
how do we capture this data? in an ideal world, we could store information for every request, but this usually isn’t practical. hdrhistogram is a tool which allows you to capture latency and retain high resolution. it also includes facilities for correcting coordinated omission and plotting latency distributions. the original version of hdrhistogram was written in java, but there are versions for many other languages.
to understand latency, you have to consider the entire distribution. do this by plotting the latency distribution curve. simply looking at the 95th or even 99th percentile is not sufficient. tail latency matters. worse yet, the median is not representative of the “common” case, the average even less so. there is no single metric which defines the behavior of latency. be conscious of your monitoring and benchmarking tools and the data they report. you can’t average percentiles.
remember that latency is not service time . if you plot your data with coordinated omission, there’s often a quick, high rise in the curve. run a “ctrl+z” test to see if you have this problem. a non-omitted test has a much smoother curve. very few tools actually correct for coordinated omission.
latency needs to be measured in the context of load, but constantly running your car into a pole in every test is not useful. this isn’t how you’re running in production, and if it is, you probably need to provision more machines. use it to establish your limits and test the sustainable throughputs in between to determine if you’re meeting your slas. there are a lot of flawed tools out there, but hdrhistogram is one of the few that isn’t. it’s useful for benchmarking and, since histograms are additive and hdrhistogram uses log buckets, it can also be useful for capturing high-volume data in production.
Published at DZone with permission of Tyler Treat, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.