Skip to content

Multi-region cache invalidation — fan-out without orphans

How to invalidate a cache across regions when origin lives in one region and caches live in many — fan-out, idempotency, and convergence guarantees.

When the application runs in three regions and each region has its own cache, an update to the source of truth must propagate to all three before users see consistent data. “Just delete the key in all regions” sounds simple until a region is temporarily unreachable, an invalidation message is lost, or one region’s clock is skewed enough that a stale entry’s TTL has not yet expired. This article is the operational view: how we design invalidation to converge under failure, and what the trade-offs look like.

How to verify

Inspect how invalidation is wired today. The signature of a healthy fan-out is “each invalidation reaches every region’s cache within a measurable budget, with retries on failure.”

# Trace an invalidation through the system
APP_LOG="$(grep 'cache_invalidate' /var/log/app/app.log | tail -1)"
echo "$APP_LOG"
# Check the bus: did the message arrive in each region?
for region in us-east eu-west ap-southeast; do
  redis-cli -h cache.$region.internal -n 1 LRANGE 'invalidation_log' -10 -1
done
# Check the resulting key state
for region in us-east eu-west ap-southeast; do
  echo "=== $region ==="
  redis-cli -h cache.$region.internal EXISTS "product:42"
done

If one region still has the key minutes after the others dropped it, your fan-out is leaky and you have an orphan.

What’s happening

Multi-region caches do not share invalidation state by default. Each region’s cache is independent; an invalidation that hits region A leaves region B and C with the old value until their TTL expires. The “fix” is to deliver the invalidation to every region — but delivery is unreliable, so you need:

  • Idempotent invalidation — the same delete repeated N times is no worse than once. Cache deletes are naturally idempotent; cache updates with a value are not (last write wins by timestamp, so include a timestamp).
  • At-least-once delivery with retry — messages may be redelivered; ensure the receiver tolerates this.
  • Convergence ceiling via TTL — every cached entry has a TTL ceiling so even with a lost invalidation, the system converges within TTL time.

The standard implementations:

  1. Pub/sub fan-out. Application writes to origin, then publishes an invalidation message to a topic. Each region’s cache subscribes and acts. Fast (sub-second) but at-most-once unless backed by durable storage.

  2. Message queue with consumer per region. Application enqueues an invalidation event; each region’s cache has a consumer that reads and acts. At-least-once with durability.

  3. Streaming changelog (CDC). A change-data-capture stream from the database (Postgres logical replication, MongoDB change streams) drives invalidation events to each region. The application does not need to know; cache invalidation is a downstream consumer of database changes.

  4. TTL-only. No active invalidation; rely entirely on TTL expiry. Simplest, but the convergence window is “however long the TTL was set to.”

The procedure

  1. Pub/sub fan-out via a global topic. Application publishes; each regional cache subscribes.

    # On origin write
    def update_product(pid, fields):
        db.products.update_one({'_id': pid}, {'$set': fields})
        payload = {'op': 'invalidate', 'key': f'product:{pid}', 'ts': time.time()}
        redis_global.publish('cache_invalidations', json.dumps(payload))
    
    # In each regional cache subscriber
    def regional_subscriber():
        p = redis_local.pubsub()
        p.subscribe('cache_invalidations')
        for msg in p.listen():
            if msg['type'] == 'message':
                data = json.loads(msg['data'])
                redis_local.delete(data['key'])
  2. Add a durable queue for guarantees. When pub/sub loses a message, the entry stays stale until TTL. For higher-importance invalidations, route through a per-region queue with retries.

    # Application
    sqs_global.send_message(QueueUrl=URL,
        MessageBody=json.dumps({'key': f'product:{pid}', 'ts': time.time()}),
        MessageGroupId=f'product:{pid}')
    
    # Per-region consumer
    while True:
        msgs = sqs_local.receive_message(MaxNumberOfMessages=10, WaitTimeSeconds=20)
        for m in msgs.get('Messages', []):
            data = json.loads(m['Body'])
            redis_local.delete(data['key'])
            sqs_local.delete_message(QueueUrl=URL, ReceiptHandle=m['ReceiptHandle'])
  3. CDC-driven invalidation via Debezium. The database emits change events; cache invalidation is a downstream consumer.

    # Debezium connector for Postgres
    { "name": "products-cdc",
      "config": {
        "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
        "database.hostname": "pg-primary.internal",
        "table.include.list": "public.products",
        "topic.prefix": "cdc"
      }
    }
    # Kafka consumers in each region read cdc.public.products
    # and DELETE cache entries keyed by id
  4. Set TTL ceilings on every cached key. Even with perfect fan-out, the safety net is TTL — pick a number you can afford as a worst-case staleness window.

    redis_client.set(f'product:{pid}', value, ex=600)   # 10 minute ceiling
  5. Monitor convergence. Emit a metric per invalidation showing how long after the write each region’s cache stopped serving the old value.

    # In the subscriber, after the delete:
    prom_invalidation_propagation_seconds.observe(time.time() - data['ts'])

Common pitfalls

  • Using pub/sub alone for invalidations that must converge — a subscriber that is restarting loses every message during the restart window.
  • Mixing “write to origin then publish” without atomicity — the write succeeds, the publish fails, the cache stays stale until TTL. Wrap both in a outbox pattern or accept the staleness window.
  • Treating the global topic as a Tier-1 dependency without HA — a topic outage stalls every cache invalidation everywhere.
  • Forgetting the TTL safety net and discovering a stale entry six months old.
  • Letting clock skew between regions misorder invalidations — include a logical timestamp (vector clock or HLC) when ordering matters.

In the engagements we run, multi-region cache invalidation is a design decision that lives in the architecture doc, not “something the cache library handles.” Stack Harbor’s multi-region environments practice documents the convergence guarantee per data class, measures it, and runs the chaos test that proves it — region outages do not become silent staleness incidents.