Tail latency is the high-latency end of a latency distribution, commonly reported as the 95th, 99th, or 99.9th percentile. A 99th-percentile latency is the threshold within which 99% of measured requests fall. The maximum is the 100th percentile. section 2.2
Averages hide variability, while the minimum and maximum describe only the extremes. High percentiles expose slow requests that users can encounter more often than intuition suggests. Latency can vary with system state and workload, including increased queuing as throughput rises. section 2.2
Parallel requests
A service that sends subrequests to several servers in parallel and waits for all responses is bounded by its slowest subrequest. Parallel execution can reduce service latency, but increasing fanout also makes slow subrequests important to the user-facing result. section 2.2
Figure 2.5 Tail latency illustrated. The diagram shows the probability of service latency being more than 1 second (y axis) as a function of parallel fanout count (x axis) depending on the probability of one fanout request being slow.
Compare how the probability of service latency exceeding one second changes with parallel fanout and the probability of a slow subrequest. section 2.2
To examine tail latency:
- Define the latency being measured and control the measurement environment. section 2.7
- Plot the samples as a histogram. Sparse samples to the right of the main concentration can reveal the tail. section 2.6.1
- Plot percentiles such as 95%, 99%, and 99.9% to compare the thresholds experienced across the distribution. section 2.2 section 2.6.1
Listing 2.2 Plotting latency distribution as a histogram
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('ping-google.com.csv')
plt.hist(df["Latency_secs"] * 1000, bins='auto')
plt.ylabel("Frequency (N = %d)" % len(df))
plt.xlabel('Latency (in milliseconds)')
plt.savefig('histogram.png', dpi=300)
Use the Python listing to read latency samples from CSV and plot their frequency in milliseconds. section 2.6.1
Listing 2.3 Plotting latency distribution with HdrHistogram
#!/usr/bin/env python3
from hdrh.histogram import HdrHistogram
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import pandas as pd
MSECS_PER_SEC = 1000
histogram = HdrHistogram(1, MSECS_PER_SEC, 4)
df = pd.read_csv('ping-google.com.csv')
for latency in df["Latency_secs"]:
histogram.record_value(latency * MSECS_PER_SEC)
data = []
for percentile in [25.0, 50.0, 90.0, 99.0, 99.9, 99.99]:
value = histogram.get_value_at_percentile(percentile)
data.append([percentile / 100, value])
hist_df = pd.DataFrame(data, columns=['Percentile', 'Value'])
fig, ax = plt.subplots()
ax.plot(hist_df["Percentile"], hist_df["Value"])
ax.grid()
ax.set_title('Latency by Percentile Distribution')
ax.set_xlabel('Percentile (%)')
ax.set_ylabel('Latency (milliseconds)')
ax.set_xscale('logit')
plt.xticks([0.25, 0.5, 0.9, 0.99, 0.999, 0.9999])
majors = ["25%", "50%", "90%", "99%", "99.9%", "99.99%"]
ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors))
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
fig.savefig('histogram-hdr.png', dpi=300)
Use HdrHistogram to record samples and plot latency at selected percentiles. section 2.6.1
Investigate variability across the stack: networking, resource sharing, background work, queuing, power management, and garbage collection are possible contributors. Hardware can matter too: cache misses introduce variable memory-access latency, while simultaneous multithreading can trade throughput and efficiency for latency degradation through contention. For ultra-low-latency applications, consider disabling it. section 2.3 section 2.3.2
