How can I make good use of caching in programming ?, Cache is used in programming

Source: Internet
Author: User

How can I make good use of caching in programming ?, Cache is used in programming


Cache is one of the most common methods to optimize system performance. By adding cache before time-consuming components (such as databases), you can reduce the number of actual calls and reduce the response time. However, you must think twice before introducing the cache. This article provides some suggestions on how to make good use of the cache through some common errors when cache is introduced.
I. Common Errors
1. cache at startup
Sometimes, we will find that the application starts slowly and eventually finds that one of the dependent services has a long response time. What should we do?
Generally, this type of problem indicates that the dependent service cannot meet the requirements. If this is a third-party service and the control is not in our hands, we may introduce cache.
In this case, the cache invalidation policy is difficult to take effect because the cache design is designed to minimize the number of services on which requests depend.
2. cache too early
The "early" mentioned here is not the application lifecycle, but the development cycle. Sometimes we can see that some developers have estimated the system bottleneck at the early stage of development and introduced the cache.
In fact, this method masks possible performance optimizations. Anyway, the return value of this service will be cached. Why should I spend time optimizing this part of code?
3. Integrated Cache
"S" in the SOLID principle represents the Single responsibility principle ). When the application integrates the cache module, the cache module and the service layer are strongly coupled and cannot run independently without the cache module.
4. cache all content
Sometimes, to reduce the response latency, you may blindly add cache to all external calls. In fact, this behavior makes it easy for developers and maintainers to realize the existence of the cache module, and finally makes an incorrect assessment of the reliability of the underlying dependency module.
5. cascading Cache
Caching all content, or only caching most of the content, may cause the cached data to contain other cached data.
If the application contains this cascade cache structure, the cache expiration time may be uncontrollable. The top-level cache must wait until each level of cache fails to be updated before the final returned data is completely updated.
6. the cache cannot be refreshed.
Normally, cache middleware provides a tool to refresh the cache. For example, in Redis, maintenance personnel can use the tools they provide to delete some data and even refresh the entire cache.
However, some temporary caches may not contain such tools. For example, simply storing data in the cache usually does not allow external tools to modify or delete the cached content. In this case, if the cache data is abnormal, the maintenance personnel can only restart the service, which greatly increases the O & M cost and response time. What's more, some caches may write the cached content in the file system for backup. In addition to restarting the service, make sure that the cached backup on the file system is deleted before the application starts.
7. Impact of Cache
The common errors that may be caused by the introduction of cache are mentioned above. These problems will not be considered in the non-Cache System.
Deploying a system heavily dependent on the cache may take a lot of time to wait for the cache to expire. For example, it may take several hours to refresh the CDN configuration and content cached by CDN after the system is released.
In addition, cache is preferred when performance bottlenecks occur, which can cause performance problems to be masked and cannot be solved. In fact, it takes a lot of time to tune the code, which is not much different from introducing cache components.
Finally, debugging costs will be greatly increased for systems that contain cache components. The code is often tracked for half a day, and the result data comes from the cache, which has nothing to do with the actually logically dependent components. The same problem may also occur after all the relevant test cases are executed, and the modified Code is not actually tested.
2. How to make good use of the cache?
1. Discard cache!
Well, caching is unavoidable in many cases. It is difficult for Internet-based systems to completely avoid using the Cache. Even the http header contains the Cache configuration: Cache-Control: max-age = xxx.
2. Understand data
To Cache data access, you must first understand the data update policy. The If-Modified-Since header can be used to determine whether the data requested by the client needs to be updated, whether to simply return the 304 Not Modified response so that the client can reuse the previous local cache data or return the latest data. In addition, to make better use of the cache in the http protocol, we recommend that you differentiate the version of the data or use eTag to mark the version of the cached data.
3. Optimize performance instead of using Cache
Caching often masks potential performance problems. Use Performance Analysis tools as much as possible to find the real cause of slow application response and fix it. For example, reduce invalid code calls and optimize SQL based on the SQL Execution Plan.
Iii. Summary
Cache is a very useful tool, but it is easy to be abused. Do not use the cache at the last moment. use other methods to optimize application performance first.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.