Do you have your own web cache knowledge system?

Source: Internet
Author: User

Shong Sundong

Hu-Hu, who has been in charge of the structure and operation of the command Automation Department in the Armed police unit, has been engaged in the Internet operation and maintenance since retiring in 2008. He has a team responsible for the operation and maintenance of a domestic e-commerce, "Saltstack Introduction and practice," the author, a senior lecturer of a college.

First, Introduction

Learning technology must be a system, this is my personal learning goal, that is, in a specific scope, the relevant technology is as far as possible to make clear, understand.

I took the Web cache related content from my "Web Operations Knowledge System" (link 1) to take out, after the completion of the formation of a separate "Web cache Knowledge System" (link 2), share to everyone, for reference only.

Note:

Link 1:https://www.unixhot.com/page/ops

Link 2:https://www.unixhot.com/page/cache

Second, the Web cache knowledge System

In the Web architecture, we often hear the word grading or layering, which is one of the more important concepts in architecture: layered design (hierarchical design).

So for a Web site, from an HTTP request generated to the server return data, will go through a very many different levels of caching, I have organized a "relatively" comprehensive Web cache hierarchy.

Click to view large HD image

This system enumerates the caching techniques and methods involved at various levels from the global seven level, this architecture contains only read cache (cache), does not contain write buffer (buffer), so many buffers are not listed, please forgive me.

    • user layer: mainly relative to the user side can use the caching technology, the most common is the browser cache.

    • Agent layer: The main use of CDN is mainly used in the industry.

    • Web tier: The Web server's own caching technology, as well as the opcode cache "Opcache".

    • Application Layer: application level such as the framework level of dynamic cache content output, as well as business-related local cache and page static, such as large e-commerce product page is almost all the use of page static technology.

    • data tier: mainly lists the caches of the distributed cache and the database itself, and there may be more to consider from a storage perspective.

    • System Layer: The operating system level of caching technology, mainly related to CPU, memory, IO.

    • Physical layer: only the raid card of the storage device and the cache area on the disk are listed, and of course there is the role of write caching.

Third, buffer and cache

Because the content of the cache grading system is more, it is difficult to elaborate in the article, then we will talk about the most familiar, but also often discussed buffer and cache.

What is buffer? What is the cache? I believe this is also a problem that has plagued many beginners for a long time. And most of the time we will call a zone buffer cache, let everyone more ignorant, whether it is buffer or Cache.

First, the literal understanding of buffer refers to buffering, and the cache refers to caching. My Personal summary is:

    • buffer: generally used for write operations, can be called write buffer.

    • cache: commonly used for read operations, can be called read cache.

1. Cache

Let's start with the cache, which is the easiest thing for us to understand. The cache is designed to improve the reading speed of the data.

For example, when we read the data we use from a distance to one of our closest buffers, this data can be read directly from the nearest cache, which obviously improves performance.

Here we take the CPU to illustrate, as Operations engineer I believe everyone knows that the CPU read files from memory to read, and the CPU speed is much higher than the memory, then if every time the CPU from memory to read is obviously relatively slow.

The CPU now increases the cache, and the CPU of the general server supports level three cache:

L1 Cache

L2 Cache

L3 Cache

CPU cache is a temporary memory between the CPU and memory, its capacity is much smaller than memory, but the exchange speed is much faster than memory, the CPU cache is stored in the CPU has just used or recycled part of the data.

When the CPU uses this part of data again, it can be called directly from the cache without having to read the memory, which reduces the CPU wait time and improves the efficiency of the system.

So we use the Taskset command to bind a process to a fixed CPU in some real operations scenarios (such as KVM tuning), in order to reduce the CPU cache Miss, which can improve performance.

2. Buffer

We understand the cache, and then we say buffer. At present, the buffer and the cache are controversial or more, mainly conceptual. I'm going to take an example of life to explain buffer.

Currently in China, car penetration is very high, a lot of friends have a driver's license, so the rules should be familiar. Let's put down the technology to do a pass test:

Look at the figure below, please answer the white dotted area called what?

Left turn to turn zone

Answer: This white area is called left turn to turn the area, we first put down the technology, to review the delivery rules, to ensure that everyone on the road safety.

The left-turning lane adds a few metres of white dashed box and connects directly to the middle of the road.

These white dotted lines are set to "left turn to turn zone" for the same straight road green light, the left turning lane of the vehicle over the stop line, advance into the area to wait for a turn, wait until the left turn signal turns green and then through the intersection.

The role of left turn to turn zone

