Sobes.tech
Senior

Where and how do you measure latency in a system?

sobes.tech AI

Answer from AI

Latency in the system is measured at key points to understand where delays occur and to optimize performance.

Where to measure:

  • On the client side (frontend) — the time from sending a request to receiving a response.
  • On the server — the request processing time, including queue times, business logic execution, and database access.
  • In the network stack — the data transmission time over the network.

How to measure:

  • Use high-precision timers (e.g., time.Now() in Go).
  • Implement distributed tracing with unique identifiers to track the request path through services.
  • Log metrics with timestamps.

Example in Go for measuring function processing time:

start := time.Now()
// call function or process request
processRequest()
elapsed := time.Since(start)
log.Printf("Latency: %s", elapsed)

It is important to measure latency at different levels and aggregate data for bottleneck analysis.