Sobes.tech
Middle

What to do if there is no data in the cache (cache miss)? What to show the user?

sobes.tech AI

Answer from AI

In the case of a cache miss (absence of data in the cache), a request for data is usually made from the primary source — a database or an external API. After receiving the data, it is written to the cache to speed up subsequent requests.

What to show the user:

  • If the data can be retrieved quickly, it is better to show the current information after loading.
  • If the delay is significant, a loading indicator or a message "Data is loading" can be displayed.
  • In some cases, it is useful to show stale data from the cache with a note that it may be outdated.

Example in Java:

String data = cache.get(key);
if (data == null) {
    data = database.query(key);
    cache.put(key, data);
}
return data;

Thus, the user receives either fresh data or an informative loading status.