TCP proxy — Envoy at L4
Forward raw TCP for binary protocols Envoy can't (or shouldn't) parse.
Envoy isn't only an HTTP proxy
Underneath http_connection_manager (HCM), Envoy has a generic L4
byte-pumping engine. The envoy.filters.network.tcp_proxy filter
exposes that engine directly: incoming bytes flow to a chosen upstream
cluster with no parsing, no headers, no HTTP-status retries.
Reach for it whenever the wire protocol isn't HTTP — Postgres, MySQL, Kafka, MQTT, custom binary RPC, or raw TLS pass-through where Envoy shouldn't terminate. You still get xDS, mTLS, outlier detection, access logs, L4 rate limiting, and SNI-based routing.
Minimal envoy.yaml — TLS-passthrough with SNI routing
static_resources:
listeners:
- name: tls_passthrough
address: { socket_address: { address: 0.0.0.0, port_value: 5432 }}
listener_filters:
- name: envoy.filters.listener.tls_inspector
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.listener.tls_inspector.v3.TlsInspector
filter_chains:
- filter_chain_match: { server_names: ["primary.db.acme.io"] }
filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: pg_primary
idle_timeout: 1h
weighted_clusters:
clusters:
- { name: pg_primary_a, weight: 90 }
- { name: pg_primary_b, weight: 10 }
- filter_chain_match: { server_names: ["replica.db.acme.io"] }
filters:
- name: envoy.filters.network.tcp_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.tcp_proxy.v3.TcpProxy
stat_prefix: pg_replica
cluster: pg_replica_pool
tcp_proxy accepts both cluster: (single) and weighted_clusters:
(L4 traffic shifting), the same way HTTP routes do.
Three pitfalls
Without the tls_inspector listener filter, Envoy can't
peek at the ClientHello before filter_chain_match.server_names
is evaluated. Every TLS connection then falls through to the default
filter chain, and you'll spend an hour wondering why "writer.db" is
hitting the replica cluster.
Long-lived database or Kafka connections will be silently reset after
the default. Set idle_timeout explicitly — often
0 to disable, or align with your TCP keepalive interval.
Some protocols use TCP half-close (one side sends FIN while the other
keeps writing). Envoy disables this by default. Set
enable_half_close: true at the listener if your protocol
needs it.
Hands-on: a real Postgres in front of Envoy
- Start a local Postgres:
docker run -d --name pg -p 6543:5432 \
-e POSTGRES_PASSWORD=secret postgres:16
- Save the snippet above (drop the SNI bits for simplicity — single
filter chain pointing at
cluster: postgres) and run Envoy on port 5432:
docker run --rm -p 5432:5432 -p 9901:9901 \
-v "$PWD/envoy.yaml:/etc/envoy/envoy.yaml" \
envoyproxy/envoy:v1.31-latest
- Connect through Envoy:
PGPASSWORD=secret psql -h localhost -p 5432 -U postgres -c 'select 1'
- Watch the L4 stats:
curl -s localhost:9901/stats | grep tcp.pg
You'll see tcp.pg.upstream_cx_total and
downstream_cx_active increment per connection.
writer.db → primary, reader.db → weighted replicas), outlier detection on TCP connect failures, and per-cluster access logs that surface in Datadog as pg_primary.cx_total. Apps stay oblivious — same connection string, smarter network.Up next
tcp_proxy is the foundation. The next two lessons stack
protocol-aware filters (MySQL/Postgres for observability, Redis for
real cluster-aware routing) on top of the same L4 pipeline.
Which network filter would you use to front a Postgres or Kafka cluster with Envoy without parsing the application protocol?