search
HomeBackend DevelopmentPHP Tutorialnginx cache system design principle

Here we use the cache system of nginx as a clue to discuss the design and related details of a cache server. I will try my best to analyze it from the perspective of design and framework. Due to space limitations, I will not go into the code here. Regarding the relevant details, everyone is welcome to join us. Participate in discussions.

After a cache server obtains a file from the backend, it is either sent directly to the client (scientific name is transparent transmission), or cached locally. When subsequent identical requests access the cache server, the local copy can be used directly. Yes, if it can be used. If a locally cached file is accessed by a subsequent request, it is called a hit in the cache. If there is no cache copy of the file locally, the cache server needs to go to the backend to obtain the file according to the configuration or resolve the domain name. This is called a cache miss, that is, a miss. For more knowledge about the cache server, we will discuss it in depth when analyzing the nginx cache system.

The storage system of nginx is divided into two categories. One is opened through proxy_store. The storage method is to store it locally according to the file path in the URL. For example, /file/2013/0001/en/test.html, then nginx will create each directory and file in the specified storage directory in sequence. The other type is opened through proxy_cache. The files stored in this way are not organized according to the URL path, but are managed using some special methods (called custom methods here). The custom methods are what we will focus on analysis. So what are the advantages of each of these two methods?

The method of storing files by URL path is relatively simple for the program to process, but the performance is not good. First of all, some URLs are very long. If we have to create such a deep directory on the local file system, opening and searching of files will be very slow (recall the process of searching for inodes through path names in the kernel). If you use a custom way to handle the pattern, although it is inseparable from files and paths, it will not increase complexity and reduce performance due to URL length. In a sense, this is a user-mode file system, and the most typical one is CFS in Squid. The method used by nginx is relatively simple, mainly relying on the md5 value of the URL for management, which we will analyze later.

Caching is inseparable from fetching content from the backend and then sending it to the client. It is easy for everyone to think of the specific processing method, which must be receiving and sending at the same time. Other methods are too inefficient, such as reading and then sending, etc. Let me mention here that nginx is receiving and sending at the same time. The structure used is ngx_event_pipe_t, which is the medium for communicating between the backend and the client. Since the structure is a general component, some special tags are needed to handle related functions involving storage, so the member cacheable takes on this important task.

p->cacheable = u->cacheable || u->store;

That is, if cacheable is 1, it needs to be stored, otherwise it will not be stored. So what do u->cacheable and u->store stand for? They respectively represent the two methods mentioned earlier, namely proxy_cache and proxy_store.

(To add some knowledge, when nginx fetches back-end data, its behavior is controlled by proxy_buffering, which is to enable response buffering for the back-end server. If buffering is enabled, nginx assumes that the proxy server can deliver the response very quickly, and will It is put into a buffer, and the relevant parameters can be set using proxy_buffer_size and proxy_buffers. If the response cannot fit into memory, it is written to the hard disk. If buffering is disabled, the response from the backend will be delivered to the client immediately.)


These are all side projects. We haven’t touched the core of nginx cache function yet. From an implementation point of view, there is a member called cache in the nginx upstream structure, and its type is ngx_shm_zone_t. If we enable the cache function, the cache member is used to manage shared memory (why is shared memory used?), and the member is NULL when stored in other ways. Another point that needs to be explained is that in the cache system, a file is usually called a store object, that is, a cache object, so you must create a store object before caching. An important question is how to choose the time to create it. What do you think about this? First we need to check whether a file needs to be cached. Obviously files requested by the GET method generally need to be cached, so in the early stage of request processing, if we see the GET method, we can create an object first. But many times, even a file requested by a GET method cannot be cached. If you create the object prematurely, you will not only waste time and space, but also destroy it in the end. So what affects the storage of GET requests? That is the Cache-control field in the response header. This field tells the proxy or browser whether the file can be cached. Generally, cache servers will cache requests by default when there is no Cache-control field in the response header.

Based on this consideration, the cache server we developed will only create cache objects after the response header is parsed and sufficient evidence of cacheability is obtained. Unfortunately, nginx does not do this.

