Question: How can you calculate the p95 latency in database performance monitoring?


In the realm of database performance monitoring, p95 latency refers to the 95th percentile latency. In simple terms, it means that 95% of requests are processed in this amount of time or less. Here's how to calculate it:

First, sort all your latency values in ascending order.

Then, find the place of the 95th percentile value in the sorted list. This can be calculated using the formula n * (95/100), where n is the number of total observations.

If the result of this calculation is a whole number, then the p95 latency is the average of the values at this position and the next highest position in the dataset. If the result is a decimal, round up to the next whole number and take the value at this position as the p95 latency value.

Here is an example of how you could calculate it in Python:

def calculate_p95(latencies):
    index = int(len(latencies) * (95 / 100))
    return latencies[index]

latencies = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
p95 = calculate_p95(latencies)
print(f'The p95 latency is {p95}ms')

In this example, latencies would be a list of latencies from your database queries in milliseconds, and the function calculate_p95() sorts this list and returns the 95th percentile latency value. Note that Python automatically rounds down when converting a float to an integer, which is why we don't need to manually round up the index even if len(latencies) * (95 / 100) is not a whole number.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.