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.

Preparing for an interview?

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

← All Databases & Caching questions