Learning center

Dependency slow for the client but fast on the server

When a client times a call as slow and the server logged it as fast, the gap was spent outside the server's timer. Split the client's time into phases: waiting for a pooled connection, DNS, TCP, TLS, the server's work, and the delay before the client's code ran again. Then compare the server's share with the server's own timing for the same request.

The symptom#

The checkout service logs a p99 of 1.8 seconds for its calls to orders, and an alarm on that latency fires. The orders access log shows the same requests completing in about 40 milliseconds. Both numbers can be right, because each side starts and stops its timer at different points.

A client timer around a library call usually covers everything the library does: taking a connection from its pool or waiting for one, resolving the name and opening a connection when none is idle, sending the request, waiting for the response, and the time until the callback or the blocked thread runs again. A server's figure covers only the part of the request that its timer surrounds, which can start only after the server has accepted the connection, and often later, when a handler receives the request. Time spent in a load balancer or in the server's accept queue falls between the two.

Where the extra time can go#

  • Connection pool wait. A client with a per-host connection cap queues requests when every connection is busy. In Go, MaxConnsPerHost limits connections in the dialing, active and idle states, and dials block at the limit. A Node.js agent's maxSockets is the maximum number of sockets per host. A database pool works the same way. Go Transport · Node.js http.Agent · Pool wait or a slow database
  • Connection setup. When no idle connection exists, the call pays for name resolution, the TCP handshake and the TLS handshake before the server sees a byte. Slow resolvers and retried SYNs show up here. Refused or timed out
  • Client scheduling. The response may arrive on time and then wait for the client to run. In Node.js, a blocked event loop delays every callback; in thread-pool clients, a saturated pool delays the thread that reads the response.
  • The path in between. A load balancer adds its own processing and queuing, and a dependency whose accept backlog is full delays new connections while its request timer reads low. Listen queue overflow

Time one request by phase from the client host#

From the same host or container as the client, curl -w prints how long each phase took. Every value is seconds since the start of the transfer, so subtract consecutive fields to get each phase. curl --write-out

curl -s -o /dev/null https://orders.example.internal/healthz -w \
'dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} pretransfer=%{time_pretransfer} first_byte=%{time_starttransfer} total=%{time_total}\n'
FieldEnds when
time_namelookupName resolution completed
time_connectTCP connect to the host or proxy completed
time_appconnectTLS handshake completed
time_pretransferThe transfer is about to begin
time_starttransferThe first byte of the response arrived
time_totalThe whole operation finished

time_starttransfer minus time_pretransfer is the server's work plus one network round trip. If that difference matches the server's figure and the earlier phases are small, the network and the server are fast, and the client's extra time is inside the client. Run it several times: a single slow resolution or a retried SYN shows up in the first two fields.

curl opens a new connection for each run and has no pool or event loop, so it measures what a fresh connection costs. A pooled client skips those phases on a reused connection. The comparison isolates the client only because curl lacks what the client has.

ss -ti shows each open connection's smoothed round-trip time in milliseconds, so you can confirm that the path itself is fast for the connections the client holds. ss(8)

ss -tin dst 10.0.3.15

Time the phases inside the client#

The phases that curl cannot see need measurement in the client process. That means instrumentation shipped with the service; reading it afterwards changes nothing.

In Go, net/http/httptrace calls GetConn before a connection is created or taken from the idle pool and GotConn once one is obtained. The time between them is pool wait plus any dial. GotConnInfo reports Reused, WasIdle and IdleTime. DNSStart, ConnectStart, TLSHandshakeStart and GotFirstResponseByte mark the other phases. httptrace ClientTrace

In Node.js, perf_hooks.monitorEventLoopDelay() returns a histogram of event loop delay in nanoseconds, sampled every 10 milliseconds by default. A p99 delay close to the latency gap means responses waited for the loop. monitorEventLoopDelay

Read the server's own timing#

Check what the server's number covers before comparing it. A server can report its timing per response in a Server-Timing header, with metrics such as db;dur=53, app;dur=47.2; the specification recommends milliseconds for durations. Print the response headers with curl to read it. Server-Timing specification · Server-Timing on MDN

curl -s -o /dev/null -D - https://orders.example.internal/healthz | grep -i '^server-timing'

Behind an Application Load Balancer, the access log splits each request into request_processing_time (from the load balancer receiving the request to sending it to a target), target_processing_time (from sending it to the target until the target started its response headers) and response_processing_time. A high target_processing_time with a low application timer points at time spent on the way to the target or inside it before the handler's timer started. ALB access log entries

For PostgreSQL, log_min_duration_statement logs statements that ran at least the configured time, which gives the database's view of a slow query. PostgreSQL logging settings

What each result means#

ObservationPoints to
curl fast, client slow, long GetConn to GotConnPool wait in the client
curl slow in time_namelookupName resolution
curl slow in time_connect or time_appconnectTCP or TLS setup, or the listener
First byte slow, server timer fast, target time highTime inside the server before its timer
Everything fast, event loop delay highClient scheduling

An illustrative fixture: two gaps#

This fixture is illustrative. checkout calls orders at 10.0.3.15, and orders reports 40 ms per request throughout.

In the first incident checkout is a Go service whose transport sets MaxConnsPerHost to 10. During a traffic peak, httptrace shows 1.5 seconds between GetConn and GotConn, with Reused true, and 45 ms from GotConn to GotFirstResponseByte. curl from the same task reports a first byte at 52 ms. Requests waited for one of ten busy connections; orders was fast.

In the second incident checkout is a Node.js service. curl again reports 50 ms, Server-Timing: app;dur=38 agrees with the orders log, and the agent's sockets are not at maxSockets. The event loop delay histogram has a p99 of 1.4 seconds during the same minutes, when a large JSON report is being serialized on the main thread. Responses arrived on time and waited for the loop.

Limits#

The comparison needs both timings for the same requests and the same minutes. Match them by request ID or trace ID; a few slow client waits disappear in a server average. Clock offset between hosts makes per-request subtraction across machines unreliable; compare durations measured on one host. A server's figure can also exclude work it does after sending the response. The full set of dependency failures

Questions#

Why does curl return fast when my service sees slow calls?

curl opens a fresh connection and has no pool, event loop or thread pool, so it skips the waits your client adds. Fast curl results with a slow client point at waiting inside the client.

What does time_starttransfer include?

It is the time from the start until the first byte of the response arrived, including time_pretransfer and the time the server needed to produce the result. Subtract time_pretransfer to get the server's work plus one round trip.

Is Server-Timing enough to prove the server was fast?

It shows what the server chose to measure. Time before its timer started, such as load balancer queuing or the accept queue, is not in it, so compare it with load balancer timings where you have them.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading