Interview › Databases & Caching
How do you keep a cache and database consistent?
Databases & Caching · Advanced level
Answer
I keep cache and DB consistent by treating the DB as source of truth, using TTLs, invalidating after commits, versioning keys, and using outbox-driven invalidation so cache events are not lost.
Technical explanation
Database should remain the source of truth unless the cache is intentionally designed as a datastore.
Keep cache and DB consistent through TTLs, delete-after-commit, versioned keys, outbox-driven invalidation, and primary reads after writes.
Graceful degradation requires low cache timeouts, circuit breakers, stale-if-safe responses, DB fallback limits, and cache warming.
Hands-on example
Outbox invalidation example:
BEGIN;
UPDATE products SET price = 19.99 WHERE id = 123;
INSERT INTO outbox(event_type, payload) VALUES ("ProductUpdated", "{id:123}");
COMMIT;
Worker reads outbox, DEL product:v2:123, publishes event, marks outbox processed.Warm top keys after failover with a throttled job and stop if RDS CPU or app latency rises.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Databases & Caching interview questions
- How would you use Redis for distributed locking, and what are the caveats?
- How do you decide what to cache and what not to cache?
- How would you use Redis for rate limiting?
- How do you handle cache warming after a restart or failover?
- What is the difference between Redis as a cache and Redis as a primary datastore?
- What is the blast radius of a cache outage, and how do you make the app degrade gracefully?
- Why are some Redis commands (KEYS, FLUSHALL) dangerous in production?
- How do you test a Redis or database failover without impacting users?