Imagine following scenario:
- Client #1 requests object A from Redis
- Redis answers with null
- Client #1 requests object A from database
- Database returns object A
- Client #1 stores object A in Redis
Everything works fine. Let's add another client between 4 and 5:
- Client #1 requests object A from Redis
- Redis answers with null
- Client #1 requests object A from database
Database returns object A
- Client #2 requests object A from Redis
- Redis answers with null
- Client #2 requests object A from dtabase
- Database returns object A
- Client #2 stores object A in Redis
- Client #2 updates object A, both in database and Redis
- Client #1 stores object A in Redis
Now object A is up to date in database, but outdated in Redis. Are there any patterns to prevent such behaviour? Obviously locking the database while waiting for Redis to store its copy isn't a solution.
I should also note I'm using NodeJS which maintains a fixed size connection pool to Redis and requests are processed asynchronously, so we can't assume that the order of queries from different clients won't be mixed in a single connection.