Skip to content

Race condition in cache invalidation when multiple workers update same key #1

Description

@Katomoto

Under concurrent load, multiple workers occasionally read a stale cache entry after invalidation because the delete and subsequent write are not atomic.

The issue appears when:

  • Worker A reads key → cache miss → fetches from DB
  • Worker B invalidates the key
  • Worker A writes stale data back to cache
  • Subsequent reads get the stale value until next TTL expiry

This is a classic cache stampede / stale write race. Fix options:

  1. Use compare-and-swap (CAS) on write
  2. Add a short lock per cache key during refresh
  3. Adopt a read-through pattern so only one worker refreshes at a time.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions