TCP vs UDP in Microservices: Performance Impact
How TCP and UDP shape microservices latency: handshakes, pooling, HTTP/2 head-of-line blocking, gRPC load balancing, Kubernetes DNS, QUIC and HTTP/3.
In microservices, TCP carries almost all service-to-service traffic (HTTP/1.1, HTTP/2, gRPC, database protocols), so its connection setup, pooling, head-of-line blocking and load balancing behaviour shape latency. UDP matters in fewer but critical places: DNS lookups inside Kubernetes, telemetry such as StatsD, and QUIC, which carries HTTP/3. Most performance problems come from how connections are managed, not from the protocol choice itself.
This article looks at TCP and UDP from the point of view of a microservices platform: what each protocol costs per request, where it causes tail latency, and which settings in Kubernetes, service meshes and client libraries fix it. If you want the application-level comparison for streaming, gaming and VoIP, read TCP vs UDP in real-time applications first. For the layer model behind both, see the OSI model.
Key takeaways
- A new TCP connection costs one round trip before data, and TLS 1.3 adds one more. Reusing connections removes both from every later request.
- HTTP/2 and gRPC multiplex many requests over one TCP connection, which saves handshakes but means a single lost packet can stall every stream on it.
- Long-lived gRPC connections defeat connection-level (L4) load balancing in Kubernetes; you need request-level balancing from a proxy, service mesh or client-side balancer.
- DNS inside Kubernetes uses UDP by default, and settings such as
ndots:5can multiply lookups per request. - QUIC (RFC 9000) runs over UDP, removes TCP's cross-stream head-of-line blocking and is the transport for HTTP/3. It is most useful at the edge, less so inside a data centre.
- Measure p99 latency and connection counts before tuning; averages hide these problems.
TCP and UDP in a microservices stack
| Traffic type | Transport | Performance concern |
|---|---|---|
| REST over HTTP/1.1 | TCP | One request at a time per connection; needs pools |
| gRPC and HTTP/2 | TCP | Multiplexing, head-of-line blocking, L4 load balancing |
| Databases (PostgreSQL, MySQL, Redis) | TCP | Connection limits, pool sizing |
| Message brokers (Kafka, RabbitMQ) | TCP | Long-lived connections, batching |
| Service discovery DNS | UDP (TCP fallback) | Lookup volume, conntrack, timeouts |
| Metrics such as StatsD | UDP | Silent packet loss under load |
| HTTP/3 at the edge | QUIC over UDP | Faster setup, better on lossy networks |
What TCP costs per request
TCP, specified in RFC 9293, gives ordered, reliable byte streams with flow and congestion control. That reliability has a cost in three places.
Connection setup
The three-way handshake takes one round trip before any request byte is sent. TLS 1.3 (RFC 8446) adds one more round trip for a full handshake. Inside one availability zone a round trip may be well under a millisecond, but across zones or regions it grows, and a service that opens a new connection per call pays it on every request. With mTLS in a service mesh, each hop has its own handshake.
Slow start
Congestion control starts each new connection with a small window and grows it as acknowledgements arrive. Short-lived connections never reach full speed, so large responses on fresh connections are slower than on warm ones.
Connection teardown and TIME_WAIT
The side that closes a TCP connection keeps it in TIME_WAIT for a period so late packets are not mistaken for a new connection. Services that open and close thousands of outbound connections per second can run out of ephemeral ports. Signs include connect errors under load and many entries in ss -s output under timewait.
ss -s # summary including timewait count
ss -tan state time-wait | wc -l
sysctl net.ipv4.ip_local_port_range # ephemeral port range
The fix is almost always connection reuse, not kernel tuning.
Connection reuse and pooling
Keep-alive connections and pools remove handshake and slow-start costs from most requests. Every major HTTP client supports pooling, but defaults vary and are often small. Settings to check:
- Maximum idle connections per host. If it is lower than your concurrency, the client keeps closing and reopening connections.
- Idle timeout. It should be shorter than the server's or load balancer's idle timeout, or the client will send requests on connections the server has already closed, which shows up as intermittent "connection reset" errors.
- Database pool size. Many small pods each holding a large pool can exceed the database's connection limit. Size pools per pod from the database maximum divided by expected replicas, or use a pooler such as PgBouncer.
HTTP/2, gRPC and head-of-line blocking
HTTP/2 (RFC 9113) multiplexes many concurrent streams on one TCP connection, and gRPC is built on HTTP/2. This removes the need for large pools and reduces handshakes. It also moves the bottleneck: because TCP delivers bytes in order, a single lost packet holds back every stream on that connection until it is retransmitted. On clean data-centre networks this is rare; on lossy or congested links it adds tail latency.
A second side effect is per-connection limits. Servers advertise a maximum number of concurrent streams. When a client hits it, new calls queue. Clients can open more connections or use a channel pool.
Why gRPC breaks L4 load balancing in Kubernetes
A Kubernetes Service with a ClusterIP is load balanced by kube-proxy (or an eBPF replacement) per connection. HTTP/1.1 clients open many connections, so traffic spreads across pods. A gRPC client opens one long-lived HTTP/2 connection and sends every request over it, so all its traffic goes to one pod, even after the Deployment scales up.
| Option | How it balances | Trade-off |
|---|---|---|
| Service mesh or L7 proxy (Envoy, Linkerd) | Per request | Extra hop and resources |
| Client-side load balancing with headless Service | Client resolves all pod IPs and balances | Logic in every client language |
| Max connection age on the server | Forces periodic reconnect | Coarse; still uneven between reconnects |
| L7 ingress or gateway for north-south traffic | Per request | Only covers traffic through the gateway |
The Kubernetes blog post on gRPC load balancing, linked below, explains the problem in detail. Our pages on sidecar proxies and traffic management and service discovery vs service mesh cover the mesh options.
Small-message latency: Nagle and delayed ACK
Nagle's algorithm holds small writes until earlier data is acknowledged, and delayed acknowledgements hold ACKs briefly. Together they can add noticeable delay to request-response protocols that send small messages. Most RPC libraries and many HTTP clients set TCP_NODELAY to disable Nagle. If you write a custom TCP protocol, set it yourself and batch writes in the application instead.
Where UDP shows up
UDP (RFC 768) sends independent datagrams with no handshake, ordering or retransmission. That makes it cheap for small, frequent messages and leaves reliability to the application.
DNS in Kubernetes
Pods resolve Service names through CoreDNS, normally over UDP port 53. The default pod resolv.conf sets ndots:5 and several search domains, so a name like api.example.com (fewer than five dots) is first tried against each cluster search domain before the absolute name. One external call can become several DNS queries, doubled again if both A and AAAA records are requested.
spec:
dnsConfig:
options:
- name: ndots
value: "2"
Lower ndots for pods that mostly call external names, or use fully qualified names with a trailing dot. Kubernetes also documents NodeLocal DNSCache, which runs a caching agent on each node; the docs note it avoids conntrack races and table exhaustion that UDP DNS traffic can cause, and it upgrades upstream queries to TCP.
Telemetry
StatsD-style metrics and some syslog setups send over UDP so the application never blocks on the metrics pipeline. The trade-off is silent loss when buffers fill. Watch receive-buffer errors with netstat -su or nstat on the collector, and prefer pull-based Prometheus metrics or OpenTelemetry over gRPC when you need completeness.
UDP Services in Kubernetes
Kubernetes Services support TCP, UDP and SCTP. UDP Services work through conntrack entries, and stale entries after a pod is replaced can send traffic to a dead endpoint for a short time. Not every cloud load balancer supports UDP or mixed protocols on one Service, so check your provider's docs.
QUIC and HTTP/3
QUIC (RFC 9000) is a transport built on UDP. It combines the transport and TLS 1.3 handshakes, so a new connection needs one round trip, and resumed connections can send data immediately (0-RTT, with replay caveats). Each stream is delivered independently, so a lost packet only delays the stream it belongs to. Connection IDs let a connection survive a client's IP change, which helps mobile users. HTTP/3 (RFC 9114) is HTTP over QUIC.
For microservices the main benefit is at the edge: users on mobile or lossy networks see faster connection setup and fewer stalls. Inside a data centre, with low loss and long-lived pooled connections, HTTP/2 over TCP is usually good enough, and QUIC's user-space processing can use more CPU. Envoy supports HTTP/3 downstream and upstream, and major CDNs offer it at the edge. Some networks block or throttle UDP, so clients fall back to HTTP/2.
Choosing a transport per service
| Situation | Recommended | Reason |
|---|---|---|
| Internal synchronous APIs | HTTP/2 or gRPC over TCP, pooled | Multiplexing, strong tooling, works with meshes |
| Public APIs and web front ends | HTTP/3 with HTTP/2 fallback at the edge | Better on mobile and lossy links |
| Transactions, payments, orders | TCP-based protocols | Ordered, reliable delivery |
| High-volume fire-and-forget metrics | UDP, accepting loss | No back-pressure on the app |
| Real-time media between services | UDP-based (RTP, WebRTC, QUIC) | Late data is worse than lost data |
A troubleshooting checklist for network latency in microservices
- Compare p50 and p99 latency per hop using tracing. A large gap usually points to queuing, retries or connection setup.
- Count new connections per second (
ss -s, proxy stats). High churn means pooling is not working. - Check per-pod request distribution for gRPC services. One hot pod suggests L4 balancing of long-lived connections.
- Look at DNS query rate and latency in CoreDNS metrics, and the
ndotssetting. - Check retransmissions with
nstatorss -ti. Retransmits cause head-of-line delays on HTTP/2. - Verify idle timeouts line up across client, proxy and load balancer.
- Review retry policies; retries on top of timeouts can multiply load during a slowdown.
Related reading: latency monitoring in high-volume microservices. Hands-on networking and Linux troubleshooting are covered in the Linux administration course, and service networking on clusters in the CKA course, in our Kharadi, Pune classroom or live online.
Frequently asked questions
Do microservices use TCP or UDP?
Mostly TCP, because HTTP/1.1, HTTP/2, gRPC and database protocols run on it. UDP is used for DNS, some telemetry, real-time media and QUIC.
Why is gRPC traffic not balanced across my Kubernetes pods?
gRPC keeps one long-lived HTTP/2 connection, and ClusterIP Services balance per connection. Use a service mesh, an L7 proxy or client-side load balancing.
What is head-of-line blocking in HTTP/2?
Because TCP delivers bytes in order, one lost packet delays all multiplexed streams on that connection until it is retransmitted. QUIC avoids this per stream.
Should internal microservices use HTTP/3?
Usually not yet. Inside a data centre, pooled HTTP/2 over TCP performs well. HTTP/3 helps most for users on mobile or lossy networks at the edge.
Why does DNS slow down my pods?
The default ndots:5 setting causes names with fewer than five dots to be tried against several search domains first. Lower ndots or use fully qualified names, and consider NodeLocal DNSCache.
How does connection pooling improve performance?
It reuses existing TCP and TLS connections, removing handshake round trips and slow start from most requests and avoiding ephemeral port exhaustion.
What is TCP_NODELAY?
A socket option that disables Nagle's algorithm so small writes are sent immediately. Most RPC libraries enable it to cut latency for small messages.
Is UDP faster than TCP?
UDP has less overhead per message and no handshake, but it gives no delivery guarantees. With pooled connections, TCP overhead per request is small for most APIs.
References
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0