nginx completes the creation of cache objects in the ngx_http_upstream_init_request function. At what stage of http processing is this function located? before establishing a connection with the backend. I personally think this place is not suitable. . . What do you think?

Regarding the creation process, you can read the function ngx_http_upstream_cache. Here I will analyze our cache by comparing it with nginx. Our request uses a member named store to establish contact with the cache object. The same goes for nginx, which has a cache member in its request structure to do the same thing. The difference is that the space corresponding to our store members is in shared memory, while nginx applies for it in r->pool (why do we do this?).

In the next step, nginx needs to generate the key of the cache object according to the configuration, which is generally calculated using md5. This key serves as the unique identifier of a cache object in the system. Many people may be worried about md5 collisions. I think this requirement is completely acceptable here if it is not particularly demanding, and the processing is relatively simple.

The next thing to deal with is, how should the files be stored on the disk?

Let’s take an example we used before: /file/2013/0001/en/test.html. Its corresponding md5 value is 8ef9229f02c5672c747dc7a324d658d0. In fact, nginx uses it as the file name. that's it? What happens if we find a directory to store files and there are a bunch of such files in it? We know that most file systems have restrictions on the number of files in a single directory, so such simple and crude processing is not possible. What to do? nginx allows you to use multi-level directories through configuration to solve this problem. To put it simply, nginx uses the levels directive to specify the number of directory levels (separated by colons) and the number of characters in each directory name. In our example, assume that the configuration levels=1:2 means that a two-level directory is used. The first-level directory name is one character, and the second-level directory name uses two characters. However, nginx supports up to 3 levels of directories, that is, levels=xxx:xxx:xxx.

So where do the characters that make up the directory name come from? Assume that our storage directory is /cache, levels=1:2, then the above file is stored like this:

/cache/0/8d/8ef9229f02c5672c747dc7a324d658d0

You see how the two directory names 0 and 8d came from, no need to explain.

After the object is created, you need to cache the object management structure, which is handled by ngx_http_file_cache_exists.

If the current directory and files already exist when creating this file, what should I do? You can go through the code and see how nginx handles it.

The discussion has come to an end. In fact, it is all preparatory work now. Next time we will discuss the processing of the arrival of back-end content.

Extended reading:

http://www.pagefault.info/?p=123

http://www.pagefault.info/?p=375

The above has introduced the design principles of nginx's cache system, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
解决方法:您的组织要求您更改 PIN 码解决方法:您的组织要求您更改 PIN 码Oct 04, 2023 pm 05:45 PM

“你的组织要求你更改PIN消息”将显示在登录屏幕上。当在使用基于组织的帐户设置的电脑上达到PIN过期限制时,就会发生这种情况,在该电脑上,他们可以控制个人设备。但是,如果您使用个人帐户设置了Windows,则理想情况下不应显示错误消息。虽然情况并非总是如此。大多数遇到错误的用户使用个人帐户报告。为什么我的组织要求我在Windows11上更改我的PIN?可能是您的帐户与组织相关联,您的主要方法应该是验证这一点。联系域管理员会有所帮助!此外,配置错误的本地策略设置或不正确的注册表项也可能导致错误。即

Windows 11 上调整窗口边框设置的方法:更改颜色和大小Windows 11 上调整窗口边框设置的方法:更改颜色和大小Sep 22, 2023 am 11:37 AM

Windows11将清新优雅的设计带到了最前沿;现代界面允许您个性化和更改最精细的细节,例如窗口边框。在本指南中,我们将讨论分步说明,以帮助您在Windows操作系统中创建反映您的风格的环境。如何更改窗口边框设置?按+打开“设置”应用。WindowsI转到个性化,然后单击颜色设置。颜色更改窗口边框设置窗口11“宽度=”643“高度=”500“>找到在标题栏和窗口边框上显示强调色选项,然后切换它旁边的开关。若要在“开始”菜单和任务栏上显示主题色,请打开“在开始”菜单和任务栏上显示主题

如何在 Windows 11 上更改标题栏颜色?如何在 Windows 11 上更改标题栏颜色?Sep 14, 2023 pm 03:33 PM

