Imagine a web application that fetches user profile data from a database. Which of the following best explains why adding a cache layer reduces the time it takes to get the data?
Think about where the data is stored and how fast it can be accessed compared to the original source.
Caching stores copies of frequently requested data in a faster storage layer, often memory, which is closer to the application or user. This avoids the slower process of querying the database every time, thus reducing latency.
In a typical client-server system, where is the best place to add a cache to reduce latency for repeated data requests?
Consider where repeated requests happen and where storing data temporarily can save time.
Placing cache between client and server (like in-memory cache or CDN) allows repeated requests to be served quickly without hitting the server or database, reducing latency significantly.
A system uses caching to reduce latency. What happens if the cache size is too small compared to the data requested?
Think about what happens when the cache cannot hold all the data needed.
If the cache is too small, it cannot store all frequently requested data, leading to more cache misses. This forces the system to fetch data from the slower database more often, increasing latency.
Why might a system choose to serve slightly stale data from cache instead of always fetching fresh data from the database?
Consider the speed difference between cache and database and the impact of data freshness.
Serving slightly stale data from cache avoids the slower process of querying the database every time. This reduces latency and improves responsiveness, though at the cost of data freshness.
A database query takes 200 ms on average. A cache lookup takes 5 ms. If 80% of requests hit the cache, what is the average latency per request?
Calculate weighted average latency based on cache hit and miss rates.
Average latency = (cache hit rate * cache latency) + (cache miss rate * (cache latency + database latency)) = (0.8 * 5) + (0.2 * (5 + 200)) = 4 + 41 = 45 ms.