<?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 Ayushanand on Medium]]></title>
        <description><![CDATA[Stories by Ayushanand on Medium]]></description>
        <link>https://medium.com/@ayushanandbtcs?source=rss-971a285747fd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gWKto_9cTMxBE4-l</url>
            <title>Stories by Ayushanand on Medium</title>
            <link>https://medium.com/@ayushanandbtcs?source=rss-971a285747fd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:53:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayushanandbtcs/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[Process vs Threads]]></title>
            <link>https://medium.com/@ayushanandbtcs/process-vs-threads-2002ccad6a17?source=rss-971a285747fd------2</link>
            <guid isPermaLink="false">https://medium.com/p/2002ccad6a17</guid>
            <category><![CDATA[backend-engineering]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ayushanand]]></dc:creator>
            <pubDate>Fri, 24 Apr 2026 21:59:16 GMT</pubDate>
            <atom:updated>2026-04-24T21:59:16.628Z</atom:updated>
            <content:encoded><![CDATA[<h3>Key idea</h3><p><strong>A process</strong> is an executing program with its own <strong>isolated address space</strong> (private memory).</p><p><strong>A thread</strong> is a <strong>lightweight process</strong> that runs code <strong>sharing the parent process’s memory</strong>.</p><h3>Process</h3><ul><li><strong>Definition:</strong> A set of instructions executed by the CPU (conceptually “the program as it runs”).</li><li><strong>Memory:</strong> Has its <strong>own isolated memory space</strong> (heap/stack/address space).</li><li><strong>Identity:</strong> Has a <strong>PID (process identifier)</strong> assigned by the OS.</li><li><strong>Scheduling:</strong> Competes for CPU time. If it blocks on I/O (disk, network, etc.), it can be descheduled so other runnable work can use the CPU.</li></ul><p><strong>Why this matters:</strong> CPU time is scarce; efficient backends try to avoid being kicked off-core by blocking operations.</p><h3>Thread</h3><ul><li><strong>Definition (Linux framing):</strong> A <strong>lightweight process (LWP)</strong>.</li><li><strong>Memory:</strong> <strong>Shares memory</strong> with the parent process (no separate address space).</li><li><strong>Identity:</strong> Has its own thread ID.</li><li><strong>Scheduling:</strong> Also scheduled on CPUs like a process.</li></ul><p><strong>Implication:</strong> Shared memory introduces <strong>contention</strong> and requires synchronization (mutexes/locks) to avoid race conditions.</p><h3>Common server architectures</h3><h3>1) Single-process, single-thread</h3><ul><li>Simple model; avoids many concurrency pitfalls.</li><li>Example: <strong>Node.js</strong> (mostly single-threaded; uses additional threads internally for some tasks).</li></ul><h3>2) Multi-process (one process per worker)</h3><ul><li>Each process has its <strong>own memory</strong> (plus optional shared memory regions).</li><li>Good for utilizing multiple CPU cores with less shared-state complexity.</li><li>Example: <strong>Nginx</strong> worker processes.</li></ul><p><strong>Shared memory note:</strong> Multiple processes can still share a separate shared-memory pool (e.g., database buffer pools).</p><h3>3) Multi-threaded (one process, many threads)</h3><ul><li>Threads share memory; typically <strong>lower memory overhead</strong> than multiple full processes.</li><li>Uses multiple cores effectively, but increases risk of race conditions and lock contention.</li><li>Examples: <strong>Apache</strong>, <strong>Envoy</strong>, <strong>SQL Server</strong>.</li></ul><h3>I/O-bound vs CPU-bound</h3><p>Performance depends on whether the workload is primarily:</p><ul><li><strong>CPU-bound:</strong> heavy computation (crypto, hashing, compression, etc.)</li><li><strong>I/O-bound:</strong> waiting on disk/network/memory access</li></ul><p>This affects whether adding threads/processes improves throughput.</p><h3>Redis example: async snapshots (fork + copy-on-write)</h3><p>Problem: Redis wants to write an <strong>on-disk snapshot</strong> while continuing to serve writes.</p><p>Naive options:</p><ul><li>Snapshot while allowing writes → inconsistent snapshot.</li><li>Pause writes during snapshot → unacceptable latency/throughput hit.</li></ul><p>Solution:</p><ul><li><strong>Fork</strong> a child process to write the snapshot.</li><li>Use OS <strong>Copy-on-Write (CoW)</strong>:</li><li>Parent and child initially share the same pages.</li><li>Only pages that get modified in the parent are copied.</li><li>Child sees a consistent “as-of-fork” view while writing the snapshot.</li></ul><h3>Thread contention example: SQL Server clustered primary key inserts</h3><p>Scenario:</p><ul><li>Table has a <strong>clustered primary key</strong> (often increasing auto-increment).</li><li>Many concurrent inserts arrive.</li><li>Inserts must land near the <strong>end (“tail”) pages</strong> of the clustered index.</li><li>Multiple threads try to modify the <strong>same memory/page</strong>, so they must acquire locks.</li></ul><p>Result:</p><ul><li>High lock contention → reduced write throughput.</li></ul><p>Mitigation idea discussed:</p><ul><li>Batching inserts such that fewer threads compete for the same tail page (e.g., fewer concurrent writers / larger batches per writer). SQL Server parallelism settings (e.g., MAXDOP) can influence how many worker threads participate.</li></ul><h3>How many processes/threads is “too many”?</h3><ul><li>If you have <strong>N cores</strong> (or hardware threads), a common rule of thumb is to run about <strong>N worker processes/threads</strong> for CPU-heavy work.</li><li>Creating far more runnable workers than cores can increase:</li><li><strong>context switching</strong></li><li>scheduling overhead</li><li>contention</li></ul><p>The OS scheduler must continuously decide which runnable task gets CPU time, and excessive runnable workers can degrade performance rather than improve it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2002ccad6a17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>