Caching makes the world go round

First thing developers learn when to speed up applications is to use some form of caching. The art of storing information for later lookup and use. You can liken it to looking up a phone number for a pizza place, and then writing that phone number down on a piece of paper.

The next time you need to call that place you go to the piece of paper instead of looking them up online again. Or the digital equivalent of the piece of paper, a contact in your phone. Now some things can be asked, does the pizza place know you stored their phone number, how do you know the pizza place did not change their phone number in the meantime?

Of course the first question is a bit weird, but in programming this makes quite a lot of sense. Can we know for sure the user has cached our data or not? In other words do they store cookies, localStorage, sessionStorage or some other form of caching. These days there can be quite a number of layering of caches.

The situation is a frontend application run in the browser, a reverse proxy that relays the requests to the backend and a backend application interacting with a database.

Starting from the backend interacting with the database, we can cache certain lookups and queries for later use. Then the reverse proxy can cache queries to the backend for later use. The browser can cache the queries to the reverse proxy for later use. That is three layers of cache that all hold data that might be up to date, it might not be. It could happend for instance that the reverse proxy and the backend both update their caches correctly but the way the request is fired to the backend the browser actually does not update its cache.

Caching is great but invalidating caches can become quite complex and hard. Not to mention if you add caching local to classes in the form of memoization for example. You can even add a third party service that only does caching for you, like a Redis instance. There can be filesystem caching which might interfere with cookies.

Only add caching once you know you can invalidate it properly and also once you know it will add speed benefits without increasing the complexity in a significant way.

#devlife #devops