<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Théodore&#39;s blog - memory</title>
    <subtitle>Writings about Rust, networking and systems programming.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://totodore.github.io/tags/memory/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://totodore.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-11-22T00:00:00+00:00</updated>
    <id>https://totodore.github.io/tags/memory/atom.xml</id>
    <entry xml:lang="en">
        <title>Memory-bounded systems and backpressure in Rust</title>
        <published>2026-11-22T00:00:00+00:00</published>
        <updated>2026-11-22T00:00:00+00:00</updated>
        
        <author>
          <name>Théodore Prévot</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://totodore.github.io/memory-backpressure/"/>
        <id>https://totodore.github.io/memory-backpressure/</id>
        
        <content type="html" xml:base="https://totodore.github.io/memory-backpressure/">&lt;p&gt;As everybody knows and especially nowadays with the RAM shortage situation because of LLMs, memory is not infinite. Therefore a lot of workloads, processes, pods need system limits. For instance in the case of embedded systems but also, more commonly in webservers, remote systems, and so on.
Indeed the machine hosting your process, be it virtual or physical won’t have infinite RAM and once it is exhausted, the kernel will slow down or kill processes through the OOM killer, which will degrade the service you are actually trying to provide.&lt;/p&gt;
&lt;p&gt;For this reason, it is quite usual to add limits to processes or pods in a Kubernetes context, usually by using Linux cgroups, which are a way to add CPU and memory limits on processes. Today we will only talk about memory limits, CPU limits being a whole different topic.&lt;/p&gt;
&lt;p&gt;So what do we mean by memory backpressure? Well this is simply the ability of a system to delay new workloads or slow down to limit its memory usage to a given threshold. There are multiple solutions depending on the programming language, the kind of system you are integrating this into and the architecture of your program. We will focus on Rust.&lt;/p&gt;
&lt;p&gt;First, what can be bound and what cannot? The kernel enforces cgroup limits (&lt;code&gt;memory.max&lt;/code&gt;) against everything charged to the cgroup, exposed as &lt;code&gt;memory.current&lt;/code&gt;: the anonymous memory of its processes, the page cache of the files they read, write or memory-map, and kernel memory allocated on their behalf, such as socket buffers and page tables. Swap is accounted separately (&lt;code&gt;memory.swap.max&lt;/code&gt;). When the limit is hit, the kernel first reclaims what it can, mostly clean page cache, and only then invokes the OOM killer. Kubernetes adds its own layer on top: when the node itself runs low on memory, the kubelet evicts pods, using their &lt;em&gt;memory working set&lt;/em&gt; to rank them. The working set is &lt;code&gt;memory.current&lt;/code&gt; minus the inactive, easily reclaimable file pages. A container exceeding its own limit is not evicted: it is killed by the kernel OOM killer.&lt;/p&gt;
&lt;p&gt;The main focus here will be the process’s Resident Set Size (or RSS): the pages mapped by the process that are currently in RAM. It contains both reclaimable file-backed pages and anonymous memory. Only the latter is a problem: the kernel cannot drop it without swapping. A process RSS holds the following kinds of data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The instructions of the program being run. These pages are backed by the executable file, so the kernel can drop them and read them back when needed.&lt;/li&gt;
&lt;li&gt;The static sections: read-only data, also backed by the file, and mutable data (&lt;code&gt;.data&lt;/code&gt;, &lt;code&gt;.bss&lt;/code&gt;) which becomes anonymous memory once written.&lt;/li&gt;
&lt;li&gt;Thread stacks: 2 MiB reserved per thread spawned with Rust’s std by default, and usually 8 MiB for the main thread. Only the pages actually touched count in RSS.&lt;/li&gt;
&lt;li&gt;Resident pages of memory-mapped files, which are file-backed and reclaimable as well.&lt;/li&gt;
&lt;li&gt;Memory allocated dynamically by the process through &lt;code&gt;malloc&lt;/code&gt;: the heap.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The only manageable one is the memory allocated dynamically. The three following solutions will only concern this kind of memory and we will count the others as an overhead to measure, either constant or growing linearly with the load.&lt;/p&gt;
&lt;p&gt;The three solutions will rely on these two known example systems:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A live ingestion HTTP server that is at the front of a data processing pipeline. We can picture this as a push based consumption model. Requests are pushed over the network, if it can’t accept traffic anymore, the server will reply a &lt;code&gt;503&lt;/code&gt; status code with a &lt;code&gt;Retry-After&lt;/code&gt; header.&lt;/li&gt;
&lt;li&gt;A data processing system that will do CPU/Memory bound processing behind a stream implementation (Kafka/NATS). We can picture this as a pull based consumption model. The downstream service will pull new work to perform only when it can and will rely on the stream to act as a buffer in the meantime.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can define this difference as cooperative backpressure vs. non-cooperative. Push based consumption model &lt;strong&gt;expects&lt;/strong&gt; that the client respects the &lt;code&gt;Retry-After&lt;/code&gt; header, but the client is not forced to. Pull based does not rely on an expected behavior.&lt;/p&gt;
&lt;h1 id=&quot;indirect-memory-bounding&quot;&gt;Indirect memory bounding&lt;/h1&gt;
&lt;p&gt;This solution can be applied to any language. It is not specific to Rust.
The easiest way to bound your system memory is to limit two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The incoming load per request. For this we need to find a metric that scales linearly with the load of the service. For example with our server implementation it could be the request body size.&lt;/li&gt;
&lt;li&gt;The maximum number of requests it can process in parallel with a semaphore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If your system implements the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/tower/latest/tower/trait.Service.html&quot;&gt;tower &lt;code&gt;Service&lt;/code&gt; trait&lt;/a&gt; (which I highly recommend for ecosystem integration purposes), you can leverage the &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/tower/latest/tower/limit/concurrency/&quot;&gt;concurrency limit layer&lt;/a&gt; for this and expose the concurrency limit as a configuration knob. If your memory allocation is tied to requests handling, this might be one of the easiest solutions.&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Example: bounding an axum server with tower layers&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_CONCURRENT_REQUESTS&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1000&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_BODY_SIZE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Router&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;route&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;/ingest&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; post&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ingest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;        ServiceBuilder&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Reply `503` + `Retry-After` when overloaded.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;HandleErrorLayer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt;_&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; BoxError&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; async&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;StatusCode&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;SERVICE_UNAVAILABLE&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;RETRY_AFTER&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Reject instead of queuing when no slot is available.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;load_shed&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; One semaphore shared by every route.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;GlobalConcurrencyLimitLayer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_CONCURRENT_REQUESTS&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Decompress first so the limit applies to the decompressed size.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;RequestDecompressionLayer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;RequestBodyLimitLayer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_BODY_SIZE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; axum&amp;#39;s own 2 MiB default limit would shadow ours.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;DefaultBodyLimit&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;disable&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; listener&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;net&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;TcpListener&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;bind&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;0.0.0.0:3000&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    axum&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;serve&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;listener&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;unwrap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; ingest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;body&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; StatusCode&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; At most `MAX_BODY_SIZE` bytes buffered per request.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; body&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    StatusCode&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;ACCEPTED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;details&gt;
&lt;summary&gt;Example: a bounded NATS JetStream pull consumer&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_CONCURRENT_EVENTS&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 8&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_EVENT_SIZE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 512&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;dyn&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Send&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sync&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; js&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; jetstream&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;async_nats&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;connect&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;localhost:4222&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; stream&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; js&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;get_or_create_stream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Config&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            name&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;EVENTS&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            subjects&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; vec!&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;events.&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Bound the load per event: bigger messages are rejected on publish.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            max_message_size&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_EVENT_SIZE&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i32&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            ..&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Default&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; consumer&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;get_or_create_consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;processor&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; pull&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Config&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            durable_name&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;processor&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;into&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;            //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Server-side bound: never more unacked events delivered to us.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            max_ack_pending&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_CONCURRENT_EVENTS&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; i64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;            ..&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Default&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;default&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    consumer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;stream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Bound the prefetch buffer (default: 200 messages, no byte limit).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;max_messages_per_batch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_CONCURRENT_EVENTS&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;max_bytes_per_batch&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_CONCURRENT_EVENTS&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_EVENT_SIZE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;messages&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;take_until&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ctrl_c&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; The stream is not polled again until one of the events is done.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;for_each_concurrent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_CONCURRENT_EVENTS&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; move&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;            let&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; return&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;            process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;payload&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;            let&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ack&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_payload&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;u8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;sleep&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Duration&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from_millis&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;50&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;h2 id=&quot;capacity-planning-and-caveats&quot;&gt;Capacity planning and caveats&lt;/h2&gt;
&lt;p&gt;Once you have ways to limit the workload, you need to tune them and for this you need to measure the metrics you picked against the real memory usage and define a rule.
There are multiple caveats with this solution that will appear in this process:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You actually need to plan limits for the worst case scenario. For our server implementation, with a 5 MB request body and a concurrency limit set to 1000 requests in parallel, you will need to reserve up to 5 GB of memory. And actually most request bodies might only weigh 1 MB.&lt;/li&gt;
&lt;li&gt;Other memory allocations, allocator overhead and memory fragmentation are not counted, so you need to account manually for them, and it might scale from inputs that are completely unrelated to the requests, like the number of CPU threads, the number of blocking threads spawned by &lt;a rel=&quot;external&quot; href=&quot;https://tokio.rs&quot;&gt;tokio&lt;/a&gt;, the allocator used, the OS, and so on…&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;byte-budget-bounding&quot;&gt;Byte-budget bounding&lt;/h1&gt;
&lt;p&gt;In fact, the capacity planning complexity and caveats come from the load per request; the maximum number of parallel requests itself being straightforward to plan.
So byte budget is another solution: bound the bytes a work task can hold, instead of the number of tasks.&lt;/p&gt;
&lt;p&gt;The simplest byte budget is a semaphore with one permit per byte. Each request reserves its body size. With a 1 GiB budget, we still serve 1000 requests of 1 MiB in parallel, without reserving 5 GiB for the worst case.&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Example: a byte budget for an axum server with a weighted semaphore&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_BODY_SIZE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; BYTE_BUDGET&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; usize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 1024&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; One permit per byte, shared by every request.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; BUDGET&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Semaphore&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Semaphore&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;const_new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;BYTE_BUDGET&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Reserve the body size from the budget, reply `503` when it is exhausted.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Added to the router with `.layer(middleware::from_fn(byte_budget))`.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; byte_budget&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;req&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Request&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; next&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Next&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Response&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; req&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;headers&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;get&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;CONTENT_LENGTH&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;and_then&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;to_str&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;parse&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; No `Content-Length` (chunked body): reserve the worst case.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;unwrap_or&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_BODY_SIZE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX_BODY_SIZE&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;_permit&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; BUDGET&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;try_acquire_many&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;size&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u32&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; else&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        return&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;StatusCode&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;SERVICE_UNAVAILABLE&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;RETRY_AFTER&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;into_response&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; The bytes are given back when the permit is dropped.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    next&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;p&gt;Be careful with compressed bodies: &lt;code&gt;Content-Length&lt;/code&gt; is the compressed size, so the budget undercounts them. Our NATS consumer already had a byte budget: &lt;code&gt;max_bytes_per_batch&lt;/code&gt; bounds the prefetch buffer in bytes.&lt;/p&gt;
&lt;p&gt;You can go further and limit the allocated memory itself, either with a memory arena or a custom allocator.
One of its biggest advantages is that it allows for a finer customization of the system behavior while having a more precise idea of what we are limiting. And you can implement different behavior if your memory bound is reached, for example you can either flush processed data eagerly if it is a scratch bounded buffer of data or refuse any incoming data.
For a scratch buffer, &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/bumpalo&quot;&gt;bumpalo&lt;/a&gt; arenas have a &lt;code&gt;set_allocation_limit&lt;/code&gt;. For a real-world example, look at DataFusion’s &lt;a rel=&quot;external&quot; href=&quot;https://docs.rs/datafusion/latest/datafusion/execution/memory_pool/trait.MemoryPool.html&quot;&gt;&lt;code&gt;MemoryPool&lt;/code&gt;&lt;/a&gt;: operators reserve bytes before allocating, and spill to disk when the reservation fails.&lt;/p&gt;
&lt;p&gt;However it might come with a heavier configuration burden as the operator needs to tweak multiple limits and understand how they interact with each other. Also it might not be applicable for every system.&lt;/p&gt;
&lt;p&gt;Indeed, using such solutions might not be an option, especially if most of the allocations issued by your service are coming from external libs. Today, it is extremely rare to find crates that support custom allocators or that are only arena based. Some workloads cannot fit the bounded arena model and the allocator trait has only &lt;a rel=&quot;external&quot; href=&quot;https://blog.rust-lang.org/2026/11/12/Rust-1.100.0/#allocator-api&quot;&gt;been stabilized&lt;/a&gt; in Rust 1.100. So far only &lt;code&gt;Vec&lt;/code&gt; and &lt;code&gt;Box&lt;/code&gt; support it.&lt;/p&gt;
&lt;h1 id=&quot;exploring-global-memory-bounding&quot;&gt;Exploring global memory bounding&lt;/h1&gt;
&lt;p&gt;A lot of memory managed languages have ways of bounding memory globally through Garbage Collection with
a best-effort mechanism. Once a given threshold is reached, the GC starts to reclaim memory eagerly at the price of more pauses. Go has &lt;code&gt;GOMEMLIMIT&lt;/code&gt;, Java has &lt;code&gt;MaxRAMPercentage&lt;/code&gt;, … However there is no standard mechanism available for this in Rust (or any language that is not memory managed).&lt;/p&gt;
&lt;p&gt;Still, you can use a custom allocator that will give you stats of your heap usage. These allocators are usually declared globally.&lt;/p&gt;
&lt;p&gt;The most basic way is to make a custom wrapper to track allocated memory:&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Example with a custom global allocator wrapper&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;GlobalAlloc&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Layout&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; System&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;sync&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;atomic&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;AtomicUsize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Ordering&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Relaxed&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Wraps the system allocator and counts the bytes currently allocated.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; CountingAlloc&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicUsize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicUsize&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;unsafe&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; impl&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; GlobalAlloc&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; CountingAlloc&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    unsafe&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; alloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; layout&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Layout&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u8&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; ptr&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; unsafe&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; System&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;alloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;layout&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; !&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;is_null&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;            ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;fetch_add&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;layout&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;size&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ptr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    unsafe&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; dealloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; ptr&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;mut&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; layout&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Layout&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        unsafe&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; System&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;dealloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ptr&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; layout&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;        ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;fetch_sub&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;layout&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;size&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;global_allocator&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; GLOBAL&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; CountingAlloc&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; CountingAlloc&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;p&gt;However allocated memory is not the most representative metric and we can do much better!&lt;/p&gt;
&lt;h2 id=&quot;using-jemalloc&quot;&gt;Using Jemalloc&lt;/h2&gt;
&lt;p&gt;jemalloc is a general-purpose allocator originally written by Jason Evans for FreeBSD’s libc, and now maintained by Meta. It is one of the most feature-complete allocators, its performance is on par with the alternatives, and its main advantage for us is that it exposes a lot of memory usage statistics, which the tikv-jemalloc-ctl crate makes easy to read from Rust. This makes it a good fit for our use case: an HTTP server at the beginning of a data processing pipeline, ingesting and validating a high volume of data before it is processed downstream. Rust bindings and a ready to use crate are available with the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/tikv/jemallocator&quot;&gt;tikv-jemallocator&lt;/a&gt; crate and maintained by the tikv organisation.&lt;/p&gt;
&lt;h3 id=&quot;first-step-collecting-jemalloc-metrics-efficiently-with-opentelemetry&quot;&gt;First step: collecting jemalloc metrics efficiently with OpenTelemetry&lt;/h3&gt;
&lt;p&gt;Jemalloc metrics are not automatically updated: you need to ask jemalloc with an &lt;code&gt;epoch::advance()&lt;/code&gt; call. This operation is quite costly, more on this at the end. Consequently this cannot be done for each request.&lt;/p&gt;
&lt;p&gt;We are using the OpenTelemetry SDK to export data to a metric backend (here VictoriaMetrics), with a Grafana frontend.
As the metric export interval is much higher than jemalloc advance calls (60s vs. 1s), we also need to track the minimum and maximum values during the collection interval of each metric, so we can have more insights.&lt;/p&gt;
&lt;p&gt;First we can define a &lt;code&gt;Sample&lt;/code&gt; struct to track the last value and the extremum of a jemalloc metric during the OTEL metric export.&lt;/p&gt;
&lt;details open=&quot;true&quot;&gt;
&lt;summary&gt;Defining a sample struct to track the last measured value and its extrema&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;global_allocator&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; GLOBAL&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; tikv_jemallocator&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Jemalloc&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; tikv_jemallocator&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Jemalloc&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; ACTIVE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; RESIDENT&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; RETAINED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Last, min and max values of a jemalloc stat since the previous collection.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    min&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    max&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Sample&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    const&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;        Self&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            min&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;u64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            max&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; AtomicU64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;usize&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; tikv_jemalloc_ctl&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; let&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;            self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;store&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;            self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;min&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;fetch_min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;            self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;max&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;fetch_max&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Read an extremum and reset it for the next collection window.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Falls back to the last value if no sample was recorded in the window.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; take&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; extremum&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;AtomicU64&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; empty&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        match&lt;/span&gt;&lt;span&gt; extremum&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;swap&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;empty&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            v&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; if&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; ==&lt;/span&gt;&lt;span&gt; empty&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;load&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            v&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    ///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Export the `name`, `name.min` and `name.max` gauges.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; register&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt; self&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; name&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; meter&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; global&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;meter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;my-service&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        let&lt;/span&gt;&lt;span&gt; gauge&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; String&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; read&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Sample&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            meter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;u64_observable_gauge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;with_unit&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;By&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;with_callback&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;obs&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; obs&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;observe&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-4 z-l-i z-d-1 z-d-i&quot;&gt;self&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;                .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;        gauge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;to_owned&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;load&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;        gauge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;.min&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;take&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;min&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; u64&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;MAX&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;        gauge&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;format!&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;.max&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; |&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;|&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;take&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;max&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;details open=&quot;true&quot;&gt;
&lt;summary&gt;Then we can sample jemalloc metrics every second and export the results&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Sample jemalloc every second, much more often than metrics are exported,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; so the min/max gauges catch the peaks between two collections.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; sample_jemalloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; mut&lt;/span&gt;&lt;span&gt; interval&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;interval&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Duration&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from_secs&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-9 z-d-11&quot;&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    loop&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        interval&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;tick&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; jemalloc caches its stats: advance the epoch to refresh them.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; epoch&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;advance&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;is_ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;            ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;allocated&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;            ACTIVE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;active&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;            RESIDENT&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;resident&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;            RETAINED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;record&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;stats&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;retained&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;read&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;dyn&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Push metrics over OTLP/HTTP (localhost:4318 by default, every 60s).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; exporter&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; MetricExporter&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;builder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;with_http&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; provider&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; SdkMeterProvider&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;builder&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;with_periodic_exporter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;exporter&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;build&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    global&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;set_meter_provider&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;provider&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;clone&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;    ALLOCATED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;register&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;jemalloc.allocated&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;    ACTIVE&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;register&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;jemalloc.active&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;    RESIDENT&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;register&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;jemalloc.resident&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;    RETAINED&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;register&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;jemalloc.retained&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;spawn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;sample_jemalloc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; ... run your service here.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;signal&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;ctrl_c&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    provider&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;shutdown&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;?&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;h3 id=&quot;second-step-measuring-those-metrics-against-a-variable-load-to-determine-what-are-the-most-representative-metrics&quot;&gt;Second step: measuring those metrics against a variable load to determine what are the most representative metrics&lt;/h3&gt;
&lt;p&gt;…&lt;/p&gt;
&lt;h3 id=&quot;third-step-not-seeing-any-representative-metrics-and-asking-yourself-what-is-wrong&quot;&gt;Third step: not seeing any representative metrics and asking yourself what is wrong!&lt;/h3&gt;
&lt;p&gt;Hopefully by reading this you will not fall into the same traps :D! Indeed I stumbled upon two main footguns.&lt;/p&gt;
&lt;p&gt;Firstly, the jemallocator crate defaults to prefixed symbols, so it won’t override the &lt;code&gt;malloc&lt;/code&gt; family of functions in the resulting binary. If you have any C dependencies like zstd for compression/decompression, it will continue to use the default libc allocator. This is a big issue because zstd’s own allocations won’t be tracked by jemalloc. Hence the usage estimation will be invalid. To circumvent this, don’t forget to enable the &lt;code&gt;unprefixed_malloc_on_supported_platforms&lt;/code&gt; feature flag in your &lt;code&gt;Cargo.toml&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;Secondly, the main cause of our weird memory behavior is THP or Transparent Huge Pages!
This Linux feature might be enabled at the OS level by default. It will automatically allocate bigger pages than the ones asked by your allocator and will re-address the previously-allocated smaller pages on it. The issue is that it might increase the memory usage drastically (because a small 4 KiB allocation might be moved to a 2 MiB page) and more importantly, it won’t be tracked as &lt;em&gt;resident&lt;/em&gt; memory by jemalloc. This only happens when the host setting is &lt;code&gt;always&lt;/code&gt;. You can check it with &lt;code&gt;cat /sys/kernel/mm/transparent_hugepage/enabled&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Also, even with traffic being much smaller, the RSS won’t reduce because most of it consists of huge pages.
The best solution is to disable this feature on the allocator’s memory with a &lt;code&gt;madvise&lt;/code&gt; syscall. Jemalloc will do that for its own mappings if you specify &lt;code&gt;MALLOC_CONF=thp:never&lt;/code&gt;. Without the unprefixed feature flag, the variable is &lt;code&gt;_RJEM_MALLOC_CONF&lt;/code&gt;. This feature can cause a lot of other unexpected issues (see the redis doc &lt;a rel=&quot;external&quot; href=&quot;https://redis.io/docs/latest/operate/oss_and_stack/management/optimization/latency/#latency-induced-by-transparent-huge-pages&quot;&gt;about transparent huge page&lt;/a&gt;) and it only benefits certain types of workloads, so I strongly encourage you to read about it!&lt;/p&gt;
&lt;p&gt;Jemalloc provides a lot of metrics for you: allocated, active, resident, retained, … The question is not which one matches with my RSS/memory working set measured by the OS: none will. The best we can do is to measure the evolution of each metric against the RSS and determine a formula.&lt;/p&gt;
&lt;p&gt;This being said, if we do our measure again with these two params, we can clearly see that the most representative metric is the resident, which is no surprise. Resident memory is the closest metric to what the OS sees, because it counts allocations, allocator overhead, fragmentation and dirty pages waiting to be removed. However, check what other metrics are available from jemalloc and whether they fit your program better! Most of the time the rule will be a linear function: &lt;code&gt;MAX_MEMORY = (limit - base) * factor&lt;/code&gt;. The base accounts for the thread stacks, the program binary and static sections. The factor is a safety margin (0.75 on the system shown below).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/thp-enabling-jemalloc-vs-rss.png&quot; alt=&quot;Memory usage metrics with an ingestion server being rolled out at the middle with MALLOC_CONF=thp:never and unprefixed_malloc_on_supported_platforms: RSS drops to around 85 MiB and moves in the same shape as the resident metric&quot; /&gt;&lt;/p&gt;
&lt;p&gt;With a new config being rolled out with &lt;code&gt;MALLOC_CONF=thp:never&lt;/code&gt; and &lt;code&gt;unprefixed_malloc_on_supported_platforms&lt;/code&gt; we can see a significant RSS drop and more importantly it now follows the same shape as the resident metric!&lt;/p&gt;
&lt;p&gt;Now let’s run a little (actually two) ingestion benchmarks to check that the resident metric follows the RSS linearly:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/images/bench-jemalloc-vs-rss.png&quot; alt=&quot;Two ingestion benchmarks, from 10:00 to 13:00 and from 16:00 to 17:00: RSS and jemalloc resident rise and fall together, about 65 MiB apart, while allocated and active stay under 72 MiB&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Here, with the two benchmarks, from 10:00 to 13:00 and from 16:00 to 17:00 we can see that resident is clearly the most appropriate metric. In comparison, the allocated metric follows the same shape but grows much less: about +30 MiB against +100 MiB for resident. The main reason is that freed memory is not released right away. It stays in dirty pages, which count as resident but not as allocated.&lt;/p&gt;
&lt;h3 id=&quot;fourth-step-applying-backpressure-against-the-resident-metric&quot;&gt;Fourth step: applying backpressure against the resident metric&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;MAX_MEMORY&lt;/code&gt; comes from the rule above. The 0.75 factor is there because the resident metric is a lagging signal. It is sampled every second, so memory keeps growing between two samples. The margin must absorb that growth.&lt;/p&gt;
&lt;p&gt;It also falls slowly. Freed memory stays in dirty pages until jemalloc purges them. So the server can keep rejecting requests for a while after the load is gone.&lt;/p&gt;
&lt;details open=&quot;true&quot;&gt;
&lt;summary&gt;Backpressure against resident metric&lt;/summary&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;///&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Reply `503` + `Retry-After` while jemalloc holds more than `MAX_MEMORY`.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; memory_limiter&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;req&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Request&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; next&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Next&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Response&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; RESIDENT&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span&gt;last&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;load&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Relaxed&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt; MAX_MEMORY&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;        return&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;StatusCode&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;SERVICE_UNAVAILABLE&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;header&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;RETRY_AFTER&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;into_response&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    next&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;run&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;req&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;await&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; ingest&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;body&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Bytes&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; StatusCode&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span&gt; body&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; validate &amp;amp; forward downstream...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    StatusCode&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-7&quot;&gt;ACCEPTED&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;tokio&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;main&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt; fn&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Result&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Box&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;dyn&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span&gt;error&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;Error&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; Configure OTEL SDK and jemalloc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-17 z-d-17&quot;&gt;    let&lt;/span&gt;&lt;span&gt; app&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt; Router&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;new&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;route&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;/ingest&lt;/span&gt;&lt;span class=&quot;z-l-8 z-d-8&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt; post&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ingest&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;        .&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;layer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;middleware&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-16&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-14 z-d-18&quot;&gt;from_fn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;memory_limiter&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-10 z-l-i z-d-4 z-d-i&quot;&gt; add a memory limiter layer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-1 z-d-3&quot;&gt;    Ok&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/details&gt;
&lt;p&gt;The same check works for our pull based consumer. Instead of replying a &lt;code&gt;503&lt;/code&gt;, it stops pulling new events while resident is above &lt;code&gt;MAX_MEMORY&lt;/code&gt;. The stream acts as a buffer in the meantime.&lt;/p&gt;
&lt;h3 id=&quot;a-small-word-about-jemalloc-metrics-performance&quot;&gt;A small word about jemalloc metrics performance&lt;/h3&gt;
&lt;p&gt;When calling &lt;code&gt;epoch::advance()&lt;/code&gt; to update jemalloc metrics, it is actually going to walk every memory arena. Jemalloc has this mechanism of having multiple memory arenas in order to reduce fragmentation and lock contention. It is controllable with the &lt;code&gt;narenas&lt;/code&gt; param and by default is set to 4 per CPU thread. As jemalloc gets the number of CPU threads with &lt;code&gt;sched_getaffinity&lt;/code&gt; and does not follow cgroups, on a 64-core host machine, narenas will be 256, even if you have a CPU limit of 1 so make sure to verify this.&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Solution&lt;/th&gt;&lt;th&gt;What it bounds&lt;/th&gt;&lt;th&gt;Hard bound?&lt;/th&gt;&lt;th&gt;Main cost&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Indirect&lt;/td&gt;&lt;td&gt;Requests × max size&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Planning for the worst case&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Byte budget&lt;/td&gt;&lt;td&gt;Bytes held by the tasks&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;More knobs, little crate support&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Global&lt;/td&gt;&lt;td&gt;The whole heap&lt;/td&gt;&lt;td&gt;No, reactive&lt;/td&gt;&lt;td&gt;Measuring and tuning the allocator&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Indirect bounding is the easiest to set up, but you have to plan for the worst case. The global allocator solution is the easiest to operate: a single limit, and every allocation is counted. It’s the one I usually reach for in the first place. I fall back to byte-budget bounding when it fits the target system better, or when I need fine-grained control: tuning individual components separately instead of applying a single global limit. They aren’t mutually exclusive, and you can combine them if you need to. Keep in mind that the global limiter is reactive, not a hard bound. If a burst can outrun the margin, keep a concurrency limit behind it. Whichever you choose, bear in mind the performance impact on your own workload.&lt;/p&gt;
&lt;p&gt;Let’s reclaim all the memory from LLMs!&lt;/p&gt;
</content>
        
    </entry>
</feed>
