In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail the application of caching technology in improving the performance of front and back-end systems in web. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Take a closer look at our existing system, from a CPU to an online trading system. Any performance problem can be effectively solved in one way: caching. Yes, caching can almost be a silver bullet to solve performance problems. The main purpose of caching is to reduce data access latency, and there are a variety of implementation methods. Different kinds of caching are introduced below.
Caching of CPU
The smallest granularity of cache you can think of is probably CPU cache. CPU not only has caches, but also divides caches into several levels, as shown in figure 1, which are L1, L2, and L3 caches, respectively. Where L1 and L2 are a core exclusive cache, while L3 is shared by multiple cores within the same CPU. Figure 1 architecture is currently the most common architecture in CPU, while CPU's architectural details about caching are more complex than this. Most CPU classifies caches into instruction caches and data caches, and some have a cache called TLB (translation lookaside buffer), which is used for fast conversion from virtual memory to physical memory.
Figure 1 CPU cache architecture
CPU is not born with cache. Take CPU of Intel as an example, L1 Cache was introduced into 386 CPU in 1992. The introduction of L3Cache was not introduced in Core i3 until 2008. As shown in figure 2, the access delay of different storage types is different. If the register access is 1 time unit, then the memory access is about 100 units. In other words, the access delay of memory is 100 times that of registers.
Figure 2 comparison of different storage access performance
Based on the above reasons, a cache module is added to the new generation of CPU design to reduce the delay of accessing in-memory data. The principle of using Cache to improve performance lies in the locality of data access, which is regional locality and time locality respectively.
Spatial Locality: for data that has just been accessed, there is a high probability that adjacent data will be accessed in the future.
Temporal Locality: data that has just been accessed has a high probability of being accessed in the future.
Operating system cache
Caching is used in many places at the operating system level. The principle of operating system cache is basically the same as that of CPU cache, which has two local features. I'm afraid the cache in the operating system is the page cache in the file system. Also referring to figure 2, you can see that the latency of accessing the disk is 100000 times that of memory, so all file systems in the Linux operating system use caching to improve their read and write performance.
In addition to the above differences in memory access performance and disk access performance, another factor is the performance difference between mechanical disk random access and sequential access. Take enterprise SATA disks as an example, the bandwidth of random writes is less than 1MB/s, while sequential writes can easily reach 100MB/s, with a difference of up to 100 times. The main reason for such a big difference is that the read and write data of the mechanical disk needs to be addressed, and the time consumed by addressing accounts for a large proportion of the total request time.
Given the above two factors, most file systems implement memory-based caching. In this way, the performance of user access to the file system has been greatly improved. Cache mainly improves the performance of accessing the file system from two aspects: one is to reduce the direct access to the disk, and the other is to convert the random access to the disk into sequential access as far as possible.
For the first aspect, after the file system data is written to the cache, the data is considered to be written successfully and the result is returned to the upper layer. Because the performance of accessing memory is 100000 times that of accessing disk. Therefore, the performance can be greatly improved naturally. In the upper part of the logic shown in figure 3, the write request writes the data to the cache in turn. There is a similar process for read requests, which is called pre-read in the file system, that is, data is read into the cache in advance, thus reducing the frequency of disk access.
Figure 3 File system page cache
For the second aspect, due to the existence of the cache, the data is sorted by the LBA of the data when it is written from the cache to the disk. This can reduce the time consumption of mechanical disk seek, and then improve the overall performance of the system.
Caching of the Web front end
At the large system level, we take the Web application as an example to introduce the front-end to back-end caching technology. This is mainly because caching is the most widely used in the field of Web development, which is very convenient for us to understand the problem. The technical field of cache is very extensive, and the technical difficulty is also very deep, this article can not cover everything, just throw a brick to attract jade.
Students who are familiar with front-end development know that in addition to dynamic content, a website also has a lot of pictures, JS scripts, CSS sample sheets and other content. While the content such as pictures, JS scripts and CSS is relatively large on the one hand, it rarely changes on the other hand, unless the website is upgraded. In view of the above reasons, whether we can cache this content to the user's local disk, so that when the user visits the site again, the content can be loaded directly from the local disk without going through the vast network.
Figure 4 browser page request
In fact, browsers already have this feature, not browsers, but HTTP protocols. As shown in the picture, when we open the browser's debugging tool, we can see that many of the contents of the requested web page are not requested from the server, but from the local disk or memory. Figure 4 shows what part of the content is when a page of a website is requested. You can see that a large part of it is not obtained from the server.
Figure 5 Web browser cache processing flow
There are two obvious benefits of getting it locally, one is that the response time of the page is very fast, and the other is that the pressure on the Web server is greatly reduced.
In HTTP, the content in the cache is determined by its response header, which is Cache-control and, of course, with the help of other fields. The whole process of caching is complex and needs to be judged based on different fields in order to determine where to get the content. The figure shows the processing flow of the entire browser.
Figure 6 is the response header content of a picture of an east home page intercepted by the browser's debugging tool. it can be seen from the picture that it contains cache control-related content.
Figure 6 HTTP protocol for Web cache
Access the link layer
In Web, a requested link can be very long, for example, if we visit a website in the United States, the entire communication link will span the entire Pacific Ocean, nearly 20, 000 kilometers away. Even Sunshine takes dozens of milliseconds to complete the mileage, and the network needs to go through a variety of relay devices, which takes nearly 200ms. Just imagine, a web page is usually made up of hundreds of elements (such as pictures, js scripts, etc.). How long will it take to open an American website in China?
The most effective technology to solve the above problems is CDN (Content Delivery Network, content delivery Network) technology, which provides faster services through divisions on edge servers close to end users. Take the above website as an example, when users visit a website in the United States from China, they will first get the content from the domestic CDN node, and if not, they will get the content from the source server in the United States. Because most of the content is available domestically, it eliminates the network latency caused by crossing the ocean.
Figure 7 schematic diagram of CDN
Figure 7 is a schematic diagram of a CDN where ORIGINAL SERVER is the source server and EDGE SERVER is the edge server. From the figure, you can see the access path of the end user.
Caching of Web backend
For Web services, data is usually stored through relational data, while data from a database is usually stored on disk. In high-load scenarios, databases often become performance bottlenecks. Therefore, in order to improve the carrying capacity of the whole service, a cache service is often added between the business server and the database. The principle of this caching service is actually similar to the CPU cache or file system cache mentioned above.
Since more than 80% of requests for a Web service are read requests, the design of the Web cache is based on this fact. This data can be cached in memory, thus reducing the load of database access. Because the cache carries the vast majority of read requests, the load on the entire database is greatly reduced.
Figure 8 Web service cache
As shown in figure 8, the Web service architecture using Redis caching, this article is just a simplified architecture, and the actual architecture is much more complex (reliability and bearer capacity need to be considered, etc.). In this architecture, the business server accesses the cache first according to the request type, and updates the cache content according to the policy. At present, the most used cache should be the Redis cache, which you can take a closer look at.
Cache applications to improve system performance. Of course, there are many more applications for caching than in this article. This is just an introduction. I hope you can get some inspiration and provide some ideas for the performance optimization of your system.
This is the end of the application of caching technology in improving the performance of web front-end and front-end systems. I hope the above content can be of some help and can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.