Posts

Showing posts from July, 2020

How to deal with cache stampede in MySQL

Image
The Cache stampede problem (also called dog-piling, cache miss storm, or cache choking) is not a big problem for small scale systems but is a common headache for large scale applications with millions of requests. It has the potential to bring down the entire system in a matter of seconds. What is Cache Stampede Consider an item that is expensive to generate, and is cached. When the cache expires, the application usually regenerates the item and writes to the cache, and continue as normal. Under very high load, when the cached item expires, multiple requests will get a cache miss and all of them will try to regenerate the cache simultaneously, which will cause a high load in the database. Cache Stampede can occur with any cache including MySQL query cache, or any external cache like Redis or Memcached. It is ok to ignore this problem when your application is small scale but very important to address the issues as you scale up.  Cache stampede can be seen as random spikes in the CPU us

The DRY (Don't Repeat Yourself) Principle

DRY stands for Don't Repeat Yourself, which basically means to avoid writing duplicate code. If you find a block of code that is repeated multiple places, consider using abstraction or moving the code to a separate method. Similarly, if you use a hard-coded value more than once, try making it a constant. This can be applied to every area of software development including database, unit tests, build systems, and documentation.  The benefit of the DRY principle is code maintenance. Changing functionalities in the future will be much easier because you don't have to remember all the places where the code needs to be changed.  An important point to remember here is to not abuse the principle. Duplication is not for code, but for functionality. It means you should never use a standard code for two functionalities that have similar code but are logically unrelated. Doing it will tightly couple two different functionalities together and changing one of them in the future will become h