默认情况下,Windows11上的标题栏颜色取决于您选择的深色/浅色主题。但是,您可以将其更改为所需的任何颜色。在本指南中,我们将讨论三种方法的分步说明,以更改它并个性化您的桌面体验,使其具有视觉吸引力。是否可以更改活动和非活动窗口的标题栏颜色?是的,您可以使用“设置”应用更改活动窗口的标题栏颜色,也可以使用注册表编辑器更改非活动窗口的标题栏颜色。若要了解这些步骤,请转到下一部分。如何在Windows11中更改标题栏的颜色?1.使用“设置”应用按+打开设置窗口。WindowsI前往“个性化”,然

OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题OOBELANGUAGE错误Windows 11 / 10修复中出现问题的问题Jul 16, 2023 pm 03:29 PM

您是否在Windows安装程序页面上看到“出现问题”以及“OOBELANGUAGE”语句?Windows的安装有时会因此类错误而停止。OOBE表示开箱即用的体验。正如错误提示所表示的那样,这是与OOBE语言选择相关的问题。没有什么可担心的,你可以通过OOBE屏幕本身的漂亮注册表编辑来解决这个问题。快速修复–1.单击OOBE应用底部的“重试”按钮。这将继续进行该过程,而不会再打嗝。2.使用电源按钮强制关闭系统。系统重新启动后,OOBE应继续。3.断开系统与互联网的连接。在脱机模式下完成OOBE的所

Windows 11 上启用或禁用任务栏缩略图预览的方法Windows 11 上启用或禁用任务栏缩略图预览的方法Sep 15, 2023 pm 03:57 PM

任务栏缩略图可能很有趣,但它们也可能分散注意力或烦人。考虑到您将鼠标悬停在该区域的频率,您可能无意中关闭了重要窗口几次。另一个缺点是它使用更多的系统资源,因此,如果您一直在寻找一种提高资源效率的方法,我们将向您展示如何禁用它。不过,如果您的硬件规格可以处理它并且您喜欢预览版,则可以启用它。如何在Windows11中启用任务栏缩略图预览?1.使用“设置”应用点击键并单击设置。Windows单击系统,然后选择关于。点击高级系统设置。导航到“高级”选项卡,然后选择“性能”下的“设置”。在“视觉效果”选

Windows 11 上的显示缩放比例调整指南Windows 11 上的显示缩放比例调整指南Sep 19, 2023 pm 06:45 PM

在Windows11上的显示缩放方面,我们都有不同的偏好。有些人喜欢大图标,有些人喜欢小图标。但是,我们都同意拥有正确的缩放比例很重要。字体缩放不良或图像过度缩放可能是工作时真正的生产力杀手,因此您需要知道如何对其进行自定义以充分利用系统功能。自定义缩放的优点:对于难以阅读屏幕上的文本的人来说,这是一个有用的功能。它可以帮助您一次在屏幕上查看更多内容。您可以创建仅适用于某些监视器和应用程序的自定义扩展配置文件。可以帮助提高低端硬件的性能。它使您可以更好地控制屏幕上的内容。如何在Windows11

10种在 Windows 11 上调整亮度的方法10种在 Windows 11 上调整亮度的方法Dec 18, 2023 pm 02:21 PM

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

如何在Safari中关闭iPhone的隐私浏览身份验证?如何在Safari中关闭iPhone的隐私浏览身份验证?Nov 29, 2023 pm 11:21 PM

在iOS17中,Apple为其移动操作系统引入了几项新的隐私和安全功能,其中之一是能够要求对Safari中的隐私浏览选项卡进行二次身份验证。以下是它的工作原理以及如何将其关闭。在运行iOS17或iPadOS17的iPhone或iPad上,如果您在Safari浏览器中打开了任何“无痕浏览”标签页,然后退出会话或App,Apple的浏览器现在需要面容ID/触控ID认证或密码才能再次访问它们。换句话说,如果有人在解锁您的iPhone或iPad时拿到了它,他们仍然无法在不知道您的密码的情况下查看您的隐私

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft