Book notes-Cache for building high-performance Web Sites

Source: Internet
Author: User

In fact, before talking about caching, there are other basic knowledge about network and server hardware and systems. In the network section, we mainly introduce the concept of network models and bandwidth, it provides a method for us to calculate a network transmission time, and how to deploy servers in China Unicom and China Telecom networks to achieve interconnection. In terms of server hardware and system capabilities, a server capability indicator is highlighted: throughput, and basic knowledge of each major component and system is introduced. Understanding and familiarity with this knowledge is indispensable for us to build an excellent system. The author's knowledge in this area is also a variety of materials, and there is not much practical experience, we recommend that you read this book or other relevant materials to learn about this topic.

Next we will go to the topic of this article. The cache will not only reflect the time-to-space change, but also save a lot of resource overhead and increase the server throughput. However, before using all the caches, we need to make a good update or expiration Policy, and we need to make preparations for re-calculation at any time.

 

First, let's talk about the cache application scenarios from the website:

1. Dynamic Content Cache

No matter whether your page is Php, aspx, or JSP, the final output content must be HTML, and these dynamic pages must finally output HTML results through a series of computations, in this way, we can cache this result. When there is the same request next time, it will be returned directly. This cache is calledPage CacheIn Asp.net, we can easily implement full or partial page cache (control cache ).

In some casesPersistent to Hard DiskIn this way, we can cache a large number of files at a low cost, for example, we can cache different results based on different URL parameters. However, if the same cache is too large, the hard disk I/O overhead may be huge. We need to group directories or other algorithms to separate directories: We use time to create hierarchical directories, you can also cache the item information to different directories based on the result of an item ID's remainder on a base.

In addition, we canPut the results (calculated results or extracted data) into the memory, Such as. Net cache or memcache, which exist in the form of key/value. This is probably the most common one.

There is also a more thorough approach:Static PageAllows users to directly access the generated HTML page. This is the most efficient in all caches, because users' requests are directly returned without going through our program, our program only manages these pages in the background.

2. Script Acceleration

The opcode cache is called in the book. When the interpreter analyzes the code, it directly generates the operate code and then executes it directly. Such languages include PHP, Python, and Ruby. The so-called opcode cache caches operate code like dynamic content, saving time in code interpretation, these can be implemented through PHP opcode cache extensions such as APC and xcache.

For example, C ++ does not compile this process. Compiling and running are two processes. After compiling it into the target program that the machine can directly run, the operation is controlled by the target program.

We know that C # is also a compilation language, but its process is quite different. It is first a C # compiler that generates Il. In actual execution, il is executed by the JIT compiler again by generating machine code. In the process of compiling il into executable code, it is compiled in units of functions, and each function is compiled only once, in the future, the operation will be run directly with the generated machine code. This is the same idea as the opcode cache mentioned above, but we don't have to worry about it any more, how do we use this mechanism? There is one in effictive C:Try to implement short and concise functions. In fact, this is also a point we should pay attention to in our program. Try to extract the same code to the method rather than scattered Inline code. functions with the same name will only be compiled once by JIT.

3. browser cache

This may be familiar to everyone, and many articles have been discussed in the garden. Browser cache is mainly for a single request URL, such as pages and page components (images, CSS, scripts, etc ). When the same URL is requested again, the browser cache may be used.

First, the browser will cache the content of the page to a specific location, but it is not self-claimed by the browser to use it. There is a negotiation process here, the coordination information is contained in the HTTP header information.

Last-modified negotiation

When a URL is requested for the first time, the dynamic program can easily add the last-modified information to the HTTP header information, while the static file will automatically add the last modification time to the HTTP header information, for example, last-modified: Wed, 13 Jun 2012 09:15:45 GMT;

If the browser finds this cache when requesting this URL again, it will add the format: If-modified-since: Wed, 13 Jun 2012 09:15:45 GMT to the request, the last modification time returned by the last request;

In this case, if it is a dynamic program, we can check whether there is any change, and then decide whether to output new content or 304. If it is a static file, the system will compare the time and return 200 or 304;

The browser receives a response. If it is 304, it uses the browser cache, and 200 uses the newly received data.

Etag negotiation

The request process is the same as last-modified, except that etag is used instead of last-modified.

