How did you invalidate the cache?
AI answer
We chose the invalidation strategy based on the type of data and freshness requirements. For frequently changing data, we used a short TTL so stale values wouldn’t persist for too long. For data that needed to be refreshed immediately, we deleted keys when the source changed: for example, after writing or updating an object, we called delete for the associated cache key.
We built keys using the object identifier and the request’s version/parameters so that different data variants wouldn’t get mixed together. For bulk changes, we used a namespace or key versioning: switching the version allows you to logically reset a set of cached data without deleting each key individually. It’s important not to scan all of Redis on every reset, and to avoid a flood of requests after a large number of keys expire — TTL jitter helps with this, as does cache-stampede protection when needed.
We also checked consistency during concurrent updates and monitored the hit rate, latency, and cache errors. The specific mechanism depends on whether a briefly stale copy is acceptable and how large the key set is.
Also asked at Коммит (Аутстафф на проект "Ростелекома")
- How to protect the system from creating multiple payments if the user presses the pay button many times?
- How is the N+1 query problem solved in ORM?
- What types of indexes are available in PostgreSQL?
- There are several services (2-3), and there is an end-to-end business scenario passing through all services. If an operation on one of the services in the middle or at the end fails, the system ends up in an inconsistent state. What methods can be used to achieve inter-service transactionality (either the entire scenario is applied, or none of it is), if each service has its own database?
- What databases have you worked with the most?
Similar questions
- Could you briefly explain the caching in this case: what was cached, and how did it help?
- What was the cache hit rate?
- What is a Cache Stampede (the problem of simultaneous TTL expiration), and how can you protect a system from degradation when hot keys are invalidated?
- What cache read and write patterns are there (Cache-Aside, Write-Through, etc.)?
- You wrote that latency dropped from 120 to 6 milliseconds. Was that mainly thanks to caching?