This will effectively increase the number of vehicles crossing the intersection, reduce the vehicle stranded, that is, make the vehicle faster turn. We can see that the area to be turned is a curved one, closer to the destination of the turn.

This left-turn-to-turn area is actually a living example of buffer that we are going to discuss.

This zone is a buffer that allows us to park the vehicle closer to the destination. Just as we write the data to a place closer to the goal.

What is buffer?

buffer: an area where data is transferred between devices that are not synchronized or that have different priority levels.

This time we take memory and disk for example, just now we said CPU speed is much faster than memory, then take the memory and hard disk compared?

The speed of memory is much faster than the speed of the hard disk. But we know that memory is volatile storage, and our persisted data is eventually saved to the hard disk.

But the hard drive is so slow, it's obviously unrealistic for the CPU to wait for the data to be fully written to the hard disk.

So take our Linux system, will be in memory set a buffer called " disk buffer ", more accurate point called "Memory disk buffer."

After the CPU writes the data to the "Memory disk buffer", it thinks that it has finished writing and then goes to do something else without waiting for the data to be written to the hard disk.

Then Linux has a kernel thread called Pdflush, which is used to write "related" data from the cache to disk. This obviously increases the speed of CPU processing. Of course, there are many mysteries, and interested friends can study in depth.

Do you really understand that?

Looking back at my previous exploration of buffer and cache, as you can read here, there's definitely a lot of confusion in mind, so is the memory cache or buffer?

Answer: The cache is also the buffer, because it does assume the function of both roles, so we often take these as the role of the cache as the buffer role calledbuffer Cache.

Of course there are other titles, we should not be too tangled, the simplest is that we can according to their role to distinguish.

OK, later people will ask you what is buffer? What is the cache time, you can confidently answer:

The first thing that caches and buffer solve is the problem of speed inconsistency, they all have a storage area.

But:

    • Cache : commonly used for read caching, used to put frequently read content into the cache, the next time to read the same content, read directly from the cache, improve read performance, buffer can have multiple levels.

    • buffer: generally used for write buffering, to solve the difference between different media storage speed, the data is written to a relatively slow than the middle of the region is not a lot of return, and then eventually write to the destination address, improve write performance. Buffers can also have multiple levels.

Note: In many cases we mix buffer with the name of the cache. There is also a unity called the cache.

3, the cache related indicators

This time we focused primarily on the cache at each level in the Web architecture, even though some levels were not just the function of the cache but also the function of buffer. Now that we're using the cache, we need to focus on some of the most important indicators of the cache.

3.1 Cache storage Location

There are three different locations for cache storage in our web schemas:

    • Client (browser cache)

    • Memory (native memory, remote server memory)

    • Hard disk (local hard disk, remote server hard disk)

It is obvious that the fastest is stored in the user's browser, followed by the Web server's memory, and finally the hard disk.

memory is fast , but after all, the price is expensive and space is limited, so we can also write data to other servers in memory, such as memcached and Redis, over the network.

The hard drive is slow , but the price is low and the space is relatively adequate. So when designing the cache, you often encounter ratings settings, like the CPU's level three cache. We can also write data over the network to other servers ' hard drives, such as NFS.

3.2 Cache Expiration Time

Since the cache is a region, the storage space for this area is bound to be limited, and all caches must have an expiration time and cannot be stored.

At different levels, you can use different methods to set the expiration time of the cache

3.3 Cache forced expiration

Now that the cache has an expiration time, what if we want the cache to expire immediately without reaching the expiration date?

This is also an issue that we need to focus on when using and designing the cache, and how to force the cache to expire after the source data has changed.

3.4 Cache Hit Rate

The most important indicator of the cache is the hit rate! That is, each time the cache lookup can be in the cache area, to find the content has been cached.

The higher the cache hit rate, the better the effect!

We can imagine that if we join the cache layer, the hit rate is very low, not to say that we have to manage the cache increased workload, data read and write path more than one layer, in theory will affect performance.

So a completely non-hit cache should not exist except for "very special" needs.

Four, leave a face question

Finally, leave a cache-related interview question: Do you need a CDN for the cloud services we use on our daily phones, such as cloud contacts, cloud messages, cloud images (that is, syncing your contacts, text messages, and pictures to the cloud)?

V. Concluding remarks:

Perhaps, you and I have the same habit is every day in the commute to read all kinds of technical public number, then, I suggest for others to share, should be good at summing up and precipitation, only in this way can become their own knowledge.

Here, we thank the efficient operations community for providing us with a platform for learning and communication.

Do you have your own web cache knowledge system?

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.