In the first request, you can generate an etag value as the HTTP header: etag: "b09ce81c4549cd1: 155db". When you send this request again, the browser will add the following to the Request Header: if-None-Match: "b09ce81c4549cd1: 155d7"; then, you can compare this value to return 200 or 304.

 

Of course, the results of these two methods are the same.Determine how to use it based on different scenarios: For example, if we use a regular static file update policy to achieve static, some pages are often regenerated, that is, the last modification time is often changed, but the content may not change, in this way, the identifier generated by the content can be used to implement cache negotiation. If we have multiple servers, each request may be handled by different servers, the etag value generated based on the content is more suitable than the last modification time.

 

In addition, we can also setExpires and cache-control in the HTTP output HeaderFor example, cache-control: Max-age = 31536000, unit: s, to set the expiration time. It is the same URL next time. the browser will first check the cache-control: Max-age, which is the relative expiration time. If it does not exist, check the time set by expires, this is an absolute expiration time.

 

In fact, in most cases, it is unlikely that we can accurately determine the expiration time. There is a good way: Set a large expiration time. If the file changes, we can replace a new file name or add a new parameter, and let the browser request a new resource.

 

The server has the absolute ability to determine whether the browser uses cached files. Similarly, different browser refresh methods also determine whether the browser uses the cache: the input URL turns to use all cached functions, will use the expiration time settings; F5/refresh, this will not consider expires but will ask whether the server can use the cache; Ctrl + F5/force refresh, no cache, no inquiry, directly request a file.

4. Server Cache

Speaking of this cache, we must be separated from the previous dynamic content cache. In the above dynamic content cache, the server receives a URL request and submits it to our dynamic program, the program determines whether the request content is cached. If yes, it is added to the output html. While the server cache is, when the server receives a URL request, the server finds that this URL is available for caching, return directly, so that we will not go through the program code we carefully constructed.

5. Reverse Proxy Cache

This should actually be included in the server cache, but there is a reverse proxy concept here, which needs to be explained. The proxy server is clearly known to everyone. It is usually called "turning the Wall". For example, we cannot access website a, B can access website a, and we can access website B, we can use B as the proxy server, send the request for A to B, B to request a, then send the content returned by A to B, and B to return the content to us.

The reverse proxy server is the proxy of the server. We request a URL. After receiving this request, the reverse proxy server, the requests are distributed to the corresponding actual request processing server based on the request information. After the actual server is processed, it is returned to the proxy and then to the browser.

Such a proxy server is equivalent to a central node. Of course, some response content can be cached. When the same request exists, the content is directly returned to the browser without going to the backend server for actual processing.

 

Write operation cache (buffer)

All of the above is to reduce the use of server read resources and reduce the database read operations or the computing time of the server. In fact, we can use the cache to buffer some write operations: the book mentions an example. For example, we do not need timely data that is so important for page traffic statistics, you can cache the number of accesses in the cache. memcache is used in the book. Because memcache supports atomic operations, the database is updated only when the number of accesses reaches 1000 or your set value, this greatly reduces the resource consumption for instant updates and allows the database to perform more important transactions.

 

Distributed cache and Scaling

As the website grows, there may be separate cache servers. Taking memcache as an example, this is also our most common choice. More and more, when a cache server cannot support our daily operations, you may add another cache server, which can be divided by business: one responsible for user-related cache and the other used for item-related cache. More and more, when one server cannot be responsible for the user-related cache, may it be split by service? In short, we will inevitably see multiple servers used to cache similar data items, but which server should we put the cache on? Which server should we use to retrieve a cache entry?

The book mentions a solution that uses the basic hash algorithm: We perform MD5 calculation on the key value to obtain a 32-bit hash code, it is also a hexadecimal long integer, so that we can convert the first few digits to a long integer, and then take the remainder of the number of your cache server, to determine which server to cache the key.

In this way, if we add a cache server, we have to sacrifice it, clear the previous cache data, and redeploy the cache rules: of course, you may have to issue an announcement before clearing the website, pause the website service for a while, upgrade the website, and re-generate the cache or automatically generate the cache when the user accesses it.

 

Last

We cannot use the cache just to use the cache. How can we use the cache well, we should consider a series of issues such as the cache time, how to increase the cache hit rate, cache update policy, expiration Policy, and cache content monitoring.

 

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.