Redis proxy — cluster-aware
Front a Redis cluster with command-level routing, hash slotting, and read-from-replica.
Why Redis gets a real proxy (not just stats)
Unlike the SQL filters (Lesson 29), envoy.filters.network.redis_proxy
is a real proxy:
- Speaks RESP (Redis serialization protocol) end-to-end.
- Understands hash slots and routes commands to the right shard.
- Fragments multi-key commands like
MGET key1 key2 key3across shards. - Follows MOVED / ASK redirections so reshards are transparent.
- Pipelines requests for throughput.
- Supports prefix-based routing (send
cache:*to one cluster,session:*to another). - Read-from-replica policy.
- Pass-through
AUTH.
Why is Redis tractable when SQL isn't? RESP is simpler and stateless per command. SQL has multi-statement transactions, prepared statements, and per-connection session state that make rewriting genuinely hard.
Minimal envoy.yaml — cluster-aware Redis with prefix routes
filter_chains:
- filters:
- name: envoy.filters.network.redis_proxy
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.redis_proxy.v3.RedisProxy
stat_prefix: redis
settings:
op_timeout: 5s
enable_redirection: true # follow MOVED/ASK transparently
enable_command_stats: true
read_policy: PREFER_REPLICA
downstream_auth_passwords:
- inline_string: "s3cret"
prefix_routes:
catch_all_route: { cluster: redis_cluster }
routes:
- prefix: "session:"
cluster: redis_sessions
- prefix: "cache:"
cluster: redis_cache
clusters:
- name: redis_cluster
cluster_type: { name: envoy.clusters.redis }
lb_policy: CLUSTER_PROVIDED
load_assignment:
cluster_name: redis_cluster
endpoints:
- lb_endpoints:
- endpoint:
address: { socket_address: { address: redis-0, port_value: 6379 }}
Three things to internalise:
enable_redirection: true— when Redis returnsMOVED(after a reshard), Envoy retries to the right node automatically. Clients never see the redirection.read_policy: PREFER_REPLICA— read commands prefer replicas; writes always go to the master. Cuts master load substantially.prefix_routes— different key prefixes can land on entirely different Redis clusters. Useful for separating session storage from the warm cache.
Three pitfalls
Envoy relays the whole transaction to the node owning the
first key. If subsequent keys live on different shards you'll
get CROSSSLOT errors. Use Redis hash tags
(, ) so
all keys map to the same slot. Without this, transactions silently
break.
Pub/Sub (SUBSCRIBE, PSUBSCRIBE),
SCRIPT LOAD to all nodes, CLIENT KILL, and
several admin commands return errors or are no-ops. Audit with
enable_command_stats: true before migrating.
Reads may hit a stale replica. For session stores, profile data, or
anything where you write then immediately read, use
read_policy: MASTER. PREFER_REPLICA is for
read-heavy caches that tolerate seconds of staleness.
Hands-on: a real Redis cluster behind Envoy
- Stand up a local Redis cluster:
docker run -d --name rcluster -p 7000-7005:7000-7005 grokzen/redis-cluster
-
Configure Envoy with the snippet above (point all three clusters at the same Redis until you want to split). Run Envoy on port 6379.
-
With
redis-cli -p 6379:
SET cache:hello world # routed to redis_cache cluster
GET session:42 # routed to redis_sessions
SET other:thing 1 # falls through to catch_all
- Force a hash-tag transaction:
MULTI
SET {u:1}:a one
SET {u:1}:b two
EXEC
Both keys hash to the same slot (u:1) so the transaction
succeeds. Drop the hash tag and watch it fail with CROSSSLOT.
- Watch per-command stats:
curl -s localhost:9901/stats | grep redis.command
redis://localhost:6379 endpoint. It handles slot remapping during reshards transparently. A single Grafana board shows redis.command.incrby.total per shard. The SDK churn stops; the smart-client repos get archived.What this combines with
The same filter-chain skeleton you've been writing applies:
tls_inspector+ filter_chain_match for SNI-based Redis routing if you have TLS terminating clients.- Outlier detection on the cluster to eject misbehaving Redis nodes.
- Access logs with command-level metadata for audit.
Redis is the most capable of Envoy's protocol-specific filters — and arguably the most production-ready non-HTTP use of Envoy.
Which Redis pattern does the Envoy redis_proxy filter NOT fully support?