<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivanshi Dhawan on Medium]]></title>
        <description><![CDATA[Stories by Shivanshi Dhawan on Medium]]></description>
        <link>https://medium.com/@shivanshidhawan?source=rss-2810f26cdd7e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9Vm-1aO19RoHrsni</url>
            <title>Stories by Shivanshi Dhawan on Medium</title>
            <link>https://medium.com/@shivanshidhawan?source=rss-2810f26cdd7e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 21:28:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivanshidhawan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[“Lock-Free” Wasn’t the Optimisation. Batching Was.]]></title>
            <link>https://medium.com/@shivanshidhawan/lock-free-wasnt-the-optimisation-batching-was-9e9d45dbdf50?source=rss-2810f26cdd7e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e9d45dbdf50</guid>
            <category><![CDATA[lock-free]]></category>
            <category><![CDATA[batching]]></category>
            <category><![CDATA[zeromq]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Shivanshi Dhawan]]></dc:creator>
            <pubDate>Mon, 13 Jul 2026 02:18:02 GMT</pubDate>
            <atom:updated>2026-07-13T15:11:56.729Z</atom:updated>
            <content:encoded><![CDATA[<p>Buried in ZeroMQ’s TCP code is a single line that turns off one of the oldest optimisations in the TCP stack, on purpose, with a comment explaining exactly why. In a different part of the codebase, there’s a queue that people usually describe as “lock-free,” but what actually makes it fast has almost nothing to do with avoiding locks. Both of these are really the same decision, made twice, at two different layers of the stack.</p><h3>The misconception</h3><p>People tend to treat “lock-free” as a synonym for “fast.” The assumption is that if you’re not blocking on a mutex, you’ve eliminated the cost of coordinating between threads. That’s not quite right; lock-free algorithms aren’t literally free of locking. The locking just moves down to the hardware level, handled by the CPU’s cache-coherency protocol every time you run an atomic instruction like compare-and-swap (CAS).</p><p>That distinction matters more than it looks like it should, because it changes what you optimise. If you think the win is “no locks,” you stop the moment you’ve swapped a mutex for a CAS loop. If you understand the win is really “cheaper, hardware-level coordination instead of OS-level coordination,” you notice you’re still paying that hardware cost on every single operation, and you start asking whether you can pay it less often.</p><h3>The actual mechanism</h3><p>Start further back, with the problem ZeroMQ was actually solving. In a naive networked application, the thread that calls send() is the same thread that talks to the kernel&#39;s socket buffer. That means the thread&#39;s progress is now tied to how fast the network, or the receiver, can keep up. Application logic and network I/O share a thread, so a slow peer or a full socket buffer stalls your application code.</p><p>ZeroMQ’s fix is to never let application code touch the network directly. When you callzmq_send(), it doesn&#39;t send anything. It queues the message onto an internal pipe and returns immediately. A separate background I/O thread owns the actual socket and does the sending asynchronously, on its own schedule. The application thread is untied from the network entirely. It hands off a message and moves on.</p><p>That handoff, from application thread to I/O thread, now sits on the critical path of every message your application sends. So the pipe connecting the two threads has to be extremely fast. The obvious implementation is a queue guarded by a mutex, but that reintroduces exactly the blocking you just tried to eliminate: now the application thread can stall waiting for the I/O thread to release a lock, instead of stalling waiting for the network.</p><p>So the pipe is built lock-free instead, using CAS to publish and consume items without ever taking an OS-level lock. Here’s the part that’s easy to miss though: a CAS operation per message is still too expensive once your message rate gets high enough, because every CAS forces a synchronization event across CPU cores. Doing that once per message caps your throughput far below what the hardware could otherwise sustain.</p><p>The actual fix is batching. The writer thread accumulates a run of messages into a small buffer that only it can see (no synchronisation needed there, since nothing else can touch it yet), then does a single CAS to publish the entire batch to the reader thread at once. The hardware synchronisation cost gets paid once per batch instead of once per message. The batch size grows automatically under load too: more backlog means more messages get folded into each CAS, which is exactly when you need the amortisation most.</p><p>One more constraint makes this tractable. Each pipe has exactly one writer thread and one reader thread, always. Building a lock-free structure that safely handles many writers and many readers at once is a much harder problem, full of subtle correctness bugs, than one with a single writer and single reader. ZeroMQ sidesteps that problem entirely. When a socket needs to fan out to multiple peers, it doesn’t build one cleverer shared structure. It just creates more single-writer, single-reader pipes, one per peer.</p><p>Now follow a message the rest of the way. It’s been batched and handed to the I/O thread, which writes it to a TCP socket. TCP has its own, much older batching mechanism: Nagle’s algorithm, which holds small writes back and coalesces them before sending, trading latency for fewer packets. ZeroMQ turns this off, explicitly, socket by socket, using TCP_NODELAY. The reasoning is spelled out directly in the source comment: the library is already doing data batching at its own level, so Nagle&#39;s algorithm wouldn&#39;t add any throughput. It would only add latency, because now two independent, uncoordinated batching mechanisms are stacked on top of each other, each deciding on its own schedule when to actually let bytes go.</p><p>That’s the real design principle here, stated as plainly as source code ever states anything. Batching should happen at exactly one layer, the layer that actually has visibility into whether there’s a backlog worth batching, and every other layer downstream should stay out of the way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fJPzCKUjQoNjOL0sc0HWmw.png" /></figure><h3>Why it matters</h3><p>This exact failure mode shows up constantly outside of ZeroMQ, usually as a mystery. The instinct to reach for another buffering knob is natural. The actual fix is almost always to figure out which single layer should own the decision, and stop letting others quietly second-guess it.</p><p>The pattern generalises well past networking. An application-level write buffer, a database client’s batching layer, an OS-level page cache, and a storage device’s own write buffer can all coexist in one write path, each with its own idea of when to flush. None of them is individually wrong. Stacked without coordination, they produce exactly the kind of latency that’s miserable to debug, because each layer looks correct in isolation and the interaction only shows up as an aggregate number nobody can trace back to a cause.</p><h3>Key takeaways</h3><ul><li>Lock-free doesn’t mean synchronisation-free. CAS moves the coordination cost to CPU-level cache coherency; it doesn’t eliminate it.</li><li>The performance win in a lock-free pipe usually isn’t the CAS itself. It’s reducing how often you have to pay for one, by batching messages behind a single CAS instead of doing one CAS per message.</li><li>Decoupling an application thread from network I/O (queue and return, instead of a blocking send()) is what frees application logic from network speed. A fast internal pipe is what makes that decoupling viable on every message, not just occasionally.</li><li>Restricting a lock-free structure to exactly one writer and one reader sidesteps a much harder correctness problem. Fan-out gets solved with more pipes, not a cleverer shared structure.</li><li>Disabling Nagle’s algorithm isn’t a general “always do this for low latency” tip here. It’s a specific consequence of already owning batching at a higher layer. Turning it off makes sense because something else is already doing that job.</li><li>When multiple layers in a system can each independently decide to buffer or delay, pick exactly one layer with enough visibility to make that call correctly, and explicitly disable the others rather than letting them interact by accident.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e9d45dbdf50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory Safety in Rust: A Deep Dive]]></title>
            <link>https://medium.com/@shivanshidhawan/memory-safety-in-rust-a-deep-dive-e0c7196caa67?source=rss-2810f26cdd7e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0c7196caa67</guid>
            <category><![CDATA[rust-programming-language]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[memory-safety]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Shivanshi Dhawan]]></dc:creator>
            <pubDate>Sat, 28 Sep 2024 23:04:12 GMT</pubDate>
            <atom:updated>2024-09-28T23:04:12.434Z</atom:updated>
            <content:encoded><![CDATA[<p>Memory safety is one of the most crucial aspects of system programming. When managing memory manually (as in C or C++), programmers often encounter issues like null pointer dereferencing, buffer overflows, use-after-free errors, and data races. These problems lead to undefined behaviour, security vulnerabilities, and hard-to-debug crashes. <strong>Rust</strong> is designed to provide memory safety guarantees while maintaining high performance, eliminating most of these issues at compile time.</p><h3>Why Memory Safety Matters</h3><p>Memory safety refers to preventing bugs and vulnerabilities caused by improper use of memory. Common errors include:</p><ul><li><strong>Dangling pointers</strong>: Accessing memory after it’s freed.</li><li><strong>Double free</strong>: Attempting to free memory that’s already freed.</li><li><strong>Buffer overflow</strong>: Writing beyond the bounds of allocated memory.</li><li><strong>Data races</strong>: Concurrent access to shared memory leading to undefined behaviour.</li></ul><p>Rust’s approach to memory safety revolves around three core concepts:</p><ol><li><strong>Ownership</strong></li><li><strong>Borrowing</strong></li><li><strong>Lifetimes</strong></li></ol><p>These mechanisms allow Rust to statically enforce memory safety, preventing most common memory errors at compile time without needing a garbage collector.</p><h3>1. Ownership: The Foundation of Memory Safety</h3><p>The ownership system is Rust’s most unique feature and the cornerstone of memory safety. Each value in Rust has a single “owner” at any time, and when the owner goes out of scope, the value is automatically deallocated.</p><h4>Example: Ownership Basics</h4><pre>fn main() {<br> let s1 = String::from(&quot;Hello&quot;);<br> let s2 = s1; // Ownership of s1 is moved to s2, s1 is no longer valid.<br>// println!(&quot;{}&quot;, s1); // This would result in a compile-time error.<br> println!(&quot;{}&quot;, s2); // s2 owns the value now.<br>}</pre><p>In this example:</p><ul><li><strong>s1</strong> is created as a String.</li><li>When we assign s1 to s2, ownership of the string is transferred, and <strong>s1</strong> is no longer valid.</li><li>Rust ensures that there’s only one owner of any heap-allocated resource at a time, avoiding <strong>double frees</strong> and dangling pointers.</li></ul><h3>2. Borrowing: Temporary Access without Ownership Transfer</h3><p>While ownership ensures no value is freed twice, it’s not always practical to move ownership every time. Rust allows for <strong>borrowing</strong>, where references to data can be passed without transferring ownership. Borrowing enforces strict rules:</p><ul><li>You can have either one mutable reference or any number of immutable references to a resource, but not both simultaneously.</li></ul><h4>Example: Borrowing in Action</h4><pre>fn main() {<br>    let s = String::from(&quot;Hello&quot;);<br>    <br>    // Immutable borrowing<br>    let len = calculate_length(&amp;s); <br>    println!(&quot;The length of &#39;{}&#39; is {}.&quot;, s, len);<br>    <br>    // Mutable borrowing<br>    let mut s2 = String::from(&quot;World&quot;);<br>    change_string(&amp;mut s2);<br>    println!(&quot;Changed string: {}&quot;, s2);<br>}<br><br>fn calculate_length(s: &amp;String) -&gt; usize {<br>    s.len() // Borrowed value cannot be modified here.<br>}<br><br>fn change_string(s: &amp;mut String) {<br>    s.push_str(&quot;!&quot;);<br>}</pre><p>Here:</p><ul><li>&amp;s is an <strong>immutable reference</strong>: we can read the string but not modify it.</li><li>&amp;mut s2 is a <strong>mutable reference</strong>: we can change the string (add &quot;!&quot;).</li></ul><p>Rust’s compiler ensures memory safety by preventing <strong>data races</strong>:</p><ul><li>If one part of the program has a mutable reference, no other part can have any reference (mutable or immutable) to that data at the same time.</li></ul><h3>3. Lifetimes: Managing Scope of References</h3><p>Lifetimes are a way for Rust to ensure that references are valid as long as they are used. While ownership and borrowing are about <em>who</em> owns the data, lifetimes define <em>how long</em> a reference can be valid.</p><p>Rust uses lifetimes to ensure that references don’t outlive the data they point to.</p><h4>Example: Lifetimes in Function Signatures</h4><pre>fn main() {<br>    let string1 = String::from(&quot;long string&quot;);<br>    let string2 = &quot;short&quot;;<br><br>    let result = longest(&amp;string1, string2);<br>    println!(&quot;The longest string is {}&quot;, result);<br>}<br><br>fn longest&lt;&#39;a&gt;(s1: &amp;&#39;a str, s2: &amp;&#39;a str) -&gt; &amp;&#39;a str {<br>    if s1.len() &gt; s2.len() {<br>        s1<br>    } else {<br>        s2<br>    }<br>}</pre><p>In this example, the function longest returns the longer of two string slices. The <strong>lifetime</strong> &#39;a ensures that the returned reference is valid as long as both input references are valid.</p><p>Without lifetimes, there would be a risk of returning a reference to data that has been deallocated, leading to a dangling pointer. Rust’s lifetimes prevent such situations at compile time.</p><h3>Memory Safety without Garbage Collection</h3><p>A key selling point of Rust is that it provides <strong>memory safety</strong> without the need for garbage collection, which is often seen as a trade-off in performance-sensitive systems programming. This is particularly important for use cases like:</p><ul><li>Operating system kernels</li><li>Embedded systems</li><li>Game engines</li><li>Real-time systems</li></ul><p>The combination of ownership, borrowing, and lifetimes ensures that:</p><ul><li>Memory is allocated and deallocated predictably and automatically.</li><li>There is minimal runtime overhead since Rust checks for memory safety at compile time.</li></ul><h3>Conclusion</h3><p>Rust’s approach to memory safety, combining ownership, borrowing, and lifetimes, is a powerful tool that prevents most common memory errors at compile time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0c7196caa67" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DNS Resolver Performance Testing]]></title>
            <link>https://medium.com/@shivanshidhawan/dns-resolver-performance-testing-f26619368665?source=rss-2810f26cdd7e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f26619368665</guid>
            <category><![CDATA[dns-servers]]></category>
            <category><![CDATA[dns-resolver]]></category>
            <category><![CDATA[dns]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Shivanshi Dhawan]]></dc:creator>
            <pubDate>Thu, 12 Sep 2024 03:08:41 GMT</pubDate>
            <atom:updated>2024-09-12T03:08:41.571Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://linux.die.net/man/1/resperf">resperf</a> tool can be utilised to evaluate the performance of a DNS resolver. This tool is designed specifically to test and measure the DNS performance under varying loads and conditions.</p><h4>How Resperf Works</h4><p>resperf functions by generating DNS queries at a steadily increasing rate. It begins at zero queries per second and can reach a maximum rate of up to 100,000 queries per second, depending on the duration of the ramp-up time. This test resperf records various performance metrics such as the rate of responses, the number of failures, and the latencies experienced. After completing the sending of queries, resperf monitor server responses for an additional 40 seconds.</p><p>The -r option in resperf is used to define the ramp-up duration, which controls how long the tool will gradually increase the traffic load. The default ramp-up duration is 60 seconds.</p><p>resperf can transition to the waiting-for-responses phase through three scenarios:</p><ol><li>The test runs for the entire designated duration and reaches the maximum query rate of 100,000 queries per second, which is uncommon.</li><li>The number of outstanding queries surpasses 65,536, usually due to the DNS resolver’s capacity limits.</li><li>resperf cannot send queries fast enough; a warning message is displayed if the query backlog reaches 1,000.</li></ol><h4>Resperf Report Overview</h4><p>The resperf report is organized into two primary sections: the Resperf Output and Graphical Plots.</p><ul><li><strong>Resperf Output Section</strong>: This section includes the tool’s output, command-line parameters, and a summary of statistics.</li><li><strong>Plots Section</strong>: This section provides two graphs that show the query/response/failure rates and latency measurements.</li></ul><h4>Determining Maximum Throughput</h4><p>To find the maximum throughput, refer to the peak Queries Per Second (QPS) value in the summary statistics. For monitoring throughput based on a certain threshold of query drops, use the -L option (e.g., -L 10 to specify a 10% query drop limit).</p><h3>Steps to Run Resperf for Testing DNS Resolver Performance in a Kubernetes Cluster</h3><ol><li><strong>Build the Resperf Docker Image</strong>: To test a DNS resolver, start by building the Docker image for resperf. Here is the Dockerfile setup:</li></ol><pre>FROM ubuntu:20.04<br>WORKDIR /app<br><br># Copy the dnsperf tarball<br>COPY dnsperf-2.13.1.tar.gz .<br><br>COPY &lt;query_filename&gt; /etc/config/resperf-queries/query_file.txt<br><br>ENV DEBIAN_FRONTEND=noninteractive<br><br>RUN apt-get update &amp;&amp; \<br>    apt-get install --fix-missing -y libssl-dev libldns-dev libck-dev libnghttp2-dev gnuplot build-essential<br><br>RUN tar zxvf dnsperf-2.13.1.tar.gz &amp;&amp; \<br>    cd dnsperf-2.13.1 &amp;&amp; \<br>    ./configure &amp;&amp; \<br>    make &amp;&amp; \<br>    make install<br><br>RUN rm -rf dnsperf-2.13.1 &amp;&amp; \<br>    rm dnsperf-2.13.1.tar.gz<br><br>ENTRYPOINT [&quot;/bin/sh&quot;]</pre><p><strong>2. Deploy the Resperf Job Manifest</strong>: Apply the following Kubernetes job manifest to run the resperf tool against your DNS resolver:</p><pre>apiVersion: batch/v1<br>kind: Job<br>metadata:<br>  name: dns-resolver-performance-test<br>  namespace: kube-system<br>spec:<br>  template:<br>    spec:<br>      nodeName: &lt;node_name&gt;<br>      containers:<br>        - name: resperf<br>          image: &lt;resperf_docker_image&gt;<br>          command:<br>            - /bin/sh<br>            - -c<br>            - |<br>              # Capture the generated HTML report file name from the output<br>              report_line=$(resperf-report -s &lt;dns_resolver_address&gt; -r 360 -d /etc/config/resperf-queries/query_file.txt 2&gt;&amp;1)<br>              report_filename=$(echo &quot;${report_line}&quot; | grep -oE &#39;Done, report is in (.+\.html)&#39; | cut -d&#39; &#39; -f5)<br>              # Move all related files to a common directory<br>              report_base=$(basename &quot;${report_filename}&quot; .html)<br>              mkdir -p /mnt/html/&quot;${report_base}&quot;<br>              mv &quot;${report_base}&quot;.* /mnt/html/&quot;${report_base}&quot;/<br>              if [ -n &quot;${report_filename}&quot; ]; then<br>                echo &quot;resperf-report completed successfully. Report file: ${report_filename} with base: ${report_base}&quot;<br>              else<br>                echo &quot;resperf-report did not complete successfully.&quot;<br>              fi<br>              sleep 900  # Pause for 15 minutes (900 seconds)<br>          volumeMounts:<br>            - name: html-artifact<br>              mountPath: /mnt<br>      restartPolicy: Never<br>      imagePullSecrets:<br>        - name: &lt;secret_name&gt;<br>      volumes:<br>      - name: html-artifact<br>        emptyDir: {}<br>  backoffLimit: 4</pre><p><strong>3. Retrieve the Resperf Report</strong>: After the job has completed, you can retrieve the performance report by copying the files from the resperf job pod to your local machine using the following command:</p><pre>kubectl cp kube-system/&lt;pod-name&gt;:/mnt/html/ &lt;path-to-local-directory&gt;</pre><p>By following these steps, you can effectively test and analyze the performance of any DNS resolver using the resperf tool. The resulting report will provide insights into the DNS resolver&#39;s handling of different traffic loads, including its throughput, latency, and failure rates.</p><p><strong>Links</strong></p><p><a href="https://linux.die.net/man/1/resperf">https://linux.die.net/man/1/resperf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f26619368665" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>