envoy-guide
Module 8 · Beyond HTTP·14 min·advanced

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 key3 across 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

YAML
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:

  1. enable_redirection: true — when Redis returns MOVED (after a reshard), Envoy retries to the right node automatically. Clients never see the redirection.
  2. read_policy: PREFER_REPLICA — read commands prefer replicas; writes always go to the master. Cuts master load substantially.
  3. prefix_routes — different key prefixes can land on entirely different Redis clusters. Useful for separating session storage from the warm cache.

Three pitfalls

MULTI / EXEC must be slot-safe

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.

Not every command is supported

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.

PREFER_REPLICA breaks read-after-write

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

Hands-on · 3-shard cluster + prefix routing
  1. Stand up a local Redis cluster:
Shell
docker run -d --name rcluster -p 7000-7005:7000-7005 grokzen/redis-cluster
  1. Configure Envoy with the snippet above (point all three clusters at the same Redis until you want to split). Run Envoy on port 6379.

  2. With redis-cli -p 6379:

Text
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
  1. Force a hash-tag transaction:
Text
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.

  1. Watch per-command stats:
Shell
curl -s localhost:9901/stats | grep redis.command
Real-world example · A chat platform with per-channel rate limits
Problem
A chat service stores per-channel rate-limit counters in Redis Cluster across 24 shards. Clients used to ship a bespoke smart client to handle MOVED replies, but six SDK languages drift out of sync — every quarter someone ships a regression.
Envoy solution
Envoy as a sidecar gives every service a dumb 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.

Quick check

Which Redis pattern does the Envoy redis_proxy filter NOT fully support?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3