Is there really a need for caching?

1. What are the costs and benefits of caching:

 

  Since we want to discuss whether caching is really necessary, we must know what are the costs and benefits (pros and cons) of caching?

  income cost
Cache + backend storage (resources)

1. Speed ​​up reading and writing

2. Reduce backend load

1. Data inconsistency

2. Code maintenance cost

3. Architecture complexity

 

  The table above should clearly express the benefits and costs of using caching. A detailed analysis will be given below

 

2. Detailed explanation of caching costs and benefits:

 

  1. The benefits are obvious. Generally speaking, a well-designed cache system can help your business achieve accelerated read and write while helping to reduce back-end load.

   (1) Accelerated reading and writing: Generally speaking, the acceleration is obvious, because the cache is usually a full-memory system, and the backend (maybe mysql, or even someone else's HTTP, RPC interface) has slow speed and poor pressure resistance The use of cache can effectively improve the user's access speed and optimize the user's experience.

   (2) Reducing back-end load: By adding cache, if there is no problem with the program and the hit rate is OK, it can help the back-end reduce the amount of access and complex calculations (join, or sql that cannot be optimized, etc.), The load on the backend is greatly reduced.

  2. Cost:

   (1) Data inconsistency: No matter how good your design is, there must be data inconsistency between cached data and authoritative data sources (which can be understood as real or back-end data sources) for a certain time window. The size of this time window can be It can be big or small, and it depends on the inconsistency of the time window that your business allows.

   (2) Code maintenance cost: After adding the cache, the code will add the relevant code of the cache based on the original data source. For example, it was only some sql, and now adding the kv cache will inevitably increase the maintenance cost of the code.

   (3) Architecture complexity: After adding the cache, such as adding redis-cluster, generally speaking, the cache will not have a special DBA like Mysql, and it is very likely that there is no full-time manager, so it also increases the complexity and maintenance of the architecture. cost.

 

3. How to choose?

 

  If the access speed and traffic volume of the current system can meet the existing requirements, there is no need to increase the cache. In fact, it is not that bad like MySQL. A well-running MySQL has no problem with QPS=1000.

  If you want to add and choose a cache, you must be able to give enough reasons. It is not for simple show technology and taking it for granted. The best way is to speak with data: how much is the speedup ratio and how much the back-end load is reduced.

 

4. What kind of scenarios need to be cached?

 

    In the company, according to my observation, no matter how to update the architecture and use various new technologies, 80% of the projects are still inseparable from SQL. Let's take SQL as the back-end data source and Redis as the cache layer. Let's talk about which ones Scenes need to be cached.

1. Complex and expensive calculations, reducing back-end load

    Taking Mysql as an example, some complex operations or calculations (such as a large number of join table operations, some grouping calculations), if not added

Cached, a lot of traffic will be performed during these complex computations.

 

2. Speed ​​up request response

    Even if a single backend data is fast enough (for example, select * from table where id=?), you can still use redis/memcache to optimize these operations for merge (for example: cache(select * from table where id in(id1,id10. ...idK))), thereby optimizing the corresponding time of the entire IO chain.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312032&siteId=291194637