<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Chill Vic’s Architecture Notes]]></title><description><![CDATA[I’m Chill Vic, on the path to systems architecture, using simple, straightforward language to explain complex systems — from databases and system design to operating systems and algorithms.]]></description><link>https://www.chillvic.dev</link><image><url>https://substackcdn.com/image/fetch/$s_!4-yR!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddabf71b-360a-4310-8311-8b9a30fb05e3_722x722.png</url><title>Chill Vic’s Architecture Notes</title><link>https://www.chillvic.dev</link></image><generator>Substack</generator><lastBuildDate>Mon, 06 Apr 2026 06:08:50 GMT</lastBuildDate><atom:link href="https://www.chillvic.dev/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Chill Vic]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[chillvic@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[chillvic@substack.com]]></itunes:email><itunes:name><![CDATA[Chill Vic]]></itunes:name></itunes:owner><itunes:author><![CDATA[Chill Vic]]></itunes:author><googleplay:owner><![CDATA[chillvic@substack.com]]></googleplay:owner><googleplay:email><![CDATA[chillvic@substack.com]]></googleplay:email><googleplay:author><![CDATA[Chill Vic]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[How to Build a High-Performance TCP Server Ep.2 – Is io_uring Always Faster Than epoll? A Deep Dive into System Internals.]]></title><description><![CDATA[From syscall overhead to shared memory queues: understanding the next evolution of Linux I/O]]></description><link>https://www.chillvic.dev/p/how-to-build-a-high-performance-tcp-0c1</link><guid isPermaLink="false">https://www.chillvic.dev/p/how-to-build-a-high-performance-tcp-0c1</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Fri, 27 Mar 2026 05:20:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!-qnL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a href="https://chillvic.substack.com/p/tcp-server-ep1-server">As mentioned in the previous episode</a>, Nginx, Redis, and Rust&#8217;s Tokio all utilize non-blocking I/O combined with <code>epoll</code> to develop high-efficiency TCP servers. However, <code>epoll</code> has its drawbacks: when <code>epoll_wait</code> returns a ready connection, you still need to perform additional <code>read</code> or <code>write</code> system calls to move data. With high concurrency, the context switching between user space and kernel space significantly impacts performance.</p><p><code>io_uring</code> is the kernel mechanism designed to solve this. <strong>But how do we read and write data without executing </strong><code>read</code><strong> &amp; </strong><code>write</code><strong> system calls?</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-qnL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-qnL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-qnL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>How to read/write without read &amp; write system calls?</h1><p>When using <code>io_uring</code>, the kernel establishes two queues:</p><ol><li><p><strong>Submission Queue (SQ)</strong>: The user-space process places I/O operation requests (e.g., read, write) into this queue for the kernel to consume.</p></li><li><p><strong>Completion Queue (CQ)</strong>: The kernel places I/O completion notifications into this queue for the user-space process to consume.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cRjA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cRjA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 424w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 848w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1272w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png" width="599" height="373" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:373,&quot;width&quot;:599,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;io_uring &#129425; Explained - Unzip.dev&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="io_uring &#129425; Explained - Unzip.dev" title="io_uring &#129425; Explained - Unzip.dev" srcset="https://substackcdn.com/image/fetch/$s_!cRjA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 424w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 848w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1272w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Because of this, even blocking I/O becomes asynchronous with <code>io_uring</code>. Operations are encapsulated into entries placed in the SQ, and results are retrieved by consuming CQ entries. An entry contains these primary fields:</p><ul><li><p><strong>fd</strong>: The I/O object (e.g., TCP connection) ID in the kernel.</p></li><li><p><strong>opcode</strong>: An enum for the entry type (e.g., read, write, or accept).</p></li><li><p><strong>data pointer</strong>: A memory pointer where the kernel reads/writes data. Usually, a byte array is initialized in user space and its pointer is passed.</p></li><li><p><strong>user_data</strong>: An identifier for the entry.</p></li></ul><p>When a user-space process receives a &#8220;read complete&#8221; entry from the CQ, it needs to know which byte array to read from. Thus, the <code>user_data</code> in the CQ entry matches the SQ entry. For example, you can cast the pointer of the byte array into an <code>int</code> and store it in <code>user_data</code>:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">// Create SQE (SQ entry)
struct request *req = malloc(sizeof(*req));                                          
sqe-&gt;opcode    = IORING_OP_READ;              
sqe-&gt;fd        = req-&gt;fd;                                                             
sqe-&gt;addr      = (uint64_t)req-&gt;buf;
sqe-&gt;user_data = (uint64_t)req;

// Receive CQE (CQ entry), use user_data to restore the request buffer
struct request *req = (struct request *)cqe-&gt;user_data;</code></pre></div><h1>Does sending entries to SQ &amp; CQ require system calls?</h1><p>While SQ and CQ are kernel objects, the kernel uses <code>mmap</code> to map them into user space, <strong>allowing user-space processes to access them directly without going through system calls.</strong> To explain <code>mmap</code>, we must first mention <strong>Virtual Memory Space</strong>:</p><h3><strong>Virtual Memory Space</strong> </h3><p>Every user-space process has its own <strong>page table</strong> that records accessible memory addresses (e.g., 0x000 ~ 0x3FF represents 1KB). However, these page table addresses are not physical memory locations. The kernel uses a mapping mechanism to map virtual memory addresses to physical ones&#8212;this is <strong>Virtual Memory Mapping</strong> technology.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!btKx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!btKx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 424w, https://substackcdn.com/image/fetch/$s_!btKx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 848w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg" width="715" height="488" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:488,&quot;width&quot;:715,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;How does the virtual memory map into the RAM?&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="How does the virtual memory map into the RAM?" title="How does the virtual memory map into the RAM?" srcset="https://substackcdn.com/image/fetch/$s_!btKx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 424w, https://substackcdn.com/image/fetch/$s_!btKx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 848w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Page Fault &amp; mmap</strong> </h3><p>The benefit of using Virtual Memory is that when a process executes <code>malloc(1G)</code>, it doesn&#8217;t need 1GB of RAM immediately. Instead, it simply records in the page table that the range 0x00000000 ~ 0x3FFFFFFF is accessible. When the CPU reads a virtual memory address and finds no corresponding physical memory, it triggers a <strong>Page Fault</strong>. Subsequently, the kernel allocates resources and establishes a link based on the actual size used (the minimum unit being 4KB)&#8212;essentially a <strong>lazy load</strong> technique.</p><p>The <code>mmap</code> technique allows a Page Fault to read data from a specified physical memory and establish a link. For instance, by mapping the process&#8217;s virtual memory space 0x000 ~ 0x3FF to the <code>io_uring</code> queue in kernel memory, a CPU read of 0x000 ~ 0x3FF that triggers a Page Fault will directly read the physical memory location of the kernel <code>io_uring</code> queue, rather than being allocated random blank physical memory. </p><p>Consequently, the process can directly access the kernel <code>io_uring</code> queue and send or read entries without through system calls.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Chill Vic&#8217;s Architecture Notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1>Is there overhead in using queues?</h1><p>Using a doubly linked list as a queue requires locking to handle concurrent push and pop operations safely. <code>io_uring</code> uses a <strong>Ring Buffer</strong> with a fixed-size array and two atomic indices (head and tail) to achieve <strong>lock-less</strong> operations:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!As7t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!As7t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 424w, https://substackcdn.com/image/fetch/$s_!As7t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 848w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg" width="512" height="418" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:418,&quot;width&quot;:512,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Back To Basics: The Ring Buffer &#8211; ShaneKirk.com&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Back To Basics: The Ring Buffer &#8211; ShaneKirk.com" title="Back To Basics: The Ring Buffer &#8211; ShaneKirk.com" srcset="https://substackcdn.com/image/fetch/$s_!As7t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 424w, https://substackcdn.com/image/fetch/$s_!As7t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 848w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Example Implementation (Rust):</strong></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use std::sync::atomic::{AtomicU64, Ordering};                                         
const SIZE: usize = 8; // Must be a power of 2

struct RingBuffer {                                                                                         
    buf: [i64; SIZE],                                                                                      
    head: AtomicU64, // Consumer index                                                                         
    tail: AtomicU64, // Producer index
}                                                                                                           
                                                                                                             
impl RingBuffer {
    fn new() -&gt; Self {
        RingBuffer {                                                                                        
            buf: [0; SIZE],
            head: AtomicU64::new(0),                                                                        
            tail: AtomicU64::new(0),                                                                       
        }
    }

    // Producer side                                                                                          
    fn push(&amp;mut self, val: i64) -&gt; bool {
        let tail = self.tail.load(Ordering::Acquire);                                                       
        let head = self.head.load(Ordering::Acquire);                                                      
                                                                                                            
        if tail - head == SIZE as u64 {
            return false; // Full                                                                           
        }                                                                                                  

        self.buf[(tail &amp; (SIZE as u64 - 1)) as usize] = val;                                                
        self.tail.store(tail + 1, Ordering::Release);
        true                                                                                                
    }                                                                                                      

    // Consumer side
    fn pop(&amp;mut self) -&gt; Option&lt;i64&gt; {
        let head = self.head.load(Ordering::Acquire);
        let tail = self.tail.load(Ordering::Acquire);                                                       
 
        if head == tail {                                                                                   
            return None; // Empty                                                                           
        }

        let val = self.buf[(head &amp; (SIZE as u64 - 1)) as usize];                                            
        self.head.store(head + 1, Ordering::Release);
        Some(val)                                                                                           
    }                                                                                                      
}</code></pre></div><p>Furthermore, arrays are contiguous. For the CPU, a single cache access can fetch the entire structure. Conversely, linked list nodes might be scattered, causing multiple cache misses.</p><h1>Besides queue optimization, what other performance boosts does io_uring offer?</h1><p>Although <code>read</code> &amp; <code>write</code> system calls are gone, <code>io_uring</code> still needs other calls, like <code>io_uring_enter</code> to notify the kernel to process tasks. However, this is still more efficient than <code>epoll</code>:</p><ul><li><p><strong>epoll</strong>: <code>epoll_wait</code> + <code>read</code>/<code>write</code> system calls.</p></li><li><p><strong>io_uring</strong>: <code>io_uring_enter</code> triggers the kernel and waits for CQ entries.</p></li></ul><h3><strong>SQPOLL Mode for Zero I/O System Calls</strong></h3><p>To achieve zero I/O system calls, <code>io_uring</code> supports the <strong>SQPOLL</strong> mode. In this mode, the kernel uses an additional kernel thread to continuously poll the SQ. The user-space process does not need to execute <code>io_uring_enter</code>; it only needs to continuously poll the CQ. The downside is that if there is no data in the CQ, the process will perform many "useless" pulls. Therefore, SQPOLL is suitable for high-intensity, low-latency request scenarios.</p><h3><strong>IOPOLL Mode for Hardware Interrupt Optimization</strong></h3><p>The second optimization concerns hardware interrupts. io_uring still relies on hardware interrupts to signal completion, after which the kernel places the I/O results into the CQ. </p><p>However, hardware interrupts are not free. When the CPU receives an interrupt, it must first execute the interrupt handler. In addition to pausing the current instruction, it also flushes subsequent instructions in the pipeline. After handling the interrupt, execution resumes from the interrupted instruction address, and the following instructions are decoded and refilled into the pipeline.<br><br>If a large number of interrupts arrive in a short period, the CPU repeatedly flushing its pipeline becomes costly. To mitigate this, the kernel uses NAPI to handle network packet interrupts. After receiving the first interrupt, it switches to a polling mode, draining packets from the NIC in batches to avoid a burst of interrupts triggered by incoming traffic.</p><p>However, NAPI is primarily designed for network packet processing. When io_uring is used for file I/O (e.g., databases), a surge of read operations can still generate a large number of interrupts. To address this, io_uring provides the IO_POLL mode, which bypasses interrupts entirely and continuously polls the device for completion events.</p><h1>Real-world Application: PostgreSQL 18</h1><p>Databases perform frequent File I/O. PostgreSQL 18 introduced an async I/O mechanism with two modes:</p><ul><li><p><strong>worker (default)</strong>: Delegates <code>read()</code>/<code>write()</code> to another process. Boosts read-heavy query performance by ~1.5x.</p></li><li><p><strong>io_uring</strong>: Uses <code>io_uring</code> for I/O. Boosts read-heavy performance by <strong>~2.7x</strong>.</p></li></ul><p><em>Note: Since </em><code>io_uring</code><em> is asynchronous, PG warns that </em><code>EXPLAIN ANALYZE</code><em> for I/O timing may become less accurate.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YXgp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YXgp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 424w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 848w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1272w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png" width="1400" height="714" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:714,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium" title="PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium" srcset="https://substackcdn.com/image/fetch/$s_!YXgp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 424w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 848w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1272w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>ref: https://pganalyze.com/blog/postgres-18-async-io</p><h1>epoll vs. io_uring benchmark - monoio vs. tokio</h1><p>In Rust, the <code>Future</code> abstraction allows swapping runtimes easily. You can refer to the <a href="https://chillvic.substack.com/p/tcp-server-ep1-server">previous article</a> for an introduction to the <code>Future</code> abstraction. <code>monoio</code> is built on <code>io_uring</code>, and its TCP echo server code looks nearly identical to <code>tokio</code>:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use monoio::{
    io::{AsyncReadRent, AsyncWriteRentExt},
    net::{TcpListener, TcpStream},
};

#[monoio::main(driver = "fusion")]
async fn main() {
    // tracing_subscriber::fmt().with_max_level(tracing::Level::TRACE).init();
    let listener = TcpListener::bind("127.0.0.1:50002").unwrap();
    println!("listening");
    loop {
        let incoming = listener.accept().await;
        match incoming {
            Ok((stream, addr)) =&gt; {
                println!("accepted a connection from {addr}");
                monoio::spawn(echo(stream));
            }
            Err(e) =&gt; {
                println!("accepted connection failed: {e}");
                return;
            }
        }
    }
}

async fn echo(mut stream: TcpStream) -&gt; std::io::Result&lt;()&gt; {
    let mut buf: Vec&lt;u8&gt; = Vec::with_capacity(8 * 1024);
    let mut res;
    loop {
        // read
        (res, buf) = stream.read(buf).await;
        if res? == 0 {
            return Ok(());
        }

        // write all
        (res, buf) = stream.write_all(buf).await;
        res?;

        // clear
        buf.clear();
    }
}
</code></pre></div><p>To evaluate the performance of io_uring and epoll, I built a project called <em><a href="https://github.com/vx416/play_iouring">play_iouring</a></em>, using the monoio and tokio libraries for benchmarking:</p><ul><li><p><strong>TCP echo server</strong>: tested with 10, 100, and 1000 concurrent TCP clients</p></li><li><p><strong>File I/O</strong>: concurrent read/write on 100 files, each with 64 KB per operation</p></li></ul><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">### File I/O (100 files, 64 KiB each)

| Benchmark | monoio | tokio | &#916; |

|---------------|------------|------------|----------------|
| Write (100 files) | ~1.32 ms | ~1.95 ms | monoio ~1.5x faster |
| Read (100 files) | ~693 &#181;s | ~1.87 ms | monoio ~2.7x faster |

  
### TCP Echo

| Benchmark | monoio | tokio | &#916; |
|--------------------|------------|------------|----------|
| 10 clients | ~4.12 ms | ~3.98 ms | ~equal |
| 100 clients | ~26.6 ms | ~27.1 ms | ~equal |
| 1000 clients | ~292 ms | ~330 ms | monoio slightly faster |</code></pre></div><p>File I/O shows a significant improvement. The reason is that Tokio does not use epoll to manage file I/O; instead, it relies on <a href="https://docs.rs/tokio/latest/tokio/fs/index.html">worker threads with blocking I/O</a>. In contrast, Monoio uses io_uring for file operations, which substantially reduces system call overhead. Therefore, file I/O is not a valid comparison between epoll and io_uring.</p><p>For the TCP echo workload, performance is nearly identical. The primary advantage of io_uring lies in reducing system calls, but in the lifecycle of packet read/write, system calls are not the dominant cost. The bottleneck is the kernel&#8217;s network stack (e.g., packet parsing, firewall rules, etc.). With only 1000 concurrent clients, the system call overhead is not significant enough to create a noticeable difference.</p><p>Moreover, under single-machine testing with a large number of concurrent clients, limited CPU cores can skew the results. Therefore, this benchmark mainly demonstrates that epoll and io_uring deliver comparable performance under typical conditions.</p>]]></content:encoded></item><item><title><![CDATA[如何打造高效 TCP Server Ep.2 - io_uring：為什麼 epoll 還不夠快？]]></title><description><![CDATA[&#24478; syscall &#38283;&#37559;&#21040; shared memory queue&#65292;&#29702;&#35299; Linux I/O &#30340;&#19979;&#19968;&#20491;&#28436;&#36914;]]></description><link>https://www.chillvic.dev/p/tcp-server-ep2-io_uring-epoll</link><guid isPermaLink="false">https://www.chillvic.dev/p/tcp-server-ep2-io_uring-epoll</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Thu, 26 Mar 2026 14:45:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!-qnL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a href="https://chillvic.substack.com/p/tcp-server-ep1-server">&#19978;&#38598;&#25552;&#21040;</a>&#65292;Nginx, Redis &amp; Rust tokio &#37117;&#29992; non-blocking I/O &#32080;&#21512; epoll &#38283;&#30332;&#39640;&#25928;&#30340; tcp server (e.g cache server or http server)&#65292;&#20294; epoll &#26377;&#20854;&#32570;&#40670;&#65292;&#30070; epoll_wait &#22238;&#20659;&#21487;&#35712;&#23531;&#30340;&#36899;&#32218;&#26178;&#65292;&#35712;&#23531;&#36039;&#26009;&#20173;&#35201;&#38989;&#22806;&#21628;&#21483; read or write system call&#65292;&#36899;&#32218;&#22810;&#25110;&#35712;&#23531;&#38971;&#32321;&#26178;&#65292;&#36889;&#20123; system call &#22312; user space &#33287; kernel space &#20043;&#38291;&#20999;&#25563;&#30340;&#26178;&#38291;&#26371;&#39023;&#33879;&#24433;&#38911;&#25928;&#33021;&#12290;<br><br>&#32780; io_uring &#23601;&#26159;&#35299;&#27770; epoll &#36889;&#20491;&#21839;&#38988;&#30340;&#21478;&#19968;&#20491; kernel &#27231;&#21046;&#65292;<strong>&#20294;&#19981;&#22519;&#34892; read &amp; write system call &#35201;&#22914;&#20309;&#35712;&#23531;&#36039;&#26009;&#65311;</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-qnL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-qnL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3120212,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/192211419?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-qnL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!-qnL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F974a7560-ce34-4fd0-a6cb-bc88e524e8e4_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#19981;&#22519;&#34892; read &amp; write system call &#35201;&#22914;&#20309;&#35712;&#23531;&#36039;&#26009;&#65311;</h1><p>&#20351;&#29992; io_uring &#26178;&#65292;kernel &#26371;&#24314;&#31435;&#20841;&#20491; queue&#65306;</p><ul><li><p><strong>Submission Queue (SQ) :</strong> user space process &#23559; I/O &#25805;&#20316; (e.g read, write) &#35531;&#27714;&#25918;&#36914;&#35442; queue &#30001; kernel process &#28040;&#36027;</p></li><li><p><strong>Completion Queue (CQ)&#65306;</strong>kernel process &#23559; I/O &#23436;&#25104;&#36890;&#30693;&#25918;&#20837;&#35442; queue &#30001; user space process &#28040;&#36027;</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cRjA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cRjA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 424w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 848w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1272w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png" width="599" height="373" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:373,&quot;width&quot;:599,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;io_uring &#129425; Explained - Unzip.dev&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="io_uring &#129425; Explained - Unzip.dev" title="io_uring &#129425; Explained - Unzip.dev" srcset="https://substackcdn.com/image/fetch/$s_!cRjA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 424w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 848w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1272w, https://substackcdn.com/image/fetch/$s_!cRjA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa991e713-48d5-4af9-adbc-c28121e50d1d_599x373.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#22240;&#27492;&#21363;&#20415;&#26159; blocking I/O &#29992; io_uring &#35712;&#23531;&#20173;&#26159;&#38750;&#21516;&#27493;&#30340;&#65292;&#35712;&#23531;&#25805;&#20316;&#26371;&#34987;&#23553;&#35037;&#25104; entry &#25918;&#36914; SQ &#20013;&#65292;&#36879;&#36942;&#28040;&#36027; CQ entry &#25343;&#21040;&#35712;&#23531;&#32080;&#26524;&#65292;&#35442; entry &#32080;&#27083;&#26377;&#20197;&#19979;&#20027;&#35201; field :</p><ul><li><p>fd (file descriptor) :  I/O object (e.g tpc &#36899;&#32218;) &#22312; kernel &#20013;&#30340; id</p></li><li><p>opcode : &#19981;&#21516; entry &#39006;&#22411;&#30340; enum (e.g read, write or accept)</p></li><li><p>data pointer : kernel &#35712;&#23531;&#36039;&#26009;&#30340;&#35352;&#25014;&#39636;&#25351;&#27161;&#65292;&#36890;&#24120;&#26371;&#22312; user space &#21021;&#22987;&#21270; bytes array &#20006;&#20659;&#35442;&#38499;&#21015;&#25351;&#27161;</p></li><li><p>user_data : &#35442; entry &#30340;&#35672;&#21029;&#34399;</p></li></ul><p>user space process  &#24478; CQ &#25910;&#21040;&#19968;&#20491; read &#23436;&#25104;&#30340; entry &#26178;&#65292;&#38656;&#30693;&#36947;&#21435;&#21738;&#20491; bytes array &#35712;&#36039;&#26009;&#65292;&#22240;&#27492; CQ entry &#20013;&#30340; user_data &#26371;&#36319; SQ entry &#21305;&#37197;&#65292;&#33021;&#24478; user_data &#35672;&#21029;&#34399;&#20013;&#25214;&#21040;&#23565;&#25033;&#30340; bytes array&#65292;&#20363;&#22914;&#25226; bytes array &#30340;&#25351;&#27161;&#35722;&#25104; int &#25918;&#20837; user_data &#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;c&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-c"># &#24314;&#31435; SQE (SQ entry)
struct request *req = malloc(sizeof(*req));                                          
sqe-&gt;opcode    = IORING_OP_READ;              
sqe-&gt;fd        = req-&gt;fd;                                                             
sqe-&gt;addr      = (uint64_t)req-&gt;buf;
sqe-&gt;user_data = (uint64_t)req;

# &#25910;&#21040; CQE (CQ entry)&#65292;&#30452;&#25509;&#29992; user_data &#36996;&#21407; request bytes array &#36039;&#26009;
struct request *req = (struct request *)cqe-&gt;user_data;  </code></pre></div><h1>&#24448; SQ &amp; CQ &#36865; entry &#19981;&#29992; system call &#21966;&#65311;</h1><p>&#38614;&#28982; SQ &amp; CQ &#26159; kernel &#35041;&#30340;&#29289;&#20214;&#65292;&#20294; kernel &#36879;&#36942; mmap system call &#23559; kernel &#30340;&#35352;&#25014;&#39636;&#29289;&#20214;&#33287; user space &#30340;&#35352;&#25014;&#39636;&#29289;&#20214;&#24314;&#31435;&#38364;&#32879;&#65292;&#35201;&#31777;&#21934;&#35498;&#26126; mmap &#25216;&#34899;&#65292;&#39318;&#20808;&#35201;&#25552;&#21040; <strong>virtual memory space</strong> &#27010;&#24565;&#65306;</p><h3>virtual memory space</h3><p>&#27599;&#20491; user space process &#26371;&#26377;&#21508;&#33258;&#30340; page table &#32000;&#37636;&#21487;&#23384;&#21462;&#30340;&#35352;&#25014;&#39636;&#20301;&#32622;&#65292;&#20363;&#22914; 0x000 ~ 0x3FF &#20195;&#34920; 1KB&#65292;&#20294; page table &#35352;&#25014;&#39636;&#20301;&#32622;&#19981;&#26159;&#23526;&#39636;&#35352;&#25014;&#39636;&#20301;&#32622;&#65292;kernel &#26371;&#38989;&#22806;&#36879;&#36942; mapping &#27231;&#21046;&#65292;&#23559; page table &#30340;&#34395;&#25836;&#35352;&#25014;&#39636;&#20301;&#32622;&#26144;&#23556;&#25104;&#23526;&#39636;&#35352;&#25014;&#39636;&#20301;&#32622;&#65292;&#20063;&#23601;&#26159; Virtual Memory Mapping &#25216;&#34899;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!btKx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!btKx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 424w, https://substackcdn.com/image/fetch/$s_!btKx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 848w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg" width="715" height="488" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:488,&quot;width&quot;:715,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;How does the virtual memory map into the RAM?&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="How does the virtual memory map into the RAM?" title="How does the virtual memory map into the RAM?" srcset="https://substackcdn.com/image/fetch/$s_!btKx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 424w, https://substackcdn.com/image/fetch/$s_!btKx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 848w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!btKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F816d5d0d-527b-452e-98a8-4b12317334ee_715x488.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Page Fault &amp; mmap &#25216;&#34899;</h3><p>&#20351;&#29992; Virtual Memory &#22909;&#34389;&#26159;&#65292;&#30070; process &#22519;&#34892; malloc(1G) &#26178;&#65292;&#19981;&#29992;&#39340;&#19978;&#32102; 1G &#30340; RAM&#65292;&#32780;&#26159;&#20808;&#22312; page table &#32000;&#37636; 0x00000000 ~ 0x3FFFFFFF &#31684;&#22285;&#37117;&#21487;&#23384;&#21462;&#65292;&#30070; CPU &#35712;&#21462; virtual memory &#30340;&#35352;&#25014;&#39636;&#20301;&#32622;&#65292;&#30332;&#29694;&#27794;&#26377;&#23565;&#25033;&#30340;&#23526;&#39636;&#35352;&#25014;&#39636;&#26178;&#26371;&#35320;&#30332; page fault&#65292;&#38568;&#24460;&#20381;&#29031;&#23526;&#38555;&#29992;&#21040;&#30340;&#22823;&#23567; (&#26368;&#23567;&#21934;&#20301;&#28858; 4Kb ) &#21435;&#21521;&#23526;&#39636;&#35352;&#25014;&#39636;&#25343;&#36039;&#28304;&#20006;&#24314;&#31435;&#38364;&#32879;&#65292;&#30456;&#30070;&#26044; lazy load &#25216;&#34899;&#12290;</p><p>&#32780; mmap &#25216;&#34899;&#22312; page fault &#26178;&#21487;&#24478;&#25351;&#23450;&#30340;&#23526;&#39636;&#35352;&#25014;&#39636;&#35712;&#36039;&#26009;&#20006;&#24314;&#31435;&#38364;&#32879;&#65292;&#20363;&#22914;&#23559; process &#30340; virtual memory space 0x000 ~ 0x3FF &#33287; kernel &#35352;&#25014;&#39636;&#20013; io_uring &#30340; queue &#24314;&#31435;&#38364;&#32879;&#65292;CPU &#35712;&#21462; 0x000 ~ 0x3FF &#35320;&#30332; page fault &#26371;&#30452;&#25509;&#35712; kernel io_uring queue &#30340;&#23526;&#39636;&#35352;&#25014;&#39636;&#20301;&#32622;&#65292;&#32780;&#19981;&#26159;&#38568;&#27231;&#20998;&#37197;&#31354;&#30333;&#30340;&#23526;&#39636;&#35352;&#25014;&#39636;&#65292;&#22240;&#27492;&#35442; process &#21487;&#30452;&#25509;&#23384;&#21462;&#21040; kernel io_uring &#30340; queue&#65292;&#19981;&#38656;&#36879;&#36942; system call &#23601;&#33021;&#24448; queue &#36865; entry &#25110;&#35712; entry&#12290;</p><h1>&#20351;&#29992; queue &#19981;&#26371;&#26377;&#38989;&#22806;&#30340;&#25928;&#33021;&#28040;&#32791;&#21966;&#65311;</h1><p>&#20351;&#29992; double linked list &#32080;&#27083;&#30340; queue &#21516;&#26178; push &amp; pop &#38656;&#35201;&#19978;&#37782;&#65292;&#32780; io_uring &#20351;&#29992; ring buffer &#36879;&#36942;&#22266;&#23450;&#22823;&#23567;&#38499;&#21015;&#21152;&#20841;&#20491; atomic value &#30340;&#35712;&#23531; index &#23526;&#29694;&#28961;&#37782;&#30340; push &amp; pop : </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!As7t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!As7t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 424w, https://substackcdn.com/image/fetch/$s_!As7t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 848w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg" width="512" height="418" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:418,&quot;width&quot;:512,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Back To Basics: The Ring Buffer &#8211; ShaneKirk.com&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Back To Basics: The Ring Buffer &#8211; ShaneKirk.com" title="Back To Basics: The Ring Buffer &#8211; ShaneKirk.com" srcset="https://substackcdn.com/image/fetch/$s_!As7t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 424w, https://substackcdn.com/image/fetch/$s_!As7t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 848w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!As7t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F996f0ae0-3c77-490d-9991-9e027c8259d2_512x418.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#31684;&#20363;&#31243;&#24335;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use std::sync::atomic::{AtomicU64, Ordering};                                         
const SIZE: usize = 8; // &#24517;&#38920;&#26159; 2 &#30340;&#20906;                                                 

struct RingBuffer {                                                                                         
      buf: [i64; SIZE],                                                                                      
      head: AtomicU64, // consumer &#23531;                                                                         
      tail: AtomicU64, // producer &#23531;
  }                                                                                                           
                                                                                                             
  impl RingBuffer {
      fn new() -&gt; Self {
          RingBuffer {                                                                                        
              buf: [0; SIZE],
              head: AtomicU64::new(0),                                                                        
              tail: AtomicU64::new(0),                                                                       
          }
      }

      // Producer &#31471;                                                                                          
      fn push(&amp;mut self, val: i64) -&gt; bool {
          let tail = self.tail.load(Ordering::Acquire);                                                       
          let head = self.head.load(Ordering::Acquire);                                                      
                                                                                                              
          if tail - head == SIZE as u64 {
              return false; // &#28415;&#20102;                                                                           
          }                                                                                                  

          self.buf[(tail &amp; (SIZE as u64 - 1)) as usize] = val;                                                
          self.tail.store(tail + 1, Ordering::Release);
          true                                                                                                
      }                                                                                                      

      // Consumer &#31471;
      fn pop(&amp;mut self) -&gt; Option&lt;i64&gt; {
          let head = self.head.load(Ordering::Acquire);
          let tail = self.tail.load(Ordering::Acquire);                                                       
   
          if head == tail {                                                                                   
              return None; // &#31354;&#30340;                                                                           
          }

          let val = self.buf[(head &amp; (SIZE as u64 - 1)) as usize];                                            
          self.head.store(head + 1, Ordering::Release);
          Some(val)                                                                                           
      }                                                                                                      
  }</code></pre></div><p>&#19988;&#38499;&#21015;&#26159;&#36899;&#32396;&#35352;&#25014;&#39636;&#31354;&#38291;&#65292;&#23565;&#26044; CPU &#20358;&#35498;&#33021;&#36879;&#36942;&#19968;&#27425; cache &#23384;&#21462;&#25343;&#21040;&#23436;&#25972;&#38499;&#21015;&#36039;&#26009;&#65292;&#30456;&#21453;&#22320; linked list &#31680;&#40670;&#21487;&#33021;&#20998;&#25955;&#22312;&#19981;&#36899;&#32396;&#35352;&#25014;&#39636;&#31354;&#38291;&#65292;CPU &#21487;&#33021;&#26371;&#26377;&#22810;&#27425; cache &#23384;&#21462;&#12290;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Chill Vic&#8217;s Architecture Notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1>&#38500;&#20102; queue &#20778;&#21270;&#65292;io_uring &#36996;&#26377;&#21738;&#20123;&#25928;&#33021;&#20778;&#21270;&#65311;</h1><p>&#38614;&#28982;&#27794;&#26377; read &#65286; write system call &#65292;&#20294; io_uring &#20173;&#38656;&#35201;&#20854;&#20182; I/O system call&#65292;&#20363;&#22914;&#25552;&#20132;&#23436;&#25152;&#26377;&#35531;&#27714;&#21040; SQ &#24460;&#65292;&#38656;&#35201;&#22519;&#34892; io_uring_enter system call &#21578;&#30693; kernel &#21487;&#24478; queue &#20013;&#34389;&#29702;&#20219;&#21209;&#65292;&#35442; system call &#26371; blocking &#30452;&#21040; CQ &#26377; entry&#65292;&#19981;&#36942; system call &#20173;&#27604; epoll &#23569;&#65306;</p><ul><li><p><strong>epoll</strong> : epoll_wait &#31561;&#20107;&#20214;&#20197;&#21450; read or write system call</p></li><li><p><strong>io_uring</strong> : io_uring_enter trigger kernel &#20006;&#31561;&#24453; CQ entry</p></li></ul><h3>SQPOLL &#27169;&#24335;&#23526;&#29694; zero I/O system call : </h3><p>&#33509;&#24819;&#23526;&#29694; zero I/O system call&#65292;io_uring &#25903;&#25588; SQPOLL &#27169;&#24335;&#65292;kernel &#26371;&#29992;&#38989;&#22806; kernel thread &#19981;&#26039; polling SQ&#65292;&#32780; user space process &#28961;&#38656;&#22519;&#34892; io_uring_enter &#21482;&#38656;&#35201;&#19981;&#26039; polling CQ &#21363;&#21487;&#65292;&#32570;&#40670;&#26159; CQ &#33509;&#27794;&#36039;&#26009;&#35442; process &#26371;&#26377;&#35377;&#22810;&#28961;&#25928;&#25289;&#21462;&#65292;&#22240;&#27492;  SQPOLL &#36969;&#29992;&#22312;<code>&#35531;&#27714;&#23494;&#38598;&#19988;&#20302;&#24310;&#36978;&#30340;&#22580;&#26223;&#20013;&#12290;</code></p><h3><code>IOPOLL &#27169;&#24335;&#20778;&#21270;&#30828;&#39636;&#20013;&#26039;&#65306;</code></h3><p>&#31532;&#20108;&#20491;&#20778;&#21270;&#26159;&#38364;&#26044;&#30828;&#39636;&#20013;&#26039;&#65292;io_uring &#19968;&#27171;&#36879;&#36942;&#30828;&#39636;&#20013;&#26039;&#35320;&#30332;&#24460;&#65292;kernel &#23559;&#35712;&#23531;&#32080;&#26524;&#25918;&#20837; CQ &#20013;&#65292;&#20294;&#30828;&#39636;&#20013;&#26039;&#19981;&#26159;&#27627;&#28961;&#25104;&#26412;&#65292;CPU &#25910;&#21040;&#20013;&#26039;&#25351;&#20196;&#24460;&#65292;&#35201;&#20808;&#22519;&#34892;&#20013;&#26039;&#25351;&#20196;&#20839;&#23481;&#65292;&#38500;&#20102;&#20572;&#27490;&#30070;&#21069;&#25351;&#20196;&#22806;&#65292;&#36996;&#26371;&#25226;&#22312;&#25351;&#20196; pipeline &#20013;&#30340;&#24460;&#32396;&#25351;&#20196;&#37117;&#31227;&#38500;&#65292;&#20013;&#26039;&#34389;&#29702;&#23436;&#24460;&#65292;&#26371;&#22238;&#21040;&#34987;&#25171;&#26039;&#30340;&#25351;&#20196;&#20301;&#22336;&#65292;&#35299;&#30908;&#24460;&#32396;&#25351;&#20196;&#25918;&#22238; pipeline&#12290;</p><p>&#33509;&#30636;&#38291;&#25910;&#21040;&#22823;&#37327;&#20013;&#26039;&#65292;CPU &#21453;&#35206;&#28165;&#31354; pipeline &#36942;&#31243;&#26159;&#24456;&#28040;&#32791;&#30340;&#65292;&#22240;&#27492; kernel &#20351;&#29992; NAPI (New API for network packet processing) &#25216;&#34899;&#34389;&#29702; network &#23553;&#21253;&#30340;&#20013;&#26039;&#25351;&#20196;&#65292;&#25910;&#21040;&#31532;&#19968;&#20491;&#20013;&#26039;&#24460;&#65292;&#26371;&#20999;&#25563;&#25104; polling &#27169;&#24335;&#65292;&#23559;&#32178;&#21345;&#35041;&#23553;&#21253;&#37117;&#35712;&#20986;&#20358;&#19968;&#27425;&#34389;&#29702;&#65292;&#36991;&#20813;&#30636;&#38291;&#22823;&#37327;&#23553;&#21253;&#35320;&#30332;&#19968;&#22534;&#20013;&#26039;&#12290;</p><p>&#20294; NAPI &#20027;&#35201;&#29992;&#26044;&#32178;&#36335;&#23553;&#21253;&#65292;&#33509;&#23559; io_uring &#29992;&#22312; File I/O (e.g &#36039;&#26009;&#24235;) &#30636;&#38291;&#22823;&#37327;&#35712;&#21462;&#20173;&#26371;&#35320;&#30332;&#19968;&#22534;&#20013;&#26039;&#65292;&#22240;&#27492; io_uring &#25552;&#20379; IO_POLL &#27169;&#24335;&#65292;&#23436;&#20840;&#19981;&#36879;&#36942;&#20013;&#26039;&#32780;&#26159;&#19981;&#26039; polling &#30828;&#39636;&#36039;&#26009;&#12290;</p><h1>io_uring &#30340;&#23526;&#38555;&#25033;&#29992;&#65306;PostgreSQL 18</h1><p>&#36039;&#26009;&#24235;&#31995;&#32113;&#26371;&#23565;&#27284;&#26696;&#26377;&#38971;&#32321;&#30340;&#35712;&#23531; I/O&#65292;&#22240;&#27492; PostgreSQL &#22312; 18 &#29256;&#26412;&#23565; File I/O &#24341;&#20837; async IO &#27231;&#21046;&#65292;&#35442;&#27231;&#21046;&#26377;&#20841;&#20491;&#27169;&#24335; worker (default) &amp; io_uring&#65306;</p><ul><li><p>worker&#65306;&#23559; read() or write() &#25351;&#20196;&#36879;&#36942; queue &#20132;&#30001; PG &#30340;&#21478;&#19968;&#20491; process &#34389;&#29702;&#65292;read heavy query &#25928;&#33021;&#25552;&#21319;&#32004; 1.5 &#20493;&#12290;</p></li><li><p>io_uring&#65306;&#23559; read() or write() &#25351;&#20196;&#25913;&#25104; io_uring &#26041;&#24335;&#34389;&#29702;&#65292;read heavy query  &#25928;&#33021;&#25552;&#21319;&#32004; 2.7 &#20493;&#12290;</p></li></ul><p>&#26377;&#27492;&#21487;&#35211; io_uring &#25928;&#33021;&#25552;&#21319;&#26368;&#22909;&#65292;&#19981;&#36942;&#30001;&#26044; io_uring &#26159;&#30064;&#27493;&#35712;&#23531;&#65292;PG &#25552;&#37266;&#29992; io_uring &#26371;&#23566;&#33268; explain analyze &#23565;&#26044; I/O &#34389;&#29702;&#26178;&#38291;&#35722;&#24471;&#19981;&#31934;&#28310;&#12290;</p><p>ref: https://pganalyze.com/blog/postgres-18-async-io</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YXgp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YXgp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 424w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 848w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1272w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png" width="1400" height="714" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:714,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium" title="PostgreSQL Async-IO using io_uring | by Sudheer Gajula | Medium" srcset="https://substackcdn.com/image/fetch/$s_!YXgp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 424w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 848w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1272w, https://substackcdn.com/image/fetch/$s_!YXgp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e042f3-e347-46a4-8248-31737afa20bc_1400x714.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>epoll vs io_uring benchmark - monoio vs tokio</h1><p><a href="https://chillvic.substack.com/p/tcp-server-ep1-server">&#22914;&#19978;&#38598;&#25152;&#35498;</a>&#65292;Rust &#26377; Future &#25277;&#35937;&#21270; async &#25805;&#20316;&#65292;&#22240;&#27492;&#21487;&#20197;&#26367;&#25563;&#19981;&#21516;&#30340;&#24213;&#23652; runtime&#65292;&#23526;&#38555;&#20195;&#30908;&#24190;&#20046;&#19981;&#29992;&#20462;&#25913;&#65292;&#20363;&#22914; monoio &#26159;&#29992; io_uring &#38283;&#30332;&#30340; async runtime&#65292;&#29992; monoio &#38283;&#30332;&#30340; TCP echo server &#33287; tokio &#31243;&#24335;&#24190;&#20046;&#19968;&#27171;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use monoio::{
    io::{AsyncReadRent, AsyncWriteRentExt},
    net::{TcpListener, TcpStream},
};

#[monoio::main(driver = "fusion")]
async fn main() {
    // tracing_subscriber::fmt().with_max_level(tracing::Level::TRACE).init();
    let listener = TcpListener::bind("127.0.0.1:50002").unwrap();
    println!("listening");
    loop {
        let incoming = listener.accept().await;
        match incoming {
            Ok((stream, addr)) =&gt; {
                println!("accepted a connection from {addr}");
                monoio::spawn(echo(stream));
            }
            Err(e) =&gt; {
                println!("accepted connection failed: {e}");
                return;
            }
        }
    }
}

async fn echo(mut stream: TcpStream) -&gt; std::io::Result&lt;()&gt; {
    let mut buf: Vec&lt;u8&gt; = Vec::with_capacity(8 * 1024);
    let mut res;
    loop {
        // read
        (res, buf) = stream.read(buf).await;
        if res? == 0 {
            return Ok(());
        }

        // write all
        (res, buf) = stream.write_all(buf).await;
        res?;

        // clear
        buf.clear();
    }
}</code></pre></div><p>&#28858;&#20102;&#28204;&#35430; io_uring &amp; epoll &#30340;&#25928;&#33021;&#65292;&#25105;&#23531;&#20102;&#19968;&#20491; <a href="https://github.com/vx416/play_iouring">play_iouring</a> &#23560;&#26696;&#20351;&#29992;  monoio &amp; tokio &#22871;&#20214;&#28204;&#35430;&#65306;</p><ul><li><p>tcp echo server : &#21516;&#26178; 10, 100, 1000 &#20491; tcp client</p></li><li><p>file I/O : &#20341;&#30332;&#35712;&#23531; 100 &#20491;&#27284;&#26696;&#65292;&#27599;&#27425; 64KB</p></li></ul><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;markdown&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-markdown">### File I/O (100 files, 64 KiB each)

| Benchmark | monoio | tokio | &#916; |

|---------------|------------|------------|----------------|
| Write (100 files) | ~1.32 ms | ~1.95 ms | monoio ~1.5x faster |
| Read (100 files) | ~693 &#181;s | ~1.87 ms | monoio ~2.7x faster |

  
### TCP Echo

| Benchmark | monoio | tokio | &#916; |
|--------------------|------------|------------|----------|
| 10 clients | ~4.12 ms | ~3.98 ms | ~equal |
| 100 clients | ~26.6 ms | ~27.1 ms | ~equal |
| 1000 clients | ~292 ms | ~330 ms | monoio slightly faster |</code></pre></div><p>File I/O &#26377;&#26126;&#39023;&#30340;&#25552;&#21319;&#65292;&#21407;&#22240;&#26159; tokio &#27794;&#29992; epoll &#31649;&#29702; File I/O &#32780;&#26159; <a href="https://docs.rs/tokio/latest/tokio/fs/index.html">worker thread + blocking I/O</a>&#65292;&#32780; monoio &#26159;&#29992; io_uring &#34389;&#29702; File I/O &#21487;&#22823;&#24133;&#28187;&#23569; system call&#65292;&#22240;&#27492; File I/O &#19981;&#33021;&#30070;&#20316; epoll &#36319; io_uring &#30340;&#27604;&#36611;&#12290;</p><p>&#32780; TCP Echo &#30340;&#25928;&#33021;&#24190;&#20046;&#19968;&#27171;&#65292;io_uring &#35201;&#26377;&#26126;&#39023;&#25928;&#33021;&#25552;&#21319;&#22312;&#26044;&#35299;&#30465;&#22823;&#37327;&#30340; system call&#65292;&#20294;&#23553;&#21253; read write &#29983;&#21629;&#36913;&#26399;&#20013;&#65292;system call &#19981;&#26159;&#26368;&#33457;&#26178;&#38291;&#30340;&#65292;&#32780;&#26159; kernel &#30340; network stack (e.g &#23553;&#21253;&#35299;&#26512;&#65292;firewall rule etc)&#65292;&#22240;&#27492; 1000 clients &#37327;&#36996;&#19981;&#22816;&#21040;&#36896;&#25104;&#39023;&#33879;&#30340; system call delay&#65292;&#20294;&#22823;&#37327; client &#36899;&#32218;&#22312;&#21934;&#27231;&#28204;&#35430;&#26178;&#65292;core &#19981;&#22816;&#19979;&#26371;&#22833;&#28310;&#65292;&#22240;&#27492;&#35442;&#22739;&#28204;&#21482;&#35657;&#26126; epoll &#36319; io_uring &#22312;&#26222;&#36941;&#24773;&#22659;&#19979;&#26377;&#24046;&#19981;&#22810;&#30340;&#24615;&#33021; &#12290;</p>]]></content:encoded></item><item><title><![CDATA[How to Build a High-Performance TCP Server  Ep1 - How Can a Single Server Handle Tens of Thousands of Concurrent Connections?]]></title><description><![CDATA[From the C10K Problem to epoll: A Deep Dive into the Performance Secrets of Nginx, Redis, and Rust Tokio]]></description><link>https://www.chillvic.dev/p/how-to-build-a-high-performance-tcp</link><guid isPermaLink="false">https://www.chillvic.dev/p/how-to-build-a-high-performance-tcp</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Tue, 24 Mar 2026 10:37:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AEe4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Nginx is renowned as a high-performance proxy server, while Redis stands as the king of caching databases. Both process individual requests with incredible speed because they almost entirely bypass disk I/O, the traditional bottleneck of system performance.</p><p><strong>But what happens when a server is hit by a massive surge of concurrent requests?</strong> </p><p>Let&#8217;s explore how Nginx and Redis manage high-traffic spikes under the hood.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AEe4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AEe4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 424w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 848w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AEe4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png" width="1456" height="2173" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/efd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2173,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8020556,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AEe4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 424w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 848w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!AEe4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefd884fd-06af-4319-90a6-10f9f5c565fb_1694x2528.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h1><strong>The C10K Problem: Why Do Servers Crash Under High Load?</strong></h1><p>When tens of thousands of client requests flood in simultaneously, even without disk bottlenecks, a server can still fail.</p><p>To return results, a server must establish a TCP connection for every request. A common &#8220;intuitive&#8221; approach is the <strong>Connection-Per-Thread</strong> model. Since each connection must transmit data without blocking others, the server creates one thread for every single connection. This leads to a massive number of threads, causing the CPU to struggle with constant <strong>Context Switching</strong> and exhausting system memory. This is the classic <strong>C10K problem</strong>: how can a single server handle 10,000 concurrent connections efficiently?</p><p>In a TCP server, the <code>accept()</code> system call establishes a connection, while <code>read()</code> and <code>write()</code> handle data. These operations behave differently depending on whether they use <strong>Blocking</strong> or <strong>Non-Blocking I/O</strong>.</p><h3>1. Blocking I/O (Connection-Per-Thread)</h3><p>In Blocking I/O, if a read() call has no data to read, it &#8220;hangs&#8221; (blocks) until data arrives. Similarly, if the kernel&#8217;s write buffer is full, write() blocks until space becomes available. Because a thread is stuck waiting for one connection, it cannot handle others. Therefore, each connection requires its own dedicated thread, leading to the resource exhaustion mentioned above.</p><h3>2. Non-Blocking I/O (One-Thread Polling)</h3><p>In Non-Blocking I/O, <code>read()</code> or <code>write()</code> calls return an error immediately if no data is available or the buffer is full. A single thread can manage multiple connections by cycling through them&#8212;if the first connection has no data, the thread moves to the next without waiting.</p><p>However, this requires a <strong>Polling</strong> loop. The thread must repeatedly check whether each TCP connection is ready for I/O. Each check is a system call. If thousands of connections are idle, the thread wastes massive amounts of CPU cycles making useless system calls.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JrMs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JrMs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JrMs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:638826,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JrMs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!JrMs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32efb81-5f58-4ec5-8d8f-b9768ae4fdd0_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>Efficient Management: I/O Multiplexing</h1><p>To manage multiple Non-Blocking I/Os efficiently, we look to the Kernel. The Kernel doesn&#8217;t rely on polling; it uses <strong>Hardware Interrupts</strong>. For instance, when a Network Interface Card (NIC) receives a packet, it sends an interrupt signal to the CPU. The CPU pauses its current task, allows the Kernel to read the data into memory, and then notifies the relevant user-space process.</p><p>Since the Kernel can actively notify processes, we can use <strong>I/O Multiplexing</strong>. This allows a user-space process to ask the Kernel to monitor multiple TCP connections at once. The process &#8220;sleeps&#8221; (blocks) on a single system call and only wakes up when at least one connection is ready for I/O.</p><h3><strong>The Evolution: select, poll, and epoll</strong></h3><p>Early Kernel solutions like <code>select</code> used a <strong>bitmap</strong> to track connections. The process would block until a connection became active, at which point the Kernel would update the bitmap. However, <code>select</code> had a limited number of connections it could track, leading to the creation of <code>poll</code>, which used an <strong>array</strong> to remove that limit.</p><p>Despite the improvement, both had two major flaws:</p><ol><li><p>The entire data structure (bitmap or array) had to be copied from user space to kernel space every time.</p></li><li><p>When the process woke up, it had to scan the <em>entire</em> structure to find which specific connection was ready.</p></li></ol><h3><strong>The Superior Mechanism: epoll</strong></h3><p><strong>epoll</strong> is an internal Kernel structure. Instead of passing a bitmap every time, you use <code>epoll_create()</code> to let the Kernel maintain a <strong>Red-Black Tree</strong> and a <strong>Double Linked List</strong>.</p><ul><li><p><strong>Red-Black Tree:</strong> Stores the TCP connections. Adding or removing a connection is <code>O(log n)</code>, making it ideal for frequent changes.</p></li><li><p><strong>Double Linked List (Ready List):</strong> When a connection receives data, the Kernel&#8217;s callback function places it into this list.</p></li></ul><p>When a process calls <code>epoll_wait()</code>, it blocks. Once a connection is ready, the Kernel wakes the process, which simply retrieves the ready connections from the linked list. There is no need to scan thousands of idle connections.</p><p>Since epoll uses a double linked list as a ready queue, it supports two triggering modes:</p><ul><li><p><strong>Level-Triggered (LT):</strong><br>As long as there is unread data on the connection, <code>epoll_wait()</code> will continue to return the file descriptor. In other words, the connection remains in the ready list until all available data has been read.</p></li><li><p><strong>Edge-Triggered (ET):</strong><br>Events are triggered only when the state changes. When new data arrives, the connection is added to the ready list, and once <code>epoll_wait()</code> returns it, it is removed. It will not be added again until new data arrives.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Fuwp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Fuwp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Fuwp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:835886,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Fuwp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Fuwp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bd0784d-58f0-4c01-84e8-6bdd00cbafb8_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>How Nginx and Redis Use Epoll</h1><p>Both Nginx and Redis typically use a single-threaded event loop (or one per CPU core) to run <code>epoll_wait()</code>. This avoids the complexities of multi-threaded data synchronization. The secret to their speed is that they <strong>never do anything that blocks</strong> the loop.</p><p><strong>The Epoll Loop Workflow:</strong></p><ol><li><p>Call <code>epoll_wait()</code> to find ready I/Os and <code>read()</code> the requests.</p></li><li><p>Process the logic.</p></li><li><p>Call <code>write()</code> to return results. If the kernel send buffer is full, the non-blocking <code>write()</code> returns an error.</p></li><li><p>Instead of waiting, the server moves the data to a temporary <strong>buffer queue</strong>, tells <code>epoll</code> to watch for a &#8220;writable&#8221; event, and moves to the next request.</p></li><li><p>Once the buffer has space, <code>epoll_wait()</code> triggers, and the server finishes the <code>write()</code>.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ny8S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ny8S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ny8S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:260021,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ny8S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!Ny8S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F407c6e9d-6c89-4c0c-a642-64411612d2f0_1024x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>By only executing <code>read</code> or <code>write</code> when the Kernel confirms they are ready, CPU utilization is maximized and memory usage remains low.</p><p>Additionally, as a proxy server, Nginx improves request forwarding throughput by leveraging the kernel&#8217;s <code>SO_REUSEPORT</code> option. </p><p>This allows multiple processes to bind to the same TCP port, with the kernel automatically distributing incoming connections across different server sockets. Each thread (or process) then manages its own set of connections using an independent <code>epoll</code> instance, enabling a concurrent architecture.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zuoC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zuoC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zuoC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:309910,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zuoC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!zuoC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3eb0dba-1a4f-4b4b-bc6a-32266a14b264_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Chill Vic&#8217;s Architecture Notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1>How to Implement a High-Performance TCP Server using Epoll: A Case Study with Rust's Tokio</h1><p>A common pattern for implementing an <code>epoll</code> loop like Nginx and Redis is the Reactor&#8211;Executor pattern : </p><ul><li><p><strong>Reactor:</strong> Manages event states (via <code>epoll_wait</code>) and wakes up tasks when I/O is ready.</p></li></ul><ul><li><p><strong>Executor:</strong> The core engine that processes the logic (e.g., Redis memory operations or Nginx HTTP parsing).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Py6a!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Py6a!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Py6a!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:464059,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Py6a!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Py6a!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9a407c7-1b1d-4d04-ae10-848d51384103_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the Rust ecosystem, the event states managed by the reactor are abstracted into the <code>Future</code> trait. This interface represents an asynchronous operation, where calling <code>poll</code> drives the internal logic of the <code>Future</code>. If the data isn't ready, it returns <code>Poll::Pending</code> and registers itself with the Reactor to be woken up later.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bCJo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bCJo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bCJo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1672190,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bCJo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!bCJo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43766010-c7cc-44bb-8549-d88dac352cfb_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Example: A Simplified Reactor-Executor</strong></h3><p>First, we define a basic SleepFuture to simulate I/O wait:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">struct SleepFuture {
    deadline: Instant,
}

impl SleepFuture {
    fn new(duration: Duration) -&gt; Self {
        SleepFuture {
            deadline: Instant::now() + duration,
        }
    }
}

impl Future for SleepFuture {
    type Output = ();
    fn poll(self: Pin&lt;&amp;mut Self&gt;, _cx: &amp;mut Context) -&gt; Poll&lt;()&gt; {
        if Instant::now() &gt;= self.deadline {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

async fn send_request() -&gt; i32 {
    SleepFuture::new(Duration::from_secs(2)).await;
    1 + 1
}

fn process_response(val: i32) {
    println!("&#34389;&#29702;&#32080;&#26524;: {}", val);
}</code></pre></div><p>Next, we implement the event loop logic for the Reactor and Executor. The Reactor polls futures and sends ready results to the Executor via a channel:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">type BoxFuture = Pin&lt;Box&lt;dyn Future&lt;Output = i32&gt; + Send&gt;&gt;;

fn reactor(futures: Vec&lt;BoxFuture&gt;, tx: mpsc::Sender&lt;i32&gt;) {
    let waker = Waker::noop();
    let mut cx = Context::from_waker(&amp;waker);

    // &#25226;&#27599;&#20491; future &#21253;&#25104; Option&#65292;&#23436;&#25104;&#24460;&#35373;&#28858; None
    let mut tasks: Vec&lt;Option&lt;BoxFuture&gt;&gt; = futures.into_iter().map(Some).collect();
    let total = tasks.len();
    let mut done_count = 0;

    loop {
        for task in tasks.iter_mut() {
            if let Some(fut) = task {
                if let Poll::Ready(val) = fut.as_mut().poll(&amp;mut cx) {
                    tx.send(val).unwrap(); // Ready&#65281;&#19999;&#32102; queue &#35041;&#38754;&#32102; executor
                    *task = None;
                    done_count += 1;
                }
            }
        }

        if done_count &gt;= total {
            break;
        }

        thread::sleep(Duration::from_millis(100)); // &#31777;&#21934;&#30340; polling &#38291;&#38548;
    }
}

fn executor(rx: mpsc::Receiver&lt;i32&gt;, total: usize) -&gt; thread::JoinHandle&lt;()&gt; {
    thread::spawn(move || {
        let mut done = 0;
        while done &lt; total {
            let val = rx.recv().unwrap();
            process_response(val);
            done += 1;
        }
        println!("executor: &#20840;&#37096;&#34389;&#29702;&#23436;&#25104;");
    })
}

fn main() {
    let (tx, rx) = mpsc::channel();

    let futures: Vec&lt;BoxFuture&gt; = vec![
        Box::pin(send_request()),
        Box::pin(send_request()),
    ];
    let total = futures.len();

    // executor worker thread&#65306;&#31561; reactor &#19999;&#32080;&#26524;&#36942;&#20358;&#23601;&#22519;&#34892;
    let handle = executor(rx, total);

    let start = Instant::now();

    // reactor&#65306;poll futures&#65292;ready &#24460;&#36879;&#36942; channel &#19999;&#32102; executor
    reactor(futures, tx);

    handle.join().unwrap();
    println!("&#20840;&#37096;&#23436;&#25104;&#65292;&#32791;&#26178;: {:?}", start.elapsed());
}
</code></pre></div><p>The above code demonstrates the abstraction capability of the <code>Future</code> trait: it encapsulates all operations as asynchronous behaviors and allows developers to define when state transitions occur. </p><p>By combining this with different underlying event loop mechanisms, one can swap out various Reactor&#8211;Executor implementations without modifying the core business logic.</p><h3>Tokio&#8217;s Real-World epoll Implementation</h3><p>With the basic concept of the Reactor-Executor pattern in mind, let&#8217;s look at a simple <strong>TCP Echo Server</strong> implemented with Tokio : </p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = [0; 1024];

            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&amp;mut buf).await {
                    // socket closed
                    Ok(0) =&gt; return,
                    Ok(n) =&gt; n,
                    Err(e) =&gt; {
                        eprintln!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };

                // Write the data back
                if let Err(e) = socket.write_all(&amp;buf[0..n]).await {
                    eprintln!("failed to write to socket; err = {:?}", e);
                    return;
                }
            }
        });
    }
}</code></pre></div><p>When you use the <code>#[tokio::main]</code> macro, it initializes a <strong>Runtime Object</strong>. The <code>block_on</code> function waits for all future events to complete. Since the primary future is a TCP server handling new connections, the runtime remains active as long as the server is running.</p><p>Under the hood, <code>#[tokio::main]</code> expands to:</p><ol><li><p><code>epoll_create()</code>: Establishes the epoll instance.</p></li><li><p><strong>Thread Pool</strong>: Creates worker threads (Executors).</p></li><li><p><strong>Timer Wheel</strong>: Manages <code>sleep</code> and <code>timeout</code> events.</p></li></ol><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">  fn main() {                                                                                                 
      let runtime = tokio::runtime::Runtime::new().unwrap();                                                                                 
      //  1. epoll_create()     &#8594; create epoll instance
      //  2. create thread pool   &#8594; worker threads                                                              
      //  3. create timer wheel   &#8594; manage sleep/timeout                                                          
                                                                                                              
      runtime.block_on(async {                                                    
          let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();                                  
          // ...                                                                                              
      });                                                                         
      // block_on start reactor &#30340; event loop                                                                 
  }  </code></pre></div><h3><strong>How Tokio Manages I/O via epoll</strong></h3><p>Tokio&#8217;s Reactor module uses <code>epoll</code> to manage network I/O. When a connection becomes readable, it is pushed into a queue for an Executor to process. </p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust"># Conceptual code example
 impl Reactor {                                                                       
      fn run(&amp;mut self) {                                                             
          loop {                                                                
              let timeout = self.nearest_timer_deadline();                            
              let ready_fds = epoll_wait(self.epoll_fd, timeout);               
                                                                                                              
              for (fd, token) in ready_fds {
                  self.run_queue.push(token.task_id); 
              }                                                                       
          }
      }
  }
</code></pre></div><p>Tokio also wraps TCP connections into Non-Blocking I/O + epoll-registered Futures. When <code>poll</code> is called, it attempts a non-blocking read. If no data is available, it registers the file descriptor with <code>epoll</code> and returns <code>Pending</code>:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust"># Conceptual code example
impl Future for TcpReadFuture {
    fn poll(mut self: Pin&lt;&amp;mut Self&gt;, cx: &amp;mut Context) -&gt; Poll&lt;Vec&lt;u8&gt;&gt; {
        match non_blocking_read(self.fd, &amp;mut self.buf) {
            Ok(data) =&gt; Poll::Ready(data), // Data available, return immediately
            Err(WouldBlock) =&gt; {
                if !self.registered {
                    // Retrieve reactor via thread-local and register to epoll
                    reactor.register(self.fd, Interest::READ, cx.waker().clone());
                    self.registered = true;
                }
                Poll::Pending // Tell the executor: "Not ready, go handle other tasks"
            }
        }
    }
}</code></pre></div><p>When executing an <code>await</code> (e.g., <code>socket.read(&amp;mut buf).await</code>), the compiler generates code conceptually similar to:</p><pre><code># Conceptual code example
loop {
    match future.poll(cx) {
        Poll::Ready(val) =&gt; break val,
        Poll::Pending =&gt; {
            return Poll::Pending;
        }
    }
}</code></pre><p>During the <code>poll</code> call, if the future involves a connection that is not yet ready (e.g., a non-blocking socket), that connection will be registered with <code>epoll</code> (via the runtime&#8217;s reactor) so it can be monitored for readiness.</p><h3><strong>The Role of </strong><code>tokio::spawn</code></h3><p>In Tokio, the executor schedules and polls futures, and it is backed by a pool of worker threads.</p><p>It is important to note that <code>tokio::spawn(async move { ... })</code> does not create a new kernel thread. Instead, it enqueues the async task into the runtime&#8217;s task queue. Worker threads then pick up and execute these tasks by polling their associated futures.</p><p>Each worker thread runs a loop similar to this:</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;javascript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-javascript"># Conceptual code example
fn worker_thread(run_queue: Arc&lt;SharedQueue&gt;, tasks: Arc&lt;TaskMap&gt;) {
    loop {
        let task_id = run_queue.pop(); // Blocks if queue is empty
        let task = tasks.get(task_id);
        match task.poll(&amp;mut cx) {
            Poll::Ready(()) =&gt; { tasks.remove(task_id); }
            Poll::Pending   =&gt; { /* Waker is registered, wait for next wake-up */ }
        }
    }
}</code></pre></div><p>Worker threads are configured when the runtime is created:</p><pre><code>#[tokio::main(flavor = &#8220;multi_thread&#8221;, worker_threads = 4)]
async fn main() {
    // your async code
}</code></pre><h3><strong>Nested Future Task</strong></h3><p>In the TCP Echo server above, each task handled by a worker thread is a nested future structure that performs connection read and write operations:</p><ul><li><p><code>spawn</code><strong> Future (ReadLoopFuture)</strong></p><ul><li><p><code>&#9492;&#9472;&#9472; socket.read().await</code> (<strong>TcpReadFuture</strong>)</p><ul><li><p><code>&#9492;&#9472;&#9472; non_blocking_read() &#8594; WouldBlock &#8594; Register epoll</code></p></li></ul></li><li><p><code>&#9492;&#9472;&#9472; socket.write().await</code> (<strong>TcpWriteFuture</strong>)</p><ul><li><p><code>&#9492;&#9472;&#9472; non_blocking_write() &#8594; WouldBlock &#8594; Register epoll</code></p></li></ul></li></ul></li></ul><p>If <code>socket.read()</code> has no data, it is placed in <code>epoll</code> to await notification from the Reactor. The Executor, receiving a <code>Pending</code> status, moves on to the next connection&#8217;s future. Once the Reactor wakes the future, the process continues to the <code>write</code> stage. If the write buffer is full, it returns <code>Pending</code> again, waiting for the next &#8220;writable&#8221; signal from the Reactor.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mONI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mONI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 424w, https://substackcdn.com/image/fetch/$s_!mONI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 848w, https://substackcdn.com/image/fetch/$s_!mONI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!mONI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mONI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png" width="1024" height="1524" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1524,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1403346,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!mONI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 424w, https://substackcdn.com/image/fetch/$s_!mONI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 848w, https://substackcdn.com/image/fetch/$s_!mONI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!mONI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc654784f-296a-4886-9b77-9667b03f0ca0_1024x1524.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Finally, the overall Tokio runtime architecture can be summarized as:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5usf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5usf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!5usf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!5usf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!5usf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5usf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:369390,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191964110?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5usf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!5usf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!5usf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!5usf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2bc4ba21-203d-4cf6-9b8f-22117491c458_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>Preview of the next part:</h1><p>Although <code>epoll</code> has been the preferred low-level technology for most high-performance servers, newer async I/O mechanisms have emerged as the kernel has evolved&#8212;most notably <code>io_uring</code>. Rust&#8217;s Tokio ecosystem also has implementations of async runtimes based on <code>io_uring</code>. In the next part, we will examine the performance limitations of <code>epoll</code> and how <code>io_uring</code> addresses them.</p>]]></content:encoded></item><item><title><![CDATA[如何打造高效 TCP Server Ep1 - 單台 Server 如何同時處理上萬連線？]]></title><description><![CDATA[&#24478; C10K &#21839;&#38988;&#21040; epoll&#65292;&#28145;&#20837; Nginx&#12289;Redis &#33287; Rust Tokio &#30340;&#39640;&#25928;&#33021;&#31192;&#23494;]]></description><link>https://www.chillvic.dev/p/tcp-server-ep1-server</link><guid isPermaLink="false">https://www.chillvic.dev/p/tcp-server-ep1-server</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Tue, 24 Mar 2026 10:01:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!2uJR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Nginx &#26159;&#36681;&#30332;&#35531;&#27714;&#30340;&#39640;&#25928;&#20195;&#29702;&#20282;&#26381;&#22120;&#65292;Redis &#21063;&#26159;&#24555;&#21462;&#36039;&#26009;&#24235;&#30340;&#29579;&#32773;&#65292;&#23427;&#20497;&#22312;&#21934;&#27425;&#35531;&#27714;&#30340;&#34389;&#29702;&#36895;&#24230;&#37117;&#26997;&#24555;&#65292;&#22240;&#28858;&#24190;&#20046;&#23436;&#20840;&#36991;&#38283;&#20102;&#36896;&#25104;&#29942;&#38968;&#30340;&#30828;&#30879; I/O&#12290;</p><p>&#20294;&#22914;&#26524;&#34389;&#29702;&#30636;&#38291;&#22823;&#37327;&#35531;&#27714;&#21602;&#65311;&#35731;&#25105;&#20497;&#28145;&#20837;&#25506;&#31350; Nginx &amp; Redis &#22914;&#20309;&#34389;&#29702;&#30636;&#38291;&#22823;&#27969;&#37327;&#65281;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2uJR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2uJR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 424w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 848w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2uJR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png" width="1456" height="2173" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2173,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:8020556,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2uJR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 424w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 848w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!2uJR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49ece0d4-eddf-4da4-ab7a-11d9e1369c51_1694x2528.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#30636;&#38291;&#28263;&#20837;&#19978;&#33836;&#20491; Client &#35531;&#27714;&#65292;&#21363;&#20415;&#27794;&#26377;&#30828;&#30879;&#29942;&#38968;&#65292;Server &#20063;&#26371;&#29190;&#21966;&#65311;</h1><p>&#30636;&#38291;&#28263;&#20837;&#22823;&#37327;&#35531;&#27714;&#26178;&#65292;Server &#35201;&#24314;&#31435;&#22823;&#37327; TCP &#36899;&#32218;&#25509;&#25910;&#35531;&#27714;&#22238;&#20659;&#32080;&#26524;&#65292;&#30001;&#26044;&#27599;&#26781; TCP &#36899;&#32218;&#22312;&#20659;&#36664;&#36942;&#31243;&#20013;&#19981;&#33021;&#20114;&#21345;&#65292;&#22240;&#27492;&#30452;&#35258;&#20570;&#27861;&#26159;&#12300;&#19968;&#26781;&#36899;&#32218;&#32129;&#19968;&#20491; Thread&#12301;&#20358;&#34389;&#29702;&#35531;&#27714;&#20006;&#20659;&#36865;&#32080;&#26524;&#65292;&#27492;&#26178;&#22823;&#37327;&#36899;&#32218;&#24314;&#31435;&#22823;&#37327;&#30340; Thread&#65292;&#36896;&#25104; CPU &#38971;&#32321;&#30340; Context Switch &#20197;&#21450;&#22823;&#37327;&#30340;&#35352;&#25014;&#39636;&#28040;&#32791;&#65292;Server &#36039;&#28304;&#19981;&#22816;&#23601;&#29190;&#20102;&#12290;</p><p><strong>&#32780;&#36889;&#20063;&#34893;&#29983;&#32147;&#20856;&#30340; C10K &#21839;&#38988;&#65292;&#22914;&#20309;&#35731;&#21934;&#21488; Server &#21516;&#26178;&#34389;&#29702; 1 &#33836;&#20491;&#36899;&#32218;&#35531;&#27714;&#65311;</strong></p><p>&#30070; client &#24448; server &#36865;&#35531;&#27714;&#26178;&#65292;TCP server &#30340; accept system call &#26371;&#24314;&#31435;&#19968;&#26781; TCP Connection&#65292;&#20006;&#36879;&#36942; read() &amp; write() system call &#35712;&#23531;&#36039;&#26009;&#65292;&#32780; TCP Connection &#21448;&#20998; Block I/O &#36319; Non-Blocking I/O&#12290; </p><h2><strong>Blocking I/O - Connection Per Thread &#27169;&#24335;</strong></h2><p>Blocking I/O &#22519;&#34892; read() &#26178;&#65292;&#33509;&#27794;&#36039;&#26009;&#21487;&#35712;&#26371;&#21345;&#20303;&#65292;&#30452;&#21040;&#26377;&#36039;&#26009;&#25165; return&#65292;&#21516;&#29702;&#22519;&#34892; write() &#26178;&#65292;&#22914;&#26524; kernel write buffer &#23531;&#28415;&#20102;&#65292;write() &#26371;&#21345;&#20303;&#65292;&#30452;&#21040; kernel buffer &#26377;&#31354;&#38291;&#24460;&#25165;&#26371; return&#12290;</p><p>&#22240;&#27492;&#22810;&#20491; Blocking I/O &#19981;&#33021;&#20849;&#29992; Thread&#65292;&#22240;&#28858;&#30070;&#31532;&#19968;&#20491;&#36899;&#32218;&#30340; read() &#22240;&#28858;&#27794;&#26377;&#36039;&#26009;&#32780;&#21345;&#20303;&#26178;&#65292;&#26371;&#23566;&#33268;&#24460;&#38754;&#36899;&#32218;&#21363;&#20415;&#26377;&#36039;&#26009;&#20063;&#34389;&#29702;&#19981;&#21040;&#65292;&#25152;&#20197; Blocking I/O &#24517;&#38920;&#19968;&#20491;&#36899;&#32218;&#25645;&#37197;&#19968;&#20491; Thread &#21435;&#22519;&#34892;&#21508;&#33258;&#30340; read loop&#12290;</p><h2>Non-Blocking I/O - One Thread Polling &#27169;&#24335;</h2><p>&#32780; Non-Blocking I/O &#22519;&#34892; read() &amp; write() &#26178;&#65292;&#27794;&#36039;&#26009;&#25110;&#19981;&#21487;&#23531;&#26371;&#30452;&#25509; return error&#65292;&#19968;&#20491; Thread &#21487;&#34389;&#29702;&#22810;&#20491; Non-Blocking I/O&#65292;&#21069;&#19968;&#20491;&#36899;&#32218;&#27794;&#36039;&#26009; return error &#24460;&#24448;&#19979;&#19968;&#20491;&#36899;&#32218;&#34389;&#29702;&#65292;&#19981;&#21345;&#20303;&#20219;&#20309;&#20154;&#12290;</p><p>&#20294; Non-Blocking I/O &#23526;&#29694; read loop &#26178;&#65292;&#24517;&#38920;&#19981;&#26039;&#35426;&#21839;&#25152;&#26377; TCP connection &#30906;&#35469;&#26377;&#35504;&#26377;&#36039;&#26009;&#21487;&#35712;&#65292;&#27599;&#27425; read() &#37117;&#26159;&#19968;&#20491; system call&#65292;&#38614;&#28187;&#23569;&#20102; Thread &#25976;&#37327;&#65292;&#20294;&#27599;&#27425;&#37117;&#35201;&#35426;&#21839;&#25152;&#26377;&#36899;&#32218;&#65292;&#26371;&#28040;&#32791;&#22823;&#37327; CPU&#65292;&#19988;&#33509;&#22823;&#37327;&#36899;&#32218;&#37117;&#27794;&#36039;&#26009;&#36996;&#21371;&#21453;&#35206;&#35426;&#21839;&#65292;&#31561;&#26044;&#20570;&#20102;&#35377;&#22810;&#28961;&#25928;&#30340; system call&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Rt2f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Rt2f!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Rt2f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:638826,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Rt2f!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Rt2f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e7442de-e1d8-439c-b292-9dad33f0cef5_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#21934;&#32218;&#31243;&#22914;&#20309;&#39640;&#25928;&#22320;&#31649;&#29702;&#22810;&#20491; Non-Blocking I/O &#65311;</h1><p>&#20351;&#29992; Non-Blocking &#30475;&#20284;&#35201;&#36650;&#35426;&#65292;&#20294;&#20854;&#23526; kernel &#22312;&#31649;&#29702;&#22810;&#20491; TCP &#36899;&#32218;&#26178;&#65292;&#19981;&#36879;&#36942;&#36650;&#35426;&#65292;&#32780;&#26159;&#30828;&#39636;&#20013;&#26039;&#35338;&#34399;&#65292;&#20363;&#22914; NIC (&#32178;&#21345;) &#25910;&#21040;&#32178;&#36335;&#23553;&#21253;&#24460;&#65292;&#26371;&#21521; CPU &#30332;&#36865;&#20013;&#26039;&#35338;&#34399;&#65292;&#27492;&#26178; CPU &#26371;&#20572;&#19979;&#25163;&#37002;&#24037;&#20316;&#65292;&#21578;&#35380; Kernel &#26377;&#32202;&#24613;&#20107;&#20214;&#65292;&#38568;&#24460; Kernel &#26371;&#25226;&#23553;&#21253;&#36039;&#26009;&#35712;&#21040;&#35352;&#25014;&#39636;&#20013;&#20006;&#36890;&#30693;&#23565;&#25033;&#30340; user space process&#12290;</p><p>&#31455;&#28982; Kernel &#21487;&#36879;&#36942;&#20013;&#26039;&#35338;&#34399;&#20027;&#21205;&#36890;&#30693;&#65292;&#37027;&#40636; user space process &#23601;&#21487;&#35731; Kernel &#19968;&#27425;&#30435;&#32893;&#22810;&#20491; TCP &#36899;&#32218;&#65292;&#31561;&#20854;&#20013;&#19968;&#20491;&#36899;&#32218;&#26377;&#36039;&#26009;&#21487;&#35712;&#26178;&#22312;&#25226; process &#21914;&#37266;&#65292;&#20063;&#23601;&#26159;&#36879;&#36942;&#19968;&#27425; Blocking &#25805;&#20316;&#30435;&#32893;&#22810;&#20491; Non-Blocking I/O&#65292;&#36991;&#20813; user space process &#35201;&#19981;&#26039;&#36865;&#20986;&#28961;&#25928;&#30340; read()&#65292;&#32780;&#29992;&#19968;&#20491; thread &#36879;&#36942;&#21934;&#19968; system call &#21516;&#26178;&#30435;&#32893;&#22810;&#26781; I/O &#36899;&#32218;&#30340;&#27231;&#21046;&#65292;&#31281;&#28858; I/O &#22810;&#24037;&#65288;I/O Multiplexing&#65289;&#12290;</p><h2>Kernel &#30340; I/O &#22810;&#24037;&#65288;I/O Multiplexing&#65289;&#25216;&#34899; - select &amp; poll</h2><p>&#20363;&#22914;&#26089;&#26399; Kernel &#25552;&#20379; select system call&#65292;&#29992; bitmap &#32080;&#27083;&#31649;&#29702;&#22810;&#20491; Non-Blocking I/O &#20659;&#20837; select&#65292;Kernel &#26371;&#23559; process &#38459;&#22622;&#65292;&#30452;&#21040;&#20219;&#19968;&#36899;&#32218;&#26377;&#36039;&#26009;&#21040;&#36948;&#24460;&#21914;&#37266;&#65292;&#20006;&#35206;&#23531; bitmap &#20839;&#23481;&#65292;&#25226;&#26377;&#36039;&#26009;&#30340;&#36899;&#32218;&#26356;&#26032;&#36914;&#21435;&#65292;&#38568;&#24460;&#34987;&#21914;&#37266;&#30340; process &#35201; scan bitmap &#20839;&#23481;&#25214;&#21040;&#23565;&#25033;&#30340; TCP &#36899;&#32218;&#35712;&#36039;&#26009;&#12290;</p><p>select &#32570;&#40670;&#26159; bitmap &#32080;&#27083;&#38480;&#21046;&#20102;&#30435;&#32893; TCP &#26368;&#22823;&#25976;&#37327;&#65292;&#22240;&#27492; poll system call &#21063;&#23559; bitmap &#32080;&#27083;&#25563;&#25104; array&#65292;&#20294;&#26159; select &amp; poll &#20173;&#26377;&#20849;&#21516;&#30340;&#32570;&#40670;&#65306;</p><ul><li><p>&#29992; user space &#36039;&#26009;&#32080;&#27083;&#31649;&#29702;&#38656;&#35201;&#30435;&#32893;&#30340; TCP &#36899;&#32218;&#65292;&#35201;&#23559;&#35442;&#36039;&#26009;&#32080;&#27083;&#24478; user space &#35079;&#35069;&#21040; kernel space</p></li><li><p>process &#34987;&#21914;&#37266;&#24460;&#65292;&#21363;&#20415;&#21482;&#26377;&#19968;&#26781;&#36899;&#32218;&#21487;&#35712;&#20173;&#35201; scan &#25972;&#20491;&#32080;&#27083; (bitmap or array) &#25165;&#33021;&#25214;&#21040;&#35442;&#36899;&#32218;</p></li></ul><h2>Kernel &#22914;&#20309;&#35373;&#35336;&#20986;&#26356;&#22909;&#30340; I/O &#22810;&#24037;&#27231;&#21046;&#65311;</h2><p>epoll &#26159; kernel &#20839;&#37096;&#31649;&#29702;&#22810;&#26781; non-blocking I/O &#30340;&#32080;&#27083;&#65292;&#22240;&#27492;&#19981;&#29992;&#22312; user space &#21478;&#22806;&#24314;&#31435; bitmap or array &#32080;&#27083;&#65292;&#36879;&#36942; epoll_create() system call&#65292;kernel &#26371;&#24314;&#31435;&#19968;&#38982;&#32005;&#40657;&#27193;&#36319; double linked list&#65292;&#20006;&#29992; epoll_ctl() system call &#23559; TCP &#36899;&#32218;&#25918;&#20837;&#32005;&#40657;&#27193;&#35731; kernel &#31649;&#29702;&#65292;&#32005;&#40657;&#27193;&#26032;&#22686;/&#21034;&#38500;&#36899;&#32218;&#30340;&#26178;&#38291;&#35079;&#38620;&#24230;&#28858; O(log n)&#65292;&#27604;&#38499;&#21015;&#26356;&#36969;&#21512;&#38971;&#32321;&#22686;&#28187;&#36899;&#32218;&#30340;&#22580;&#26223;&#12290; </p><p>user space process &#22519;&#34892; epoll_wait() system call&#65292;kernel &#26371;&#38459;&#22622;&#35442; process&#65292;&#30070;&#25910;&#21040;&#20013;&#26039;&#35338;&#34399;&#20006;&#34389;&#29702;&#23436;&#23553;&#21253;&#24460;&#65292;&#26371;&#36879;&#36942;&#20107;&#20808;&#35387;&#20874;&#30340; callback &#23559;&#23565;&#25033;&#36899;&#32218;&#25918;&#20837; double linked list&#65292;&#38568;&#24460;&#21914;&#37266; process&#65292;process &#30452;&#25509;&#24478; double linked list &#20013;&#29554;&#21462;&#21487;&#35712;&#30340;&#36899;&#32218;&#65292;&#19981;&#29992; scan &#25972;&#20491;&#32080;&#27083;&#12290;</p><p>&#19988;&#26377; double linked list &#32080;&#27083;&#30070;&#20316; queue&#65292;epoll &#33021;&#23526;&#29694;&#20841;&#31278;&#35320;&#30332;&#27169;&#24335;&#65306;</p><ul><li><p>Level-Triggered&#65306;&#30070;&#36899;&#32218;&#36996;&#26377;&#36039;&#26009;&#21487;&#35712;&#65292;epoll_wait() &#23601;&#26371;&#22238;&#20659;&#65292;&#20063;&#23601;&#26159;&#35498;&#27794;&#25226;&#25152;&#26377;&#36039;&#26009;&#35712;&#23436;&#21069;&#65292;&#19981;&#26371;&#24478; double linked list &#20013;&#31227;&#38500;&#12290;</p></li><li><p>Edge-Triggered&#65306;&#29376;&#24907;&#35722;&#21270;&#26178;&#35320;&#30332;&#65292;&#30070;&#26377;&#26032;&#36039;&#26009;&#21040;&#26178;&#65292;&#25918;&#36914; double linked list&#65292;epoll_wait() &#19968;&#22238;&#20659;&#23601;&#31227;&#38500;&#65292;&#30452;&#21040;&#19979;&#27425;&#26377;&#26032;&#36039;&#26009;&#25269;&#36948;&#25165;&#22312;&#25918;&#36914;&#21435;&#12290;</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Q5lA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Q5lA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Q5lA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:835886,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Q5lA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Q5lA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1da64fa9-6524-4fc7-a78a-2ff17f8a7b7b_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>Redis &amp; Nginx &#22914;&#20309;&#20351;&#29992; Epoll&#65311;</h1><p>&#20351;&#29992; epoll &#26178;&#65292;&#24120;&#35211;&#27169;&#24335;&#26159;&#29992;&#19968;&#20491; thread &#22519;&#34892; epoll_wait()&#65292;&#36991;&#20813;&#22810;&#32218;&#31243;&#21516;&#26178;&#34389;&#29702;&#30456;&#21516; TCP connection &#23566;&#33268;&#35712;&#21462;&#36039;&#26009;&#34987;&#25130;&#26039;&#65292;&#22240;&#27492;&#21934;&#32218;&#31243;&#30340; epoll loop &#22519;&#34892;&#36942;&#31243;&#23601;&#38656;&#35201;&#36991;&#20813;&#20219;&#20309;&#21487;&#33021;&#30340;&#38459;&#22622;&#12290;</p><p>epoll loop &#36942;&#31243;&#65306;</p><ol><li><p>&#20808;&#36879;&#36942; epoll_wait() &#25214;&#21040;&#21487;&#35712; I/O&#65292;&#28982;&#24460;&#22519;&#34892; read() &#35712;&#35531;&#27714;</p></li><li><p>&#34389;&#29702;&#23436;&#35531;&#27714;&#24460;&#65292;&#22519;&#34892; write() &#65292;&#33509; kernel &#30340; send buffer &#24050;&#28415;&#65292;non-blocking write() &#26371;&#36820;&#22238; error</p></li><li><p>&#25910;&#21040; error &#24460;&#23559;&#36039;&#26009;&#20808;&#23531;&#21040; buffer queue &#20006;&#22519;&#34892; epoll_ctl() &#30435;&#32893;&#35442; I/O &#30340;&#21487;&#23531;&#20107;&#20214;&#65292;&#28982;&#24460;&#22519;&#34892;&#19979;&#19968;&#27425;&#30340; epoll_wait() </p></li><li><p>&#31561;&#21040; send buffer &#26377;&#20301;&#32622;&#65292;epoll_wait() &#22238;&#20659;&#21487;&#23531; I/O&#65292;&#22312;&#23559; buffer queue &#20013;&#30340;&#32080;&#26524;&#23531;&#20837;&#65292;&#32780; write() &#25351;&#20196;&#26371;&#20808;&#23559;&#36039;&#26009;&#23531;&#21040; kernel &#30340; send buffer &#20013;&#65292;&#38568;&#24460;&#22312;&#30064;&#27493;&#23531;&#20837;&#32178;&#21345;&#65292;&#22240;&#27492;&#22519;&#34892;&#24456;&#24555;</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8Q8Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:260021,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!8Q8Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9747d58-4bc8-472e-9623-b1d389396ac0_1024x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#36879;&#36942;&#19978;&#38754;&#27969;&#31243;&#65292;&#21487;&#30332;&#29694; Nginx &amp; Redis &#30340;&#35373;&#35336;&#37117;&#26159;&#27704;&#36960;&#19981;&#20570;&#26371;&#21345;&#20303;&#30340;&#34892;&#29234; (&#38500;&#20102; epoll_wait)&#65292;&#21482;&#26377;&#22312;&#21487;&#35712;&#25110;&#21487;&#23531;&#30340;&#26178;&#20505;&#25165;&#22519;&#34892; read() &#25110; write()&#65292;&#22240;&#27492; CPU &#20351;&#29992;&#29575;&#36229;&#39640;&#65292;&#27794;&#26377;&#32114;&#27627;&#28010;&#36027;&#65292;Thread &#25976;&#37327;&#20063;&#24456;&#23569;&#65292;&#35352;&#25014;&#39636;&#29992;&#37327;&#19981;&#22823;&#65292;&#36889;&#23601;&#26159;&#28858;&#20160;&#40636; Nginx &amp; Redis &#33021;&#22914;&#27492;&#39640;&#25928;&#34389;&#29702;&#30636;&#38291;&#22823;&#27969;&#37327;&#30340;&#31192;&#35363;&#65281;<br></p><p>&#27492;&#22806; Nginx &#20316;&#28858; proxy server &#28858;&#25552;&#39640;&#35531;&#27714;&#36681;&#30332;&#21534;&#21520;&#37327;&#65292;&#26371;&#36879;&#36942; kernel &#30340; SO_REUSEPORT flag&#65292;&#35731;&#19981;&#21516; process &#30340; tcp server &#30435;&#32893;&#30456;&#21516; port&#65292;kernel &#26371;&#33258;&#21205;&#20998;&#37197;&#36899;&#32218;&#21040;&#19981;&#21516; tcp server socket&#65292;&#32780;&#27599;&#19968;&#20491; thread &#21508;&#33258;&#29992;&#29544;&#31435;&#30340; epoll &#31649;&#29702;&#19981;&#21516;&#36899;&#32218;&#65292;&#23526;&#29694;&#20341;&#30332;&#26550;&#27083;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LwRz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LwRz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LwRz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:309910,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LwRz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!LwRz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88fe8ea6-13c3-443c-ad76-5c7b8fe0d7c9_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Chill Vic&#8217;s Architecture Notes! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1>&#22914;&#20309;&#29992; epoll &#23526;&#29694;&#39640;&#25928;&#30340; TCP Server&#65292;&#24050; Rust tokio &#22871;&#20214;&#28858;&#20363;&#65281;</h1><p>&#24120;&#35211;&#23526;&#20316; epoll loop &#30340; design pattern &#28858; reactor executor -  </p><p>reactor &#31649;&#29702;&#20107;&#20214;&#29376;&#24907;&#20006;&#21914;&#37266;&#20107;&#20214;&#65292;&#20363;&#22914; epoll_wait &#30435;&#32893; TCP &#36899;&#32218;&#29376;&#24907;&#65292;&#24478;&#19981;&#21487;&#35712;&#29376;&#24907;&#20006;&#21487;&#35712;&#26178;&#65292;process &#26371;&#34987;&#21914;&#37266;&#24478; queue &#20013;&#29554;&#24471;&#35442;&#36899;&#32218;&#12290;</p><p>executor &#21063;&#26159;&#34389;&#29702;&#35531;&#27714;&#30340;&#26680;&#24515;&#20803;&#20214;&#65292;&#20363;&#22914; redis &#36000;&#36012;&#34389;&#29702;&#35352;&#25014;&#39636;&#36039;&#26009;&#20197;&#21450; nginx &#34389;&#29702; http protocol &#35299;&#26512;&#36681;&#30332;&#31561;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7g-V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7g-V!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7g-V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:464059,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7g-V!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!7g-V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3578309-4a3d-4af8-884e-63c26d63a740_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#32780;&#22312; Rust &#29983;&#24907;&#31995;&#20013;&#65292;&#23559;&#34987; reactor &#20803;&#20214;&#25152;&#31649;&#29702;&#30340;&#20107;&#20214;&#29376;&#24907;&#65292;&#25277;&#35937;&#21270;&#25104; Future trait&#65292;&#35442;&#20171;&#38754;&#20195;&#34920; async &#25805;&#20316;&#65292;&#36879;&#36942;&#22519;&#34892; poll &#20358;&#23526;&#38555;&#22519;&#34892; Future &#20839;&#37096;&#37007;&#36655;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WOFj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WOFj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WOFj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png" width="1024" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1672190,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WOFj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!WOFj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7acd134-7886-4554-a37d-2c26a6bea6ca_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>&#20351;&#29992; Rust Future &#38283;&#30332;&#19968;&#20491;&#31777;&#21934;&#30340; reactor &#36319; executor &#27169;&#24335;</h2><p><strong>step 1 - &#23526;&#29694;&#19968;&#20491; time sleep future &#29992;&#20358;&#27169;&#25836;&#35531;&#27714;&#24310;&#36978;</strong></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">struct SleepFuture {
    deadline: Instant,
}

impl SleepFuture {
    fn new(duration: Duration) -&gt; Self {
        SleepFuture {
            deadline: Instant::now() + duration,
        }
    }
}

impl Future for SleepFuture {
    type Output = ();
    fn poll(self: Pin&lt;&amp;mut Self&gt;, _cx: &amp;mut Context) -&gt; Poll&lt;()&gt; {
        if Instant::now() &gt;= self.deadline {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

async fn send_request() -&gt; i32 {
    SleepFuture::new(Duration::from_secs(2)).await;
    1 + 1
}

fn process_added_value(val: i32) {
    println!("&#34389;&#29702;&#21152;&#27861;&#32080;&#26524;: {}", val);
}</code></pre></div><p>step 2 - &#23526;&#29694; reactor-executor pattern&#65292;reactor &#36000;&#36012; trigger Ready &#29376;&#24907;&#30340; future &#23559;&#32080;&#26524;&#36879;&#36942; queue &#36865;&#32102; executor &#34389;&#29702;&#65292;executor &#21021;&#22987;&#21270;&#19968;&#20491; thread &#34389;&#29702; queue &#20013;&#30340;&#36039;&#26009;&#12290;<br></p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">type BoxFuture = Pin&lt;Box&lt;dyn Future&lt;Output = i32&gt; + Send&gt;&gt;;

fn reactor(futures: Vec&lt;BoxFuture&gt;, tx: mpsc::Sender&lt;i32&gt;) {
    let waker = Waker::noop();
    let mut cx = Context::from_waker(&amp;waker);

    let mut tasks: Vec&lt;Option&lt;BoxFuture&gt;&gt; = futures.into_iter().map(Some).collect();
    let total = tasks.len();
    let mut done_count = 0;

    loop {
        for task in tasks.iter_mut() {
            if let Some(fut) = task {
                if let Poll::Ready(val) = fut.as_mut().poll(&amp;mut cx) {
                    tx.send(val).unwrap(); // Ready&#65281;&#19999;&#32102; executor
                    *task = None;
                    done_count += 1;
                }
            }
        }

        if done_count &gt;= total {
            break;
        }

        thread::sleep(Duration::from_millis(100)); 
    }
}

fn executor(rx: mpsc::Receiver&lt;i32&gt;, total: usize) -&gt; thread::JoinHandle&lt;()&gt; {
    thread::spawn(move || {
        let mut done = 0;
        while done &lt; total {
            let val = rx.recv().unwrap();
            process_added_value(val);
            done += 1;
        }
        println!("executor: &#20840;&#37096;&#34389;&#29702;&#23436;&#25104;");
    })
}</code></pre></div><p>step 3 - &#23436;&#25104; main function &#22519;&#34892;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">fn main() {
    let (tx, rx) = mpsc::channel();
    let futures: Vec&lt;BoxFuture&gt; = vec![Box::pin(send_request()), Box::pin(send_request())];

    let total = futures.len();
    let handle = executor(rx, total);

    let start = Instant::now();
    reactor(futures, tx);
    handle.join().unwrap();
    println!("&#20840;&#37096;&#23436;&#25104;&#65292;&#32791;&#26178;: {:?}", start.elapsed());
}
</code></pre></div><p>&#19978;&#36848;&#20195;&#30908;&#23637;&#31034;&#20102; Future trait &#30340;&#25277;&#35937;&#21270;&#33021;&#21147;&#65292;&#21487;&#25226;&#25152;&#26377;&#25805;&#20316;&#23553;&#35037;&#25104; async &#34892;&#28858;&#65292;&#33258;&#34892;&#23450;&#32681;&#29376;&#24907;&#36681;&#25563;&#26178;&#27231;&#65292;&#25645;&#37197;&#19981;&#21516;&#24213;&#23652;&#30340; event loop &#27231;&#21046;&#65292;&#21487;&#22312;&#19981;&#20462;&#25913;&#26680;&#24515;&#26989;&#21209;&#31243;&#24335;&#26781;&#20214;&#19979;&#65292;&#26367;&#25563;&#19981;&#21516;&#30340; reactor-executor &#30340;&#23526;&#20316;&#12290;</p><h2>Tokio &#30340; epoll &#26550;&#27083;</h2><p>&#32780; tokio &#20316;&#28858; rust &#20013;&#30693;&#21517;&#30340; async runtime&#65292;&#20854;&#24213;&#23652;&#26159;&#29992; epoll &#20358;&#23526;&#29694; reactor-executor&#65292;&#25105;&#20497;&#20358;&#30475;&#19968;&#19979;&#26368;&#31777;&#21934;&#30340; TCP echo server&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = [0; 1024];

            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&amp;mut buf).await {
                    // socket closed
                    Ok(0) =&gt; return,
                    Ok(n) =&gt; n,
                    Err(e) =&gt; {
                        eprintln!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };

                // Write the data back
                if let Err(e) = socket.write_all(&amp;buf[0..n]).await {
                    eprintln!("failed to write to socket; err = {:?}", e);
                    return;
                }
            }
        });
    }
}</code></pre></div><p><code>#[tokio::main]</code>&#36889;&#20491; macro &#26371;&#21021;&#22987;&#21270;&#19968;&#20491; runtime object&#65292;block_on &#23601;&#26159;&#31561;&#25152;&#26377; future &#20107;&#20214;&#37117;&#34389;&#29702;&#23436;&#65292;&#32780;&#20027;&#35201;&#30340; future &#29289;&#20214;&#26159;&#19968;&#20491; tcp server &#34389;&#29702;&#26032;&#36899;&#32218;&#65292;&#22240;&#27492; TCP server &#27794;&#38364;&#38281; runtime object &#19981;&#26371;&#32080;&#26463;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust"> #[tokio::main] &#23637;&#38283;&#24460;&#65292;&#23526;&#38555;&#19978;&#26159;&#65306;                                                       
  fn main() {                                                                                                 
      let runtime = tokio::runtime::Runtime::new().unwrap();                    
      //  &#20839;&#37096;&#20570;&#20102;&#65306;                                                              
      //  1. epoll_create()     &#8594; &#24314;&#31435; epoll &#23526;&#20363;                                                             
      //  2. &#24314;&#31435; thread pool   &#8594; worker threads                                                              
      //  3. &#24314;&#31435; timer wheel   &#8594; &#31649;&#29702; sleep/timeout                                                          
                                                                                                              
      runtime.block_on(async {                                                    
          let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();                                  
          // ...                                                                                              
      });                                                                         
      // block_on &#21855;&#21205; reactor &#30340; event loop                                                                 
  }  </code></pre></div><h3>Tokio &#22914;&#20309;&#25226;&#36899;&#32218;&#25918;&#20837; Epoll &#20013;&#65311;</h3><p><code>tokio::net::TcpListener</code> &#36319; <code>tokio::io::{AsyncReadExt, AsyncWriteExt}</code> &#26377;&#21029;&#26044; std &#22871;&#20214;&#20013;&#30340; socket &#29289;&#20214;&#65292;&#23526;&#20316;&#20102; epoll &#29256;&#26412;&#30340; Future&#65292;&#20363;&#22914;&#19979;&#38754;&#31034;&#24847; code : </p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">impl Future for TcpReadFuture {
    type Output = Vec&lt;u8&gt;;

    fn poll(mut self: Pin&lt;&amp;mut Self&gt;, cx: &amp;mut Context) -&gt; Poll&lt;Vec&lt;u8&gt;&gt; {
        match non_blocking_read(self.fd, &amp;mut self.buf) {
            Ok(data) =&gt; Poll::Ready(data), // &#26377;&#36039;&#26009;&#65292;&#30452;&#25509;&#22238;&#20659;
            Err(WouldBlock) =&gt; {
                if !self.registered {
                    reactor.register(self.fd, Interest::READ, cx.waker().clone());
                    self.registered = true;
                }
                Poll::Pending                        
            }
        }
    }
}</code></pre></div><p>&#35712;&#21040;&#36039;&#26009;&#23601;&#22238;&#20659; OK&#65292;&#27794;&#35712;&#21040;&#36879;&#36942; <code>epoll_ctl</code> &#25918;&#21040; epoll &#32080;&#27083;&#20013;&#65292;&#20006;&#22238;&#20659; pending&#65292;&#32780;&#22519;&#34892; <code>await (e.g socket.read(&amp;mut buf).await)</code> &#26178;&#65292;&#32232;&#35695;&#22120;&#26371;&#29986;&#29983; :</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">loop {
    match future.poll(cx) {
        Poll::Ready(val) =&gt; break val,
        Poll::Pending =&gt; {
            return Poll::Pending;
        }
    }
}</code></pre></div><p>&#22519;&#34892; poll &#26178;&#26371;&#23559;&#35442; future &#20013;&#30340; connection &#25918;&#20837; epoll &#20013;&#12290;<br><br>&#26368;&#24460; tokio &#30340; reactor &#27169;&#32068;&#26371;&#22519;&#34892; <code>epoll_wait</code> &#25214;&#21040;&#21487;&#35712;&#36899;&#32218;&#65292;&#25918;&#20837; queue &#20013;&#20132;&#30001;&#19981;&#21516; worker thread &#34389;&#29702;&#12290;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;rust&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-rust">// reactor &#31034;&#24847; code
 impl Reactor {                                                                       
      // Reactor &#29544;&#31435;&#36939;&#20316;&#65292;&#21482;&#36000;&#36012;&#21914;&#37266;
      fn run(&amp;mut self) {                                                             
          loop {                                                                
              let timeout = self.nearest_timer_deadline();                            
              let ready_fds = epoll_wait(self.epoll_fd, timeout);               
                                                                                                              
              for (fd, token) in ready_fds {
                  self.run_queue.push(token.task_id);  // &#19999;&#36914;&#20849;&#20139; queue               
              }                                                                       
          }
      }
  }</code></pre></div><h3>Tokio &#30340; worker thread &#22914;&#20309;&#29986;&#29983;?</h3><p>&#30070; TCP server &#25910;&#21040;&#19968;&#20491;&#36899;&#32218;&#24460;&#22519;&#34892;&#30340; tokio::spawn(async move {...})&#65292;&#35442; spawn &#20006;&#19981;&#26371;&#21021;&#22987;&#21270; kernel thread&#65292;&#32780;&#26159;&#23559;&#36889;&#20491; async future &#29289;&#20214;&#25918;&#36914; worker queue&#65292;&#20132;&#30001;&#19981;&#21516; kernel thread (executor) &#22519;&#34892;&#12290;</p><p>&#27599;&#20491; kernel thread &#26371;&#24478; queue &#20013;&#29554;&#21462; future task &#20006;&#22519;&#34892; poll &#65292;&#27010;&#24565; code &#22914;&#19979;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;javascript&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-javascript"># Conceptual code example
fn worker_thread(run_queue: Arc&lt;SharedQueue&gt;, tasks: Arc&lt;TaskMap&gt;) {
    loop {
        let task_id = run_queue.pop(); // Blocks if queue is empty
        let task = tasks.get(task_id);
        match task.poll(&amp;mut cx) {
            Poll::Ready(()) =&gt; { tasks.remove(task_id); }
            Poll::Pending   =&gt; { /* Waker is registered, wait for next wake-up */ }
        }
    }
}</code></pre></div><p>&#32780; worker thread &#26371;&#22312;&#24314;&#31435; runtime &#30340;&#26178;&#20505;&#25351;&#23450; thread &#25976;&#37327;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:null}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext">#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
async fn main() {
    // your async code
}</code></pre></div><h3>Nested Future Task</h3><p>&#19978;&#38754; TCP Echo server &#20013;&#65292;worker thread &#34389;&#29702;&#30340;&#27599;&#20491; task &#26159;&#22519;&#34892; connection read &amp; write &#30340; nested future &#32080;&#27083;&#65306;</p><div class="highlighted_code_block" data-attrs="{&quot;language&quot;:&quot;plaintext&quot;,&quot;nodeId&quot;:&quot;ba04273a-45b9-4ac4-a106-64197daaf38e&quot;}" data-component-name="HighlightedCodeBlockToDOM"><pre class="shiki"><code class="language-plaintext"> spawn Future&#65288;ReadLoopFuture&#65289;                                                                           
    &#9492;&#9472;&#9472; socket.read().await&#65288;TcpReadFuture&#65289;                                                                  
          &#9492;&#9472;&#9472; non_blocking_read() &#8594; WouldBlock &#8594; register epoll                                                   
    &#9492;&#9472;&#9472; socket.write().await&#65288;TcpWriteFuture&#65289;                                                                
          &#9492;&#9472;&#9472; non_blocking_write() &#8594; WouldBlock &#8594; register epoll </code></pre></div><p>socket read &#33509;&#27794;&#36039;&#26009;&#65292;&#26371;&#25918;&#21040; epoll &#31561; reactor &#36890;&#30693;&#65292;&#27492;&#26178; executor &#25910;&#21040; socket.read() return &#30340; pending &#29376;&#24907;&#24460;&#26371;&#34389;&#29702;&#19979;&#19968;&#20491;&#36899;&#32218;&#30340; future&#65292;&#31561; reactor &#21914;&#37266;&#35442; future &#24460;&#24448;&#19979;&#34389;&#29702;&#36914;&#34892; write&#65292;&#21516;&#27171;&#33509; write buffer &#28415;&#20102;&#26371;&#22238; pending &#31561; reactor &#20877;&#27425;&#21914;&#37266;&#25165;&#23531;&#20837;&#25104;&#21151;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DeiY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DeiY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 424w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 848w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DeiY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png" width="1024" height="1524" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1524,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1403346,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!DeiY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 424w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 848w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 1272w, https://substackcdn.com/image/fetch/$s_!DeiY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F127dabc6-fbe4-46be-a4c3-68731e3e8c34_1024x1524.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#26368;&#32066;&#25972;&#20491; tokio runtime &#26550;&#27083;&#28858;&#65306;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t1Dh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t1Dh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t1Dh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:369390,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/191958789?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t1Dh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!t1Dh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbfc6ba1b-fe1c-4105-a6e0-01bcd19909b0_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#19979;&#38598;&#38928;&#21578;</h1><p>&#38614;&#28982; epoll &#25552;&#20379;&#26159;&#22823;&#37096;&#20998;&#39640;&#25928; server &#30340;&#24213;&#23652;&#25216;&#34899;&#39318;&#36984;&#65292;&#20294;&#38568;&#33879; kernel &#28436;&#36914;&#65292;&#20840;&#26032;&#30340; async I/O &#34389;&#29702;&#25216;&#34899;&#20063;&#38568;&#33879;&#20986;&#29694;&#65292;&#20063;&#23601;&#26159; io_uring&#65292;&#19988; rust tokio &#20063;&#26377;&#29992; io_uring &#23526;&#29694; async runtime&#65292;&#19979;&#19968;&#38598;&#26371;&#20171;&#32057; epoll &#30340;&#25928;&#33021;&#21839;&#38988;&#65292;&#20197;&#21450; io_uring &#22914;&#20309;&#35299;&#27770;&#65281;</p>]]></content:encoded></item><item><title><![CDATA[How to ACE the "Design a Rate Limiter" system design interview.]]></title><description><![CDATA[Rate Limiter Algorithm Explanation and Use Case Analysis]]></description><link>https://www.chillvic.dev/p/how-to-give-a-full-score-answer-to</link><guid isPermaLink="false">https://www.chillvic.dev/p/how-to-give-a-full-score-answer-to</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Sun, 22 Feb 2026 10:15:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!E3zn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Rate limiting is a critical component of system design and a frequently asked topic in technical interviews. To provide a &#8220;100-point&#8221; answer when asked how to implement a rate limiter, you need to go beyond just listing algorithms and demonstrate a deep understanding of their trade-offs, business applications, and distributed system challenges.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E3zn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E3zn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 424w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 848w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E3zn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png" width="1456" height="2170" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2170,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7608568,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/188783999?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E3zn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 424w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 848w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!E3zn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2872293-8946-41df-95a5-0bb06364b02b_1696x2528.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h1>Interviewer: How would you clearly explain the trade-offs and use cases of different rate limiting algorithms?</h1><p>The first step is to categorize rate limiters based on their logic.</p><h2><strong>Time-Window Based Limiters</strong></h2><p>These allow <code>N</code> requests within a specified timeframe. They are generally divided into <strong>Fixed Window</strong> and <strong>Sliding Window</strong>.</p><ul><li><p><strong>Fixed Window:</strong> Requests are counted within fixed time intervals (e.g., 1:00&#8211;1:01, 1:01&#8211;1:02).</p><ul><li><p><strong>Pros:</strong> Extremely simple to implement by mapping timestamps to buckets.</p></li><li><p><strong>Cons:</strong> The &#8220;Boundary Problem.&#8221; A burst of traffic can occur at the junction of two windows. </p><p>For example, if the limit is 100/min, a user could send 100 requests at 1:01:59 and another 100 at 1:02:00, effectively doubling the allowed rate in just two seconds.</p></li></ul></li><li><p><strong>Sliding Window:</strong> This tracks the exact arrival time of each request within the last <code>X</code> seconds.</p><ul><li><p><strong>Implementation:</strong> You can use a <strong>Redis Sorted Set</strong> to store every request timestamp. Before checking the limit, you remove all timestamps older than the current window (<code>current_time</code> - <code>window_size</code>).</p></li><li><p><strong>Trade-off:</strong> While this provides precise control and eliminates boundary bursts, the storage and processing costs are high for high-concurrency traffic.</p></li><li><p><strong>Optimization:</strong> An &#8220;Approximated Sliding Window&#8221; can be used, which divides the window into <code>N</code> smaller buckets. This sacrifices a bit of precision for high efficiency while still smoothing out the boundary spikes of the Fixed Window.</p></li></ul></li></ul><h3><strong>When to use Time-Window Limiters?</strong></h3><p>These are ideal for <strong>Quota Management</strong> where the goal is to limit total resource consumption over time.</p><ul><li><p><strong>Examples:</strong> Limiting a user to 10 SMS messages per hour or 1,000 API calls per day for a subscription tier.</p></li></ul><p><strong>The Downside:</strong> </p><p>If a user exhausts their 120-request-per-minute limit in the first 10 seconds, the server remains idle for the remaining 50 seconds. <strong>While acceptable for resource-intensive APIs</strong> (where waiting is a reasonable business constraint), this &#8220;forced idling&#8221; is detrimental to <strong>Service-Oriented APIs</strong> (e.g., payments or order placement), <strong>where every second of waiting could result in lost transactions or market opportunities.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h2>Usage-Based Limiters: Token Bucket &amp; Leaky Bucket</h2><p>For service-based APIs, the goal is <strong>Traffic Shaping</strong>&#8212;maintaining a stable, continuous flow rather than just enforcing a hard time-range quota.</p><h3><strong>Token Bucket</strong></h3><p>Tokens represent the &#8220;capacity&#8221; to process a request. Tokens are added to the bucket at a fixed <code>refill_rate</code> (e.g., 1 token/sec).</p><ul><li><p><strong>Implementation Detail:</strong> An efficient way to implement this is not through a background thread, but by recording the <code>last_request_arrival_time</code>. When a new request arrives, you calculate the <code>elapsed_time</code>, multiply it by the <code>refill_rate</code> to determine how many tokens to add, and cap it at the bucket&#8217;s maximum capacity.</p></li><li><p><strong>Behavior:</strong> For a limit of 120/min, you could set an initial capacity of 120 tokens and a refill rate of 2/sec. If a user exhausts all 120 tokens in the first second, they don&#8217;t have to wait 50 seconds; they can continue at a steady pace of 2 requests per second as tokens refill.</p></li><li><p><strong>The &#8220;Burst&#8221; Logic:</strong> <strong>The Token Bucket algorithm allows for an initial burst of traffic; however, once a burst occurs, it immediately throttles the rate, converting the traffic into a stable flow.</strong></p></li></ul><h3><strong>Leaky Bucket</strong></h3><p><strong>If your system strictly cannot allow bursts, you should use the Leaky Bucket instead.</strong> Imagine a bucket with a fixed capacity and a hole at the bottom. Requests enter the bucket and are "leaked" (processed) at a constant, predetermined rate.</p><ul><li><p><strong>Implementation Detail:</strong> Rather than a literal queue data structure, an efficient implementation uses time. You maintain a <code>next_free_time</code> variable representing when the next request can be processed:</p><p>Python</p></li></ul><pre><code><code>if request_arrival_time &gt;= next_free_time:
    process_immediately()
    next_free_time = now() + interval
else:
    wait_time = next_free_time - request_arrival_time
    next_free_time += interval
    sleep(wait_time) # or reject if the queue capacity is exceeded</code></code></pre><ul><li><p><strong>Behavior:</strong> With a processing rate of 2/sec (0.5s interval), the Leaky Bucket guarantees that exactly one request is handled every 0.5 seconds, effectively smoothing out any spikes. The bucket&#8217;s &#8220;capacity&#8221; here refers to how many requests can be queued before they are rejected, but it never allows for a simultaneous burst of processing.</p></li></ul><h3><strong>Practical Comparison:</strong></h3><p>In most real-world scenarios, the <strong>Token Bucket</strong> is more practical because it offers better responsiveness by allowing minor bursts. However, the <strong>Leaky Bucket</strong> is essentially a "simplified queue." </p><p>For instance, if an admin triggers 10 heavy report-generation tasks at once, a Leaky Bucket prevents OOM (Out of Memory) by forcing them to be processed sequentially at a safe speed, achieving the benefits of a Queue + Worker setup without the architectural complexity.<br></p><h1>Interviewer: How to Configure Rate Limit Parameters?</h1><p>When an interviewer asks how to set the actual limits, they aren&#8217;t looking for a magic number. They want to see a <strong>Configuration Strategy</strong>:</p><ul><li><p><strong>Identify Dimensions:</strong></p><ul><li><p><strong>Authenticated APIs:</strong> Limit by <code>User ID</code> or <code>API Key</code>.</p></li><li><p><strong>Public APIs:</strong> Limit by <code>IP Address</code>. (Note the caveat: multiple users might share one IP, such as a corporate Wi-Fi, and you must handle proxy headers to get the true client IP).</p></li></ul></li><li><p><strong>Identify Resource Bottlenecks:</strong></p><ul><li><p>Analyze the capacity of your Database, Redis, or third-party services (SMTP, SMS gateways). Know the connection pool limits and the external provider&#8217;s own rate limits.</p></li></ul></li><li><p><strong>Data-Driven Decisions:</strong> * Don&#8217;t guess. Analyze production logs to find <strong>P95 and P99</strong> request rates as a baseline.</p><ul><li><p>Set up real-time monitoring to observe <strong>429 (Too Many Requests)</strong> error rates and system resource utilization to fine-tune the limits dynamically.</p></li></ul></li></ul><h1>Interviewer: How to implement a Distributed Rate Limiter?</h1><p>As systems scale, rate limiting data must be stored in a centralized store, typically <strong>Redis</strong>. The primary challenge here is handling <strong>Race Conditions</strong> under high concurrency.</p><p><strong>The Atomicity Problem:</strong> Even though Redis is single-threaded, a rate-limiting check involves multiple steps: <code>GET counter</code>, <code>CHECK if limit exceeded</code>, and <code>INCR counter</code>. If these are sent as separate commands, Redis cannot guarantee they will be executed as one uninterrupted block. Two different API servers could <code>GET</code> the same counter value, both pass the <code>CHECK</code>, and both increment it, leading to a limit violation.</p><p><strong>The Solution: Lua Scripts</strong> </p><p>To ensure <strong>Atomicity</strong>, you must wrap the logic in a <strong>Lua script</strong> and send it to Redis. Redis executes the entire script as a single, atomic command. During its execution, no other commands can interfere. </p><p>This prevents race conditions entirely and ensures that the &#8220;check-and-set&#8221; logic is thread-safe across the entire distributed cluster.</p>]]></content:encoded></item><item><title><![CDATA[The Core of Concurrent Programming: Atomic Operations are Not Locks]]></title><description><![CDATA[An Analysis of CPU MESI and Atomic Mechanisms]]></description><link>https://www.chillvic.dev/p/the-core-of-concurrent-programming</link><guid isPermaLink="false">https://www.chillvic.dev/p/the-core-of-concurrent-programming</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Fri, 13 Feb 2026 22:13:52 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!b_rP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Atomic operations are the bedrock of concurrent programming. For instance, a Mutex is often implemented using an atomic <strong>Compare-And-Swap (CAS)</strong> at its lowest level. While many initially view Atomics as "CPU-level locks," they are actually mechanisms designed to solve memory synchronization issues across multiple CPU cores in a concurrent read/write environment.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b_rP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b_rP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!b_rP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>The Problem: Why Multi-Core Concurrency Fails</h1><p>Due to physical limitations in hardware circuitry, larger memory capacity results in higher latency. To mitigate this, CPUs use a <strong>Memory Hierarchy</strong> to reduce average access costs.</p><p>Each core uses <strong>Registers</strong> to store data during execution. To avoid hitting the RAM for every variable, cores read data in <strong>Cache Lines (typically 64B)</strong>. These are stored in different cache levels:</p><ul><li><p><strong>L1:</strong> Private to each core.</p></li><li><p><strong>L2:</strong> Mostly private (architecture dependent).</p></li><li><p><strong>L3:</strong> Shared across all cores.</p></li><li><p><strong>RAM:</strong> The final main memory storage.</p></li></ul><p>When a core looks for data, it checks L1 &#8594; L2 &#8594; L3 &#8594; RAM. Once found, the data is filled back up the chain (L3 &#8594; L2 &#8594; L1). If a cache is full, it evicts existing lines to make room.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rqNm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rqNm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rqNm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>The <code>x++</code> Race Condition</h3><p>When multiple cores execute <code>x++</code>, the actual instructions are:</p><pre><code>LOAD x &#8594; register
ADD 1
STORE register &#8594; x</code></pre><p>If every STORE had to go straight to RAM, the CPU would be unbearably slow. Instead, modern processors use a per-core store buffer, which temporarily queues writes. These updates are handed off to the cache subsystem, committed into the L1 cache line, and the line is marked dirty. <strong>The data is not written back to RAM (or the lower cache levels such as L3) immediately&#8212;it is propagated only later, typically when the cache line is evicted or a coherence event forces it.</strong></p><p>Because L1 is not shared, two threads on different cores might have their own local copies of the same cache line. They increment their local versions, leading to a <strong>Race Condition</strong> where updates are lost.</p><h1>The Solution: Cache Coherency and MESI</h1><p>To solve this, hardware designers created a <strong>Coherence Interconnect</strong>&#8212;a high-speed pathway for cores to communicate. Rather than constantly sending data, they exchange <strong>Cache Line States</strong> to establish <strong>Ownership and and trigger write-backs</strong>. This is governed by the <strong>MESI Protocol</strong>:</p><ul><li><p><strong>M (Modified):</strong> This core has modified the line; it&#8217;s the only copy, and it&#8217;s &#8220;dirty&#8221; (not yet in RAM).</p></li><li><p><strong>E (Exclusive):</strong> This core is the only one with this line; it matches RAM.</p></li><li><p><strong>S (Shared):</strong> Multiple cores have this line; it matches RAM.</p></li><li><p><strong>I (Invalid):</strong> The data is stale because another core modified it.</p></li></ul><h3>The MESI State Machine</h3><ul><li><p><strong>I &#8594; E/S:</strong> Core reads the latest state.</p></li><li><p><strong>S &#8594; M:</strong> Core gains ownership to write.</p></li><li><p><strong>M &#8594; I:</strong> Core receives a notification that another core modified the line.</p></li><li><p><strong>M &#8594; S:</strong> Core sees another core wants to read; it immediately syncs to L3/RAM and changes to Shared.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VsWl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VsWl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!VsWl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To ensure only one core has ownership at a time, the system uses a <strong>BUS</strong> (or Ring/Mesh interconnect). Cores <strong>Snoop</strong> on the BUS for requests and must return an <strong>ACK</strong> before the BUS processes the next request.</p><h3><strong>The Role of the BUS in Serialization:</strong> </h3><p>The coherence interconnect (the BUS) acts as a bottleneck by design to enforce order. When multiple cores try to write to the same cache line, the BUS serializes these requests, ensuring they are handled one by one.</p><p>For instance, if Core <code>A</code> and Core <code>B</code> simultaneously issue a <code>PrWr</code> (Processor Write) request for cache line <code>X</code> to the BUS:</p><ol><li><p>Through bus <strong>arbitration</strong>, the system <strong>serializes</strong> the requests and handles Core A's operation first.</p></li><li><p>When Core B receives Core A&#8217;s <code>PrWr</code> via snooping, it transitions its own cache line X state to <strong>I (Invalid)</strong>. Once Core A receives ACKs from all cores, it changes its state to <strong>M (Modified)</strong>.</p></li><li><p>Subsequently, when the BUS processes Core B&#8217;s <code>PrWr</code>, Core B finds its state is already <strong>I</strong> and cannot transition directly to <strong>M</strong>. It returns a <strong>NACK</strong> and switches to a <strong>PrRd</strong> (Processor Read) request.</p></li><li><p>Once Core A completes its update and receives Core B&#8217;s <strong>PrRd</strong>, it detects that its own state is <strong>M</strong>, <strong>triggers a write-back</strong> to L3 or RAM, and changes its state to <strong>S (Shared)</strong>. This allows Core B to read the latest data.</p></li><li><p>Finally, Core B issues another <code>PrWr</code> to obtain ownership and executes its update.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hVN_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hVN_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hVN_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5693263,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187900483?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hVN_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!hVN_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60092699-5e59-4839-a06e-3c1fe884de5d_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><blockquote><p><strong>In simple terms, MESI defines the state machine for cache lines, while the centralized BUS serializes the order of state transitions.</strong> <br><br>Through the snooping and ACK mechanisms, multiple cores ensure that at any given point, cache line states are consistent across the system&#8212;preventing two cores from both holding the <strong>M</strong> state for the same cache line simultaneously.</p></blockquote><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h1>Why MESI Isn&#8217;t Enough: The Need for Atomic Instructions</h1><p>Even with MESI, the <code>x++</code> problem persists because <code>x++</code> is multiple steps. <br><br>The <code>x++</code> operation is composed of three distinct steps:</p><pre><code>LOAD x &#8594; register
ADD 1
STORE register &#8594; x</code></pre><p>This process requires two separate state transitions: <br><br>1. The LOAD triggers an <code>I</code> &#8594; <code>S</code> transition, where the CPU moves <code>x</code> into a register and then performs the ADD. <br>2. Later, the STORE triggers an <code>S</code> &#8594; <code>M</code> transition to write the data back to L1 (and eventually to L3 or RAM when requested).<br></p><p>The problem is that <strong>simultaneous LOADs are perfectly legal</strong> under the MESI protocol. If Core A and Core B both LOAD <code>x</code> into their registers at the same time, they each hold a local copy of the same value. Even though Core B&#8217;s <strong>STORE</strong> will technically wait for Core A to finish its write (due to BUS arbitration), Core B will still perform the <strong>ADD</strong> using the <strong>stale value already sitting in its register</strong>.</p><p>In short, the <strong>Invalid (I)</strong> state transition only affects the cache line; it does <strong>not</strong> affect data that has already been moved into a register. <strong>Once a value is in the register, it is &#8220;locked in&#8221; for that execution cycle.</strong></p><h3>How Atomic Solves the Race: <code>LOCK XADD</code></h3><p>To resolve this, atomic operations transform <code>x++</code> into a specialized instruction, such as LOCK XADD. This instruction forces the entire read-modify-write sequence to act as a single, indivisible state transition.</p><p>For example, when two cores execute <code>loop { atomic.add(&amp;x, 1) }</code>, they both issue a write request (<code>PrWr</code>) to the BUS:</p><ul><li><p>If the BUS arbitrates and processes Core A first, Core A will execute the <strong>entire LOAD &#8594; ADD &#8594; STORE sequence in one go</strong>.</p></li><li><p>Core A will <strong>not return an ACK</strong> to any subsequent requests until all three steps are completed.</p></li></ul><p>This ensures that by the time the BUS handles the next request (from Core B), Core A has already synchronized the updated data to the system. This effectively prevents the possibility of Core A and Core B performing a <strong>simultaneous LOAD</strong> into their registers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cloi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cloi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!cloi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!cloi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!cloi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cloi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:333797,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187900483?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cloi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!cloi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!cloi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!cloi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91508a20-c798-4a4e-a4c4-2d4b8f718696_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This atomic mechanism is not limited to <code>x++</code> or <code>x--</code> it is also the foundation for the <strong>CAS (Compare-And-Swap)</strong> operation:</p><pre><code>// Conceptual CAS Logic
LOAD flag  -&gt; register
if flag == 0
   flag  =  1
   STORE register -&gt; flag
   STORE true -&gt; ok
else 
   STORE false -&gt; ok</code></pre><p><strong>CAS is the heart of a Mutex:</strong></p><pre><code>// Conceptual Logic
lock   =&gt; for (!atomic.CAS(&amp;lock, 0, 1)) { /* spin/park */ }
unlock =&gt; atomic.CAS(&amp;lock, 1, 0)</code></pre><h1>Optimization Pitfalls: Store Buffers and Invalidation Queues</h1><p><strong>If Atomics solve the multi-step problem, why do we need atomic.Load and atomic.Store for single-step operations?</strong> </p><p>The answer lies in Performance Optimizations that cause Memory Reordering.</p><p>While MESI ensures cache consistency, the <strong>ACK mechanism</strong> introduces two significant performance bottlenecks (latencies):</p><ul><li><p><strong>Write Latency:</strong> When Core A issues a <code>PrWr</code>, it must wait for ACKs from all other cores (confirming they&#8217;ve invalidated their copies) before it can actually commit the update. During this period, the core is forced to <strong>stall</strong>.</p></li><li><p><strong>Invalidation Latency:</strong> When Core B receives Core A&#8217;s <code>PrWr</code>, it must mark its L1 cache line as <strong>Invalid (I)</strong> before sending the ACK. Even though cache is fast, it is significantly slower than registers. If the cache is busy, Core A stays stalled even longer.</p></li></ul><p>To optimize performance, CPUs implement <strong>Delayed Execution</strong>:</p><p><strong>The Hardware &#8220;Cheats&#8221;: Store Buffers &amp; Invalidation Queues</strong></p><ul><li><p><strong>To solve Write Latency:</strong> Instead of stalling, the core places the write command into a <strong>Store Buffer</strong> (a local queue) and moves on to the next instruction immediately.<br></p><p><em><strong>Example:</strong></em>  a = 1; b = y;</p><pre><code>STORE 1 =&gt; a
LOAD y =&gt; register
STORE register =&gt; b</code></pre><p>For the <code>STORE 1 =&gt; a</code> is buffered while the core proceeds to <code>LOAD y</code>. If <code>y</code> is already in a valid local state (<strong>E</strong> or <strong>S</strong>), the core loads it into a register and then executes <code>STORE register =&gt; b</code> before the original <code>a = 1</code> even receives its ACKs.</p></li><li><p><strong>To solve Invalidation Latency:</strong> When a core receives a <code>PrWr</code> from another core, it doesn&#8217;t invalidate its cache line immediately. Instead, it places the request into an <strong>Invalidation Queue</strong> and sends the ACK instantly to let the sender continue.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BjKx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BjKx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BjKx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>The Consequence: Store-Load Reordering</h3><p>These optimizations cause the order of memory operations to &#8220;desync&#8221; from the code&#8217;s intended sequence. Consider this classic scenario:</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
x = 1 
if y == 0 {
   x_res = 1
}

core B
y = 1
if x == 0 {
   y_res = 1
}</code></pre><p><strong>In a perfectly sequential world, it is impossible for both </strong><code>x_res</code><strong> and </strong><code>y_res</code><strong> to be </strong><code>1</code><strong>. At least one write must "happen" before the other core&#8217;s check.</strong></p><p>However, if both Core A and Core B use <strong>Store Buffers</strong>, they might both buffer their respective writes (<code>x=1</code> and <code>y=1</code>) and execute their loads (<code>if y == 0</code> and <code>if x == 0</code>) before the buffers are flushed. The result? Both cores see <code>0</code> for the variables, and both <code>x_res</code> and <code>y_res</code> become <code>1</code>. This is the <strong>Store-Load Reorder</strong> problem.</p><h1>How Atomics Fix Reordering?</h1><p><code>atomic.Store</code> and <code>atomic.Load</code> solve this by inserting <strong>Memory Barriers</strong> (Fences):</p><h2><code>1. atomic.Store</code> as a Write Barrier</h2><p><code>atomic.Store(&amp;x, 1)</code> essentially tells the CPU: &#8220;You cannot execute any subsequent <code>LOAD</code> instructions until the <strong>Store Buffer</strong> is cleared.&#8221; It forces the core to wait until <code>x = 1</code> receives its ACKs and is committed to the cache.</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1) 
// barrier: Before loading y into the register, the store operation on x must be completed first.
if y == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1) 
// barrier: Before loading x into the register, the store operation on y must be completed first.
if x == 0 {
   y_res = 1
}</code></pre><p><strong>However, relying solely on atomic.Store does not prevent the following race condition, as invalidation queue optimizations may delay visibility across cores.</strong></p><ol><li><p><strong>Core A</strong> executes <code>atomic.Store(&amp;x, 1)</code>. The write barrier forces it to clear its <strong>Store Buffer</strong>, ensuring <code>x=1</code> is sent out to the coherence interconnect (BUS) via a <code>PrWr</code> (Processor Write) request.</p></li><li><p><strong>Core B</strong> receives Core A&#8217;s <code>PrWr</code> for <code>x</code>. However, to optimize performance, Core B does <strong>not</strong> immediately mark its local cache line as <strong>I (Invalid)</strong>. Instead, it places the invalidation request into its <strong>Invalidation Queue</strong> and sends back an <strong>ACK</strong> immediately.</p></li><li><p><strong>Core B</strong> then proceeds to its next instruction: <code>if x == 0</code>.</p></li><li><p>Since Core B has not yet processed its <strong>Invalidation Queue</strong>, its local cache still thinks <code>x</code> is valid and contains the value <code>0</code>. Core B reads this <strong>stale value</strong>, leading to <code>y_res=1</code>.</p></li><li><p>The same sequence happens inversely for Core A reading <code>y</code>.</p></li></ol><p><strong>Ultimately, even though the write was &#8220;sent,&#8221; the receiver&#8217;s &#8220;read&#8221; was still stale because the invalidation hadn&#8217;t been applied.</strong></p><h2>2. <code>atomic.Load</code> as a Read Barrier</h2><p>To prevent this, we must use <code>atomic.Load</code>, which acts as a <strong>Read Barrier</strong>. Before the <code>LOAD</code> instruction is executed, it forces the core to check its <strong>Invalidation Queue</strong>.</p><ul><li><p>If the target cache line is pending in the queue, the core <strong>must</strong> process the invalidation first.</p></li><li><p>The core then sees the line is <strong>Invalid (I)</strong> and is forced to fetch the latest value <code>x=1</code> from the interconnect.</p></li></ul><p>By combining <code>atomic.Store</code> and <code>atomic.Load</code>:</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1) 
if atomic.Load(&amp;y) == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1) 
if atomic.Load(&amp;x) == 0 {
   y_res = 1
}</code></pre><p>The <strong>Store Buffer</strong> is flushed on the sender's side, and the <strong>Invalidation Queue</strong> is prioritized on the receiver's side. This synchronization ensures that <code>x_res</code> and <code>y_res </code>will <strong>never</strong> both be <code>1</code>.</p><blockquote><p><strong>Technical Note:</strong> While the Invalidation Queue is conceptually a &#8220;queue,&#8221; the hardware implementation often uses status bits/metadata to check for pending invalidations instantly, rather than performing a linear scan.</p></blockquote><h1>Go&#8217;s High-Level Encapsulation Compared to Rust&#8217;s Fine-Grained Memory Ordering Control</h1><h2>Go&#8217;s High-Level Encapsulation</h2><p>Go&#8217;s <code>atomic.Load</code> and <code>atomic.Store</code> provide <strong>Sequential Consistency (SeqCst)</strong>.</p><ul><li><p><code>atomic.Store</code><strong>:</strong> Acts as a <strong>Write Barrier</strong>, forcing the Store Buffer to clear before moving on.</p></li><li><p><code>atomic.Load</code><strong>:</strong> Acts as a <strong>Read Barrier</strong>, forcing the core to process its Invalidation Queue to ensure it sees the most recent state.</p></li></ul><h2>Rust&#8217;s Granular Control</h2><p>While Go keeps it simple, Rust allows you to specify the <strong>Memory Ordering</strong>:</p><h3><strong>Release/Acquire:</strong> </h3><ul><li><p>Release (on Store): Ensures all <strong>previous</strong> writes are visible.</p></li><li><p>Acquire (on Load): Ensures all <strong>subsequent</strong> reads see the latest data.</p></li></ul><p>The Release/Acquire pattern in above scenario does not fully prevent the race condition:</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1, Release) 
if atomic.Load(&amp;y, Acquire) == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1, Release) 
if atomic.Load(&amp;x, Acquire) == 0 {
   y_res = 1
}</code></pre><p> Since the Release barrier precedes the STORE to x, the write may remain in the store buffer and be propagated later, while the LOAD of y can still be issued beforehand.</p><h4>Proper Use Case for the Release/Acquire Pattern:</h4><p>Consider the following scenario:</p><pre><code>flag = 0, x = 0
core A
x = 1 
atomic.Store(&amp;flag, 1, Release)

core B
for atomic.Load(&amp;flag, Acquire) { print(x) } // guarentee to print 1</code></pre><p>The Release/Acquire pattern is intended for one-way &#8220;handover&#8221; synchronization.</p><p>Core A performs the data write <code>x = 1</code> and then publishes the update by storing to flag with <code>Release</code> semantics. Core B spins on flag using <code>Acquire</code> semantics; once it observes <code>flag == 1</code>, it is guaranteed to see the preceding write to x.</p><p>Release/Acquire is designed for "Handover" (One-way) synchronization, making them faster but trickier to use than SeqCst.</p><h3><strong>Relaxed</strong>:</h3><p>No barriers at all. It only guarantees that the operation itself is atomic (no torn writes), but provides no guarantees about the order of surrounding instructions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7Ur4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>Example code demonstrating the use of atomic in Go and Rust</h1><h2>Go: Flag Competition &amp; Memory Visibility</h2><p>The following Go code demonstrates two goroutines competing for a flag. Because we use <code>atomic.Store</code> and <code>atomic.Load</code> (which provide <strong>Sequential Consistency</strong> in Go), the program remains stable.</p><pre><code>func main() {
&#9;for i := 0; ; i++ {
&#9;&#9;x, y, r1, r2 := int64(0), int64(0), int64(0), int64(0)
&#9;&#9;wg := sync.WaitGroup{}
&#9;&#9;wg.Add(2)

&#9;&#9;go func() {
&#9;&#9;&#9;defer wg.Done()
&#9;&#9;&#9;atomic.StoreInt64(&amp;x, 1)
&#9;&#9;&#9;if atomic.LoadInt64(&amp;y) == 0 { // if Store-Load reordering occurs, y will read 0
&#9;&#9;&#9;&#9;r1 = 1
&#9;&#9;&#9;}
&#9;&#9;}()

&#9;&#9;go func() {
&#9;&#9;&#9;defer wg.Done()
&#9;&#9;&#9;atomic.StoreInt64(&amp;y, 1)
&#9;&#9;&#9;if atomic.LoadInt64(&amp;x) == 0 { // if Store-Load reordering occurs, x will read 0
&#9;&#9;&#9;&#9;r2 = 1
&#9;&#9;&#9;}
&#9;&#9;}()

&#9;&#9;wg.Wait()
&#9;&#9;if r1 == 1 &amp;&amp; r2 == 1 {
&#9;&#9;&#9;&#9;&#9;&#9;panic(fmt.Sprintf("Observed reordering at iteration %d: r1=%d, r2=%d\n", i, r1, r2))

&#9;&#9;}
&#9;}
}</code></pre><h4>How to Break It:</h4><ol><li><p><strong>Remove </strong><code>atomic.Load</code><strong>:</strong> Change the reader to a simple <code>if flag == 1</code>. Because the <strong>Invalidation Queue</strong> might not be flushed immediately, the reader might see <code>flag == 1</code> but still read a stale <code>x == 0</code> from its own cache. It will likely panic after a short run.</p></li><li><p><strong>Remove both Store and Load:</strong> Use standard assignments (<code>flag = 1</code>). The <strong>Store Buffer</strong> and <strong>Caching</strong> will cause the program to panic almost instantly, as the cores will be completely out of sync regarding the order of operations.</p></li></ol><h2>Rust: Fine-Grained Ordering (Release/Acquire)</h2><p>While Go simplifies things with a single atomic model, Rust allows us to use <strong>Release</strong> and <strong>Acquire</strong> semantics. This is perfect for &#8220;Handover&#8221; scenarios where one thread produces data and another consumes it.</p><h4>Rust Example: Ordered Printing (1-1000)</h4><p>In this example, two threads work in tandem to print numbers. We use <code>Release</code> when writing to the flag to ensure previous memory operations are visible, and <code>Acquire</code> when reading to ensure we see those changes.</p><pre><code><code>use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;

fn main() {
    let n = 1000;
    let i = Arc::new(AtomicUsize::new(1));
    let flag = Arc::new(AtomicBool::new(true));
    let (i_a, flag_a) = (Arc::clone(&amp;i), Arc::clone(&amp;flag));
    let t1 = thread::spawn(move || {
        loop {
            if flag_a.load(Ordering::Acquire) {
                let current: usize = i_a.fetch_add(1,Ordering::Relaxed);
                if current &gt; n {
                    flag_a.store(false, Ordering::Release);
                    break;
                }
                println!("{}", current);
                flag_a.store(false, Ordering::Release);
            }
        }
    });

    let (i_b, flag_b) = (Arc::clone(&amp;i), Arc::clone(&amp;flag));
    let t2 = thread::spawn(move || {
        loop {
            if !flag_b.load(Ordering::Acquire) {
                let current: usize = i_b.fetch_add(1,Ordering::Relaxed);
                if current &gt; n {
                    flag_b.store(true, Ordering::Release);
                    break;
                }
                println!("{}", current);
                flag_b.store(true, Ordering::Release);
            }
        }
    });
    t1.join().unwrap();
    t2.join().unwrap();
}</code></code></pre><h4>Why this works:</h4><ul><li><p><code>Ordering::Release</code><strong>:</strong> On the <code>store</code>, it acts as a gatekeeper. It ensures that every memory operation before this store (like the <code>println!</code> or any data modification) is completed and visible to other cores.</p></li><li><p><code>Ordering::Acquire</code><strong>:</strong> It forces the core to process its Invalidation Queue before the load, ensuring the cache line is fully up-to-date. This allows the core to &#8220;see&#8221; data synchronized by another core&#8217;s Release.</p></li><li><p><strong>Performance:</strong> This is more efficient than the default <code>SeqCst</code> (Sequential Consistency) because it doesn&#8217;t require a total global ordering across all cores&#8212;only a synchronized &#8220;handshake&#8221; between these two specific threads.</p></li></ul>]]></content:encoded></item><item><title><![CDATA[併發程式的核心 - Atomic 不是鎖，那 Atomic 是什麼？]]></title><description><![CDATA[CPU MESI &#20197;&#21450; Memory Ordering &#35299;&#26512;]]></description><link>https://www.chillvic.dev/p/atomic-atomic</link><guid isPermaLink="false">https://www.chillvic.dev/p/atomic-atomic</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Fri, 13 Feb 2026 06:39:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!b_rP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>atomic &#26159;&#20341;&#30332;&#31243;&#24335;&#22522;&#30990;&#65292;&#20363;&#22914; mutex &#24213;&#23652;&#26159;&#29992; atomic Compare And Swap &#23526;&#29694;&#30340;&#65292;&#28982;&#32780;&#22312;&#25105;&#28145;&#20837;&#30740;&#31350;&#37666;&#65292;&#23565; atomic &#35469;&#30693;&#26159; CPU &#23652;&#32026;&#37782;&#65292;&#30740;&#31350;&#30332;&#29694;&#65292;atomic &#26159;&#29992;&#20358;&#35299;&#27770;&#22810;&#26680;&#20341;&#30332;&#35712;&#23531;&#36039;&#26009;&#24773;&#27841;&#19979;&#65292;&#19981;&#21516; cpu core  &#20043;&#38291;&#21516;&#27493;&#35352;&#25014;&#39636;&#36039;&#26009;&#30340;&#27231;&#21046;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b_rP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b_rP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2035465,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!b_rP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!b_rP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35b46c0e-9f57-4eb7-9302-33ef832d2932_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#29694;&#20195; CPU &#26550;&#27083;&#28858;&#20309;&#65311;&#22914;&#20309;&#36319; RAM &#20132;&#20114;&#65311;</h1><p>&#30001;&#26044;&#21463;&#38480;&#30828;&#39636;&#38651;&#36335;&#30340;&#29289;&#29702;&#38480;&#21046;&#65292;&#35352;&#25014;&#39636;&#23481;&#37327;&#36234;&#22823;&#65292;CPU &#23384;&#21462;&#24310;&#36978;&#36234;&#39640;&#65292;&#22240;&#27492; CPU &#25505;&#29992;&#22810;&#23652;&#35352;&#25014;&#39636;&#38542;&#23652;&#65288;memory hierarchy&#65289;&#20358;&#38477;&#20302;&#24179;&#22343;&#23384;&#21462;&#25104;&#26412;&#12290;</p><p>&#27599;&#20491; core &#22519;&#34892;&#26178;&#26371;&#29992; register &#20786;&#23384;&#36939;&#31639;&#26178;&#29992;&#30340;&#36039;&#26009;&#65292;&#28858;&#36991;&#20813;&#27599;&#22519;&#34892;&#21040;&#19968;&#20491;&#35722;&#25976;&#23601;&#23384;&#21462;&#19968;&#27425;&#35352;&#25014;&#39636;&#65292;core &#24050; cache line (64B) &#28858;&#21934;&#20301;&#24478;&#35352;&#25014;&#39636;&#35712;&#36039;&#26009;&#12290;</p><p>&#35712; cache line &#19981;&#26371;&#25972;&#22376;&#25918;&#21040; register &#20013;&#65292;&#32780;&#26159;&#25918;&#22312;&#19981;&#21516;&#23652;&#32026;&#30340; cache&#65292;&#20363;&#22914; L1, L2, L3 &#36319; RAM&#65306;</p><ul><li><p>L1 &#26159;&#27599;&#20491; core &#31169;&#26377;</p></li><li><p>L2 &#22810;&#25976;&#20063;&#26159; per-core &#31169;&#26377;&#65292;&#37096;&#20998;&#26550;&#27083;&#21487;&#20849;&#20139;</p></li><li><p>L3 &#26159;&#22810; core &#20849;&#20139;&#30340;&#26368;&#24460;&#19968;&#23652;&#24555;&#21462;</p></li><li><p>RAM &#26368;&#32066;&#30340;&#20027;&#35352;&#25014;&#39636;&#23384;&#20786;</p></li></ul><p>&#22823;&#23567;&#26159; RAM &gt; L3 &gt; L2 &gt; L1 &gt; register &#65292;&#23384;&#21462;&#36895;&#24230;&#21063;&#30456;&#21453;&#12290;<br><br>core &#26597;&#36039;&#26009;&#26178;&#65292;&#26371;&#24478; L1 &#38283;&#22987;&#26597;&#65292;miss &#24460;&#25214; L2  -&gt; L3 -&gt; RAM&#65292;&#25214;&#21040;&#36039;&#26009;&#24460;&#26371;&#19968;&#36335;&#22238;&#22635;&#65292;&#25918;&#21040; L3 -&gt; L2 -&gt; L1&#65292;&#22238;&#22635;&#26178;&#35352;&#25014;&#39636;&#19981;&#22816;&#23601;&#26371; evict &#20854;&#20182; cache line &#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rqNm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rqNm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7985507,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rqNm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!rqNm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb171134f-c3b4-4f29-a6f1-83ecbcce1c9d_2816x1536.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>&#22810;&#23652;&#35352;&#25014;&#39636;&#26550;&#27083;&#26371;&#26377;&#20160;&#40636;&#21839;&#38988;&#65311; </h1><p>&#30070;&#27599;&#20491; core &#22519;&#34892; x++ &#23526;&#38555;&#25351;&#20196;&#26159;:</p><pre><code>LOAD x &#8594; register
ADD 1
STORE register &#8594; x</code></pre><p>&#33509;&#27599;&#27425; STORE &#37117;&#23531;&#22238; RAM&#65292;core &#22519;&#34892;&#36895;&#24230;&#26371;&#35722;&#24930;&#65292;&#22240;&#27492; CPU &#35373;&#35336;&#26371;&#23559;&#32080;&#26524;&#20786;&#23384;&#22312;&#21478;&#19968;&#20491;&#30828;&#39636;&#20803;&#20214;&#65292;store buffer&#65292;&#30456;&#30070;&#26044;&#27599;&#20491; core &#21508;&#33258;&#30340; queue&#65292;&#20132;&#30001; cache subsystem &#23559;&#26356;&#26032;&#25552;&#20132;&#21040; L1 cache line &#20006;&#27161;&#35352;&#28858; dirty&#65292;&#30070; cache line eviction &#25110;&#32773;&#25910;&#21040; coherence write back &#20107;&#20214;&#26178;&#23531;&#22238;&#26356;&#20302;&#23652;&#24555;&#21462;&#25110; RAM&#12290;</p><p>&#39006;&#20284;&#26368;&#32066;&#19968;&#33268;&#24615;&#65292;&#20808;&#25552;&#20132;&#21040; L1&#65292;&#20294; L1 &#19981;&#26159;&#20849;&#20139;&#30340;&#65292;&#35201;&#31561; L1 &#28415;&#20102;&#35442; cache line &#34987; evict &#25165;&#26371;&#36880;&#27493;&#23531;&#22238; RAM&#65292;&#26368;&#32066;&#35731;&#20854;&#20182; core &#35712;&#21040;&#26356;&#26032;&#24460;&#36039;&#26009;&#12290;</p><p>&#22240;&#27492;&#30070;&#20841;&#20491; thread &#22312;&#19981;&#21516; core &#22519;&#34892; <code>for loop { x++ }</code> &#65292;&#19981;&#21516; core &#23559;&#36039;&#26009;&#25918;&#21040;&#21508;&#33258;&#30340; L1 cache&#65292;&#36617;&#20837; register &#36939;&#31639;&#65292;&#22519;&#34892;&#23436;&#22312;&#23531;&#22238; L1 cache&#65292;&#24444;&#27492;&#20043;&#38291;&#26377;&#33879;&#30456;&#21516; cache line &#30340;&#19981;&#21516;&#21103;&#26412;&#65292;&#29992;&#21508;&#33258;&#21103;&#26412;&#21453;&#35206;&#22519;&#34892; ++ &#65292;&#26368;&#32066;&#32080;&#26524;&#26371; race condition&#12290;<br></p><h1>CPU &#22810;&#26680;&#26550;&#27083;&#22914;&#20309;&#35299;&#27770; cache line &#36039;&#26009;&#19981;&#21516;&#27493;&#30340;&#21839;&#38988;&#65311;</h1><p>&#35201;&#35299;&#27770;&#19981;&#21516;&#27493;&#21839;&#38988;&#65292;&#39318;&#20808;&#35201;&#24314;&#31435;&#39640;&#25928;&#30340;&#21516;&#27493;&#31649;&#36947;&#65292;&#22810; core &#20043;&#38291;&#22312;&#30828;&#39636;&#35373;&#35336;&#19978;&#26371;&#26377; coherence interconnect &#36890;&#36947;&#20132;&#25563; cache line &#36039;&#35338;&#12290;</p><p>&#35442;&#36890;&#36947;&#20027;&#35201;&#19981;&#26159;&#29992;&#20358;&#20132;&#25563;&#26356;&#26032;&#24460;&#30340; cache line &#20839;&#23481;&#65292;&#22240;&#28858;&#20341;&#30332;&#23531;&#20837;&#30340;&#24773;&#22659;&#20013;&#65292;&#25105;&#20497;&#38656;&#35201;&#30906;&#20445;&#23531;&#20837;&#38918;&#24207;&#65292;&#36319;&#23531;&#20837;&#24460;&#35201;&#31435;&#21051;&#21516;&#27493;&#25165;&#19981;&#26371; race condition&#12290;</p><p>&#22240;&#27492;&#35442;&#36890;&#36947;&#20027;&#35201;&#26159;&#20132;&#25563; cache line &#30340;&#29376;&#24907;&#65292;&#20006;&#29992;&#29376;&#24907;&#34920;&#36948; cache line &#30340; ownership&#65292;&#35442;&#29376;&#24907;&#27231;&#21046;&#31281;&#28858; MESI&#65288;Modified, Exclusive, Shared, Invalid&#65289;&#65306;</p><ul><li><p>M : &#35442; cache line &#34987;&#26412; core &#20462;&#25913;&#65292;&#20294;&#23578;&#26410;&#23531;&#22238;&#26356;&#20302;&#23652; cache &#25110; RAM</p></li><li><p>E : &#35442; cache line &#20677;&#23384;&#22312;&#26044;&#26412; core&#65292;&#20839;&#23481;&#33287; RAM &#19968;&#33268;&#65292;&#23578;&#26410;&#34987;&#20462;&#25913;</p></li><li><p>S : &#35442; cache line &#21487;&#33021;&#23384;&#22312;&#22810;&#20491; core&#65292;&#20839;&#23481;&#33287; RAM &#19968;&#33268;&#65292;&#23578;&#26410;&#34987;&#20462;&#25913;</p></li><li><p>I : &#35442; cache line &#34987;&#20854;&#20182; core &#20462;&#25913;&#65292;&#19981;&#21487;&#20351;&#29992;&#65292;&#20351;&#29992;&#21069;&#36879;&#36942; coherence interconnect &#36319;&#20854;&#20182; core &#25343;&#25110;&#32773;&#21435; L3 or RAM &#25343;</p></li></ul><p>MESI &#26159; cache line &#30340;&#29376;&#24907;&#27231;&#65292;&#23450;&#32681;&#20102; cache line &#29376;&#24907;&#30340;&#20999;&#25563;&#65306;</p><ul><li><p>&#24478;&#12300;&#28961;&#12301;&#21040;&#12300;&#26377;&#12301;(I &#8594; E &#25110; I &#8594; S): core &#35712;&#21462; cache line &#26368;&#26032;&#29376;&#24907;</p></li><li><p>&#24478;&#12300;&#35712;&#12301;&#21040;&#12300;&#23531;&#12301;(S &#8594; M) : core &#29554;&#21462;&#35442; cache line ownership &#20006;&#26356;&#26032;</p></li><li><p>&#24478;&#12300;&#26377;&#12301;&#21040;&#12300;&#28961;&#12301;( M &#8594; I) : core &#25910;&#21040;&#21029;&#20154;&#20462;&#25913;&#35442; cache line &#36890;&#30693;&#65292;&#20462;&#25913;&#29376;&#24907;&#25104; I</p></li><li><p>&#24478;&#12300;&#39634;&#12301;&#21040;&#12300;&#28136;&#12301;( M &#8594; S) : core &#25910;&#21040;&#21029;&#20154;&#35712;&#35442; cache line &#36890;&#30693;&#65292;&#30332;&#29694;&#29376;&#24907;&#27794;&#21516;&#27493;&#21040; L3 or RAM&#65292;&#31435;&#39340;&#21516;&#27493;&#20006;&#25226;&#29376;&#24907;&#35722;&#25104; S&#65292;&#19981;&#26371;&#31561; evict &#25165;&#21516;&#27493;&#36039;&#26009;</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VsWl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VsWl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7869899,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!VsWl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!VsWl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd8257c8f-f587-4bfa-8011-e9fc234b5719_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#28858;&#30906;&#20445;&#21516;&#19968;&#26178;&#38291;&#21516;&#20491; cache line &#21482;&#26377;&#19968;&#20491; core &#29554;&#24471;&#23531;&#20837; ownership&#65292;coherence interconnect &#36890;&#36947;&#26371;&#26159;&#19968;&#20491; BUS&#65292;&#22810;&#20491; core &#30340;&#23531;&#20837;&#35531;&#27714; (PrWr) &#26371;&#34987; BUS sequential &#30340;&#22519;&#34892;&#65292;&#22810;&#20491; core &#26371;&#30435;&#32893; (snooping) BUS &#30340;&#35531;&#27714;&#65292;&#25910;&#21040;&#22238; ACK&#65292;BUS &#25910;&#21040;&#25152;&#26377; core &#30340; ACK &#25165;&#34389;&#29702;&#19979;&#20491;&#35531;&#27714;&#12290;<br><br>&#20363;&#22914; core A &amp; core B &#21516;&#26178;&#23565; BUS &#30332;&#35531; cache line X &#30340; PrWr&#65292;BUS &#20808;&#34389;&#29702; core A &#30340; PrWr&#65292;&#65306;</p><ol><li><p> &#30070; core B &#25910;&#21040; core A &#30340; PrWr &#26371;&#25226;&#33258;&#24049;&#30340; cache line X &#29376;&#24907;&#25913;&#25104; I&#65292;&#32780; core A &#25910;&#21040;&#22823;&#23478;&#30340; ACK &#26371;&#25226;&#29376;&#24907;&#25913;&#25104; M</p></li><li><p> &#38568;&#24460; BUS &#22312;&#36865;&#20986; core B &#30340; PrWr &#26178;&#65292;core B &#30340;&#29376;&#24907;&#26159; I &#27794;&#36774;&#27861;&#25913;&#25104; M&#65292;&#21482;&#33021; NACK&#65292;&#28982;&#24460;&#25913;&#36865; PrRd</p></li><li><p>&#30070; core A &#23436;&#25104;&#26356;&#26032;&#65292;&#25910;&#21040; core B &#30340; PrRd&#65292;&#30332;&#29694;&#29376;&#24907;&#26159; M&#65292;&#23559;&#36039;&#26009;&#23531;&#22238; L3 or RAM&#65292;&#27492;&#26178; core B &#22312;&#21435;&#35712;&#23601;&#33021;&#25343;&#21040;&#26368;&#26032;&#30340;&#36039;&#26009;</p></li><li><p>&#26368;&#24460; core B &#20877;&#36865;&#19968;&#27425; PrWr &#25343;&#21040; ownership &#24460;&#22519;&#34892;&#26356;&#26032;</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7ILW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7ILW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7ILW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5693263,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7ILW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7ILW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba754220-96d0-436d-a76e-4e6f52534627_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>&#31777;&#21934;&#20358;&#35498;&#65292;MESI &#23450;&#32681;&#20102; cache line &#29376;&#24907;&#27231;&#65292;&#20013;&#24515;&#21270; BUS &#30906;&#35469;&#29376;&#24907;&#20999;&#25563;&#38918;&#24207;&#65292;&#22810; core &#36879;&#36942; snooping &#36319; ACK &#27231;&#21046;&#30906;&#20445;&#21516;&#19968;&#26178;&#38291;&#40670;&#65292;&#22823;&#23478;&#23565; cache line &#29376;&#24907;&#26159;&#19968;&#33268;&#30340;&#65292;&#19981;&#26371;&#21516;&#26178;&#20841;&#20491; core &#23565;&#21516;&#20491; cache line &#37117;&#26159;&#65325;&#12290;</p><p>note: &#29694;&#20195; CPU &#26550;&#27083;&#22312; coherence interconnect &#36890;&#36947;&#35373;&#35336;&#26371;&#20778;&#21270;&#25928;&#33021;&#65292;&#20363;&#22914;&#29992; ring bus &#25110; mesh interconnect&#65292;&#37197;&#21512; snoop filter &#25110; directory-based protocol&#12290;</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h1>&#20294;&#36889;&#27171;&#23601;&#33021;&#35299;&#27770; loop { x++ } &#30340;&#21839;&#38988;&#21966;&#65311;</h1><p>x++ &#25351;&#20196;&#26159;&#65306;</p><pre><code>LOAD x &#8594; register
ADD 1
STORE register &#8594; x</code></pre><p>&#26159;&#20841;&#20491;&#29376;&#24907;&#20999;&#25563;&#65292;LOAD &#23559; I =&gt; S&#65292;&#27492;&#26178; CPU &#23559; x &#25918;&#21040; register&#65292;&#28982;&#24460; ADD 1&#65292;&#22519;&#34892; STORE &#24478; S =&gt; M &#23559;&#36039;&#26009;&#23531;&#22238; L1 &#31561;&#21029;&#20154;&#35201;&#35712;&#26178;&#23531;&#22238; L3 or RAM&#12290;</p><p>&#32780;&#21516;&#26178; LOAD &#26159;&#21512;&#27861;&#30340;&#65292;&#30070; core A &amp; core B &#21516;&#26178;&#25226; x &#25918;&#21040; register&#65292;core A &#20808;&#22519;&#34892; STORE &#25226; +1 &#24460;&#32080;&#26524;&#23531;&#22238;&#65292;core B &#35201;&#22519;&#34892; STORE &#26178;&#65292;&#38614;&#28982;&#26371;&#31561; core A &#22519;&#34892;&#23436;&#65292;&#20294;&#26159;&#26371;&#29992; register &#20013; core A STORE &#21069;&#30340;&#20540; ADD 1 &#65292;&#26368;&#32066;&#20173;&#26371; race condition&#12290;<br></p><p>&#31777;&#21934;&#20358;&#35498;  invalid &#19981;&#34389;&#29702; register &#36039;&#26009;&#20839;&#23481;&#65292;register &#19968;&#20294;&#25918;&#20837;&#23601;&#30906;&#23450;&#20102;&#65292;&#28858;&#35299;&#27770;&#35442;&#21839;&#38988;&#65292;atomic &#21487;&#23559; x++ &#35722;&#25104;&#29305;&#27530;&#25351;&#20196; LOCK XADD&#65292;&#26371;&#25226;&#35712;&#23531;&#25805;&#20316;&#35722;&#25104;&#19968;&#20491;&#29376;&#24907;&#20999;&#25563;&#30906;&#20445;&#20854;&#21407;&#23376;&#24615;&#12290;</p><p>&#20363;&#22914;&#20841;&#20491; core &#21516;&#26178;&#22519;&#34892; loop { atomic.add(&amp;x, 1) }&#65292;core A &amp; core B &#26371;&#21516;&#26178;&#30332;&#20986; PrWr &#35531;&#27714;&#65292;&#30070; core A &#20808;&#22519;&#34892;&#65292;&#20182;&#26371;&#19968;&#27425;&#23436;&#25104; LOAD =&gt; ADD =&gt; STORE&#65292;&#19977;&#27493;&#39519;&#23436;&#25104;&#24460;&#25165;&#22238;&#25033;&#19979;&#20491;&#35531;&#27714;&#30340; ACK&#65292;&#36889;&#27171;&#21487;&#30906;&#20445;&#35442; cache line &#19979;&#20491;&#29376;&#24907;&#20999;&#25563;&#26178;&#65292;core A &#24050;&#21516;&#27493;&#26356;&#26032;&#24460;&#36039;&#26009;&#32102;&#22823;&#23478;&#65292;&#36991;&#20813;&#20102; core A &amp; core B &#21516;&#26178; LOAD x &#21040; register &#30340;&#21487;&#33021;&#12290;<br></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eNbn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eNbn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eNbn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:333797,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eNbn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!eNbn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d364649-990c-4404-b7a5-fbe8cddd135d_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#35442;&#21407;&#23376;&#25805;&#20316;&#38500;&#20102; x++ or x-- &#36996;&#21487;&#29992;&#22312; CAS (compare and swap) :</p><pre><code>LOAD flag  -&gt; register
if flag == 0
   flag  =  1
   STORE register -&gt; flag
   STORE true -&gt; ok
else 
   STORE false -&gt; ok</code></pre><p>CAS &#26159; mutex &#30340;&#26680;&#24515;&#65292;&#20363;&#22914; </p><pre><code>lock =&gt; for (atomic.CAS(&amp;lock, 0, 1))
unlock =&gt; (atomic.CAS(&amp;lock, 1, 0))</code></pre><h1>&#31455;&#28982; atomic &#20027;&#35201;&#35299;&#27770;&#22810;&#27493;&#39519;&#30340;&#21407;&#23376;&#24615;&#21839;&#38988;&#65292;&#37027;&#40636;&#20687; go &#30340; atomic.Store &#36319; atomic.Load &#21482;&#26377;&#21934;&#27493;&#39519;&#28858;&#20309;&#38656;&#35201; atomic&#65311;</h1><p>MESI &#23526;&#29694;&#29376;&#24907;&#19968;&#33268;&#65292;&#20294; ACK &#27231;&#21046;&#26371;&#24433;&#38911;&#25928;&#33021;&#65292;&#20027;&#35201;&#26377;&#20841;&#20491;&#24310;&#36978;&#65306;</p><p><strong>&#23531;&#20837;&#24310;&#36978;&#65306;</strong>&#30070; core A &#24819;&#23531;&#36039;&#26009; PrWr &#26178;&#65292;&#24517;&#38920;&#31561;&#25152;&#26377;&#26680;&#24515;&#37117;&#22238;&#20659; ACK&#65288;&#30906;&#35469;&#24050;&#22833;&#25928;&#65289;&#25165;&#33021;&#30495;&#30340;&#26356;&#26032;&#65292;&#36889;&#27573;&#26178;&#38291; core &#21482;&#33021;&#20572;&#19979;&#20358;&#65288;stall&#65289;&#12290;</p><p><strong>&#22833;&#25928;&#24310;&#36978;&#65306;</strong>&#30070; core B &#25910;&#21040; core A &#30340; PrWr &#26178;&#65292;&#23427;&#24517;&#38920;&#23559; L1 &#20013; cache line &#27161;&#35352;&#28858; I &#24460;&#25165;&#22238;&#20659; ACK&#65292;&#24555;&#21462;&#38614;&#24555;&#65292;&#20294;&#34389;&#29702;&#36895;&#24230;&#20173;&#27604; register &#24930;&#24471;&#22810;&#65292;&#33509; cache &#27491;&#22312;&#24537;&#65292;core A &#23601;&#35201;&#31561;&#26356;&#20037;&#12290;</p><p>&#28858;&#20778;&#21270;&#25928;&#33021;&#65292;cpu &#20351;&#29992;&#24310;&#36978;&#22519;&#34892;&#20778;&#21270;&#65306;</p><p>&#35299;&#27770;&#23531;&#20837;&#24310;&#36978;&#65292;core &#19981;&#26371; stall &#32780;&#26159;&#23559;&#35201;&#31561; ack &#30340;&#25351;&#20196;&#25918;&#21040; store buffer &#24460;&#24448;&#19979;&#19968;&#20491;&#25351;&#20196;&#34389;&#29702;&#65292;&#20363;&#22914; :</p><pre><code>a = 1; 
b = y;

STORE 1 =&gt; a
LOAD y =&gt; register
STORE register =&gt; b</code></pre><p>STORE 1 &#35201;&#31561; ACK&#65292;&#20808;&#25918;&#21040; store buffer &#24448;&#19979;&#22519;&#34892; LOAD y&#65292;&#22914;&#26524; y &#30340; cache line &#29376;&#24907;&#26159; E or S &#23601;&#21487;&#30452;&#25509;&#25918;&#21040; register&#65292;&#28982;&#24460;&#20877;&#22238;&#21435;&#22519;&#34892; STORE 1 =&gt; a &#36319; STORE register =&gt; b&#12290;</p><p>&#35299;&#27770;&#22833;&#25928;&#24310;&#36978;&#65292;core &#25910;&#21040;&#21029;&#20154;&#30340; PrWr &#19981;&#26371;&#39340;&#19978;&#21435;&#35352;&#25014;&#39636;&#27161;&#35352; I&#65292;&#32780;&#26159;&#25918;&#21040; invalidation queue &#23601;&#22238; ACK&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BjKx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BjKx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6857849,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BjKx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!BjKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c6e26-8e3b-43f9-afc3-b1f55dbc17ae_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>&#32780;&#19978;&#36848;&#20841;&#20491;&#20778;&#21270;&#26371;&#24433;&#38911;&#36039;&#26009;&#35712;&#23531;&#30340;&#38918;&#24207;&#22833;&#38728;&#65292;&#20363;&#22914;&#65306;</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
x = 1 
if y == 0 {
   x_res = 1
}

core B
y = 1
if x == 0 {
   y_res = 1
}</code></pre><p>&#27491;&#24120;&#22519;&#34892;&#38918;&#24207;&#19979;&#65306;</p><ol><li><p>core A &#23531;&#25104;&#21151;</p></li><li><p>core A &#26597;&#35426; y == 0&#65292;&#23531; x_res </p></li><li><p>core B &#23531;&#25104;&#21151;</p></li><li><p>core B &#27298;&#26597; x != 0</p></li></ol><p>&#25110;&#32773; core B &#20808;&#23531;&#25104;&#21151;&#65292;&#21448;&#25110;&#32773; core 1 &amp; core 2 &#37117;&#20808;&#23531;&#25104;&#21151;&#65306;</p><ol><li><p>core A &#23531;&#25104;&#21151;</p></li><li><p>core B &#23531;&#25104;&#21151;</p></li><li><p>core A &#27298;&#26597; y != 0</p></li><li><p>core B &#27298;&#26597; x != 0</p></li></ol><p>&#32317;&#32780;&#35328;&#20043;&#65292;&#19981;&#21487;&#33021;&#21516;&#26178;&#20986;&#29694; x_res &#36319; y_res &#21516;&#26178;&#28858; 1 &#30340;&#24773;&#27841;&#12290;<br><br>&#20294;&#22914;&#26524; core A &amp; core B &#28858;&#20778;&#21270;&#23531;&#20837;&#24310;&#36978;&#65292;&#23559; load y &amp; load x &#25552;&#21069;&#22519;&#34892;&#65292;&#23601;&#21487;&#33021;&#20986;&#29694; x_res &amp; y_res &#21516;&#26178;&#28858; 1 &#30340;&#24773;&#27841;&#65292;&#20063;&#23601;&#26159;&#20439;&#31281;&#30340; Store-Load Reorder &#21839;&#38988;&#12290;</p><h1>go &#30340; atomic.Store &#36319; atomic.Load &#22914;&#20309;&#35299;&#27770; Store-Load Reorder &#21839;&#38988;&#65311;</h1><p></p><p>go &#30340; atomic.Store &#26412;&#36074;&#26159;&#22312; Store &#24460;&#35373;&#32622;&#19968;&#20491; barrier&#65292;&#24375;&#21046; core &#28165;&#31354; store buffer&#65292;&#20363;&#22914; :</p><pre><code>atomic.Store(&amp;x, 1) 
b = y;</code></pre><p>&#38614;&#28982; STORE 1 =&gt; x &#34987;&#25918;&#21040; store buffer&#65292;&#20294;&#22312;&#22519;&#34892; LOAD y &#21069;&#26371;&#24375;&#21046;&#28165;&#31354; store buffer &#20063;&#23601;&#26159;&#31561; STORE 1 &#25910;&#21040; ACK &#20006;&#23436;&#25104;&#26356;&#26032;&#65292;&#30906;&#20445; STORE 1 =&gt; x &#20808;&#34987;&#22519;&#34892;&#12290;</p><p>&#20294;&#21482;&#26377; atomic.Store &#20173;&#19981;&#33021;&#35299;&#27770;&#19979;&#38754;&#21839;&#38988;&#65306;</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1) 
if y == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1) 
if x == 0 {
   y_res = 1
}</code></pre><p>&#22240;&#28858; core B &#25910;&#21040; core B &#30340; PrWr&#65292;core B &#19981;&#26371;&#39340;&#19978;&#27161;&#35352; I&#65292;&#32780;&#26159;&#25918;&#21040; invalidation queue&#65292;&#33509; core B &#22519;&#34892; LOAD x &#26178;&#65292;invalidation queue &#36996;&#27794;&#28165;&#31354;&#65292;core B &#23601;&#26371;&#35712;&#21040; core A &#26356;&#26032;&#21069;&#30340;&#20540;&#65292;&#20173;&#21487;&#33021;&#20986;&#29694; x_res &amp; y_res &#37117;&#26159; 1 &#30340;&#24773;&#27841;&#12290;</p><p>&#32780; atomic.Load &#26371;&#22312; LOAD &#20043;&#21069;&#35373;&#32622; barrier &#24375;&#21046;&#27298;&#26597; invalidation queue &#20013;&#26377;&#27794;&#26377;&#35442; cache line&#65292;&#26377;&#30340;&#35441;&#26356;&#26032;&#25104; I &#29376;&#24907;&#65292;&#20006;&#30906;&#20445;&#35712;&#21040;&#26368;&#26032;&#36039;&#26009;&#65292;&#22240;&#27492;&#65306;</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1) 
if atomic.Load(&amp;y) == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1) 
if atomic.Load(&amp;x) == 0 {
   y_res = 1
}</code></pre><p>&#23601;&#32085;&#23565;&#19981;&#26371;&#20986;&#29694; x_res &amp; y_res &#21516;&#26178;&#28858; 1 &#30340;&#24773;&#27841;&#12290;</p><p>note: &#38614;&#28982;&#35498; invalidation queue &#31281;&#20316; queue&#65292;&#20294;&#27298;&#26597; cache line &#22312;&#19981;&#22312; invalidation queue &#26412;&#36074;&#19978;&#26159;&#29992;&#29376;&#24907;&#30908;&#65292;&#32780;&#19981;&#26159;&#30495;&#30340; scan queue&#12290;</p><h1>go &#30340; atomic &#36319; rust &#30340; atomic &#20043;&#38291;&#26377;&#20160;&#40636;&#19981;&#19968;&#27171;&#65311;</h1><p>go &#30340; atomic.Load &amp; atomic.Store &#26412;&#36074;&#19978;&#39006;&#20284; SeqCst ordering&#65292;&#20063;&#23601;&#26159;&#22312; STORE &#20043;&#24460;&#65292;LOAD &#20043;&#21069;&#35373;&#19979; barrier&#65292;&#36889;&#27171;&#21487;&#20445;&#35657;&#23531;&#20837;&#26041;&#30340;&#25351;&#20196;&#19981;&#26371;&#34987; reorder&#12290;</p><p>&#20294;&#20687; rust &#26356;&#24213;&#23652;&#30340;&#35486;&#35328;&#23601;&#25903;&#25588;&#20854;&#20182; atomic barrier &#27231;&#21046;&#65292;&#20363;&#22914;  Release/Acquire &#65292;Release &#26159;&#22312; STORE &#20043;&#21069;&#35373;&#32622; barrier&#65292;Acquire &#22312; LOAD &#20043;&#21069;&#35373;&#19979; barrier&#12290;</p><p>&#20363;&#22914;&#19979;&#38754;&#26696;&#20363;&#65306;</p><pre><code>x = 0, y = 0, x_res =0, y_res = 0

core A
atomic.Store(&amp;x, 1, Release) 
if atomic.Load(&amp;y, Acquire) == 0 {
   x_res = 1
}

core B
atomic.Store(&amp;y, 1, Release) 
if atomic.Load(&amp;x, Acquire) == 0 {
   y_res = 1
}</code></pre><p>&#26371; race condition&#65292;&#22240;&#28858; Release &#21482;&#20445;&#35657; STORE 1 =&gt; x &#20043;&#21069;&#28165;&#31354; store buffer&#65292;STORE 1 =&gt; x &#21487;&#34987;&#25918;&#21040; store buffer &#24460;&#23601;&#24448;&#19979;&#22519;&#34892;&#65292;&#26371;&#30332;&#29983; Store-Load Reorder &#30340;&#24773;&#27841;&#12290;</p><p>&#22240;&#27492; Release/Acquire &#21482;&#33021;&#29992;&#22312;&#21934;&#21521;&#20659;&#36664;&#30340;&#24773;&#27841;&#65292;&#20363;&#22914;&#65306;</p><p>core A</p><pre><code>flag = 0, x = 0
core A
x = 1 
atomic.Store(&amp;flag, 1, Release)

core B
for atomic.Load(&amp;flag, Acquire) { print(x) }</code></pre><p>core B &#30340; stdout &#19968;&#23450;&#26159;&#36664;&#20986; 1</p><p>&#26368;&#24460; rust &#36996;&#26377; Relaxed Ordering &#19981;&#35373;&#32622;&#20219;&#20309; barrier &#20294;&#20445;&#35657;&#36889;&#27425;&#25351;&#20196;&#26159;&#21407;&#23376;&#24615;&#30340;&#65292;&#20363;&#22914; atomic.Add(&amp;x, 1, Relaxed)&#12290;<br></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7Ur4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png" width="1456" height="794" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:794,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7470164,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/187826606?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7Ur4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 424w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 848w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!7Ur4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4d9dcaa-790f-442f-869f-1285948a4d45_2816x1536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><br></p><h1>Go &#33287; Rust &#31684;&#20363;&#20195;&#30908;</h1><p>&#19979;&#38754; go &#20195;&#30908;&#23637;&#31034;&#20841;&#20491; thread &#31478;&#29229; flag &#30340;&#20195;&#30908;&#65306;</p><p> &#30001;&#26044;&#27491;&#30906;&#21152;&#19978; atomic&#65292;&#25152;&#20197;&#19981;&#26371; break&#65292;&#21487;&#20197;&#35430;&#33879;&#20570;&#20197;&#19979;&#35519;&#25972;&#65306;</p><ol><li><p>&#31227;&#38500; atomic.Load&#65292;&#36305;&#19968;&#38499;&#23376;&#24460;&#26377;&#27231;&#26371; break</p></li><li><p>&#31227;&#38500; atomic.Store &amp; atomic.Load &#36305;&#19968;&#19979;&#24460;&#24456;&#24555;&#23601;&#26371; break</p></li></ol><pre><code>func main() {
&#9;for i := 0; ; i++ {
&#9;&#9;x, y, r1, r2 := int64(0), int64(0), int64(0), int64(0)
&#9;&#9;wg := sync.WaitGroup{}
&#9;&#9;wg.Add(2)

&#9;&#9;go func() {
&#9;&#9;&#9;defer wg.Done()
&#9;&#9;&#9;atomic.StoreInt64(&amp;x, 1)
&#9;&#9;&#9;if atomic.LoadInt64(&amp;y) == 0 { // if Store-Load reordering occurs, y will read 0
&#9;&#9;&#9;&#9;r1 = 1
&#9;&#9;&#9;}
&#9;&#9;}()

&#9;&#9;go func() {
&#9;&#9;&#9;defer wg.Done()
&#9;&#9;&#9;atomic.StoreInt64(&amp;y, 1)
&#9;&#9;&#9;if atomic.LoadInt64(&amp;x) == 0 { // if Store-Load reordering occurs, x will read 0
&#9;&#9;&#9;&#9;r2 = 1
&#9;&#9;&#9;}
&#9;&#9;}()

&#9;&#9;wg.Wait()
&#9;&#9;if r1 == 1 &amp;&amp; r2 == 1 {
&#9;&#9;&#9;fmt.Printf("Observed reordering at iteration %d: r1=%d, r2=%d\n", i, r1, r2)
&#9;&#9;&#9;break
&#9;&#9;}
&#9;}
}
</code></pre><p>&#19979;&#38754; rust &#20195;&#30908;&#23637;&#31034;&#19981;&#21516; atomic ordering &#23531;&#27861;&#65306;<br>&#20351;&#29992; Release &amp; Acquire &#23526;&#29694;&#21934;&#21521;&#30340; flag &#20659;&#36664;&#65292;&#29992;&#20841;&#20491; thread &#25353;&#29031;&#38918;&#24207;&#21360;&#20986; 1 ~ 1000</p><pre><code>use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;

fn main() {
    let n = 1000;
    let i = Arc::new(AtomicUsize::new(1));
    let flag = Arc::new(AtomicBool::new(true));
    let (i_a, flag_a) = (Arc::clone(&amp;i), Arc::clone(&amp;flag));
    let t1 = thread::spawn(move || {
        loop {
            if flag_a.load(Ordering::Acquire) {
                let current: usize = i_a.fetch_add(1,Ordering::Relaxed);
                if current &gt; n {
                    flag_a.store(false, Ordering::Release);
                    break;
                }
                println!("{}", current);
                flag_a.store(false, Ordering::Release);
            }
        }
    });

    let (i_b, flag_b) = (Arc::clone(&amp;i), Arc::clone(&amp;flag));
    let t2 = thread::spawn(move || {
        loop {
            if !flag_b.load(Ordering::Acquire) {
                let current: usize = i_b.fetch_add(1,Ordering::Relaxed);
                if current &gt; n {
                    flag_b.store(true, Ordering::Release);
                    break;
                }
                println!("{}", current);
                flag_b.store(true, Ordering::Release);
            }
        }
    });
    t1.join().unwrap();
    t2.join().unwrap();
}</code></pre>]]></content:encoded></item><item><title><![CDATA[Kubernetes Network 是怎麼運作的？下集]]></title><description><![CDATA[&#35299;&#26512; eBPF &#39640;&#25928;&#30340;&#31192;&#23494;]]></description><link>https://www.chillvic.dev/p/kubernetes-network-084</link><guid isPermaLink="false">https://www.chillvic.dev/p/kubernetes-network-084</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Sun, 01 Feb 2026 08:31:28 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!heO9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>K8S CNI (Container Network Interface) &#23450;&#32681;&#20102; K8S &#32178;&#36335;&#36890;&#35338;&#38656;&#35201;&#30340; API&#65292;&#19978;&#38598;&#20171;&#32057; CNI &#22914;&#20309;&#23526;&#29694; Pod &#20043;&#38291;&#30340;&#36890;&#35338;&#65292;&#26368;&#24460;&#25552;&#21040;&#20659;&#32113; iptables &#23526;&#29694;&#30340; L4 load balance &#25928;&#33021;&#36611;&#24046;&#65292;&#21487;&#25913;&#29992; IPVS&#65292;&#20294;&#23526;&#21209;&#19978; K8S v1.35 &#24460;&#65292;&#22312; kube-proxy &#20013;&#23559; IPVS &#27161;&#35352;&#28858;&#26820;&#29992;&#65292;&#28858;&#20160;&#40636;&#65311;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!heO9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!heO9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!heO9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!heO9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!heO9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!heO9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2186252,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/186481457?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!heO9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!heO9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!heO9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!heO9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f9d70d5-d3f5-4783-a153-4d26c56bc11f_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h1>&#22238;&#39015;&#65292;&#28858;&#20309; IPVS &#26371;&#20986;&#29694;&#65311;</h1><p>K8S &#20013;&#30340; L4 load balance &#21151;&#33021;&#21487;&#23559;&#22810;&#20491;&#36865;&#32102;&#30456;&#21516; service IP &#30340; TCP &#24314;&#31435;&#35531;&#27714;&#65292;&#36865;&#32102;&#19981;&#21516;&#30340; pod IP&#65292;&#20659;&#32113;&#29992;&#27861;&#26159; iptables&#12290;</p><p>iptables &#26159; kernel &#20013; netfilter module &#20839;&#24314;&#30340;&#35215;&#21063;&#31649;&#29702;&#27169;&#32068;&#65292;netfilter &#22312;&#23553;&#21253;&#36942;&#28670;&#36681;&#30332;&#36942;&#31243;&#20013;&#23450;&#32681;&#20102;&#19981;&#21516; hook &#40670;&#65292; iptable &#36000;&#36012;&#20786;&#23384;&#31649;&#29702;&#36889;&#20123; hook &#30340;&#23553;&#21253;&#35215;&#21063;&#65292;&#20363;&#22914;&#26159;&#21542;&#35201; drop&#65292;allow or forward&#12290;</p><p>&#20294; iptable &#28961;&#27861;&#23458;&#35069;&#21270; hook callback&#65292;&#32780;&#26159;&#25552;&#20379;&#26377;&#38480;&#30340; hook &#35215;&#21063;&#37197;&#32622;&#65292;&#22240;&#27492;&#29992; iptable &#23526;&#29694; L4 load balance &#26178;&#65292;&#26371;&#21463;&#38480;&#26044; iptable &#21151;&#33021;&#20197;&#21450;&#20839;&#37096;&#23526;&#20316;&#32780;&#24433;&#38911;&#25928;&#33021;&#12290;</p><p>&#22909;&#22312; netfilter &#21478;&#22806;&#25552;&#20379; nf_register_net_hook() &#33021;&#25554;&#20837;&#23458;&#35069;&#21270;&#30340; callback&#65292;&#32780; IPVS &#23601;&#26159;&#36879;&#36942;&#25554;&#20837;&#23458;&#35069;&#21270; callback &#23526;&#29694;&#39640;&#25928;&#30340; L4 load balance&#12290;</p><p>&#20363;&#22914; iptables &#32570;&#40670;&#26159;&#38656;&#35201; for loop &#22810;&#26781;&#35215;&#21063;&#21152;&#19978;&#27231;&#29575;&#21629;&#20013;&#23526;&#29694; load balance&#65292;&#20294; IPVS &#20316;&#28858;&#29544;&#31435;&#30340; module &#19981;&#21463;&#38480; iptables &#20839;&#37096;&#32080;&#27083;&#65292;&#21487;&#29992; hashmap &#23526;&#29694; O(1) &#30340; load balance&#12290;</p><h1>&#20294;&#28858;&#20309;&#35201;&#26820;&#29992; IPVS&#65311;</h1><p>IPVS &#38614;&#28982;&#26159;&#39640;&#25928; L4 load balance&#65292;&#20294; netfilter &#25972;&#20491;&#27169;&#32068;&#36996;&#26377;&#24456;&#22810;&#21151;&#33021;&#65292;&#20363;&#22914;&#32000;&#37636; service IP &#36319; pod IP &#36681;&#25563;&#30340; NAT &amp; DNAT &amp; conntrack &#21151;&#33021;&#65292;&#22914;&#26524; IPVS &#35201;&#26356;&#23436;&#21892;&#26356;&#39640;&#25928;&#65292;&#23601;&#35201;&#23526;&#29694; netfilter &#27169;&#32068;&#20854;&#20182;&#21151;&#33021;&#12290;</p><p>&#36889;&#26178;&#20986;&#29694;&#19968;&#20491;&#38283;&#30332;&#25225;&#25799;&#65292;&#33287;&#20854;&#33457;&#26178;&#38291;&#23436;&#21892;&#19968;&#20491; plugin &#23450;&#20301;&#30340;&#24037;&#20855;&#65292;&#35731;&#20182;&#26377;&#26356;&#22810;&#21151;&#33021;&#65292;&#19981;&#22914;&#23559;&#21407;&#26412; netfilter &#30340; iptables &#27169;&#32068;&#20462;&#25913;&#25104;&#65292;&#20351;&#29992;&#26356;&#39640;&#25928;&#32080;&#27083;&#31649;&#29702;&#35215;&#21063;&#30340;&#27169;&#32068;&#65292;&#22240;&#27492; netables &#23601;&#35477;&#29983;&#20102;&#65292;&#26368;&#24460; K8S v1.35 &#24460;&#23559; IPVS &#27161;&#27880;&#28858; deprecated&#65292;&#25913;&#32780;&#25512;&#34214;&#29992; netables&#12290;</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h1>&#26356;&#39640;&#25928;&#30340; CNI &#26550;&#27083;&#65292;eBPF &#25216;&#34899;&#22914;&#20309;&#21152;&#36895; Pod &#20043;&#38291;&#36890;&#35338;&#65311;</h1><p>&#26089;&#26399; K8S Cluster&#65292;CNI &#21482;&#36000;&#36012; Pod &#30340; IP &#20998;&#37197;&#36319; Pod &#20043;&#38291;&#36890;&#35338;&#65292;&#20363;&#22914; Flannel &#36319; kube-proxy &#25645;&#37197;&#65292;&#20294;&#26032;&#33288; CNI (e.g cilium) &#25505;&#29992; eBPF &#25216;&#34899;&#65292;&#19981;&#21482; Pod &#20043;&#38291;&#36890;&#35338;&#26356;&#24555;&#65292;&#29978;&#33267;&#33021;&#26367;&#20195; kube-proxy L4 Load balance &#21151;&#33021;&#12290;</p><p>&#20659;&#32113; Flannel CNI &#25216;&#34899;&#65292;Pod &#20043;&#38291;&#36890;&#35338;&#26371;&#36879;&#36942; overlap &#25216;&#34899;&#65292;&#23559; TCP packet &#23553;&#35037;&#25104; UDP packet&#65292;&#31680;&#40670;&#25509;&#25910;&#24460;&#65292;UDP packet &#26371;&#20808;&#32147;&#36942; kernel &#30340; netfilter &#27169;&#32068; forward &#32102; vxlan device &#65292;&#28982;&#24460; vxlan device &#35299;&#21253;&#24460;&#65292;&#23559; TCP packet &#20877;&#32147;&#36942;&#19968;&#27425; netfilter &#27169;&#32068;&#65292;&#25214;&#21040; veth pair &#24314;&#31435;&#30340;&#34395;&#25836;&#32178;&#21345;&#65292;&#36865;&#32102; container&#65292;&#22240;&#27492;&#26371;&#32147;&#36942;&#20841;&#27425; netfilter&#12290;</p><p>eBPF &#26159;&#21487;&#22312; kernel &#23652;&#25554;&#20837;&#23458;&#35069; hook &#37007;&#36655;&#30340;&#25216;&#34899;&#65292;&#20294;&#25554;&#20837;&#40670;&#19981;&#21463;&#38480;&#26044; netfilter modul&#65292;&#32780;&#26159; kernel &#26377;&#26292;&#38706;&#30340; hook &#40670;&#37117;&#26377;&#27231;&#26371;&#25554;&#20837;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Mb_r!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Mb_r!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 424w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 848w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 1272w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Mb_r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png" width="1456" height="1582" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1582,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Hello eBPF: A Packet Logger in Pure Java using TC and XDP Hooks (13) -  Mostly nerdlessMostly nerdless&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Hello eBPF: A Packet Logger in Pure Java using TC and XDP Hooks (13) -  Mostly nerdlessMostly nerdless" title="Hello eBPF: A Packet Logger in Pure Java using TC and XDP Hooks (13) -  Mostly nerdlessMostly nerdless" srcset="https://substackcdn.com/image/fetch/$s_!Mb_r!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 424w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 848w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 1272w, https://substackcdn.com/image/fetch/$s_!Mb_r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa50b2a5e-664b-4a4f-ab7c-502df74c874d_1554x1688.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://mostlynerdless.de/wp-content/uploads/2024/08/network_stack-1.png</p><p>&#22240;&#27492; eBPF &#25216;&#34899;&#30340; CNI&#65292;&#21487;&#22312; kernel &#25910;&#21040;&#23553;&#21253;&#65292;&#36914;&#20837; netfilter &#27169;&#32068;&#21069;&#65292;&#25554;&#20837;&#19968;&#20491; hook callback&#65292;&#20358;&#32350;&#25481; netfilter &#30340;&#22519;&#34892;&#65292;&#32780; TC (traffic control) &#26159;&#22312; netfilter &#21069;&#36000;&#36012;&#27969;&#37327;&#25511;&#21046;&#30340;&#27169;&#32068;&#65306;</p><p>TC ingress&#65306;&#31649;&#29702; inbound packet &#30340;&#34389;&#29702;&#36895;&#24230;&#65292;&#26377; rate limit &#27231;&#21046;&#65292;&#36319;&#23565; packet &#25110; socket &#25171;&#27161;&#31844;</p><p>TC egress&#65306;&#25511;&#21046; outbound packet&#65292;&#21487;&#20570;&#21040;&#37325;&#26032;&#32232;&#25490;&#65292;&#20381;&#29031; ingress &#30340;&#27161;&#31844;&#27770;&#23450; priority</p><p>&#33509;&#29992; eBPF &#22312; TC Ingress &#25554;&#20837;&#19968;&#20491; hook&#65292;&#20006;&#23526;&#29694; overlay &#35299;&#21253;&#21152;&#23553;&#21253; forward &#30340;&#21151;&#33021;&#65292;&#23601;&#33021;&#36991;&#20813; netfilter &#22519;&#34892;&#20841;&#27425;&#30340; CPU &#28040;&#32791;&#12290;</p><p>&#20363;&#22914;&#22312; kernel &#20013;&#65292;&#27599;&#20491;&#23553;&#21253;&#37117;&#26159;&#19968;&#22602;&#35352;&#25014;&#39636; (e.g bytes array)&#65292;&#32780; skb &#26159; kernel &#29992;&#20358;&#31649;&#29702;&#23553;&#21253;&#35352;&#25014;&#39636;&#30340;&#32080;&#27083;&#65292;eBPF &#21487;&#36879;&#36942;&#20462;&#25913; skb &#30340;&#35352;&#25014;&#39636;&#36215;&#22987;&#20301;&#32622;&#65292;&#36339;&#36942; overlap header&#23526;&#29694;&#35299;&#21253;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iOlf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iOlf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 424w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 848w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 1272w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iOlf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png" width="264" height="191" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:191,&quot;width&quot;:264,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Linux &#32178;&#36335;&#23376;&#31995;&#32113;sk_buffer &#35443;&#32048;&#20998;&#26512;1. &#23450;&#32681;2. &#27010;&#36848;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Linux &#32178;&#36335;&#23376;&#31995;&#32113;sk_buffer &#35443;&#32048;&#20998;&#26512;1. &#23450;&#32681;2. &#27010;&#36848;" title="Linux &#32178;&#36335;&#23376;&#31995;&#32113;sk_buffer &#35443;&#32048;&#20998;&#26512;1. &#23450;&#32681;2. &#27010;&#36848;" srcset="https://substackcdn.com/image/fetch/$s_!iOlf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 424w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 848w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 1272w, https://substackcdn.com/image/fetch/$s_!iOlf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05b65d30-5806-4f09-9f80-54bf1eb39a10_264x191.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>image source : http://jared.web.fc2.com/sk_buffer_analysis.pdf</p><p>&#26368;&#24460;&#36681;&#30332;&#21487;&#36879;&#36942; bpf_redirect &#36889;&#20491; BPF helper&#65292;bpf_redirect &#38656;&#25509;&#25910; ifindex &#21443;&#25976;&#65292; ifindex &#26159;&#32178;&#21345;&#35373;&#20633;&#22312; kernel process &#20013;&#30340; id&#65292;&#36889;&#20491; id &#26371;&#22312; veth pair &#24314;&#31435;&#26159;&#29554;&#24471;&#65292;&#21487;&#34987; CNI control panel &#31649;&#29702;&#65292;&#30070; eBPF  bpf_redirect &#36681;&#30332;&#23553;&#21253;&#24460;&#65292;&#26371;&#22238;&#20659; TC_ACT_REDIRECT&#65292;&#21578;&#35380; TC module &#23553;&#21253;&#34987; redirect&#65292;&#19981;&#29992;&#24448;&#19979;&#32102; netfilter&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MNqF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MNqF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 424w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 848w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 1272w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MNqF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png" width="1456" height="817" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:817,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&#35793;] &#22914;&#20309;&#22522;&#20110;Cilium &#21644;eBPF &#25171;&#36896;&#21487;&#24863;&#30693;&#24494;&#26381;&#21153;&#30340;Linux&#65288;InfoQ, 2019&#65289;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="&#35793;] &#22914;&#20309;&#22522;&#20110;Cilium &#21644;eBPF &#25171;&#36896;&#21487;&#24863;&#30693;&#24494;&#26381;&#21153;&#30340;Linux&#65288;InfoQ, 2019&#65289;" title="&#35793;] &#22914;&#20309;&#22522;&#20110;Cilium &#21644;eBPF &#25171;&#36896;&#21487;&#24863;&#30693;&#24494;&#26381;&#21153;&#30340;Linux&#65288;InfoQ, 2019&#65289;" srcset="https://substackcdn.com/image/fetch/$s_!MNqF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 424w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 848w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 1272w, https://substackcdn.com/image/fetch/$s_!MNqF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ca8f294-fab8-4f61-8f7a-402dcb58a1b2_1685x946.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://arthurchiao.art/blog/how-to-make-linux-microservice-aware-with-cilium-zh/</p><h1>&#32350;&#36942; netfilter &#24460;&#65292;kube-proxy &#23601;&#29992;&#19981;&#21040;&#20102;&#65292;&#24590;&#40636;&#23526;&#29694; service endpoint &#65311;</h1><p>kube-proxy &#26159;&#36879;&#36942; netfilter &#23526;&#29694; service endpoint &#30340; Layer 4 Load balance&#65292;&#19968;&#26086; eBPF CNI &#32350;&#36942; netfilter&#65292;&#23601;&#28961;&#27861;&#29992; kube-proxy&#65292;&#22240;&#27492;&#35201;&#21478;&#22806;&#38283;&#30332; Layer 4 Load balance&#12290;</p><p>eBPF &#23526;&#29694; Layer 4 Load balance &#30340;&#22909;&#34389;&#20043;&#19968;&#26159; conntrack &#21487;&#33258;&#34892;&#31649;&#29702;&#65292;&#30001;&#26044; TCP &#36899;&#32218;&#26159;&#26377;&#29376;&#24907;&#30340;&#65292;&#36899;&#32218;&#20839;&#23553;&#21253;&#38920;&#36865;&#24448;&#21516;&#19968;&#20491; pod IP&#65292;&#20294; client &#21482;&#30475;&#21040; service IP&#65292;&#22240;&#27492; kube-proxy &#36879;&#36942; netfilter conntrack &#27231;&#21046;&#65292;&#20786;&#23384; service IP &#36319; pod IP &#30340;&#36681;&#25563;&#65292;&#32780; netfilter &#30340; conntrack &#19981;&#21482;&#26159; IP &#36681;&#25563;&#65292;&#36996;&#29992;&#26044;&#23526;&#29694;&#26377;&#29376;&#24907;&#30340;&#38450;&#28779;&#29254;&#27231;&#21046;&#65292;&#20363;&#22914;&#19981; DROP &#36899;&#32218;&#24314;&#31435;&#24460;&#30340;&#23553;&#21253;&#65292;&#25152;&#20197; conntrack &#36996;&#26371;&#35352;&#37636;&#38989;&#22806;&#36039;&#35338;&#12290;</p><p>&#20294;&#21934;&#32020; Load balance &#20854;&#23526;&#21487;&#32000;&#37636;&#26356;&#23569;&#30340;&#36039;&#26009;&#65292;&#19988; eBPF &#33258;&#34892;&#31649;&#29702; conntrack &#20063;&#19981;&#26371;&#21463;&#38480; kernel nf_conntrack_max &#21443;&#25976;&#38480;&#21046;&#65292;&#19988;&#33021;&#20778;&#21270; hashmap &#30340;&#20786;&#23384;&#32080;&#27083;&#65292;&#26356;&#39640;&#25928;&#22320;&#20056;&#36617;&#22823;&#37327; TCP &#36899;&#32218;&#12290;</p><h1>&#20160;&#40636;&#26159; service mesh&#65311;&#38750; eBPF service mesh &#26377;&#20160;&#40636;&#25928;&#33021;&#21839;&#38988;&#65311;</h1><p>eBPF &#38614;&#28982;&#23526;&#29694; Layer4 Load Balance&#65292;&#20294;&#35442; Load balance &#20006;&#19981;&#33021;&#29992;&#22312; gRPC &#19978;&#12290;</p><p>K8S &#20839;&#37096;&#26381;&#21209;&#28317;&#36890;&#65292;&#24120;&#35211;&#30340; Layer7 &#21332;&#23450;&#26159; gRPC&#65292;gRPC &#29992; TCP &#38263;&#36899;&#32218;&#65292;&#19968;&#26086;&#36899;&#32218;&#24314;&#31435;&#65292;&#25152;&#26377; gRPC &#35531;&#27714;&#37117;&#26371;&#36865;&#24448;&#21516;&#20491; backend server&#12290;</p><p>&#23526;&#29694; gRPC load balance &#24120;&#35211; solution &#26159; service mesh&#65292;&#27599;&#20491; pod &#25645;&#37197;&#29544;&#31435; proxy &#31281;&#28858; sidecar&#65292;proxy &#24444;&#27492;&#20043;&#38291;&#24314;&#31435; gRPC &#36899;&#32218;&#65292;&#24418;&#25104;&#32178;&#29376;&#38364;&#20418;&#65292;server to server &#28317;&#36890;&#26178;&#65292;client &#35531;&#27714;&#36879;&#36942; proxy &#36865;&#20986;&#65292;&#27492;&#26178; client proxy &#21487;&#27770;&#23450;&#35201;&#36865;&#24448;&#21738;&#20491; server proxy &#23526;&#29694; load balance&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Dr9H!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dr9H!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 424w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 848w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 1272w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dr9H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png" width="808" height="425" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:425,&quot;width&quot;:808,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Service Mesh &#36890;&#35338;&#22522;&#30990;&#32080;&#27083;- .NET | Microsoft Learn&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Service Mesh &#36890;&#35338;&#22522;&#30990;&#32080;&#27083;- .NET | Microsoft Learn" title="Service Mesh &#36890;&#35338;&#22522;&#30990;&#32080;&#27083;- .NET | Microsoft Learn" srcset="https://substackcdn.com/image/fetch/$s_!Dr9H!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 424w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 848w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 1272w, https://substackcdn.com/image/fetch/$s_!Dr9H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15306b72-9dc8-4e05-9839-88c2362977f5_808x425.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://learn.microsoft.com/zh-tw/dotnet/architecture/cloud-native/service-mesh-communication-infrastructure</p><p>&#22240;&#27492; sidecard proxy &#35201;&#25509;&#25910; pod &#30340; inbound &amp; outbound request&#65292;&#32780;&#38750; eBPF &#30340; service mesh &#26159;&#36879;&#36942; iptables &#20462;&#25913; netfilter &#36335;&#30001;&#35215;&#21063;&#65292;&#25226;&#25152;&#26377; outbound address &#37117;&#20462;&#25913;&#25104; proxy address&#65292;&#35731; pod &#25152;&#26377;&#35531;&#27714;&#37117;&#36879;&#36942; side car proxy &#36865;&#20986;&#12290;</p><p>service mesh &#27794;&#27861;&#29992; iptable &#30340; FORWARD rule &#30452;&#25509;&#36681;&#30332;&#65292;&#22240;&#28858; FORWARD &#26159;&#29992;&#22312;&#19981;&#21516;&#32178;&#36335;&#25509;&#21475;&#65292;&#20294; side car &#36319; server &#26159;&#21516;&#19968;&#20491;&#32178;&#36335;&#25509;&#21475;&#65292;&#25152;&#20197;&#36681;&#30332;&#35201;&#36879;&#36942;&#20462;&#25913; dest address&#12290;</p><p>&#20462;&#25913; dest address &#24460;&#65292;&#36996;&#35201;&#20877;&#32147;&#36942; kernel &#30340; netfilter &#27169;&#32068;&#19968;&#27425;&#65292;&#23553;&#21253;&#25165;&#26371;&#21040; proxy&#65292;&#31561;&#26044;&#35498;  server to server &#36879;&#36942; side car proxy &#28317;&#36890;&#26178;&#65292;&#26371;&#26377;&#20841;&#27425; netfilter &#30340; datapath&#65306;</p><p>1. &#31532;&#19968;&#27425; netfilter &#20462;&#25913; dest address &#21040; proxy </p><p>2. &#31532;&#20108;&#27425; netfilter &#20381;&#29031;&#26032;&#30340; dest address forward &#23553;&#21253;&#32102; proxy</p><p>&#20841;&#27425; netfilter &#20195;&#34920;&#26356;&#22810; CPU &#28040;&#32791;&#65292;&#32780; eBPF &#33021;&#35299;&#27770;&#35442;&#21839;&#38988;&#12290;</p><h1>eBPF &#22914;&#20309;&#23526;&#29694;&#26356;&#39640;&#25928;&#30340; service mesh&#65311;</h1><p>&#39318;&#20808; eBPF &#21487;&#22312; server &#23565;&#22806;&#24314;&#31435; tcp &#36899;&#32218;&#26178;&#65292;&#25876;&#25130;&#24314;&#31435;&#35531;&#27714;&#65292;&#23559; dest address &#20462;&#25913;&#25104; proxy address&#65292;server &#30475;&#20284;&#23565;&#22806;&#24314;&#31435;&#20102; tcp &#36899;&#32218;&#65292;&#23526;&#38555;&#26159;&#33287; side car proxy &#24314;&#31435;&#36899;&#32218;&#65292;&#27492;&#33289;&#36991;&#20813;&#20102;&#36879;&#36942; netfilter &#21435;&#36681;&#30332;&#21040; proxy&#12290;</p><p>&#36899;&#32218;&#24314;&#31435;&#24460;&#65292;&#36991;&#20813; server &#24448; proxy &#36865;&#23553;&#21253;&#26178;&#35201;&#32147;&#36942; netfilter&#65292;eBPF &#26371;&#29992; Sockmap &#32080;&#27083;&#65292;&#23559;&#20841;&#20491;&#19981;&#21516; process (server &amp; proxy) &#22312; kernel &#20013; socket &#29289;&#20214;&#20570;&#38364;&#32879;&#65292;&#30070; server &#36865;&#23553;&#21253;&#20986;&#21435;&#26178;&#65292;&#26371;&#34987; eBPF hook &#25876;&#25130;&#65292;&#30332;&#29694; dest socket &#22312; Sockmap &#26377;&#38364;&#32879;&#65292;&#21487;&#30452;&#25509;&#24448;&#23565;&#25033;&#30340; socket &#36865;&#65292;&#19981;&#38656;&#32147;&#36942; netfilter&#12290;</p><p>&#21407;&#26412;&#27794; eBPF &#30340; service mesh &#65292;side car &#35201;&#25876;&#25130; outbound request &#38656;&#32147;&#36942;&#20841;&#27425; netfilter&#65292;&#26377;&#20102; eBPF &#26159; 0 &#27425;&#65292;&#22240;&#27492;&#25928;&#33021;&#26356;&#22909;&#65292;&#29978;&#33267; eBPF &#19979;&#30340; service mesh &#33021;&#26356;&#22909;&#22320;&#25903;&#25588; per node &#30340; proxy &#26550;&#27083;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!swcx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!swcx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 424w, https://substackcdn.com/image/fetch/$s_!swcx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 848w, https://substackcdn.com/image/fetch/$s_!swcx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 1272w, https://substackcdn.com/image/fetch/$s_!swcx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!swcx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png" width="1456" height="645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:645,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;How eBPF will solve Service Mesh - Goodbye Sidecars&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="How eBPF will solve Service Mesh - Goodbye Sidecars" title="How eBPF will solve Service Mesh - Goodbye Sidecars" srcset="https://substackcdn.com/image/fetch/$s_!swcx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 424w, https://substackcdn.com/image/fetch/$s_!swcx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 848w, https://substackcdn.com/image/fetch/$s_!swcx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 1272w, https://substackcdn.com/image/fetch/$s_!swcx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd313742a-e8a8-4dee-848c-b716d5d522b4_2560x1134.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://isovalent.com/blog/post/2021-12-08-ebpf-servicemesh/</p>]]></content:encoded></item><item><title><![CDATA[Kubernetes Network 是怎麼運作的？上集]]></title><description><![CDATA[&#35299;&#26512; Pod &#20043;&#38291;&#30340;&#32178;&#36335;&#36890;&#35338;&#25216;&#34899;]]></description><link>https://www.chillvic.dev/p/kubernetes-network</link><guid isPermaLink="false">https://www.chillvic.dev/p/kubernetes-network</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Tue, 30 Dec 2025 10:25:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!rkwx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p> K8S CNI (Container Network Interface) &#23450;&#32681;&#20102; K8S &#32178;&#36335;&#36890;&#35338;&#38656;&#35201;&#30340; API&#65292;&#20294;&#24213;&#23652; CNI &#23526;&#20316;&#31350;&#31455;&#26159;&#22914;&#20309;&#23526;&#29694; K8S &#32178;&#36335;&#36890;&#35338;&#30340;&#21602;&#65311;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rkwx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rkwx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 424w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 848w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rkwx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png" width="1456" height="2170" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2170,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5389161,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182943666?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rkwx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 424w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 848w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 1272w, https://substackcdn.com/image/fetch/$s_!rkwx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c9f1e58-835e-4a31-bcad-2336dedbbe76_1696x2528.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>&#39318;&#20808;&#65292;&#25105;&#20497;&#20808;&#22238;&#31572;&#19968;&#20491;&#26368;&#22522;&#30990;&#21839;&#38988;&#65306;</p><h1>K8S &#22312;&#32178;&#36335;&#23652;&#35201;&#35299;&#27770;&#20160;&#40636;&#21839;&#38988;&#65311;</h1><p>K8S cluster &#26371;&#26377;&#22810;&#20491; worker node&#65292;&#27599;&#20491; node &#26377;&#22810;&#20491; pod&#65292;&#19968;&#20491; pod &#26159;&#19968;&#20491; container &#26377;&#33258;&#24049;&#30340; network namespace&#65292;&#38548;&#38626;&#20102;&#32178;&#36335;&#21487;&#35222;&#31684;&#22285;&#65292;&#22240;&#27492;&#21021;&#22987;&#26178;&#65292;pod &#20043;&#38291;&#26159;&#30475;&#19981;&#21040;&#24444;&#27492;&#30340;&#65292;&#21363;&#20415;&#36889;&#20123; pod &#37117;&#26159;&#22312;&#21516;&#19968;&#20491; node &#35041;&#38754;&#12290;</p><h2>&#22914;&#20309;&#35731;&#30456;&#21516; Node &#20043;&#38291;&#30340; pod &#33021;&#20114;&#30456;&#20132;&#25563;&#23553;&#21253;&#65311;</h2><p>&#23567;&#26178;&#20505;&#21435;&#26379;&#21451;&#23478;&#29609;&#19990;&#32000;&#24093;&#22283;&#65292;&#21363;&#20415;&#27794;&#26377;&#32178;&#36335;&#65292;&#21482;&#35201;&#38651;&#33126;&#26377;&#32178;&#21345;&#65292;&#36879;&#36942;&#32178;&#32218;&#25509;&#19978;&#20132;&#25563;&#22120;&#65292;&#23601;&#21487;&#22312;&#21312;&#32178;&#30475;&#21040;&#24444;&#27492;&#30340;&#38651;&#33126;&#36899;&#32218;&#23565;&#25136;&#12290;</p><p>&#30456;&#21516;&#27010;&#24565;&#65292;&#21487;&#22312; kernel &#20013;&#24314;&#31435;&#34395;&#25836;&#20132;&#25563;&#22120; (aka linux bridge)&#65292;&#32000;&#37636; IP &amp; Mac address &#30340;&#36681;&#25563;&#65292;&#20006;&#36879;&#36942; veth pair &#23559;&#25152;&#26377; pod &#25509;&#19978;&#34395;&#25836;&#20132;&#25563;&#22120;&#65292;&#32780; veth pair &#39006;&#20284;&#34395;&#25836;&#32178;&#32218;&#65292;&#25509;&#19978;&#30636;&#38291;&#65292;kernel &#26371;&#28858;&#36889;&#20491; namespace &#24314;&#31435;&#34395;&#25836;&#32178;&#36335;&#25509;&#21475;&#65292;&#39006;&#20284;&#34395;&#25836;&#32178;&#21345;&#20006;&#20998;&#37197; Mac Address &#21644;&#32129;&#23450;&#20320;&#25351;&#23450;&#30340; IP &#65292;&#36889;&#27171;&#25152;&#26377; pod &#21487;&#36879;&#36942; linux bridge &#20132;&#25563;&#23553;&#21253;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f7-g!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f7-g!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 424w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 848w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 1272w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!f7-g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png" width="430" height="370" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53538f46-096f-496f-90a4-3156a11e99ca_430x370.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:370,&quot;width&quot;:430,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Fun with veth-devices, Linux bridges and VLANs in unnamed Linux network  namespaces &#8211; III | Linux-Blog &#8211; Dr. M&#246;nchmeyer / anracon&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Fun with veth-devices, Linux bridges and VLANs in unnamed Linux network  namespaces &#8211; III | Linux-Blog &#8211; Dr. M&#246;nchmeyer / anracon" title="Fun with veth-devices, Linux bridges and VLANs in unnamed Linux network  namespaces &#8211; III | Linux-Blog &#8211; Dr. M&#246;nchmeyer / anracon" srcset="https://substackcdn.com/image/fetch/$s_!f7-g!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 424w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 848w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 1272w, https://substackcdn.com/image/fetch/$s_!f7-g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53538f46-096f-496f-90a4-3156a11e99ca_430x370.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://linux-blog.anracom.com/2017/11/14/fun-with-veth-devices-linux-bridges-and-vlans-in-unnamed-linux-network-namespaces-iii/</p><h2>&#37027;&#40636;&#36328; Node &#20043;&#38291;&#30340; pod &#35201;&#24590;&#40636;&#20132;&#25563;&#23553;&#21253;&#65311;</h2><p>&#30070; destination IP &#19981;&#22312; linux bridge &#20013;&#65292;&#21218;&#24517;&#22312;&#19981;&#21516;&#27231;&#22120;&#19978;&#65292;&#27492;&#26178; pod &#30340; outbound &#23553;&#21253;&#35201;&#36879;&#36942; node &#30340; root kernel &#36865;&#20986;&#65292;&#19988; destination IP &#19981;&#22312;&#26159; pod IP &#32780;&#26159;&#21478;&#19968;&#20491; node IP&#65292;&#36889;&#27171;&#22806;&#37096; layer 3 &#32178;&#36335;&#25165;&#30693;&#36947;&#24590;&#40636;&#36681;&#36865;&#23553;&#21253;&#12290;</p><p>&#20363;&#22914; podA -&gt; podB&#8217;s IP -&gt; nodeA -&gt; nodeB&#8217;s IP -&gt; nodeB -&gt; podB&#8217;s IP -&gt; podB</p><p>&#20294;&#19978;&#38754;&#36889;&#26781;&#36335;&#32147;&#26377;&#20841;&#20491;&#21839;&#38988;&#35201;&#20811;&#26381;&#65306;</p><h3>node B &#25910;&#21040; destination IP &#26159; node IP &#26178;&#65292;&#35201;&#24590;&#30693;&#36947;&#35442;&#23553;&#21253;&#35201;&#32102;&#21738;&#20491; pod&#65311;</h3><p>&#31572;&#26696;&#26159;&#36879;&#36942; overlay <strong>network</strong> &#36319; <strong>tunneling</strong> &#25216;&#34899;&#65306;</p><p><strong>tunneling &#25216;&#34899;</strong>&#26159;&#23553;&#21253;&#30090;&#21152;&#25216;&#34899;&#65292;&#20363;&#22914;&#21407;&#23553;&#21253; destination ip &#26159; podB&#8217;s IP&#65292;&#36879;&#36942; tunneling &#30090;&#21152;&#19968;&#23652;&#65292;&#35731; destination ip &#35722;&#25104; nodeB&#8217;s IP&#65292;&#30070; node B &#25910;&#21040;&#24460;&#31227;&#38500;&#30090;&#21152;&#36039;&#35338;&#65292;&#23601;&#33021;&#35703;&#24471; pod B&#8217;s IP&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Oav7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Oav7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 424w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 848w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 1272w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Oav7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png" width="940" height="481" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:481,&quot;width&quot;:940,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;VxLAN vs NVGRE: What's the Difference? - GeeksforGeeks&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="VxLAN vs NVGRE: What's the Difference? - GeeksforGeeks" title="VxLAN vs NVGRE: What's the Difference? - GeeksforGeeks" srcset="https://substackcdn.com/image/fetch/$s_!Oav7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 424w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 848w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 1272w, https://substackcdn.com/image/fetch/$s_!Oav7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bb884ce-c33a-4a9c-adbc-8fe3b6acaca2_940x481.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://www.geeksforgeeks.org/computer-networks/vxlan-vs-nvgre-whats-the-difference/</p><p>&#32780;&#23526;&#38555;&#30340;&#30090;&#21152;&#36319;&#31227;&#38500;&#26159;&#36879;&#36942; <strong>overlay network</strong>&#65292;&#20363;&#22914; VXLAN &#22312; kernel &#20013;&#36939;&#34892;&#19968;&#20491; UDP service bind &#22312; 4789 port&#65292;&#39006;&#20284;&#19968;&#20491;&#34395;&#25836; router (vxlan device)&#65292;Node A &#36865;&#23553;&#21253;&#26178;&#65292;&#25226; dest IP &#26159; podB&#8217;s IP &#30340; TCP &#23553;&#21253;&#23553;&#35037;&#25104; dest IP &#26159; nodeB&#8217;s IP &#30340; UDP &#23553;&#21253;&#65292;&#36865;&#24448; 4789 port&#65292;VXLAN service &#25910;&#21040;&#24460;&#26371;&#35299;&#26512; UDP &#29554;&#24471;&#32102; podB&#8217;s IP &#30340; TCP &#23553;&#21253;&#65292;&#32780;&#36889;&#20491; UDP network &#26159;&#30090;&#21152;&#22312; TCP network &#19978;&#30340;&#65292;&#25152;&#20197;&#31281;&#28858; overlay network&#12290;</p><h3>node A &#35201;&#24590;&#40636;&#23559; podB&#8217;s IP &#25563;&#25104; nodeB&#8217;s IP&#65311;</h3><p>&#39318;&#20808; K8S &#38656;&#35201;&#20570;&#21040;&#22312;&#27599;&#20491; pod &#21021;&#22987;&#21270;&#26178;&#20998;&#37197;&#19968;&#20491; private IP&#65292;&#19988;&#35442; IP &#36328; Node &#20043;&#38291;&#19981;&#33021;&#30896;&#25758;&#65292;&#22240;&#27492;&#38656;&#35201;&#29544;&#31435;&#30340; control panel server &#36000;&#36012;&#31649;&#29702;&#25152;&#26377; node &#21487;&#29992;&#30340; CIDR (e.g 10.0.0.0/32 )&#12290;</p><p>&#25509;&#33879;&#19968;&#27171;&#36879;&#36942; overlay network&#65292;node A &#23559; dest IP &#26159; podB&#8217;s IP &#30340; TCP &#23553;&#21253;&#36865;&#32102;&#65292;VXLAN &#36889;&#20491;&#34395;&#25836; router (vxlan device)&#65292;VXLAN &#26371;&#36879;&#36942;&#24478; control panel &#29554;&#24471;&#19981;&#21516; Node &#36000;&#36012;&#30340; CIDR &#65292;&#23559;&#23553;&#21253;&#23553;&#35037;&#25104; dest IP &#26159; nodeB&#8217;s IP &#30340; UPD &#23553;&#21253;&#36865;&#20986;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DQa_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DQa_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 424w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 848w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DQa_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg" width="599" height="299" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:299,&quot;width&quot;:599,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;ENCOR Training &#187; VXLAN Tutorial&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="ENCOR Training &#187; VXLAN Tutorial" title="ENCOR Training &#187; VXLAN Tutorial" srcset="https://substackcdn.com/image/fetch/$s_!DQa_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 424w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 848w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!DQa_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31675199-59d2-4ce4-969e-4201e81f60c1_599x299.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://www.digitaltut.com/vxlan-tutorial/2</p><p>&#26377;&#20102;&#35731; Pod &#20043;&#38291;&#28317;&#36890;&#30340;&#33021;&#21147;&#24460;&#65292;&#25509;&#19979;&#20358;&#35201;&#35299;&#27770; Pod IP &#37325;&#21855;&#24460;&#26371;&#19968;&#30452;&#25913;&#35722;&#30340;&#21839;&#38988;&#12290;</p><h1>&#22914;&#20309;&#29992;&#19968;&#20491;&#22266;&#23450; private IP &#36865;&#35531;&#27714;&#21040;&#29305;&#23450;&#30340; Pod&#65311;</h1><p>&#30070; Pod &#26356;&#26032;&#37325;&#21855;&#65292;IP &#37325;&#20998;&#37197;&#26371;&#25913;&#35722;&#65292;&#23565; Client &#31471;&#38750;&#24120;&#19981;&#26041;&#20415;&#65292;&#22240;&#27492; K8S &#25552;&#20379; Service Endpoint&#65292;&#24314;&#31435; Service Endpoint &#26178;&#65292;K8S &#26371;&#20998;&#37197;&#19968;&#20491; IP&#65292;&#20006;&#36879;&#36942; label selector &#25214;&#21040;&#35201;&#32129;&#23450;&#30340; pod&#65292;&#23559;&#36865;&#24448;&#35442; service IP &#30340;&#35531;&#27714; forward &#21040; pod &#20013;&#65292;&#21482;&#35201; service &#19981;&#37325;&#24314;&#65292;pod &#19981;&#26039;&#37325;&#21855;&#37117;&#19981;&#24433;&#38911; client &#29992; service IP &#36865;&#35531;&#27714;&#21040; pod&#12290;</p><p>&#32780;&#23559;&#30456;&#21516; service IP &#35531;&#27714;&#36681;&#30332;&#32102;&#19981;&#21516; pod &#30340;&#21151;&#33021;&#65292;&#26412;&#36074;&#19978;&#20006;&#19981;&#26159; Pod &#20043;&#38291;&#30340;&#32178;&#36335;&#36890;&#35338;&#65292;&#32780;&#26159;&#19968;&#20491; Layer4 Load Balance&#65292;&#22240;&#27492;&#20659;&#32113; CNI &#20363;&#22914; Flannel &#19981;&#36000;&#36012;&#36681;&#30332;&#65292;&#21482;&#36000;&#36012; Pod &#20043;&#38291;&#30340; Layer 3 &#36890;&#35338;&#12290;</p><p>&#22240;&#27492; Layer4 Load Balance &#26159;&#30001; kube-proxy &#19968;&#20491; K8S &#26550;&#27083;&#20013;&#30340;&#26680;&#24515;&#27169;&#32068;&#23526;&#29694;&#30340;&#12290;</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h1>kube-proxy &#22914;&#20309;&#23559; destination IP &#26159; service IP &#30340;&#23553;&#21253; forward &#32102; pod &#21602;&#65311;</h1><p>&#26089;&#26399;&#29256;&#26412;&#26159;&#29992; iptable&#65292;iptable &#26159;&#20351;&#29992; kernel netfilter module &#30340;&#32178;&#36335;&#23553;&#21253;&#35215;&#21063;&#31649;&#29702;&#24037;&#20855;&#65292;netfilter &#22312; kernel &#20013;&#36000;&#36012;&#23553;&#21253;&#35299;&#26512; &amp; &#36899;&#32218;&#36861;&#36452;&#65292;&#20854;&#25552;&#20379;&#22312;&#35299;&#26512;&#21069;&#36319;&#35299;&#26512;&#24460;&#25554;&#20837; hook &#21151;&#33021;&#65292;&#20363;&#22914; layer 3 &#35299;&#26512;&#24460;&#65292;&#25554;&#20837;&#19968;&#20491;&#38459;&#25803;&#29305;&#23450; ip &#30340; hook &#23601;&#33021;&#23526;&#29694;&#38450;&#28779;&#29254;&#21151;&#33021;&#65292;&#32780;&#36889;&#23601;&#26159; iptable &#26368;&#37325;&#35201;&#30340;&#21151;&#33021;&#20043;&#19968;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PBqt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PBqt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 424w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 848w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 1272w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PBqt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png" width="979" height="774" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:774,&quot;width&quot;:979,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&#36830;&#25509;&#36319;&#36394;&#65288;conntrack&#65289;&#65306;&#21407;&#29702;&#12289;&#24212;&#29992;&#21450;Linux &#20869;&#26680;&#23454;&#29616;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="&#36830;&#25509;&#36319;&#36394;&#65288;conntrack&#65289;&#65306;&#21407;&#29702;&#12289;&#24212;&#29992;&#21450;Linux &#20869;&#26680;&#23454;&#29616;" title="&#36830;&#25509;&#36319;&#36394;&#65288;conntrack&#65289;&#65306;&#21407;&#29702;&#12289;&#24212;&#29992;&#21450;Linux &#20869;&#26680;&#23454;&#29616;" srcset="https://substackcdn.com/image/fetch/$s_!PBqt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 424w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 848w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 1272w, https://substackcdn.com/image/fetch/$s_!PBqt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F989c95d5-6a6e-4e55-b3c2-192c0320ca27_979x774.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://arthurchiao.art/blog/conntrack-design-and-implementation-zh/</p><p>iptable &#21487;&#22312;&#19981;&#21516;&#26178;&#38291;&#40670;&#24314;&#31435;&#19981;&#21516;&#21151;&#33021;&#30340;&#35215;&#21063;&#65306;</p><ul><li><p>PREROUTING&#65306;kernel layer 3 &#35299;&#21253;&#24460;&#65292;&#36914;&#20837; routing &#35215;&#21063;&#21069;</p></li><li><p>INPUT&#65306;&#22519;&#34892;&#23436; routing &#35215;&#21063;&#65292;&#30332;&#29694;&#26159;&#33258;&#24049;&#30340;&#23553;&#21253;&#65292;&#34389;&#29702;&#36889;&#20491;&#23553;&#21253;&#21069;</p></li><li><p>FORWARD&#65306;&#22519;&#34892;&#23436; routing &#35215;&#21063;&#65292;&#30332;&#29694;&#19981;&#26159;&#33258;&#24049;&#30340;&#65292;forward &#20986;&#21435;&#32102;&#20854;&#20182;&#20154;&#21069;</p></li><li><p>OUTPUT&#65306;user space process &#36865;&#23553;&#21253;&#32102; kernel&#65292;&#22519;&#34892; routing &#20043;&#21069;</p></li><li><p>POSTROUTING &#65306;&#23436;&#25104; routing decision&#65292;&#30906;&#23450;&#32178;&#36335;&#20986;&#21475;&#20171;&#38754;&#24460;&#65292;&#36865;&#20986;&#23553;&#21253;&#20043;&#21069;</p></li></ul><p>&#24120;&#35211;&#38450;&#28779;&#29254;&#26159;&#22312; INPUT &#21152;&#19978;&#29305;&#23450; IP DROP &#35215;&#21063;&#65292;&#21487;&#25803; inbound request&#65292;&#22312; OUTPUT &#21152;&#19978; DROP &#25803; outbound request&#12290;</p><p>&#30070;&#23553;&#21253;&#32147;&#36942; VXLAN &#35299;&#21253;&#29554;&#24471; dest service IP &#30340; TCP &#23553;&#21253;&#26178;&#65292;&#21487;&#24314;&#31435; PREROUTING &#35215;&#21063;&#65292;&#23559; service IP &#20462;&#25913;&#25104; pod IP &#20358;&#23526;&#29694;&#23553;&#21253;&#36681;&#30332;&#65292;&#30070;&#19968;&#20491; service IP &#32972;&#24460;&#26377;&#22810;&#20491; pod &#26178;&#65292;&#26371;&#24314;&#31435;&#22810;&#26781;&#35215;&#21063;&#65292;&#36879;&#36942;&#35373;&#23450; probability &#23526;&#29694; load balancing&#65292;&#20363;&#22914;&#26377;&#19977;&#20491; pod&#65292;&#26371;&#24314;&#31435;&#19977;&#20491; PREROUTING &#35215;&#21063;&#65292;&#30001;&#26044;&#35215;&#21063;&#26159;&#19968;&#26781;&#26781;&#38918;&#24207;&#22519;&#34892;&#30340;&#65292;&#25152;&#20197;&#31532;&#19968;&#26781;&#22519;&#34892;&#25104;&#21151;&#27231;&#29575; 33%&#65292;&#31532;&#20108;&#26781; 50%&#65292;&#31532;&#19977;&#26781; 100&#65285;&#65292;&#23601;&#33021;&#22312;&#27231;&#29575;&#19978;&#35731;&#35531;&#27714;&#24179;&#22343;&#36865;&#24448;&#19977;&#20491;&#19981;&#21516; pod&#12290;</p><p>&#20462;&#25913; dest IP &#24460;&#36914;&#20837; routing &#36865;&#24448; bridge &#36681;&#30332;&#32102; pod &#25110;&#32773;&#32102; vxlan device &#36865;&#24448;&#20854;&#20182; node&#12290;</p><h1>&#38500;&#20102;&#20462;&#25913; destination IP &#20043;&#22806;&#65292;&#28858;&#20309;&#36996;&#35201;&#20462;&#25913; source IP&#65311;</h1><p>pod &#25910;&#21040; request &#24460;&#65292;&#36865;&#20986; response &#26178;&#26371;&#38656;&#35201;&#23559; source IP &#24478; pod IP &#36996;&#21407;&#25104; service IP&#65292;&#20294;&#26159;&#28858;&#20309;&#65311;</p><p>&#21407;&#22240;&#22312;&#26044; TCP &#24314;&#31435;&#26159;&#26377;&#29376;&#24907;&#30340;&#65292;&#26371;&#36879;&#36942; (src_ip, src_port, dest_ip, dest_port, protocol) &#32068;&#25104;&#36899;&#32218; ID&#65292;&#32000;&#37636;&#35442;&#36899;&#32218;&#20013;&#23553;&#21253;&#25509;&#25910;&#29376;&#27841;&#65292;&#26377;&#27794;&#26377;&#36986;&#28431;&#65292;&#35201;&#19981;&#35201; retry&#65292;client &#24314;&#31435; TCP &#26178;&#26159;&#29992; service IP &#28858; dest_ip&#65292;&#22914;&#26524; response &#30340; src_ip &#19981;&#26159; service IP &#22312; client kernel &#20013;&#26371;&#28961;&#27861;&#21305;&#37197;&#35442;&#23553;&#21253;&#23660;&#26044;&#21738;&#20491; TPC &#36899;&#32218;&#32780;&#34987;&#19999;&#26820;&#65292;&#22240;&#27492;&#38656;&#35201;&#20462;&#25913; response &#30340; src_ip&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kcxX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kcxX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 424w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 848w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kcxX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg" width="700" height="365" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:365,&quot;width&quot;:700,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Napatech blog - What is a flow?&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Napatech blog - What is a flow?" title="Napatech blog - What is a flow?" srcset="https://substackcdn.com/image/fetch/$s_!kcxX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 424w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 848w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!kcxX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc99fdf41-b0cb-408f-bf86-0207a5b69b31_700x365.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://www.napatech.com/what-is-a-flow/</p><p>&#20294;&#19981;&#26159;&#25152;&#26377; response &#30340; src_ip &#37117;&#35201;&#25913;&#65292;&#20551;&#35373; pod A &#24448; pod B &#24314;&#31435;&#20841;&#26781; TCP connections&#65292;src_port &#26371;&#38568;&#27231;&#20998;&#37197;&#65306;</p><ul><li><p>conn_1 &#20351;&#29992; service IP &#65292;id &#28858; (src_ip, src_port_1, service_ip, dest_port, protocol)</p></li><li><p>conn_2 &#30452;&#25509;&#29992; pod IP&#65292;id &#28858; (src_ip, src_port_2, pod_ip, dest_port, protocol)</p></li></ul><p>&#37027;&#21482;&#26377; conn_1 &#30340; response src_ip &#35201;&#24478; service_ip &#25913;&#25104; pod_ip&#65292;conn_2 &#21063;&#35201;&#20445;&#30041;&#65292;&#35201;&#20570;&#21040;&#36889;&#20214;&#20107;&#24773;&#65292;&#23601;&#38656;&#35201;&#32000;&#37636;&#27599;&#20491;&#21407;&#22987; connection &#30340; id&#65292;&#20063;&#23601;&#26159; netfilter conntrack &#21151;&#33021;&#12290;</p><p>conntrack &#21487;&#29992; connection id &#32000;&#37636;&#32147;&#36942; netfilter &#24314;&#31435;&#30340; TCP &#36899;&#32218;&#65292;&#20197;&#21450;&#36681;&#25563;&#34920;&#65292;&#26377;&#20102; conntrack &#21487;&#20197;&#36942;&#28670;&#20986; conn_2 &#27794;&#26377;&#36681;&#25563;&#36942;&#65292;&#19981;&#38656;&#35201;&#20462;&#25913;&#65292;&#32780; conn_1 &#26377;&#36681;&#25563;&#36942;&#65292;&#38656;&#36996;&#21407; src_ip &#28858; service IP&#12290;</p><h1>&#20351;&#29992; iptable &#26377;&#20160;&#40636;&#32570;&#40670;&#65311;</h1><p>iptable &#20027;&#35201;&#26159;&#38450;&#28779;&#29254;&#21151;&#33021;&#65292;&#20294; K8S service IP &#30906;&#26159; forward + load balancer&#65292;&#22240;&#27492;&#20351;&#29992; iptable &#26377;&#20841;&#20491;&#32570;&#40670;&#65306;</p><ul><li><p>&#25928;&#33021;&#24046;&#65306;iptable &#38656;&#35201; for loop &#25152;&#26377;&#35215;&#21063;&#65292;&#30452;&#21040;&#31532;&#19968;&#20491;&#35215;&#21063;&#21629;&#20013;&#24460;&#25165; break&#65292;&#22914;&#26524; service &#24456;&#22810;&#65292;pod &#24456;&#22810;&#65292;&#25928;&#33021;&#26371;&#24456;&#24046;</p></li><li><p>load balancer&#65306;&#21482;&#33021;&#29992;&#27231;&#29575;&#23526;&#20316;&#65292;&#27794;&#26377;&#30906;&#23450;&#65292;&#23458;&#35069;&#21270;&#31243;&#24230;&#20302;&#65292;&#20363;&#22914;&#19981;&#33021;&#29992; Round Robin</p></li></ul><p>&#32780; <strong>IPVS</strong> &#26159; Linux kernel &#36879;&#36942; netfilter hook &#23526;&#20316;&#30340; L4 Load Balancer &#27169;&#32068;&#65292;&#23433;&#35037; ipvsadm &#24037;&#20855;&#24460;&#65292;&#21487;&#30452;&#25509;&#24448; IPVS &#24314;&#31435; routing table&#65292;&#26159;&#19968;&#20491; hashmap &#32080;&#27083;&#65292;O(1) &#26597;&#25214;&#25928;&#33021;&#26356;&#22909;&#65292;&#19988;&#25903;&#25588;&#22810;&#31278; load balance &#31574;&#30053;&#65292;&#20363;&#22914; Round Robin or Least Connections&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OXiC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OXiC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 424w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 848w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OXiC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg" width="1024" height="576" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:576,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;IPTables vs IPVS in kubernetes&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="IPTables vs IPVS in kubernetes" title="IPTables vs IPVS in kubernetes" srcset="https://substackcdn.com/image/fetch/$s_!OXiC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 424w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 848w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!OXiC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3295978d-5000-4eeb-9870-8bd643199f00_1024x576.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://www.linkedin.com/pulse/iptables-vs-ipvs-kubernetes-vivek-grover/</p><p>&#26377;&#20102; IPVS &amp; ipvsadm &#20043;&#24460;&#65292;&#20173;&#38656;&#35201;&#19968;&#20491; process &#21435;&#30435;&#32893; control panel &#30340; pod &amp; service &#35722;&#21270;&#36890;&#30693;&#65292;&#26377;&#26032;&#30340; pod &#24314;&#31435;&#38656;&#35201;&#32129;&#23450; service IP&#65292;&#23601;&#35201;&#36879;&#36942; ipvsadm &#20462;&#25913; routing table&#65292;&#32780;&#36889;&#20491; process &#23601;&#26159; kube-proxy&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6hbj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6hbj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 424w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 848w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 1272w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6hbj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png" width="827" height="451" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:451,&quot;width&quot;:827,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:137998,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182943666?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6hbj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 424w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 848w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 1272w, https://substackcdn.com/image/fetch/$s_!6hbj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcb62ead6-818b-42ab-8cab-1cf5c3bd9a82_827x451.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source : https://www.alibabacloud.com/blog/improving-kubernetes-service-network-performance-with-socket-ebpf_599446</p><h1>&#19979;&#38598;&#38928;&#21578;</h1><p>&#20351;&#29992; IPVS &#23526;&#29694; L4 load balancer &#25928;&#33021;&#24050;&#32147;&#19981;&#37679;&#20102;&#65292;&#20294;&#22909;&#36996;&#35201;&#26356;&#22909;&#65292;&#22312;&#20043;&#21069; cloudflare &#25991;&#31456;&#20013;&#25552;&#21040;&#65292;ebpf &#20063;&#21487;&#23526;&#29694; L4 load balancer&#65292;&#19979;&#38598;&#23601;&#20358;&#35498;&#35498;&#65292;ebpf &#30340; load balancer &#26356;&#22909;&#22312;&#21738;&#35041;&#12290;</p><p>&#27492;&#22806;&#65292;&#38614;&#28982;&#26377; L4 load balancer&#65292;&#20294;&#26159;&#35201;&#23526;&#29694; grpc load balancing &#36890;&#24120;&#38656;&#35201;&#25645;&#37197; service mesh + sidecar&#65292;&#28858;&#20160;&#40636;&#21482;&#26377; sidecar &#33021;&#35299;&#27770; grpc load balancing&#65292;&#32780; ebpf &#21448;&#24590;&#40636;&#35731; sidecar &#25928;&#33021;&#22823;&#24133;&#25552;&#21319;&#65292;&#26371;&#19968;&#36215;&#22312;&#19979;&#38598;&#35498;&#26126;&#65281;</p>]]></content:encoded></item><item><title><![CDATA[CDN is More Than Just Cache: Breaking Down Cloudflare’s Architecture by OSI 7 Layers]]></title><description><![CDATA[Analysis of the Technology Stack: BGP, Anycast, Unimog, QUIC, and Pingora]]></description><link>https://www.chillvic.dev/p/cdn-is-more-than-just-cache-breaking</link><guid isPermaLink="false">https://www.chillvic.dev/p/cdn-is-more-than-just-cache-breaking</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Mon, 22 Dec 2025 02:32:30 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!XS-x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Cloudflare, a dominant force in the CDN market, provides far more than simple caching. it integrates WAF, DDoS protection, and even allows engineers to execute JavaScript at the edge via Cloudflare Workers. It has effectively become one of the most critical infrastructures of the modern internet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XS-x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XS-x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XS-x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3483183,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182281728?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XS-x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!XS-x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18948c6b-c297-4ecd-9a95-2b8cf3143ae5_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>What does the architecture of such a vital infrastructure look like? Let&#8217;s explore through the following deep dive:</p><h1>How to Map a Single Domain to Global Edge Proxies?</h1><p>The primary function of a CDN is to route requests to the nearest edge proxy based on the user's location.</p><p>Typically, one domain maps to one public IP. <strong>Geo DNS</strong> supports mapping the same domain to different public IPs based on geography. During a recursive DNS query, the Geo DNS server identifies the client&#8217;s location via their IP and returns the corresponding regional public IP.</p><p><strong>However, this method has flaws:</strong></p><ol><li><p><strong>Latency:</strong> If the Geo DNS server is in the US, the query might travel a long distance just to find a public IP.</p></li><li><p><strong>DNS Caching:</strong> DNS has TTL (Time to Live) values. If a public IP needs to be replaced due to a failure, it takes time for the change to propagate globally, preventing instant failover.</p></li></ol><p>To solve this, Cloudflare uses <strong>Anycast</strong> technology to map a single public IP to physical machines in multiple locations simultaneously.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TIFm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TIFm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 424w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 848w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1272w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png" width="797" height="442" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:442,&quot;width&quot;:797,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TIFm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 424w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 848w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1272w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</p><h1>How Does Anycast Map One IP to Multiple Locations?</h1><p>Networks are divided into LANs and WANs. Within a region, an <strong>Autonomous System (AS) </strong>(e.g., an ISP) manages a specific network range (e.g., 36.224.0.0/13) . When packets enter the WAN to travel between ASs, <strong>they use the Border Gateway Protocol (BGP) to exchange routing information (a.k.a, IP Prefixes).</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hKER!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hKER!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 424w, https://substackcdn.com/image/fetch/$s_!hKER!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 848w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg" width="800" height="615" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:615,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hKER!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 424w, https://substackcdn.com/image/fetch/$s_!hKER!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 848w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.cloudns.net/blog/understanding-bgp-a-comprehensive-guide-for-beginners/">https://www.cloudns.net/blog/understanding-bgp-a-comprehensive-guide-for-beginners/</a></p><p>For example, an ISP in Taiwan uses BGP to announce to an ISP in Japan that it is responsible for a specific IP range (e.g., <strong>36.224.0.0/13</strong>). When a Japanese user sends a request to an IP within that range (e.g., <strong>36.224.1.1</strong>), the Japanese ISP's routing table directs the packet to Taiwan&#8217;s border router. That border router then forwards the request to the final destination server within the local network.</p><p><strong>Anycast</strong> registers the same IP Prefix with different ASs via multiple border routers. Because different border routers maintain different routing tables, the same public IP will be forwarded to different locations depending on the source AS.</p><p><strong>Cloudflare leverages its status as an independent AS to register IP prefixes at the edge of various networks globally. This ensures that a Taiwan-based request reaches a Taiwan edge proxy via a local border router, minimizing latency by keeping traffic local.</strong></p><p>BGP is a Layer 3 protocol, meaning forwarding doesn&#8217;t require deep packet inspection, leading to high performance and availability. If a Japan data center fails, BGP automatically finds the next best path (e.g., to Taiwan) for that same IP.</p><h1>Is Anycast + BGP the Perfect Solution?</h1><p>Not quite. BGP routing isn&#8217;t based on geographic distance or RTT (Round Trip Time); it&#8217;s based on <strong>AS Path cost</strong>, which is largely determined by administrative policies.</p><p>For example, ASs may charge each other for transit. Network administrators can set the <strong>BGP path cost</strong> based on peering costs rather than geographic distance. To mitigate this, Cloudflare establishes direct peering with ASs to ensure <strong>Local AS Path</strong> priority.<br><br>Note: You can refer to the detailed BGP routing rules <strong><a href="https://networklessons.com/bgp/bgp-attributes-and-path-selection">here</a></strong>.<br><br>Additionally, BGP suffers from <strong>Hijacking</strong>. A hacker can announce the same IP Prefix to other ASs with a lower &#8220;cost,&#8221; diverting legitimate Cloudflare traffic to their own servers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VxRG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VxRG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 424w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 848w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1272w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png" width="800" height="416" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:416,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VxRG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 424w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 848w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1272w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/bgp-hijack-detection/">https://blog.cloudflare.com/bgp-hijack-detection/</a></p><p><strong>The solution is to verify AS identity during registration (similar to HTTPS certificates)</strong>, though not all ISPs implement this yet. You can use https://isbgpsafeyet.com/ to check whether your ISP has implemented a verification process.</p><p>Additionally, the nature of <strong>Anycast</strong> allows multiple proxy servers in the same region to share the same public IP. <strong>This can lead to the following issue: TCP packets belonging to the same connection might be routed to different servers.</strong></p><p>Once a client establishes a TCP connection with a server, the server&#8217;s kernel stores the <strong>TCP connection ID (src_ip, src_port, dest_ip, dest_port)</strong> and its <strong>corresponding state</strong> (such as which packets have been received and the expected sequence number of the next packet) in memory to ensure packet ordering. <strong>If a packet from that same TCP connection is sent to a different server that lacks the specific connection state, that server will close the connection.</strong></p><h1>How to Solve TCP Instability in Anycast</h1><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><p>The network path for a packet is:</p><ol><li><p><strong>Client</strong> &#8594; ISP Gateway &#8594; BGP Selection &#8594; <strong>Border Gateway</strong></p></li><li><p><strong>Border Gateway</strong> &#8594; <strong>NAT</strong> (Public IP to Private IP)</p></li><li><p><strong>NAT</strong> &#8594; Private Network <strong>Edge Proxy</strong></p></li></ol><p>Layer 2 Gateways use <strong>ECMP (Equal-Cost Multi-Path)</strong> to hash a connection's "4-tuple" (Source/Dest IP and Port) to ensure packets from the same connection go to the same proxy. </p><p>However, when edge proxies are added or removed, the <strong>ECMP algorithm</strong> becomes inconsistent, and it cannot distribute traffic based on the actual <strong>workload</strong> of each proxy.</p><p>Therefore, Cloudflare developed the <strong>Unimog Layer 4 Load Balancer</strong> using <strong>XDP (<a href="https://open.substack.com/pub/chillvic/p/observability-without-the-surgery?utm_campaign=post-expanded-share&amp;utm_medium=post%20viewer">eBPF</a>)</strong>, <strong>granting every proxy server the ability to forward packets</strong>. eBPF allows the implementation of a control plane within a user-space process, providing high flexibility&#8212;for instance, <strong>using <a href="https://github.com/kkdai/maglev">Meglev hashing</a> and monitoring proxy workload to dynamically adjust routing.</strong></p><p>Furthermore, because the proxies themselves possess forwarding capabilities, <strong>NAT</strong> redirection is no longer required. Instead, the proxy can use <strong>IP Tunneling</strong> to encapsulate the packet, modifying the destination IP to a private IP before sending it out.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gGs7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gGs7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 424w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 848w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1272w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png" width="757" height="406" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d44ef998-1e71-4875-a561-8c984b442c35_757x406.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:406,&quot;width&quot;:757,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!gGs7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 424w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 848w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1272w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/">https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</a></p><p>In addition to the Layer 4 Load Balancer, Cloudflare utilizes another key technology at Layer 4: <strong>QUIC</strong>!</p><h1><strong>What is QUIC, and What Problems Does it Solve?</strong></h1><p>QUIC is a Layer 4 protocol built on UDP, used in HTTP/3 to solve performance bottlenecks in TCP:</p><h3><strong>Head-of-Line (HoL) Blocking:</strong> </h3><p>HTTP/2 resolves the Layer 7 <strong>Head-of-Line (HoL) Blocking</strong> issues found in HTTP/1. It allows multiple requests to be sent simultaneously over a single TCP connection without waiting for the response of the current request. Once the application layer receives the response packets for different requests, it uses <strong>stream IDs</strong> to reassemble them into the complete payload.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4qid!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4qid!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 424w, https://substackcdn.com/image/fetch/$s_!4qid!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 848w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1272w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png" width="625" height="618" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:625,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4qid!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 424w, https://substackcdn.com/image/fetch/$s_!4qid!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 848w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1272w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://juejin.cn/post/6844903984524705800">https://juejin.cn/post/6844903984524705800</a></p><p>However, <strong>Head-of-Line Blocking also exists at Layer 4. From TCP&#8217;s perspective, it cannot distinguish between packets belonging to different requests</strong>; it treats them as part of the same connection and insists on receiving them in strict order. </p><p>If a single packet from &#8220;Response A&#8221; is lost, the kernel will not deliver the payload of &#8220;Response B&#8221; to the user-space process&#8212;even if all of Response B&#8217;s packets have arrived safely&#8212;until that missing packet is retransmitted and received.</p><p><strong>QUIC&#8217;s solution is to move the stream ID logic from Layer 7 down to</strong> <strong>Layer 4</strong>. This ensures that while packets within a single stream must remain ordered, different streams are independent of one another. Furthermore, because HTTP/3 allows the application layer (e.g., the browser) to parse and reassemble payloads, <strong>QUIC removes the need for the kernel to maintain packet ordering for multiple streams, thereby reducing the complexity of kernel memory management.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uGTS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uGTS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 424w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 848w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1272w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png" width="800" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b14cec7d-3d3d-4666-a04d-d48742377551_800x457.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uGTS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 424w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 848w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1272w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.researchgate.net/figure/The-head-of-line-blocking-problem-and-the-stream-based-solution_fig1_344551663">https://www.researchgate.net/figure/The-head-of-line-blocking-problem-and-the-stream-based-solution_fig1_344551663</a></p><h3><strong>Connection Establishment Requires a Handshake</strong></h3><p>Before transmitting data, <strong>TCP</strong> requires a handshake process:</p><ol><li><p>Client<code> SYN</code> &#8594; Server<code> </code></p></li><li><p>Server<code> SYN-ACK</code> &#8594; Client </p></li><li><p>Client<code> ACK</code> &#8594; Server</p></li></ol><p>Only then can data transmission begin. </p><p>In contrast, <strong>QUIC</strong> allows the client to send data along with the very first <code>SYN</code> packet, effectively enabling <strong>0-RTT</strong> (Zero Round-Trip Time) data transmission.</p><p>However, when <strong>TLS</strong> is supported, it typically requires <strong>1-RTT</strong>. This is achieved, for example, through the <strong>Diffie&#8211;Hellman algorithm</strong>:</p><ol><li><p><strong>Client</strong> &#8594; sends <code>Public Key</code> &#8594; <strong>Server</strong>.</p></li><li><p><strong>Server</strong> uses its <code>Private Key</code> + <code>Client&#8217;s Public Key</code> to calculate a <strong>Shared Secret</strong>.</p></li><li><p><strong>Server</strong> &#8594; sends a <code>Session Ticket</code> (i.e., the encrypted shared secret) + <code>Server&#8217;s Public Key</code> &#8594; <strong>Client</strong>.</p></li></ol><p>Finally, the <strong>Client</strong> uses its own <code>Private Key</code> + <code>Server&#8217;s Public Key</code> to calculate the same <strong>Shared Secret</strong>. The client then encrypts its request using this secret and sends it to the server along with the session ticket.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y-v-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 424w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 848w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1272w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png" width="800" height="419" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:419,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 424w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 848w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1272w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://vocal.com/voip/quic/">https://vocal.com/voip/quic/</a></p><p>Furthermore, <strong>session tickets</strong> can be stored on the client&#8217;s local disk, allowing them to be reused after a disconnection. Upon reconnecting, there is no need to exchange public keys or recalculate a shared secret; data can be sent immediately, achieving <strong>true 0-RTT</strong>. </p><p>However, this poses a risk of <strong>Replay Attacks</strong>. To mitigate this, the server-side periodically rotates the encryption keys used to generate session tickets, sets a short TTL (Time to Live) for the tickets, and only allows <strong>GET requests</strong> to utilize 0-RTT.</p><p>Beyond performance optimization, QUIC implements its own <strong>retransmission and flow control mechanisms</strong>. For example, if the server&#8217;s memory buffer is full, it will signal the client to stop sending data; similarly, if the RTT (Round-Trip Time) becomes too high, it will instruct the client to pause new transmissions until all missing packets are received.</p><p>Traditional TCP lacks the concept of &#8220;streams.&#8221; For the same packet, even if it is retransmitted, the sequence number remains identical. This leads to <strong>ACK ambiguity</strong>, where the server cannot distinguish whether an incoming ACK is for the original packet or the retransmitted one, <strong>making RTT calculations imprecise.</strong></p><p>In contrast, QUIC&#8217;s ordering is determined by the <strong>stream</strong>, and each stream has its own independent sequence. Packet IDs, however, increment globally (across all streams). <strong>This allows the system to distinguish whether an ACK is for a retransmission or the original packet, leading to more accurate RTT measurements and more reliable flow control.</strong></p><p>Additionally, because QUIC&#8217;s retransmission and flow control are implemented in <strong>user space</strong> (while the kernel simply sees UDP), it offers much greater flexibility. It can be tailored to specific business requirements&#8212;for instance, in streaming scenarios where retransmission requirements are lower but strict flow control is essential.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AMcy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AMcy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 424w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 848w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1272w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png" width="800" height="357" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:357,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AMcy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 424w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 848w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1272w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://datatracker.ietf.org/meeting/98/materials/slides-98-edu-sessf-quic-tutorial-00.pdf">https://datatracker.ietf.org/meeting/98/materials/slides-98-edu-sessf-quic-tutorial-00.pdf</a></p><h1>Beyond performance optimization, what other benefits does Cloudflare gain from using QUIC?</h1><p>Cloudflare implemented QUIC in Rust (via the <a href="https://github.com/cloudflare/quiche">quiche</a> library) and identified key advantages and challenges during implementation:</p><h3><strong>Benefit &#8212; Connection Migration</strong></h3><p>A TCP connection is identified by a 4-tuple: <code>(src_ip, src_port, dest_ip, dest_port)</code>. Once a user switches networks and changes their source IP (e.g., moving from 5G to Wi-Fi), the connection must be re-established.</p><p>In contrast, <strong>QUIC</strong> uses a <strong>Connection ID (CID)</strong>, which is a random number stored on the client side. As long as the CID remains the same, the state can be recognized even if the user switches networks.</p><p>As mentioned in the Anycast architecture earlier, standard routers at Layer 3 use <strong>ECMP</strong> to decide routes based on the 4-tuple hash. When a user&#8217;s source IP changes, the same CID would typically be forwarded to a different server. However, by adding <strong>CID-based forwarding</strong> to the <strong>Unimog Layer 4 load balancer</strong>, Cloudflare enables users to switch networks without dropping their connection.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!d-QA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!d-QA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 424w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 848w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg" width="800" height="580" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:580,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!d-QA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 424w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 848w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://zhuanlan.zhihu.com/p/311221111</p><h3><strong>Challenge &#8212; Reflection Attacks</strong></h3><p>Since QUIC is built on <strong>UDP</strong>, it is susceptible to <strong>reflection attacks</strong>, a common UDP exploit. Hackers forge the victim&#8217;s IP and send massive requests to a server, causing the server to flood the victim&#8217;s network with large volumes of data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x2rJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 424w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 848w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1272w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png" width="800" height="402" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:402,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 424w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 848w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1272w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://docs.aws.amazon.com/whitepapers/latest/aws-best-practices-ddos-resiliency/udp-reflection-attacks.html">https://docs.aws.amazon.com/whitepapers/latest/aws-best-practices-ddos-resiliency/udp-reflection-attacks.html</a></p><p>For instance, QUIC&#8217;s TLS handshake requires sending certificates and public keys to the client&#8212;a significant amount of data. A hacker could repeatedly send TLS requests to force the server to bombard the victim with these large payloads.</p><p>TCP avoids this problem by using a small-data handshake; it only sends certificates after receiving a <code>Client ACK</code>. If a hacker forges an IP, they won&#8217;t receive the <code>SYN-ACK</code> and thus cannot return the <code>Client ACK</code>.</p><p>The fundamental solution for QUIC remains a handshake where the server sends an encrypted token to the client and requires it to be returned. However, this method sacrifices the <strong>0-RTT</strong> feature. Consequently, Cloudflare is exploring alternatives like <strong>ECDSA certificates</strong> or <strong>certificate compression</strong> to reduce the payload size.</p><p>In addition to implementing QUIC in Rust, Cloudflare also developed its own proxy server, <strong><a href="https://github.com/cloudflare/pingora">Pingora</a></strong>, to replace Nginx.</p><h1>Why Did Cloudflare Replace Nginx with Pingora?</h1><p>Nginx follows a <strong>process-based</strong> architecture, <strong>utilizing the </strong><code>SO_REUSEPORT</code><strong> socket system call to allow multiple processes to bind to the same port.</strong> The kernel then distributes sockets to these processes based on a hash of the 4-tuple: <code>(src_ip, src_port, dest_ip, dest_port)</code>.</p><p>The downside of a process-based model is that implementing <strong>shared memory</strong> is complex and restrictive. For example, connections from the proxy to the <strong>origin server</strong> cannot be shared across processes. Furthermore, since Nginx is written in C, it carries inherent risks of memory leaks.</p><p>Cloudflare developed <strong>Pingora</strong>, a <strong>thread-based</strong> proxy written in <strong>Rust</strong>. While it retains an architecture similar to Nginx&#8212;<strong>with multiple kernel threads binding to the same port and each thread having its own independent </strong><code>epoll</code><strong> instance&#8212;it allows for the sharing of TCP connection information to origin servers across threads.</strong> Additionally, for scenarios involving exceptionally large payloads or file transfers, Pingora utilizes <strong>zero-copy</strong> techniques. This prevents the CPU overhead caused by copying massive amounts of data from kernel space to user space.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zJVi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zJVi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg" width="800" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zJVi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.linkedin.com/posts/pal-arnab_what-is-pingora-pingora-is-a-new-http-proxy-activity-7106102391397113856-B8rJ/">https://www.linkedin.com/posts/pal-arnab_what-is-pingora-pingora-is-a-new-http-proxy-activity-7106102391397113856-B8rJ/</a></p><p>Moreover, Nginx carries significant <strong>developmental baggage</strong>. <strong>For instance, it often uses Lua scripts for feature extension, but Lua&#8217;s weak typing makes it difficult to maintain and results in lower performance.</strong> Nginx is also modularly built with many overlapping features; even unused modules can impact overall performance. Benchmark results show that Pingora delivers higher throughput, consumes less CPU, and offers greater stability compared to Nginx.</p><h1><strong>How Does Cloudflare Defend Against DDoS?</strong></h1><p>Anycast is inherently more resilient to DDoS attacks than Geo DNS. <strong>When a hacker resolves a domain to an IP and launches an attack, the requests are distributed to different locations via BGP, providing Layer 3 traffic shedding.</strong></p><p>Other <strong>Layer 3 DDoS defense mechanisms include</strong> <strong>Unicast Reverse Path Forwarding (uRPF)</strong>. In a DDoS attack, hackers often forge the source IP to avoid receiving server responses (and to hide their identity). Layer 3 checks the routing table to see if a valid path exists back to that source IP; if not, the source IP is considered forged, and the packet is dropped.</p><p>At <strong>Layer</strong> 4, <strong>SYN Flood</strong> <strong>is a common DDoS attack</strong>. During a TCP handshake, the client sends the first <code>SYN</code> packet. Upon receiving it, the server records the TCP state (source IP, port, initial sequence number, etc.) in the kernel and returns a <code>SYN-ACK</code>. <strong>A SYN flood sends a massive volume of </strong><code>SYN</code><strong> packets to exhaust the kernel&#8217;s memory by filling it with these states.</strong></p><p>One solution for SYN floods is <strong>SYN Cookies</strong>. Instead of storing the state in memory, the server encodes the state into a number used as the initial sequence number in the <code>SYN-ACK</code>. When the client responds with a <code>CLIENT-ACK</code> (where <code>seq = initial sequence number + 1</code>), the server verifies the connection, decodes the state, and only then allocates memory for the connection.</p><p>Additionally, Cloudflare uses <strong>eBPF</strong> to insert hooks at the kernel level. These hooks monitor <strong>PPS (Packets Per Second)</strong> and <strong>BPS (Bits Per Second)</strong> to dynamically adjust Layer 4 rate-limiting strategies.</p><p>Finally, Layer 7 rate limiting is not merely rule-based. Through <strong>Bot Management</strong>, Cloudflare shares attack data across its global proxies. By analyzing behavioral patterns, if an attack is detected in the US, the relevant data is synchronized to Japan, allowing Japanese proxies to block the same bot preemptively.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wLus!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wLus!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 424w, https://substackcdn.com/image/fetch/$s_!wLus!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 848w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1272w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png" width="800" height="439" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52269e1c-7abe-477b-a450-003a88d5262a_800x439.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:439,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wLus!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 424w, https://substackcdn.com/image/fetch/$s_!wLus!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 848w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1272w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/cloudflare-bot-management-machine-learning-and-more/">https://blog.cloudflare.com/cloudflare-bot-management-machine-learning-and-more/</a></p><h1><strong>Conclusion</strong></h1><p>What Cloudflare built is not just a CDN. It is a globally distributed system where routing policy, kernel behavior, transport protocols, and application logic are tightly coupled.</p><p>Once you see this, tools like <a href="https://open.substack.com/pub/chillvic/p/observability-without-the-surgery?utm_campaign=post-expanded-share&amp;utm_medium=web">eBPF</a> stop being &#8220;advanced tricks.&#8221; They are no longer optional observability tools &#8212; they become inevitable building blocks.</p><p>After reading this article, Cloudflare&#8217;s incident reports will no longer feel mysterious. You&#8217;ll start recognizing the real constraints and trade-offs behind those failures.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lbc_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lbc_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 424w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 848w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 1272w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lbc_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png" width="1456" height="3007" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:3007,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1004733,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182281728?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lbc_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 424w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 848w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 1272w, https://substackcdn.com/image/fetch/$s_!lbc_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F74189db0-96aa-4f64-853f-3ee217305aeb_3663x7565.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1><strong>References</strong></h1><p><a href="https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/">https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</a><br><a href="https://blog.cloudflare.com/the-road-to-quic/">https://blog.cloudflare.com/the-road-to-quic/</a><br><a href="https://blog.cloudflare.com/how-we-built-pingora-the-proxy-that-connects-cloudflare-to-the-internet/https://www.cloudflare.com/ru-ru/learning/dns/what-is-anycast-dns/">https://blog.cloudflare.com/how-we-built-pingora-the-proxy-that-connects-cloudflare-to-the-internet/https://www.cloudflare.com/ru-ru/learning/dns/what-is-anycast-dns/</a><br><a href="https://blog.cloudflare.com/async-quic-and-http-3-made-easy-tokio-quiche-is-now-open-source/">https://blog.cloudflare.com/async-quic-and-http-3-made-easy-tokio-quiche-is-now-open-source/</a><br><a href="https://medium.com/codavel-blog/quic-vs-tcp-tls-and-why-quic-is-not-the-next-big-thing-d4ef59143efd">https://medium.com/codavel-blog/quic-vs-tcp-tls-and-why-quic-is-not-the-next-big-thing-d4ef59143efd</a></p>]]></content:encoded></item><item><title><![CDATA[CDN 絕對不只是 Cache — 從 OSI 7 層解析 Cloudflare 架構]]></title><description><![CDATA[BGP & anycast & unimog & QUIC & pingora &#25216;&#34899;&#26855;&#35299;&#26512;]]></description><link>https://www.chillvic.dev/p/cdn-cache-osi-7-cloudflare</link><guid isPermaLink="false">https://www.chillvic.dev/p/cdn-cache-osi-7-cloudflare</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Mon, 22 Dec 2025 01:46:33 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!uxTK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Cloudflare &#20316;&#28858; CDN &#38712;&#20027;&#19981;&#21482;&#25552;&#20379; cache&#65292;&#36996;&#20860;&#39015; WAF &amp; &#38450; DDos &#65292;&#29978;&#33267; Cloudflare Workers &#33021;&#35731;&#24037;&#31243;&#24107;&#22312; edge proxy &#20013;&#22519;&#34892; js &#20195;&#30908;&#65292;&#24050;&#28982;&#25104;&#28858;&#32178;&#36335;&#26368;&#37325;&#35201;&#30340;&#22522;&#24314;&#20043;&#19968;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uxTK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uxTK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uxTK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3483183,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182279446?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uxTK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!uxTK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ce076e-2e69-4bb2-af9e-4ce87fe97f3b_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>&#22914;&#27492;&#37325;&#35201;&#22522;&#24314;&#20854;&#26550;&#27083;&#26159;&#20160;&#40636;&#21602;&#65311;&#19968;&#36215;&#36879;&#36942;&#19979;&#38754;&#21839;&#38988;&#20358;&#25506;&#35342;&#65306;</p><h1>&#22914;&#20309;&#23559;&#21934;&#19968; Domain &#21305;&#37197;&#21040;&#19981;&#21516;&#22320;&#29702;&#20301;&#32622;&#30340; edge proxy&#65311;</h1><p>CDN &#39318;&#35201;&#21151;&#33021;&#26159;&#20381;&#29031;&#29992;&#25142;&#22320;&#29702;&#20301;&#32622;&#65292;&#23559;&#30456;&#21516; domain &#30340;&#35531;&#27714;&#36865;&#21040;&#38626;&#20182;&#36817;&#30340; edge proxy&#12290;</p><p>&#36890;&#24120;&#19968;&#20491; Domain &#23565;&#25033;&#19968;&#20491; public IP&#65292;&#32780; Geo DNS &#25903;&#25588;&#30456;&#21516; Domain &#20381;&#29031;&#22320;&#29702;&#20301;&#32622;&#21305;&#37197;&#19981;&#21516; public IP&#65292;&#22312; DNS &#20998;&#23652;&#26597;&#35426;&#19979;&#65292;&#26371;&#25214;&#21040; Geo DNS &#20006;&#29992; client IP &#35299;&#26512;&#22320;&#29702;&#20301;&#32622;&#24460;&#26597;&#21040; public IP&#12290;</p><p>&#20294;&#35442;&#26041;&#27861;&#26377;&#35377;&#22810;&#32570;&#40670;&#65306;</p><p>1. &#20551;&#35373; Geo DNS &#22352;&#33853;&#22312;&#32654;&#22283;&#27231;&#25151;&#65292;&#28858;&#20102;&#25214;&#21040; public IP &#21487;&#33021;&#20808;&#32350;&#21040;&#36611;&#36960;&#30340;&#22320;&#26041;<br>2. DNS &#26377;&#24555;&#21462;&#19988; TTL &#21487;&#33021;&#24456;&#38263;&#65292;&#33509;&#35201;&#26367;&#25563; public IP &#26371;&#38656;&#35201;&#19968;&#27573;&#26178;&#38291;&#25165;&#29983;&#25928;&#65292;&#30332;&#29983;&#25925;&#38556;&#36983;&#31227;&#28961;&#27861;&#21363;&#26178;&#29983;&#25928;</p><p>&#37027;&#26377;&#27794;&#26377;&#26356;&#24555;&#65292;&#21487;&#29992;&#24615;&#26356;&#22909;&#30340;&#26041;&#24335;&#65311;</p><p>&#33287;&#20854;&#23559;&#19968;&#20491; Domain &#32129;&#23450;&#19981;&#21516; public IP&#65292;<strong>Cloudflare &#29992; anycast &#25216;&#34899;&#23559;&#19968;&#20491; public IP &#26144;&#23556;&#21040;&#19981;&#21516;&#20301;&#32622;&#30340;&#27231;&#22120;&#19978;</strong>&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TIFm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TIFm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 424w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 848w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1272w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png" width="797" height="442" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:442,&quot;width&quot;:797,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TIFm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 424w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 848w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1272w, https://substackcdn.com/image/fetch/$s_!TIFm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe100dac6-cf57-4fad-9224-7983aa6eb4e0_797x442.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</p><h1>anycast &#22914;&#20309;&#23559;&#19968;&#20491; public IP &#26144;&#23556;&#21040;&#19981;&#21516;&#20301;&#32622;&#30340;&#27231;&#22120;&#19978;&#65311;</h1><p>&#32178;&#36335;&#20998;&#25104; Local Area Network (LAN) &amp; Wide Area Network (WAN)&#65292;&#30456;&#21516;&#21312;&#22495;&#26371;&#29992;&#30456;&#21516;&#32178;&#22495;&#65292;&#20363;&#22914;&#20013;&#33775;&#38651;&#20449; (ISP) &#26159; 36.224.0.0/13&#65292;&#32780;&#30456;&#21516;&#32178;&#22495;&#19979;&#65292;&#32178;&#32218;&#30001;&#19968;&#20491; Autonomous System (AS) &#24314;&#32622;&#33287;&#31649;&#29702;&#65292;&#20659;&#36664;&#24456;&#24555;&#65292;&#29978;&#33267;&#19981;&#29992; CDN&#12290;</p><p>&#30070;&#23553;&#21253;&#36914;&#20837; WAN &#24460;&#65292;&#36317;&#38626;&#25289;&#38263;&#65292;&#35201;&#36328; AS &#20043;&#38291;&#20659;&#36664;&#65292;&#19981;&#21516; AS &#31649;&#29702;&#19981;&#21516;&#32178;&#22495;&#65292;&#24444;&#27492;&#38656;&#35201; protocol &#20132;&#25563;&#36335;&#30001;&#36039;&#35338; (aka IP Prefix)&#65292;&#35442; protocol &#28858; Border Gateway Protocol (BGP)&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hKER!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hKER!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 424w, https://substackcdn.com/image/fetch/$s_!hKER!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 848w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg" width="800" height="615" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:615,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hKER!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 424w, https://substackcdn.com/image/fetch/$s_!hKER!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 848w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!hKER!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb387b521-7f9f-431f-a623-8a808d9f330d_800x615.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.cloudns.net/blog/understanding-bgp-a-comprehensive-guide-for-beginners/">https://www.cloudns.net/blog/understanding-bgp-a-comprehensive-guide-for-beginners/</a></p><p>&#20363;&#22914;&#20013;&#33775;&#38651;&#20449;&#29992; BGP &#21578;&#35380;&#26085;&#26412; ISP 36.224.0.0/13 &#26159;&#25105;&#36000;&#36012;&#30340;&#65292;&#30070;&#26085;&#26412; ISP &#36865; request &#21040; 36.224.1.1 &#26178;&#65292;&#26371;&#23559; request forward &#21040;&#20013;&#33775;&#38651;&#20449;&#30340; border router&#65292;&#30001;&#35442; router &#23559; request &#36865;&#21040; server &#19978;&#12290;</p><p>&#32780; anycast &#21487;&#23559;&#19968;&#32068; IP Prefix &#36879;&#36942; BGP &#33287;&#19981;&#21516; AS &#35387;&#20874;&#22312;&#19981;&#21516;&#30340; border router &#19978;&#65292;&#19981;&#21516; border router &#32000;&#37636;&#19981;&#21516;&#30340; routing table&#65292;&#30456;&#21516;&#30340; public IP &#26371;&#22240;&#28858;&#20358;&#28304; AS &#19981;&#21516;&#65292;forward &#21040;&#19981;&#21516; border router&#65292;&#26368;&#32066;&#21040;&#19981;&#21516; server &#19978;&#12290;</p><p><strong>Cloudflare &#20316;&#28858;&#29544;&#31435; AS&#65292;&#21487;&#36319;&#19981;&#21516; AS &#35387;&#20874; IP Prefix (e.g 103.21.244.0/22)&#65292;&#19981;&#21516; AS &#29992;&#30070;&#22320;&#30340; border router &#21644; edge proxy &#36000;&#36012;&#35442;&#32178;&#22495;&#25152;&#26377; IP</strong>&#65292;&#20363;&#22914;&#24478;&#21488;&#28771;&#36865; request &#21040; 103.21.244.1&#65292;&#36879;&#36942; BGP &#25214;&#21040;&#21488;&#28771;&#30070;&#22320;&#30340; border router &#65292;&#26368;&#32066;&#35531;&#27714;&#36865;&#21040;&#21488;&#28771;&#30070;&#22320; edge proxy&#12290;</p><p><strong>BGP &#26159; layer 3 protocol&#65292;forward request &#19981;&#38656;&#35201;&#23652;&#23652;&#35299;&#26512;&#65292;&#25928;&#33021;&#22909;&#65292;&#19988;&#21487;&#29992;&#24615;&#26356;&#39640;</strong>&#65292;&#20363;&#22914; 103.21.244.1 &#22312;&#26085;&#26412;&#30340; edge proxy &#22240;&#28858;&#27231;&#25151;&#22833;&#28779;&#20986;&#21839;&#38988;&#65292;&#19981;&#29992;&#23559; domain &#25563;&#25104;&#21478;&#19968;&#20491; public IP&#65292;&#26085;&#26412; ISP &#36879;&#36942; BGP &#26371;&#30332;&#29694;&#21488;&#28771;&#26377;&#36335;&#21487;&#36890;&#24448; 103.21.244.1&#65292;&#20006; forward &#36942;&#21435;&#65292;&#38614;&#28982;&#36317;&#38626;&#35722;&#36960;&#65292;&#20294;&#30906;&#20445;&#21487;&#29992;&#24615;&#12290;</p><h1>anycast + BGP &#26159;&#23436;&#32654; Solution &#21966;&#65311;</h1><p>anycast &#22522;&#26044; BGP &#36335;&#30001;&#35215;&#21063;&#23559;&#30456;&#21516; public IP &#36865;&#24448;&#38752;&#36817;&#29992;&#25142;&#30340; server&#65292;&#20294; BGP &#20006;&#19981;&#20381;&#29031;&#22320;&#29702;&#20301;&#32622;&#25110;&#32773;&#23553;&#21253; RTT &#27770;&#23450;&#36335;&#30001;&#65292;&#32780;&#26159; AS Path &#30340;&#25104;&#26412;&#65292;&#32780;&#25104;&#26412;&#20027;&#35201;&#38752; Admin &#37197;&#32622; (policy based)&#12290;</p><p>&#20363;&#22914; A &#22283;&#33287; B &#22283;&#32178;&#32218;&#23565;&#25509;&#26178;&#65292;A &#22283;&#33509;&#36319;&#24456;&#22810;&#20854;&#20182; AS &#23565;&#25509;&#65292;B &#22283;&#23601;&#26371;&#21463;&#24800;&#65292;&#22240;&#27492; A &#22283;&#21487;&#33021;&#26371;&#20687; B &#22283;&#25910;&#37666;&#65292;&#33509;&#23553;&#21253;&#24478; B &#22283;&#20986;&#30332;&#65292;&#21487;&#32147;&#30001; A &#25110; C &#21040; D&#65292;&#20551;&#35373; C &#27794;&#25910;&#37666;&#65292;&#23601;&#26371;&#20808;&#36208; C&#65292;&#21363;&#20415;&#30446;&#30340;&#22320;&#38626; A &#26356;&#36817;&#12290;</p><p><strong>&#32780; Cloudflare &#26371;&#33287; AS &#23565;&#25509; Local AS Path &#30906;&#20445;&#20778;&#20808;&#27402;&#12290;</strong></p><p>note: &#35443;&#32048; BGP route &#35215;&#21063;&#21487;&#21443;&#32771;<a href="https://networklessons.com/bgp/bgp-attributes-and-path-selection">&#36889;&#35041;</a>&#12290;</p><p>&#21478;&#22806; BGP &#26377;&#21163;&#25345;&#21839;&#38988;&#65292;&#39405;&#23458;&#21487;&#29992;&#30456;&#21516; IP Prefix &#33287;&#20854;&#20182; AS &#35387;&#20874;&#65292;&#20006;&#35373;&#27861;&#35731;&#21040;&#36948;&#20182;&#30340; AS Path &#25104;&#26412;&#35722;&#20302;&#65292;&#23601;&#21487;&#33021;&#25226;&#21407;&#26412;&#26371;&#36208;&#21040;&#27491;&#30906; Cloudflare &#27969;&#37327;&#36681;&#21040;&#39405;&#23458;&#26381;&#21209;&#19978;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VxRG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VxRG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 424w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 848w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1272w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png" width="800" height="416" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:416,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VxRG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 424w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 848w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1272w, https://substackcdn.com/image/fetch/$s_!VxRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd08c23aa-1800-4a17-a4fc-78790e9442a6_800x416.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/bgp-hijack-detection/">https://blog.cloudflare.com/bgp-hijack-detection/</a></p><p>&#35299;&#27861;&#26159;&#35387;&#20874; IP Prefix &#26178;&#35201;&#39511;&#35657; AS &#36523;&#20221;&#65292;&#39006;&#20284; HTTPS &#30340; domain &#24977;&#35657;&#39511;&#35657;&#65292;&#20294;&#38750;&#25152;&#26377; AS &#37117;&#20570;&#36889;&#20214;&#20107;&#65292;&#25152;&#20197;&#20173;&#26377;&#39080;&#38570;&#65292;&#21487;&#29992;  https://isbgpsafeyet.com/  &#26597;&#20320;&#30340; ISP &#26377;&#27794;&#26377;&#39511;&#35657;&#27969;&#31243;&#12290;</p><p>&#21478;&#22806; anycast &#29305;&#24615;&#35731;&#30456;&#21516; public IP &#22312;&#21516;&#21312;&#22495;&#20063;&#21487;&#26377;&#22810;&#20491; proxy server&#65292;&#22240;&#27492; TCP &#23553;&#21253;&#21487;&#33021;&#36865;&#24448;&#19981;&#21516; server&#65292;&#23566;&#33268;&#20197;&#19979;&#21839;&#38988;&#65306;</p><p>&#30070; client &#33287; server &#24314;&#31435; TCP &#36899;&#32218;&#24460;&#65292;&#28858;&#20102;<strong>&#30906;&#20445;&#23553;&#21253;&#30340;&#38918;&#24207;&#24615;&#65292;server kernel &#26371;&#22312;&#35352;&#25014;&#39636;&#20013;&#35352;&#37636; TCP &#36899;&#32218; ID &#21644;&#23565;&#25033;&#29376;&#24907;</strong>&#65292;&#20363;&#22914;&#25910;&#21040;&#21738;&#20123;&#23553;&#21253;&#65292;&#19979;&#19968;&#20491;&#23553;&#21253;&#32232;&#34399;&#31561;&#65292;&#33509;<strong>&#30456;&#21516; TCP &#36899;&#32218;&#23553;&#21253;&#36865;&#24448;&#19981;&#21516; server &#19988;&#35442; server &#27794;&#26377;&#23565;&#25033;&#36899;&#32218;&#29376;&#24907;&#26371;&#38364;&#38281;&#36899;&#32218;</strong>&#12290;</p><h1>&#22914;&#20309;&#35299;&#27770; anycast &#21934;&#19968; public IP &#26377;&#22810;&#21488; server &#23566;&#33268; TCP &#26039;&#32218;&#21839;&#38988;&#65311;</h1><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><p>&#19968;&#20491;&#36328;&#21312;&#23553;&#21253;&#32178;&#36335;&#36335;&#24465;&#28858;&#65306;<br>1. client &#24478; ISP gateway &#20986;&#30332;&#65292;&#36879;&#36942; BGP &#36984;&#21040; border gateway<br>2. border gateway &#20659;&#32102; NAT&#65292;NAT &#23559; dest ip &#24478; public ip &#36681;&#25104; private ip<br>3. NAT &#20659;&#32102; private network &#30340; edge proxy</p><p>&#30070; border gateway &#30332;&#29694;&#30456;&#21516; dest ip &#26377;&#22810;&#26781;&#36335;&#24465;&#26178;&#65292;&#26371;&#29992; <strong>ECMP (Equal-Cost Multi-Path)</strong> &#31639;&#27861;&#65292;hash(source ip, source port, dest ip, dest port) % path_cnt &#26041;&#24335; forward&#65292;&#30906;&#20445;&#30456;&#21516;&#36899;&#32218;&#23553;&#21253;&#24448;&#21516;&#20491; NAT &amp; edge proxy&#12290;</p><p>&#20294;&#22686;&#21152;&#25110;&#28187;&#23569; edge proxy&#65292;ECMP &#31639;&#27861;&#26371;&#19981;&#19968;&#33268;&#65292;&#19988;&#28961;&#27861;&#20381;&#29031; proxy workload &#20998;&#30332;&#12290;</p><p>&#22240;&#27492; Cloudflare &#20351;&#29992; XDP (<a href="https://open.substack.com/pub/chillvic/p/observability-without-the-surgery?utm_campaign=post-expanded-share&amp;utm_medium=post%20viewer">ebpf</a> ) &#38283;&#30332;&#20102; unimog Layer 4 Load balancer&#65292;&#35731;&#27599;&#20491; proxy server &#26377;&#36681;&#30332;&#23553;&#21253;&#33021;&#21147;&#65292;ebpf &#33021;&#22312; user space process &#23526;&#20316; control panel&#65292;&#24392;&#24615;&#39640;&#65292;&#20363;&#22914;&#29992; <a href="https://github.com/kkdai/maglev">meglev hashing</a> &#36319;&#30435;&#25511; proxy workload &#20358;&#35519;&#25972;&#36335;&#30001;&#12290;</p><p>&#19988; proxy &#26377; forward &#33021;&#21147;&#23601;&#19981;&#38656;&#35201; NAT &#36681;&#30332;&#65292;proxy &#21487;&#29992; Tunneling &#23559;&#23553;&#21253;&#23553;&#35037;&#19968;&#23652;&#65292;&#20462;&#25913; dest ip &#25104; private IP &#36865;&#20986;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gGs7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gGs7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 424w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 848w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1272w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png" width="757" height="406" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d44ef998-1e71-4875-a561-8c984b442c35_757x406.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:406,&quot;width&quot;:757,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gGs7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 424w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 848w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1272w, https://substackcdn.com/image/fetch/$s_!gGs7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd44ef998-1e71-4875-a561-8c984b442c35_757x406.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/">https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</a></p><p>&#38500;&#20102; Layer 4 Load balancer&#65292;Cloudflare &#36996;&#22312; Layer 4 &#20351;&#29992;&#20854;&#20182;&#25216;&#34899;&#65292;&#20063;&#23601;&#26159; QUIC&#65281;</p><h1>&#20160;&#40636;&#26159; QUIC&#65311;QUIC &#35299;&#27770;&#20160;&#40636;&#21839;&#38988;&#65311;</h1><p>QUIC &#26159;&#22522;&#26044; UDP &#38283;&#30332;&#30340; Layer 4 &#26032;&#21332;&#23450;&#65292;&#20027;&#35201;&#25645;&#37197; HTTP/3 &#65292;&#29992;&#20358;&#35299;&#27770; HTTP/2 &amp; TCP &#30340;&#25928;&#33021;&#21839;&#38988;&#65306;</p><h3><strong>Head-of-Line Blocking</strong></h3><p>HTTP/2 &#35299;&#27770; HTTP/1 Layer 7 Head-of-Line Blocking&#65292;&#19981;&#29992;&#31561;&#30070;&#21069; request &#30340; response &#23601;&#33021;&#36865;&#19979;&#19968;&#20491; request&#65292;&#21487;&#29992;&#19968;&#20491; TCP connection &#21516;&#26178;&#36865;&#20986;&#22810;&#20491; request&#65292;&#25033;&#29992;&#23652;&#25910;&#21040;&#19981;&#21516; request &#23565;&#25033; response &#23553;&#21253;&#24460;&#65292;&#26371;&#29992; stream id &#32068;&#22238;&#23436;&#25972; payload&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4qid!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4qid!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 424w, https://substackcdn.com/image/fetch/$s_!4qid!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 848w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1272w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png" width="625" height="618" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:625,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4qid!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 424w, https://substackcdn.com/image/fetch/$s_!4qid!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 848w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1272w, https://substackcdn.com/image/fetch/$s_!4qid!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce233a1f-f212-4377-ab0c-e53eb7ffd8b3_625x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://juejin.cn/post/6844903984524705800">https://juejin.cn/post/6844903984524705800</a></p><p>&#20294; Layer 4 &#20063;&#26377; Head-of-Line Blocking &#65292;TCP &#35222;&#35282;&#28961;&#27861;&#20998;&#36776;&#23553;&#21253;&#23660;&#26044;&#19981;&#21516; request&#65292;&#26371;&#23559;&#23553;&#21253;&#35222;&#28858;&#30456;&#21516;&#36899;&#32218;&#19988;&#35201;&#26377;&#24207;&#22320;&#25509;&#25910;&#65292;&#30070; A response &#23569;&#20102;&#19968;&#20491;&#23553;&#21253;&#65292;&#21363;&#20415; B response &#30340; payload &#24050;&#32147;&#23436;&#25972;&#25269;&#36948;&#65292;&#21482;&#35201;&#36986;&#22833;&#23553;&#21253;&#27794;&#21040;&#65292;kernel &#23652;&#23601;&#19981;&#26371;&#25226; B response payload &#32102; user space process&#12290;</p><p>QUIC &#35299;&#27861;&#26159;&#23559; Layer 7 &#30340; stream id &#25644;&#31227;&#21040; Layer 4 &#35299;&#26512;&#65292;&#27599;&#20491; stream &#23553;&#21253;&#35201;&#26377;&#24207;&#65292;&#36328; stream &#19981;&#31649;&#65292;&#27492;&#22806;&#30001;&#26044; HTTP/3 &#22312;&#25033;&#29992;&#23652; (i.e &#28687;&#35261;&#22120;) &#21487;&#35299;&#26512;&#23553;&#21253;&#37325;&#32068; payload&#65292;QUIC &#19981;&#29992;&#22312; kernel &#32173;&#35703;&#22810;&#20491; stream &#30340;&#23553;&#21253;&#25490;&#24207;&#65292;&#38477;&#20302; kernel &#35352;&#25014;&#39636;&#31649;&#29702;&#35079;&#38620;&#24230;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uGTS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uGTS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 424w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 848w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1272w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png" width="800" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b14cec7d-3d3d-4666-a04d-d48742377551_800x457.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uGTS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 424w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 848w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1272w, https://substackcdn.com/image/fetch/$s_!uGTS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb14cec7d-3d3d-4666-a04d-d48742377551_800x457.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.researchgate.net/figure/The-head-of-line-blocking-problem-and-the-stream-based-solution_fig1_344551663">https://www.researchgate.net/figure/The-head-of-line-blocking-problem-and-the-stream-based-solution_fig1_344551663</a></p><h3>&#36899;&#32218;&#24314;&#31435;&#35201;&#25569;&#25163;</h3><p>TCP &#20659;&#36039;&#26009;&#21069;&#65292;&#35201;&#20808; Client SYN =&gt; Server SYN-ACK =&gt; Client Client-ACK &#24460;&#25165;&#38283;&#22987;&#20659;&#36865;&#36039;&#26009;&#65292;&#32780; QUIC &#26159; client &#22312;&#30332;&#36865;&#31532;&#19968;&#20491; SYN &#26178;&#65292;&#36899;&#24118;&#25226; data &#30332;&#20986;&#21435;&#65292;&#31561;&#26044; 0-RTT &#23601;&#21487;&#20659;&#36664;&#36039;&#26009;&#12290;</p><p>&#19981;&#36942;&#25903;&#25588; TLS &#26371;&#26159; 1-RTT&#65292;&#20363;&#22914;&#36879;&#36942; Diffie&#8211;Hellman &#31639;&#27861; &#65306;</p><p>1. Client =&gt; public key =&gt; Server<br>2. Server &#29992; server&#8217;s private key + client&#8217;s public key &#31639;&#20986;&#19968;&#20491; share secret<br>3. Server =&gt; session ticket (i.e &#21152;&#23494;&#30340; share secret) + server&#8217;s public key =&gt; Client</p><p>Client &#29992; client&#8217;s private key + server&#8217;s public key &#31639;&#20986;&#19968;&#27171; share secret&#65292;&#29992;&#35442; secret &#21152;&#23494; request&#65292;&#36899;&#21516; session ticket &#20659;&#32102; server&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y-v-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 424w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 848w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1272w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png" width="800" height="419" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:419,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y-v-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 424w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 848w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1272w, https://substackcdn.com/image/fetch/$s_!Y-v-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5bfae427-7358-4c82-b6fe-4bb3ce922422_800x419.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://vocal.com/voip/quic/">https://vocal.com/voip/quic/</a></p><p>&#27492;&#22806; session ticket &#21487;&#20786;&#23384;&#22312; client &#30828;&#30879;&#20013;&#65292;&#26039;&#32218;&#24460;&#20173;&#21487;&#32396;&#29992;&#65292;&#37325;&#36899;&#19981;&#29992;&#20132;&#25563; public key &#31639; share secret&#65292; &#21487;&#30452;&#25509;&#20659;&#36039;&#26009;&#26159;&#30495; 0-RTT&#65292;&#20294;&#39080;&#38570;&#23601;&#26159;&#37325;&#25918;&#25915;&#25802;&#65292;&#22240;&#27492; server side &#26371;&#23450;&#26399;&#25563;&#29986;&#29983; session ticket &#30340;&#21152;&#23494; key &#20197;&#21450;&#35373;&#23450; session ticket TTL&#65292;&#20006;&#21482;&#20801;&#35377; GET request &#21487; 0-RTT&#12290;</p><p>&#38500;&#20102;&#20778;&#21270;&#25928;&#33021;&#65292;QUIC &#20063;&#23526;&#29694;&#37325;&#20659;&#36319;&#27969;&#37327;&#25511;&#21046;&#27231;&#21046; &#65292;&#20363;&#22914;&#30070; server memory buffer &#28415;&#20102;&#65292;&#26371;&#21483; client &#20808;&#19981;&#35201;&#20659;&#65292;&#25110;&#32773; RTT time &#22826;&#38263;&#20063;&#26371;&#21483; client &#20808;&#19981;&#35201;&#20659;&#26032;&#30340;&#65292;&#31561;&#36986;&#28431;&#23553;&#21253;&#37117;&#25910;&#21040;&#12290;</p><p>&#20659;&#32113; TCP &#27794;&#26377; stream &#27010;&#24565;&#65292;&#30456;&#21516;&#23553;&#21253;&#65292;&#21363;&#20415;&#37325;&#20659;&#24207;&#34399;&#20063;&#30456;&#21516;&#65292;&#23566;&#33268;&#25910;&#21040; ACK &#26178;&#19981;&#30693;&#36947;&#26159;&#31532;&#19968;&#20491;&#23553;&#21253;&#36996;&#26159;&#37325;&#20659;&#23553;&#21253;&#30340; ACK&#65292;&#31639; RTT &#26371;&#19981;&#28310;&#12290;</p><p>&#32780; QUIC &#38918;&#24207;&#26159; stream &#27770;&#23450;&#65292;stream &#20839;&#26377;&#29544;&#31435; seq&#65292;&#23553;&#21253; id &#21487;&#20840;&#22495; (&#36328; stream) &#36958;&#22686;&#65292;&#21487;&#20998;&#36776;ACK &#26159;&#37325;&#20659;&#30340;&#36996;&#26159;&#31532;&#19968;&#20491;&#30340;&#65292;RTT &#26356;&#31934;&#28310;&#65292;&#27969;&#37327;&#25511;&#21046;&#26356;&#19981;&#26371;&#35492;&#21028;&#12290;</p><p>&#27492;&#22806; QUIC &#37325;&#20659;&#36319;&#27969;&#37327;&#25511;&#21046;&#26159;&#22312; user space &#23526;&#29694;&#30340;&#65292;kernel &#23601;&#26159; UDP&#65292;&#24392;&#24615;&#26356;&#22823;&#65292;&#21487;&#37341;&#23565;&#26989;&#21209;&#38656;&#27714;&#35519;&#25972;&#65292;&#20363;&#22914;&#20018;&#27969;&#24773;&#22659;&#20013;&#65292;&#37325;&#20659;&#35201;&#27714;&#19981;&#39640;&#65292;&#20294;&#27969;&#37327;&#35201;&#25511;&#21046;&#22909;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AMcy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AMcy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 424w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 848w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1272w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png" width="800" height="357" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:357,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AMcy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 424w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 848w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1272w, https://substackcdn.com/image/fetch/$s_!AMcy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ec7921b-ea46-4e2a-8e0d-2945f17d4e8b_800x357.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://datatracker.ietf.org/meeting/98/materials/slides-98-edu-sessf-quic-tutorial-00.pdf">https://datatracker.ietf.org/meeting/98/materials/slides-98-edu-sessf-quic-tutorial-00.pdf</a></p><h1>&#38500;&#20102;&#25928;&#33021;&#20778;&#21270;&#65292;Cloudflare &#20351;&#29992; QUIC &#36996;&#26377;&#20160;&#40636;&#22909;&#34389;&#65311;</h1><p>Cloudflare &#29992; rust &#23526;&#20316;&#20102; QUIC &#21151;&#33021; (<a href="https://github.com/cloudflare/quiche">https://github.com/cloudflare/quiche</a>) &#20006;&#22312;&#23526;&#20316;&#20013;&#30332;&#29694;&#22909;&#34389;&#36319;&#25361;&#25136;&#65306;</p><h3>&#22909;&#34389;&#8202;&#8212;&#8202;Connection Migration</h3><p>TCP &#36899;&#32218; ID &#26159; (src_ip, src_port, dest_ip, dest_port) &#19968;&#26086;&#29992;&#25142;&#20999;&#25563;&#32178;&#36335;&#20006;&#25913;&#35722; src ip (e.g 5G =&gt; 4G)&#65292;&#36899;&#32218;&#23601;&#35201;&#37325;&#24314;&#31435;&#65292;&#20294; QUIC &#36899;&#32218; ID &#26159;&#38568;&#27231;&#32232;&#34399;&#65292;&#20786;&#23384;&#22312; Client &#31471;&#65292;&#29992;&#25142;&#20999;&#25563;&#32178;&#36335; CID &#19968;&#27171;&#23601;&#21487;&#36776;&#35672;&#29376;&#24907;&#12290;</p><p>&#22312;&#19978;&#38754; anycast &#26550;&#27083;&#25552;&#21040;&#65292;&#19968;&#33324; router &#22312; Layer 3 &#26371;&#29992; ECMP hash(src_ip, src_port, dest_ip, dest_port) &#27770;&#23450;&#36335;&#30001;&#65292;&#30070;&#29992;&#25142; src ip &#25913;&#35722;&#65292;&#30456;&#21516; CID &#20173;&#26371; forward &#21040;&#19981;&#21516; server&#65292;&#32780; Cloudflare &#21482;&#35201;&#22312; unimog layer 4 load balancer &#21152;&#19978; CID forward&#65292;&#23601;&#33021;&#23526;&#29694;&#29992;&#25142;&#25563;&#32178;&#36335;&#19981;&#37325;&#36899;&#30340;&#21151;&#33021;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!d-QA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!d-QA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 424w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 848w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg" width="800" height="580" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:580,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!d-QA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 424w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 848w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!d-QA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec48a853-7e5c-49f2-8291-b56b5b6cb5c4_800x580.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: https://zhuanlan.zhihu.com/p/311221111</p><h3>&#25361;&#25136;&#8202;&#8212;&#8202;Reflection Attacks</h3><p>QUIC &#24314;&#27083;&#22312; UDP &#19978;&#65292;&#32780; reflection attacks &#26159; UDP &#24120;&#35211;&#25915;&#25802;&#65292;&#39405;&#23458;&#20605;&#36896;&#29992;&#25142; IP&#65292;&#29992;&#35442; IP &#19981;&#26039;&#21521; server &#35531;&#27714;&#22823;&#37327;&#36039;&#26009;&#65292;&#23566;&#33268;&#29992;&#25142;&#32178;&#36335;&#22622;&#29190;&#65292;&#20363;&#22914; QUIC &#30340; TLS &#35201;&#20659; cert + public key &#32102; client&#65292;&#26159;&#19968;&#22823;&#22376;&#36039;&#26009;&#65292;&#39405;&#23458;&#21487;&#19981;&#26039;&#30332;&#36865; TLS &#35531;&#27714;&#35731;&#29992;&#25142;&#19981;&#26039;&#25910;&#21040;&#36039;&#26009;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x2rJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 424w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 848w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1272w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png" width="800" height="402" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:402,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!x2rJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 424w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 848w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1272w, https://substackcdn.com/image/fetch/$s_!x2rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77348327-3d5d-40b2-a757-e252d3b6763f_800x402.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://docs.aws.amazon.com/whitepapers/latest/aws-best-practices-ddos-resiliency/udp-reflection-attacks.html">https://docs.aws.amazon.com/whitepapers/latest/aws-best-practices-ddos-resiliency/udp-reflection-attacks.html</a></p><p>TCP &#29992;&#23569;&#37327;&#36039;&#26009; hand shake &#36991;&#20813;&#35442;&#21839;&#38988;&#65292;&#25910;&#21040; Client ACK &#24460;&#25165;&#20659; cert + public key&#65292;&#33509;&#39405;&#23458;&#20605;&#36896; IP &#26159;&#25910;&#19981;&#21040; SYN-ACK &#20063;&#28961;&#27861;&#22238;&#20659; Client ACK&#65292;&#22240;&#27492;&#26681;&#35299;&#20173;&#26159; hand shake&#65292;server &#36865;&#19968;&#20491;&#21152;&#23494; token &#32102; client &#20006;&#35201;&#27714;&#20182;&#22238;&#20659;&#65292;&#20294;&#35442;&#26041;&#27861;&#29351;&#29298; 0-RTT &#29305;&#24615;&#65292;&#22240;&#27492; Cloudflare &#22312;&#22039;&#35430; ECDSA cert &#25110;&#22739;&#32302; cert &#36039;&#26009;&#12290;</p><p>Cloudflare &#38500;&#20102;&#29992; rust &#23526;&#20316; QUIC &#20043;&#22806;&#65292;&#36996;&#23526;&#20316;&#20102; proxy server&#8202;&#8212;&#8202;pingora &#29992;&#20358;&#21462;&#20195; nginx &#12290;</p><h1>&#28858;&#20160;&#40636; Cloudflare &#19981;&#29992; nginx &#35201;&#33258;&#24049;&#38283;&#30332; pingora&#65311;</h1><p>nginx &#26159; process based &#26550;&#27083;&#65292;&#29992; socket SO_REUSEPORT system call &#35731;&#22810;&#20491; process bind &#30456;&#21516; port&#65292;kernel &#20381;&#29031; hash(src_id, src_port, dest_ip, dest_port) &#20998;&#37197; socket &#32102; process &#34389;&#29702;&#12290;</p><p>process based &#32570;&#40670;&#26159; share memory &#23526;&#20316;&#35079;&#38620;&#19988;&#26377;&#38480;&#21046;&#65292;&#20363;&#22914; proxy =&gt; original server &#30340;&#36899;&#32218;&#19981;&#33021;&#20849;&#29992;&#65292;&#19988; nginx &#26159; c &#23531;&#30340;&#65292;&#26377; memory leak &#39080;&#38570;&#12290;</p><p>Cloudflare &#29992; rust &#23531;&#20102;&#19968;&#20491; thread based &#30340; proxy&#8202;&#8212;&#8202;pingora&#65292;&#27839;&#29992; nginx &#30340;&#26550;&#27083;&#65292;&#22810;&#20491; kernel thread bind &#30456;&#21516; port&#65292;&#27599;&#20491; thread &#26377;&#29544;&#31435; epoll instance&#65292;&#20849;&#20139; original server tcp &#36899;&#32218;&#36039;&#35338;&#65292;&#32780;&#22312; payload &#29305;&#22823;&#25110;&#27284;&#26696;&#20659;&#36664;&#24773;&#22659;&#29992; zero copy &#36991;&#20813;&#22823;&#37327; bytes &#24478; kernel space copy &#21040; user space&#65292;&#26371;&#21507; CPU&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zJVi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zJVi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg" width="800" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zJVi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zJVi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb652ea3f-de17-46d2-bee7-3b112309f10d_800x800.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://www.linkedin.com/posts/pal-arnab_what-is-pingora-pingora-is-a-new-http-proxy-activity-7106102391397113856-B8rJ/">https://www.linkedin.com/posts/pal-arnab_what-is-pingora-pingora-is-a-new-http-proxy-activity-7106102391397113856-B8rJ/</a></p><p>&#27492;&#22806; nginx &#26377;&#35377;&#22810;&#38283;&#30332;&#21253;&#34993;&#65292;&#20363;&#22914;&#29992; lua script &#25844;&#20805;&#21151;&#33021;&#65292;&#20294; lua &#24369;&#22411;&#21029;&#19981;&#22909;&#32173;&#35703;&#65292;&#25928;&#33021;&#20063;&#36611;&#24046;&#65292;nginx &#27169;&#32068;&#21270;&#30090;&#24456;&#22810;&#21151;&#33021;&#65292;&#26377;&#20123;&#21151;&#33021;&#29992;&#19981;&#21040;&#21371;&#26371;&#24433;&#38911;&#25928;&#33021;&#65292;benchmark &#32080;&#26524; pingora &#27604; nginx &#21534;&#21520;&#37327;&#22823;&#65292;CPU &#29992;&#37327;&#23569;&#20063;&#36611;&#31337;&#23450;&#12290;</p><h1>Cloudflare &#22914;&#20309;&#38450;&#31684; DDos&#65311;</h1><p>anycast &#26412;&#36523;&#27604; Geo DNS &#26356;&#33021;&#36969;&#25033; DDos&#65292;&#30070;&#39405;&#23458;&#29992; DNS &#35299;&#26512;&#21040; IP &#24460;&#25915;&#25802;&#65292;&#35531;&#27714;&#26371;&#34987; BGP &#20998;&#27969;&#21040;&#19981;&#21516;&#22320;&#26041;&#65292;&#23660;&#26044; layer 3 &#20998;&#27969;&#12290;</p><p>&#32780; layer 3 &#38450; DDos &#27231;&#21046;&#36996;&#26377; &#21453;&#21521;&#36335;&#30001;&#27298;&#26597;&#65292;DDos &#20013;&#65292;&#28858;&#36991;&#20813;&#25910;&#21040; server response &#26371;&#20605;&#36896; src ip&#65292;&#22240;&#27492; layer 3 &#21487;&#27298;&#26597; routing table &#20013;&#26159;&#21542;&#23384;&#22312;&#21487;&#22238;&#21435;&#36335;&#24465;&#65292;&#33509;&#27794;&#20195;&#34920; src ip &#26159;&#20605;&#36896;&#65292;&#19999;&#26820;&#23553;&#21253;&#12290;</p><p>layer 4 &#30340; DDos &#25915;&#25802;&#24120;&#35211;&#30340;&#26377; SYN flood&#65292;TCP &#20132;&#25569; client &#36865;&#20986;&#31532;&#19968;&#20491; SYN&#65292;server &#25910;&#21040;&#26371;&#22312; kernel &#35041;&#32000;&#37636; tcp &#29376;&#24907; (src ip, src port, init seq number etc) &#20006;&#22238;&#20659; SYN-ACK&#65292;SYN flood &#26159;&#20659;&#36865;&#22823;&#37327; TCP SYN &#35731; kernel &#35352;&#25014;&#39636;&#22622;&#28415;&#22823;&#37327;&#29376;&#24907;&#12290;</p><p>SYN flood &#35299;&#27861;&#20043;&#19968;&#26159;&#29992; SYN cookies&#65292;&#23559;&#29376;&#24907;&#32232;&#30908;&#25104;&#19968;&#20491;&#25976;&#23383;&#30070;&#20316; init seq number&#65292;&#25918;&#22312; ACK &#35041;&#38754;&#19981;&#20786;&#23384;&#22312;&#35352;&#25014;&#39636;&#65292;&#30070; client &#25910;&#21040;&#24460;&#35201;&#22238;&#20659; CLIENT-ACK (seq=init seq number+1)&#65292;server &#25910;&#21040;&#30906;&#23450;&#36899;&#32218;&#24314;&#31435;&#25104;&#21151;&#22312;&#35299;&#30908;&#29376;&#24907;&#20786;&#23384;&#21040;&#35352;&#25014;&#39636;&#12290;</p><p>&#27492;&#22806;&#20063;&#26371;&#29992; eBPF &#22312; kernel &#23652;&#25554;&#20837; hook &#65292;&#30435;&#25511; PPS (Packets Per Second) &amp; BPS (Bit Per Second) &#35519;&#25972; layer 4 rate limit &#31574;&#30053;&#12290;</p><p>&#26368;&#32066; layer 7 rate limit &#19981;&#26159;&#21934;&#32020; rule based&#65292;&#26159;&#36879;&#36942; Bot Management &#20849;&#20139;&#20840;&#29699; proxy &#34987;&#25915;&#25802;&#32147;&#39511;&#65292;&#20998;&#20139;&#20998;&#26512;&#34892;&#28858;&#27169;&#24335;&#32080;&#26524;&#65292;&#30070;&#32654;&#22283;&#34987;&#25915;&#25802;&#26178;&#65292;&#30456;&#38364;&#25976;&#25818;&#21487;&#21516;&#27493;&#32102;&#26085;&#26412;&#65292;&#30456;&#21516; bot &#23601;&#28961;&#27861;&#25915;&#25802;&#21040;&#26085;&#26412;&#12290;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wLus!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wLus!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 424w, https://substackcdn.com/image/fetch/$s_!wLus!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 848w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1272w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png" width="800" height="439" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52269e1c-7abe-477b-a450-003a88d5262a_800x439.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:439,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wLus!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 424w, https://substackcdn.com/image/fetch/$s_!wLus!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 848w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1272w, https://substackcdn.com/image/fetch/$s_!wLus!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52269e1c-7abe-477b-a450-003a88d5262a_800x439.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>image source: <a href="https://blog.cloudflare.com/cloudflare-bot-management-machine-learning-and-more/">https://blog.cloudflare.com/cloudflare-bot-management-machine-learning-and-more/</a></p><h1>&#32080;&#35542;</h1><p>&#30070;&#24478; CDN Server &#29702;&#35299; Cloudflare &#21487;&#33021;&#26371;&#35469;&#28858;&#26159;&#19968;&#20491;&#26377; Cache &#21151;&#33021;&#30340; edge proxy&#65292;&#20294;&#24478; OSI &#32178;&#36335;&#26550;&#27083;&#35282;&#24230;&#65292;&#26371;&#30332;&#29694;&#20182;&#20570;&#20102;&#24456;&#22810;&#20107;&#24773;&#65306;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V-5R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V-5R!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 424w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 848w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 1272w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V-5R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png" width="1456" height="3007" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:3007,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1003090,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.chillvic.dev/i/182279446?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V-5R!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 424w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 848w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 1272w, https://substackcdn.com/image/fetch/$s_!V-5R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7221062d-65c6-4b50-b7dc-faac6f59485e_3663x7565.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>&#30456;&#20449;&#22823;&#23478;&#30475;&#23436;&#36889;&#31687;&#24460;&#65292;&#22312;&#21435;&#30475; Cloudflare &#30340;&#25925;&#38556;&#22577;&#21578;&#25991;&#31456;&#33021;&#26356;&#22909;&#22320;&#29702;&#35299;&#20182;&#20497;&#25552;&#21040;&#30340;&#25216;&#34899;&#21839;&#38988;&#12290;</p><h1>References:</h1><p><a href="https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/">https://blog.cloudflare.com/unimog-cloudflares-edge-load-balancer/</a><br><a href="https://blog.cloudflare.com/the-road-to-quic/">https://blog.cloudflare.com/the-road-to-quic/</a><br><a href="https://blog.cloudflare.com/how-we-built-pingora-the-proxy-that-connects-cloudflare-to-the-internet/https://www.cloudflare.com/ru-ru/learning/dns/what-is-anycast-dns/">https://blog.cloudflare.com/how-we-built-pingora-the-proxy-that-connects-cloudflare-to-the-internet/https://www.cloudflare.com/ru-ru/learning/dns/what-is-anycast-dns/</a><br><a href="https://blog.cloudflare.com/async-quic-and-http-3-made-easy-tokio-quiche-is-now-open-source/">https://blog.cloudflare.com/async-quic-and-http-3-made-easy-tokio-quiche-is-now-open-source/</a><br><a href="https://medium.com/codavel-blog/quic-vs-tcp-tls-and-why-quic-is-not-the-next-big-thing-d4ef59143efd">https://medium.com/codavel-blog/quic-vs-tcp-tls-and-why-quic-is-not-the-next-big-thing-d4ef59143efd</a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Observability without the surgery: How to monitor your code without touching a single line of source.]]></title><description><![CDATA[eBPF: The &#8220;Superpowers&#8221; for Linux Kernel Observability]]></description><link>https://www.chillvic.dev/p/observability-without-the-surgery</link><guid isPermaLink="false">https://www.chillvic.dev/p/observability-without-the-surgery</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Sun, 21 Dec 2025 08:19:20 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Tks4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>eBPF (Extended Berkeley Packet Filter) is a revolutionary technology that allows us to run sandboxed programs within the Linux kernel. It enables us to enhance system security and observability without modifying a single line of application code. </p><p>One of its most powerful features is real-time monitoring of CPU usage, memory allocation, and network packet inspection at the kernel level.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Tks4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Tks4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 424w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 848w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 1272w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Tks4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png" width="1456" height="1090" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1090,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:259896,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://chillvic.substack.com/i/182168417?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Tks4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 424w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 848w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 1272w, https://substackcdn.com/image/fetch/$s_!Tks4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e2998b3-0c2f-41bc-96dc-e2277b741402_4800x3595.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1><strong>How to implement real-time monitoring without changing the code?</strong></h1><p>When an application runs, it interacts with hardware and resources via <strong>System Calls (syscalls)</strong>. For example, <code>read()</code> and <code>write()</code> are used for File and Network I/O.</p><p>By attaching <strong>hooks</strong> to these syscalls, we can intercept them right before they execute or right after they finish. This allows us to collect execution parameters, return values, and precise timing data&#8212;all without the application ever knowing it&#8217;s being watched. For instance, hooking <code>read()</code> and <code>write()</code> allows you to monitor HTTP requests or database queries in real-time.</p><h1><strong>Isn&#8217;t it dangerous to hook the Kernel?</strong></h1><p><strong>Arbitrarily inserting code into the kernel is extremely risky.</strong> A simple bug&#8212;such as a division by zero or an infinite loop&#8212;can crash the entire kernel and trigger a kernel panic, bringing down every process on the machine.</p><p>To make safe kernel extensibility possible, eBPF relies on <strong>a restricted programming model and strict verification</strong>, rather than trust.</p><p>Although eBPF programs can be written in C syntax, they are not general-purpose programs and <strong>must conform to strict compilation and verification constraints.</strong> <strong>During compilation,</strong> <strong>unsupported constructs&#8212;such as heap allocation (</strong><code>malloc</code><strong>) or unrestricted pointer usage&#8212;are rejected</strong>, because the eBPF instruction set simply does not support them.</p><p>After compilation, the resulting eBPF bytecode is passed to the kernel. Before it is allowed to execute, <strong>the eBPF verifier performs a full safety analysis. The verifier ensures that the program cannot consume unbounded resources</strong>&#8212;no infinite loops, no recursion, no excessive stack usage, and no unsafe memory access.</p><p>Finally, the eBPF program can be safely executed inside the kernel. At this stage, the <strong>kernel must be able to transform the verified eBPF bytecode into executable machine code.</strong></p><p><strong>Early eBPF implementations executed bytecode through an interpreter-based virtual machine.</strong> While functionally correct, interpretation introduced significant overhead and was unsuitable for performance-critical kernel paths.</p><p><strong>Modern eBPF therefore relies on JIT compilation, which is capable of transforming eBPF bytecode into native machine code for the target CPU architecture, allowing eBPF programs to run at near-native speed within the kernel runtime.</strong></p><h1><strong>How is the collected data stored?</strong></h1><p>Since eBPF programs are restricted, they cannot perform complex I/O operations (like writing directly to an external database). Instead, <strong>eBPF provides eBPF Maps&#8212;efficient data structures (Hash maps, Arrays, Ring Buffers, etc.) shared between the kernel and user space.</strong></p><p>These maps can be accessed efficiently using <code>mmap</code>, a virtual memory mechanism that maps kernel-managed physical pages into the virtual address space of a user-space process. </p><p><strong>With this approach, user-space programs can read data directly from memory, avoiding repeated </strong><code>read()</code><strong>-style system calls that would otherwise require copying data between kernel and user space.</strong></p><p>Once the data is read from memory, the user-space program can then forward it to a database, monitoring system, or any other external service.</p><h1><strong>Can we monitor functions that don&#8217;t trigger System Calls?</strong></h1><p>Yes. While many eBPF hooks (like <code>sk_msg</code>) focus on the network or socket layers, eBPF also provides <strong>uprobes (User Probes)</strong>.</p><p>Unlike syscall hooks, uprobes can modify the binary in memory. They insert a &#8220;trap&#8221; instruction at the entry and exit points of a specific function in your application. </p><p>When the process reaches a probed function, <strong>execution is interrupted by a trap that transfers control to the kernel, where the associated eBPF hook</strong>  <strong>(e.g., using </strong><code>bpf_ktime_get_ns</code><strong> to record CPU time)  is executed</strong>. This can happen both at function entry and at function return, allowing eBPF programs to observe execution before and after the function runs.</p><h1><strong>How can I start building with eBPF?</strong></h1><p>Let&#8217;s build a simple project that uses <code>uprobes</code> to measure a function&#8217;s execution time.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><h3>Prerequisites</h3><ul><li><p><strong>Vagrant &amp; Virtual Machine</strong>: eBPF is natively supported only on Linux. If you are using macOS or Windows, you must develop within a Linux environment (e.g., Ubuntu via Vagrant).</p></li><li><p><strong>Clang &amp; LLVM</strong>: BPF programs are written in C and compiled into bytecode using Clang/LLVM.</p></li><li><p><strong>Go</strong>: We will use Go to write the user-space process that manages the eBPF lifecycle.</p></li></ul><h3>Step 1: Define eBPF Maps and Structures</h3><p>First, we define a <strong>Hash Map</strong> to store function entry timestamps (indexed by Process ID), a <strong>Ring Buffer</strong> to transfer calculated durations to the user-space process, and an <strong>event</strong> struct to standardize the data format shared between the kernel and user space.</p><pre><code>
// Define a hash map to store start times; map name is "start_times"
struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 1024);
    __type(key, u32);
    __type(value, u64);
} start_times SEC(".maps");

// Define a ring buffer to send events to user space; map name is "events"
struct {
    __uint(type, BPF_MAP_TYPE_RINGBUF);
    __uint(max_entries, 256 * 1024);
} events SEC(".maps");

// Define the event structure for data transfer
struct event {
    u32 pid;
    u64 start_time;
    u64 duration_ns;
};</code></pre><h3><strong>Step 2 : write a eBPF program</strong></h3><p>The <code>SEC</code> macro is an eBPF-specific tool used to define the hook or section type for a specific block of code.</p><h4>Entry Hook</h4><p>This hook triggers at the start of the function, records the current timestamp, and stores it in the hash map.</p><pre><code>SEC("uprobe")
int trace_func_entry(struct pt_regs *ctx) {
    // get current pid by removing lower 32 bits which is tid
    u32 pid = bpf_get_current_pid_tgid() &gt;&gt; 32;
    u64 ts = bpf_ktime_get_ns();

    // store the start time in the map
    bpf_map_update_elem(&amp;start_times, &amp;pid, &amp;ts, BPF_ANY);
    bpf_printk("trace_func_entry hit: PID %d\n", pid);
    return 0;
}</code></pre><h4>Exit Hook</h4><p>This hook triggers when the function returns. It calculates the elapsed time and submits the result to the ring buffer.</p><pre><code>SEC("uprobe")
int trace_func_exit(struct pt_regs *ctx) {
    // get current pid by removing lower 32 bits which is tid
    u32 pid = bpf_get_current_pid_tgid() &gt;&gt; 32;
    u64 *start_ts = bpf_map_lookup_elem(&amp;start_times, &amp;pid);
    
    if (!start_ts) return 0;
    u64 end_ts = bpf_ktime_get_ns();
    // initialize and submit event to ring buffer
    struct event *e = bpf_ringbuf_reserve(&amp;events, sizeof(*e), 0);
    if (e) {
        e-&gt;pid = pid;
        e-&gt;duration_ns = end_ts - *start_ts;
        bpf_ringbuf_submit(e, 0);
    }
    bpf_printk("trace_func_exit hit: PID %d\n", pid);

    // cleanup the start time from the map
    bpf_map_delete_elem(&amp;start_times, &amp;pid);
    return 0;
}</code></pre><h3>Step 3: Write the User-Space Process</h3><p>The user-space process (written in Go) is responsible for:</p><ol><li><p>Loading the eBPF bytecode into the kernel.</p></li><li><p>Attaching the <code>uprobe</code> hooks by resolving the binary path and function offsets.</p></li><li><p>Initializing the ring buffer and consuming events for display.</p></li></ol><p>We use the <code>github.com/aquasecurity/libbpfgo</code> library. Note that <code>libbpfgo</code> depends on <code>libbpf</code> (C library), so you must use <strong>CGO</strong> for compilation.</p><pre><code>// Event definition shared with eBPF program
// struct event { u32 pid; u64 start_time; u64 duration_ns; };
type Event struct {
&#9;Pid        uint32
&#9;_          [4]byte // Padding for alignment
&#9;StartTime  uint64
&#9;DurationNs uint64
}

func main() {
&#9;binaryPath := os.Args[1]
&#9;symbolName := os.Args[2]

&#9;_, err := os.Stat(binaryPath)
&#9;if err != nil {
&#9;&#9;terminate(fmt.Errorf("binary path error: %v", err))
&#9;}

&#9;// load eBPF object file
&#9;bpfModule, err := libbpfgo.NewModuleFromFile("main.ebpf.o")
&#9;if err != nil {
&#9;&#9;terminate(err)
&#9;}
&#9;defer bpfModule.Close()

&#9;// load eBPF program into the kernel
&#9;if err := bpfModule.BPFLoadObject(); err != nil {
&#9;&#9;terminate(err)
&#9;}

&#9;// get and attach Uprobe (Entry)
&#9;entryProg, err := bpfModule.GetProgram("trace_func_entry")
&#9;if err != nil {
&#9;&#9;terminate(err)
&#9;}

&#9;offset, err := common.SymbolToOffset(binaryPath, symbolName)
&#9;if err != nil {
&#9;&#9;terminate(err)
&#9;}

&#9;probeLink, err := entryProg.AttachUprobe(-1, binaryPath, uint32(offset))
&#9;if err != nil {
&#9;&#9;terminate(fmt.Errorf("failed to attach uprobe: %v", err))
&#9;}
&#9;if probeLink == nil {
&#9;&#9;terminate(fmt.Errorf("failed to attach uprobe: link is nil"))
&#9;}
&#9;defer probeLink.Destroy()

&#9;// get and attach Uprobe (Exit)
&#9;exitProg, err := bpfModule.GetProgram("trace_func_exit")
&#9;if err != nil {
&#9;&#9;terminate(err)
&#9;}

&#9;returnOffsets, err := FindReturnOffsets(binaryPath, symbolName)
&#9;if err != nil {
&#9;&#9;terminate(fmt.Errorf("failed to find return offsets: %v", err))
&#9;}
&#9;if len(returnOffsets) == 0 {
&#9;&#9;terminate(fmt.Errorf("no return offsets found for symbol: %s", symbolName))
&#9;}
&#9;uretLink, err := exitProg.AttachURetprobe(-1, binaryPath, uint32(offset+returnOffsets[0]))
&#9;if err != nil {
&#9;&#9;terminate(fmt.Errorf("failed to attach uretprobe: %v", err))
&#9;}
&#9;if uretLink == nil {
&#9;&#9;terminate(fmt.Errorf("failed to attach uretprobe: link is nil"))
&#9;}
&#9;defer uretLink.Destroy()

&#9;// initialize Ring Buffer
&#9;eventsChannel := make(chan []byte)
&#9;rb, err := bpfModule.InitRingBuf("events", eventsChannel)
&#9;if err != nil {
&#9;&#9;terminate(err)
&#9;}

&#9;// start Ring Buffer polling
&#9;rb.Poll(1000)

&#9;// setup signal handling
&#9;sigChan := make(chan os.Signal, 1)
&#9;signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
&#9;fmt.Println("Tracing... Press Ctrl+C to stop.")
&#9;ctx, cancel := context.WithCancel(context.Background())
&#9;// event processing loop
&#9;go func() {
&#9;&#9;for {
&#9;&#9;&#9;select {
&#9;&#9;&#9;case &lt;-ctx.Done():
&#9;&#9;&#9;&#9;return
&#9;&#9;&#9;default:
&#9;&#9;&#9;}
&#9;&#9;&#9;rawBytes := &lt;-eventsChannel
&#9;&#9;&#9;var event Event
&#9;&#9;&#9;err := binary.Read(bytes.NewReader(rawBytes), binary.LittleEndian, &amp;event)
&#9;&#9;&#9;if err != nil {
&#9;&#9;&#9;&#9;fmt.Printf("failed to decode received data: %v\n", err)
&#9;&#9;&#9;&#9;continue
&#9;&#9;&#9;}
&#9;&#9;&#9;fmt.Printf("PID: %d | Function Duration: %v ms\n",
&#9;&#9;&#9;&#9;event.Pid, float64(event.DurationNs)/1e6)
&#9;&#9;}
&#9;}()

&#9;&lt;-sigChan
&#9;cancel()
&#9;rb.Stop()
&#9;fmt.Println("Exiting...")
}

func terminate(err error) {
&#9;fmt.Fprintf(os.Stderr, "Error: %v\n", err)
&#9;os.Exit(1)
}</code></pre><h3>Full Codebase</h3><p>To simplify setup, you can access the entire codebase&#8212;including the Vagrant configuration and Makefile&#8212;at the link below: <strong>[<a href="https://github.com/vx416/ebpf_test">https://github.com/vx416/ebpf_test</a>]</strong></p><h1><strong>Conclusion</strong></h1><p>eBPF enables safe, efficient, and real-time observability at the kernel level without requiring any modifications to the application code. </p><p>To leverage its power, we don't need to reinvent the wheel&#8212;we can rely on proven tools like:</p><ul><li><p><strong>Pyroscope</strong>: Utilizes eBPF to generate high-resolution, real-time CPU flame graphs for continuous profiling.</p></li><li><p><strong>Pixie</strong>: A Kubernetes-native observability platform that uses eBPF to automate request tracing, database query profiling, and process-level CPU analysis.</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Stop Trying to Build the "Perfect" System: A Practical Guide to CAP Theorem.]]></title><description><![CDATA[Why every distributed system is a game of trade-offs, from etcd&#8217;s consistency to Cassandra&#8217;s availability.]]></description><link>https://www.chillvic.dev/p/stop-trying-to-build-the-perfect</link><guid isPermaLink="false">https://www.chillvic.dev/p/stop-trying-to-build-the-perfect</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Sat, 20 Dec 2025 09:57:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!jbcc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jbcc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jbcc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 424w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 848w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 1272w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jbcc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png" width="832" height="1248" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1248,&quot;width&quot;:832,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1410836,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://chillvic.substack.com/i/182157665?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jbcc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 424w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 848w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 1272w, https://substackcdn.com/image/fetch/$s_!jbcc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87534a23-61f6-4639-8e1c-f812016ed442_832x1248.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Today, I&#8217;m going to talk about the famous CAP theorem in a practical way by answering the following questions:</p><ul><li><p>What is a distributed system and why do people use it?</p></li><li><p>What is the CAP theorem?</p></li><li><p>How do you choose between designing a CP or an AP system?</p></li></ul><h1>What is a distributed system and why do people use it?</h1><p>A distributed system consists of multiple independent machines that collaborate via communication protocols. The benefits of such a system are <strong>availability</strong> and <strong>flexibility</strong>.</p><p>When multiple machines work together to accomplish tasks, you don&#8217;t have to worry if one or two machines stop working unexpectedly&#8202;&#8212;&#8202;because the others continue operating.</p><p>Additionally, when the system encounters a sudden surge in throughput, you can simply add new machines without any downtime. <strong>In contrast, vertical scalability (upgrading a single machine) typically requires downtime and has physical limitations.</strong></p><p>Although distributed systems offer critical advantages, <strong>designing a perfect distributed system is impractical.</strong></p><p><strong>That&#8217;s where the CAP theorem comes into play to guide architectural decisions.</strong></p><h1>What is the CAP theorem?</h1><p>In a system with many machines, <strong>it&#8217;s unrealistic to expect everything to work flawlessly.</strong> Since machines communicate over networks, and network conditions are inherently unstable, routers may fail, bandwidth may become saturated, and packets may be delayed&#8202;&#8212;&#8202;making it impossible to assume timely delivery.</p><p>As a system architect, you must design for failure and build robustness into your system.</p><p>So what happens when some machines can&#8217;t communicate with each other?</p><p>In a distributed database, a machine crash or a lost network connection <strong>can lead to inconsistent data&#8202;&#8212;&#8202;because some machines may miss updates and fall out of sync. As a result, users accessing different servers may see different versions of the same data.</strong></p><p><strong>This is the moment when you have to make a trade-off:</strong></p><ul><li><p>If you prioritize data consistency, you must halt the system until the crashed machine recovers and synchronizes missing data.</p></li><li><p>If you prioritize availability, you allow the system to continue operating despite temporary inconsistencies.</p></li></ul><p>This trade-off is at the heart of the CAP theorem:</p><blockquote><p><strong>Consistency and Availability cannot both be fully guaranteed when your distributed system must tolerate network partitions.</strong></p></blockquote><p>CAP stands for:</p><p>P&#8202;&#8212;&#8202;Partition Tolerance</p><p>A&#8202;&#8212;&#8202;Availability</p><p>C&#8202;&#8212;&#8202;Consistency</p><p>Every distributed system must make trade-offs among these three properties, typically sacrificing either consistency (C) or availability (A). <strong>Partition tolerance (P) cannot be sacrificed, because a distributed system must assume that partition failures will occur.</strong></p><h1><strong>How to Choose Between CP and AP Systems?</strong></h1><p>Now that you understand the CAP theorem, let&#8217;s explore how to choose between CP and AP systems using database systems as examples.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p><p>When designing a database, <strong>users primarily care about read and write operations, and different business contexts may create higher loads on either reads or writes.</strong> Thus, your system should prioritize the more critical operation.</p><h3><strong>If your system handles heavy write throughput:</strong></h3><p>You will need multiple machines to handle write requests. However, <strong>with multiple writers, maintaining consistency becomes more challenging, because it&#8217;s difficult to reach consensus among multiple leaders.</strong></p><p>Since read consistency is less critical in this scenario, temporary data inconsistencies can be tolerated. In this case, you should choose an AP system. The system prioritizes availability, ensuring that users can continue writing new data even when some machines are down.</p><h3><strong>Cassandra: An AP System Example</strong></h3><p><strong>Cassandra</strong> is a distributed database optimized for high write throughput and is a classic example of an <strong>AP system</strong>. </p><p>It accepts writes on multiple nodes and propagates updates to replicas asynchronously. <strong>When conflicts occur, Cassandra typically resolves them using a timestamp-based Last-Write-Wins (LWW) rule, where writes with newer timestamps overwrite older ones.</strong> </p><p>As a result, occasional read inconsistencies can occur, but Cassandra prioritizes write availability and guarantees eventual consistency, allowing it to scale extremely well.</p><h3><strong>If your system prioritizes read consistency:</strong></h3><p>You may deploy several machines to handle reads and boost read performance, but only one machine should handle writes to avoid conflicts.</p><p>With a single source of truth for writes, you can ensure that every update is consistently synchronized across the system. <strong>However, if the single write node crashes, the system must elect a new write node&#8202;&#8212;&#8202;a process that causes temporary downtime, sacrificing availability.</strong> So, consistency is guaranteed, but availability is compromised during failures.</p><h3><strong>etcd: An CP System Example</strong></h3><p><strong>etcd</strong> is a distributed key-value store primarily used for storing system metadata.</p><p><strong>Because metadata is at the heart of system coordination &#8212; for example, Kubernetes uses etcd to store cluster state and scheduling information &#8212; data inconsistency can lead to inconsistent system behavior and unpredictable errors.</strong> Moreover, metadata is typically read very frequently but updated relatively infrequently.</p><p><strong>To guarantee strong consistency, etcd uses the Raft consensus algorithm to ensure that every update is committed and replicated across the cluster before it becomes visible.</strong> It also provides multiple follower nodes to serve read requests, improving read performance while maintaining consistency.</p><h1>Conclusion</h1><p>The CAP theorem reminds us that no distributed system can simultaneously guarantee consistency, availability, and partition tolerance. When designing a system, it&#8217;s crucial to understand your business priorities, choose the right technologies, and make conscious trade-offs.</p>]]></content:encoded></item><item><title><![CDATA[On the Path to Systems Architecture]]></title><description><![CDATA[Making complex systems easier to understand &#8212; one piece at a time.]]></description><link>https://www.chillvic.dev/p/coming-soon</link><guid isPermaLink="false">https://www.chillvic.dev/p/coming-soon</guid><dc:creator><![CDATA[Chill Vic]]></dc:creator><pubDate>Fri, 19 Dec 2025 23:26:14 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!gyHF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gyHF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gyHF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gyHF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png" width="1024" height="1536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1536,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1077256,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://chillvic.substack.com/i/182134713?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gyHF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 424w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 848w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 1272w, https://substackcdn.com/image/fetch/$s_!gyHF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25523643-5162-49dc-980a-cd6217e3a860_1024x1536.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>I&#8217;m Chill Vic.</strong></p><p>Living in the modern world, I can feel the power and convenience of the digital systems that surround us every day. As a software engineer, I&#8217;m naturally curious about the architectures behind them &#8212; why they are designed the way they are, and what problems they are meant to solve.</p><p><strong>This curiosity is what led me onto the path of system architecture.</strong></p><p>Here, I use simple, straightforward language to explain complex systems &#8212; covering databases, operating systems, system design, algorithms, and AI.</p><p>My workout trainer once said that pain means your muscles are getting stronger.<br>But when it comes to learning systems, I&#8217;d rather avoid unnecessary pain.</p><p>My goal is to explain things so clearly that even my mom can understand &#8212; and hopefully make your learning journey a little easier along the way.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.chillvic.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.chillvic.dev/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item></channel></rss>