Member-only story
How to cache well in Go
Cache is essential for speeding up application APIs, so it is indispensable in the initial design stage if there are high performance requirements.
If caching is needed during the design phase, the most important thing is to Estimate how much memory is needed 。
We first need to clarify what content of data we need to cache.
It is not advisable to cache all the data used in applications with continuously increasing user numbers.
Due to the limitation of single machine physical resources on the local memory of the application, unlimited cached data will eventually result in OOM, leading to the forced exit of the application.
If it is a distributed cache, the high hardware cost also requires us to perform a trade off.
If When there are no limitations on physical resources, it is best to put them all into the fastest physical devices.
But the reality of business scenarios does not allow it, so we need to divide the data into hot and cold data, and even archive and compress the cold data appropriately, storing it in cheaper media.
Analyzing which data can be stored in local memory is the first step in doing a good job of local caching.
Balance between stateless applications
Since the data is stored locally in the application, in a distributed system, our application is no longer stateless.