Cache

Last Updated Jul 09, 2021

What is a cache?

A cache is a place in a network where frequently accessed data can be stored to improve load times. Requests have to travel a long way to the origin server to retrieve data, and if requests are frequently being made, a cache can improve performance if the data being retrieved is cacheable. The request will check the caches along the way first for the data it is looking for, eventually retrieving it from the origin server if the data isn't found. In REST APIs, GET requests are almost always cached. Being "cacheable" is an architectural constraint in REST API.  

Optimizing the network using caching improves the overall quality-of-service in the following ways:

  • Reduce bandwidth
  • Reduce latency
  • Reduce load on servers
  • Hide network failures

What data is cacheable?

A server response defines what data is cacheable and how it is cached with a cache control header. Designing the cache system is part of a computer engineer's job, because they want to make the network experience efficient and fast, but also safe. Another aspect of cache control is how "fresh" the cache data is. A user will be unhappy with old data, no matter how efficiently it is delivered.

How do I freshen a cache?

There are a few ways to ensure your cache has fresh (no older than 2 or 3 days) data.  

Cache busting: When a browser checks a resource at a URI, if that resource already exists, it is loaded from cache. Cache busting changes the URI, so if a request comes for the resource, the URI appears new to the browser, which dutifully retrieves the new resource.  

A similar option of cache busting is changing query string, the information after the `?` in a URL. You might see `?v=2`, for example, in a URL. The browser sees a different resource because query strings can affect what will be returned, and loads a fresh version of the resource.

Header request: Issuing a HEAD request returns metadata on the resource, and this can include information like when it was last accessed. Conditional requests can also be used with this information, such as `must-revalidate` forcing a revalidation if `max-age` is stale.  

Content Delivery Networks

Maybe you've had to download software from the internet and been offered a list of servers, with the suggestion you pick the closest one for the fastest speeds. That is the idea behind Content Delivery Networks, but they've become a much more intelligent tool as software delivery becomes more widely distributed. Caches can now perform logic computation, and tailor their responses based on information in request headers at a very granular level.  

Conclusion

Caches replicate commonly accessed files to deliver them across networks more efficiently. In modern server architecture, they have become an intelligent tool for managing resource overhead, delivering both static and dynamic content to consumers quickly, and generally handling resources that the application used to have to manage.

Try Abstract's suite of free API's,
trusted by 100,000+ developers.
Get started