<?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 Mohamad on Medium]]></title>
        <description><![CDATA[Stories by Mohamad on Medium]]></description>
        <link>https://medium.com/@mahdi.com.haidar?source=rss-3529f5e2cee3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*f-4cHSTBmDK2U6Pw0pE3hw.jpeg</url>
            <title>Stories by Mohamad on Medium</title>
            <link>https://medium.com/@mahdi.com.haidar?source=rss-3529f5e2cee3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:34:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mahdi.com.haidar/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[Pre-Production Performance Checklist]]></title>
            <link>https://medium.com/@mahdi.com.haidar/pre-production-performance-checklist-summary-cbc2327059a8?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbc2327059a8</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[load-testing]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Fri, 07 Aug 2026 21:51:49 GMT</pubDate>
            <atom:updated>2026-08-07T21:54:06.138Z</atom:updated>
            <content:encoded><![CDATA[<p>These are few performance-checks you should consider running before deploying a new release.</p><h3>Key Takeaways</h3><ul><li><strong>Core Purpose:</strong> To evaluate whether a software change will make a system slower, less scalable, or more resource-intensive under realistic traffic.</li><li><strong>Standardized Structure:</strong> Every potential failure is evaluated by its primary failure mode, the specific test shape required to catch it, its affected secondary resource, and the resulting user impact.</li></ul><h3>Core Components</h3><h3>1. Common Load Test Shapes</h3><p>The guide defines 17 specific load-testing profiles to expose targeted system bottlenecks (rather than just generically increasing traffic):</p><ul><li><strong>Scale &amp; Volume:</strong> Gradual, Peak, Burst, Spike, Sustained, Payload scaling, Data volume scaling.</li><li><strong>State &amp; Reliability:</strong> Soak (hours long), Concurrent, Mixed CPU+I/O, Cold cache, Cache expiry burst, Hot key load, Queue overload, large file/data workload.</li><li><strong>Dependency Reliability:</strong> Dependency latency injection, Dependency failure injection.</li></ul><h3>2. Categorized Failure Scenarios</h3><p>The checklist breaks down common performance traps across <strong>10 technical domains</strong>:</p><ul><li><strong>CPU-Bound Work:</strong> High-cost compute like file parsing, video transcoding, or heavy batching (watch for processing time and peak memory).</li><li><strong>Memory Pressure:</strong> Inefficient allocations, large payloads, or unbounded caches (watch for GC pauses, heap growth, and OOM crashes).</li><li><strong>Database Performance:</strong> N+1 queries, deep pagination, missing indexes, and write contention on hot rows.</li><li><strong>Connection Pools:</strong> Exhaustion of reusable client/backend connections caused by high request volume, slow database/cache writes or reads, slow external API calls, or long-running transactions.</li><li><strong>Thread/Worker Starvation:</strong> Blocked workers causing high latency even while CPU usage remains low.</li><li><strong>Network &amp; Payload Inefficiency:</strong> Excessive serialization, chatty microservices (fan-out), and bloated response sizes.</li><li><strong>Disk/Storage I/O:</strong> Storage bottlenecks caused by heavy logging, file exports, or database disk scans.</li><li><strong>Cache Failures:</strong> Cold caches after deploys, stampedes upon TTL expiration, and hot keys overwhelming specific shards.</li><li><strong>Queue &amp; Backlog Issues:</strong> Job producers outpacing background consumers, retry storms, and message bloat.</li><li><strong>Nonlinear Scaling:</strong> Systems where small increases in data size cause exponential increases in latency/cost (super-linear growth curves).</li></ul><h3>3. Secondary Resource Classification Reference</h3><p>Categorizes resource bottlenecks to help identify the primary fix target: <strong>CPU, Memory, Disk I/O, Network I/O, Threads/Workers, Connection Pools, DB CPU, DB I/O, DB Locks, Cache Capacity, Queue Capacity,</strong> and <strong>External Dependencies</strong>.</p><h3>Quick Decision Mapping</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Udn0efO8Y7WtQjjR9GVi-w.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbc2327059a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide to Azure AD: Client Apps, Backend APIs, Permissions, Scopes, Audience, and Claims]]></title>
            <link>https://medium.com/@mahdi.com.haidar/a-guide-to-azure-ad-client-apps-backend-apis-permissions-scopes-audience-and-claims-8aefba71c1ff?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/8aefba71c1ff</guid>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[azure-ad]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[token]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Sun, 24 May 2026 14:13:31 GMT</pubDate>
            <atom:updated>2026-05-24T15:11:12.122Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Please note that Azure AD has been renamed to Microsoft Entra ID. However, I still prefer the old name, so I’ll be using it throughout this article.</em></p><p>Implementing authentication and authorization in Azure Active Directory (Azure AD / Microsoft Entra ID) comes down to understanding how applications communicate using tokens.</p><p>Before diving into the architecture, let’s look at a concrete example of what an Azure AD Access Token actually looks like and how it drives the security process.</p><h3>1. Example of an Azure AD Token</h3><p>When a client application successfully authenticates, Azure AD issues a cryptographically signed JSON Web Token (JWT). The backend API decodes this token to read key-value pairs called <strong>Claims</strong>.</p><p>An abstract representation of a decoded token payload looks like this:</p><pre>{<br>  &quot;aud&quot;: &quot;api://my-backend-api-id&quot;,<br>  &quot;iss&quot;: &quot;https://sts.windows.net/your-tenant-id/&quot;,<br>  &quot;scp&quot;: &quot;reports.read reports.write&quot;,<br>  &quot;upn&quot;: &quot;user@company.com&quot;,<br>  &quot;exp&quot;: 1785234200<br>}</pre><h4>How Authentication and Authorization Work via the Token:</h4><ul><li><strong>Authentication (Who is calling?):</strong> The token itself proves identity. The iss (Issuer) claim verifies that the token was genuinely created and digitally signed by your trusted Azure AD tenant. The upn (User Principal Name) identifies exactly which user is logged in.</li><li><strong>Authorization (What are they allowed to do?):</strong> The backend API reads the aud (Audience) claim to ensure the token was meant for <em>this</em> specific API and checks the scp (Scopes) claim to see what specific actions (reports.read, reports.write) the client is permitted to perform.</li></ul><h3>2. Real-World Applications vs. Azure AD Applications</h3><p>There is a clear difference between your actual software code and its identity configuration in the cloud:</p><ul><li><strong>The Real-World Application:</strong> This is the actual program running on a server or a browser, such as a React frontend website or a .NET Core Web API.</li><li><strong>The Azure AD Application (App Registration):</strong> This is a digital registration profile inside your Azure portal. It represents your real-world application in the identity system, allowing Azure AD to manage its login rules, secret keys, and permissions.</li></ul><h3>3. Client Applications vs. Backend Applications</h3><p>In most setups, your architecture is split into two distinct roles:</p><ul><li><strong>The Client App:</strong> The application that requests access to resources (e.g., a mobile app or a website frontend). It handles user login and requests tokens from Azure AD.</li><li><strong>The Backend App:</strong> The API or service that holds and protects the data. It receives requests from the Client App and verifies the incoming token using Azure AD rules before allowing access.</li></ul><h3>4. Permissions and Scopes</h3><p>To control what a Client App can do inside a Backend App, you use <strong>Scopes</strong>.</p><ul><li>Instead of giving full access, the Backend App defines a list of narrow permissions (scopes) like read, write, or delete in the Azure Portal under <strong>&quot;Expose an API&quot;</strong>.</li><li>The Client App is then assigned a specific subset of these scopes under <strong>“API Permissions”</strong>.</li><li>Before the Client App can successfully get a token containing these scopes, a user or a tenant administrator must grant <strong>Consent</strong> (approval).</li></ul><h3>5. Audience, Scopes, and Claims Explained</h3><p>When working with tokens in your backend code, you will interact with these three main pieces of data:</p><ul><li><strong>Audience (</strong><strong>aud):</strong> The intended recipient of the token. It points to the Backend App. The backend must always check this; if the aud doesn’t match its own ID, it rejects the request.</li><li><strong>Scopes (</strong><strong>scp):</strong> The permissions the Client App has been granted to use on the backend.</li><li><strong>Claims:</strong> All the data pieces packed inside the token (including audience, scopes, expiration time, and user details). The backend reads these claims to make decisions.</li></ul><h3>6. How the Backend Decodes and Verifies the Token</h3><p>When an HTTP request arrives with a token in the header (Authorization: Bearer &lt;token&gt;), the backend middleware (like ASP.NET Core JWT Bearer authentication or Node.js Passport-Azure-AD) processes it automatically.</p><p>Behind the scenes, the backend library performs three critical verification steps:</p><ul><li><strong>Cryptographic Validation (Signature Check):</strong> A JWT is composed of three sections separated by dots: Header.Payload.Signature. The backend downloads Azure AD&#39;s public signing keys via a secure, well-known endpoint. It uses these keys to mathematically verify that the token was signed by Azure AD and hasn&#39;t been altered during transit.</li><li><strong>Timing Check:</strong> It reads the exp (expiration time) and nbf (not before) claims to confirm that the token is currently valid and has not expired.</li><li><strong>Policy and Logic Checks:</strong> Once the library confirms the token is structurally authentic, the application code inspects the claims for authorization:</li><li><strong>Audience Check (Explained below in more details):</strong> It matches the aud value against its own registered Application ID URI.</li><li><strong>Scope Check (Explained below in more details):</strong> It parses the scp string to confirm the required scope for the endpoint is present.</li></ul><h4>The Audience (aud) Check</h4><p>The audience check is a strict <strong>string matching</strong> security guard. It ensures a token intended for one app cannot be stolen and used against another app.</p><ul><li><strong>What the token contains:</strong> “aud”: “api://my-backend-api-id”</li><li><strong>What your backend code knows:</strong> You configure your backend middleware (like ASP.NET, Spring Boot, or Node.js) with your API’s unique identity: <em>options.Audience = &quot;api://my-backend-api-id&quot;;</em></li><li><strong>The comparison logic:</strong> When a request arrives, the backend compares the two strings: <em>Is Token &quot;aud&quot; (&quot;api://my-backend-api-id&quot;) == Configured Audience (&quot;api://my-backend-api-id&quot;)? -&gt; YES. Proceed.</em></li></ul><p><strong>Why is the Audience (</strong><strong>aud) Check</strong> <strong>critical?</strong></p><p>If a malicious or misconfigured client gets a token meant for a completely different service (for example, a public email API with <em>&quot;aud&quot;: &quot;https://graph.microsoft.com&quot;)</em>, and tries to send that token to <em>your</em> API, your backend will run the comparison: <em>Is “https://graph.microsoft.com&quot; == “api://my-backend-api-id”?</em> -&gt; <strong>NO</strong>! The backend immediately throws an HTTP 401 Unauthorized and stops execution right there.</p><h4>The Scope (scp) Check</h4><p>Once the backend confirms the token is genuinely meant for itself (Audience passes), it checks <strong>what specific actions</strong> the client is allowed to perform inside the application.</p><ul><li><strong>What the token contains:</strong> Azure AD bundles all approved permissions into a single space-separated string inside the scp claim:&quot;scp&quot;: &quot;reports.read reports.write&quot;</li><li><strong>What your backend code expects:</strong> In your source code, you protect individual endpoints (routes) by specifying the exact scope string required to access them. For example, on your GET /api/reports endpoint, you add a rule: [Authorize(Scopes = &quot;reports.read&quot;)]</li></ul><p><strong>The scope comparison logic:</strong></p><ul><li>The backend’s authorization middleware takes the raw scp string, splits it by spaces into an array [&quot;reports.read&quot;, &quot;reports.write&quot;], and looks for a match:Does the array [&quot;reports.read&quot;, &quot;reports.write&quot;] contain &quot;reports.read&quot;? -&gt; YES. Allow the user to run the code inside the GET endpoint.</li><li>If the client tries to call a different endpoint — like DELETE /api/reports which requires the reports.delete scope—the check fails: Does the array [&quot;reports.read&quot;, &quot;reports.write&quot;] contain &quot;reports.read&quot;? -&gt; No. The backend immediately returns 403 Forbidden response, meaning: &quot;I know who you are, but you do not have the specific permissions required to execute this operation&quot;.</li></ul><h4>Visually Putting it Together in the Backend Flow:</h4><pre>Incoming HTTP Request with Token<br>               │<br>               ▼<br>┌──────────────────────────────┐<br>│  1. Cryptographic Validation │ ---&gt; Checks signature and expiration<br>└──────────────┬───────────────┘<br>               │ Pass<br>               ▼<br>┌──────────────────────────────┐<br>│      2. Audience Check       │ ---&gt; Checks if token&#39;s &quot;aud&quot; matches <br>└──────────────┬───────────────┘      this API&#39;s Application ID URI<br>               │ Pass<br>               ▼<br>┌──────────────────────────────┐<br>│       3. Scope Check         │ ---&gt; Splits &quot;scp&quot; string and checks if the<br>└──────────────┬───────────────┘      required scope for this endpoint exists<br>               │ Pass<br>               ▼<br> Execute Endpoint Controller Code</pre><h3>7. The Complete Step-by-Step Flow (Putting It All Together)</h3><p>Here is exactly how the entire ecosystem works at runtime during a live request:</p><ul><li><strong>Login:</strong> The user opens the Client App (e.g., a React website) and clicks login. They are redirected to Azure AD to input credentials.</li><li><strong>Token Request:</strong> Once logged in, the Client App asks Azure AD for an Access Token, requesting specific scopes (like reports.read) defined by the Backend API.</li><li><strong>Token Issuance:</strong> Azure AD checks its configuration. If the Client App has permission and consent is granted, Azure AD bundles the aud, scp, and user details into a payload, signs it, and returns the encrypted token string to the Client App.</li><li><strong>API Call:</strong> The Client App attaches this token string to the HTTP request:</li><li>HTTPGET /api/reports HTTP/1.1 Host: api.company.com Authorization: Bearer eyJhbGciOiJSUzI1NiIs...</li><li><strong>Backend Verification:</strong> The Backend API intercepts the call:</li><li>It verifies the <strong>Signature</strong> using Azure AD’s public keys.</li><li>It checks the <strong>Expiration</strong> (exp).</li><li>It confirms the <strong>Audience</strong> (aud) belongs to this API.</li><li><strong>Authorization Check:</strong> The API controller checks the scopes: <em>Does the </em><em>scp claim contain </em><em>reports.read?</em> Yes.</li><li><strong>Response:</strong> The backend executes the request and returns the data securely. If any check in steps 5 or 6 had failed, the backend would immediately stop execution and return an HTTP 401 Unauthorized or 403 Forbidden response.</li></ul><h3>8. How to Configure Different Permissions for Different Clients</h3><p>If you have multiple client apps (like a mobile app for customers and an internal admin dashboard), you should not give them the same access level. You configure this in Azure AD like this:</p><ul><li><strong>Define Scopes:</strong> In the Backend App registration, go to <strong>“Expose an API”</strong> and create separate scopes (e.g., Data.Read and Data.Admin).</li><li><strong>Assign Permissions:</strong> In the Client App registration for the customer app, go to <strong>“API Permissions”</strong> and add <em>only</em> the Data.Read scope. For the admin tool client, add the Data.Admin scope.</li><li><strong>Admin Consent:</strong> If a scope is sensitive, enable the option that requires an Administrator to approve it before it can be used.</li></ul><h3>Conclusion</h3><p>Azure AD applications act as a bridge between your physical code and the identity platform. By defining clear boundaries using Client Apps, Backend APIs, and explicit scopes, you ensure that every application component only accesses the exact data it needs to function.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8aefba71c1ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Task vs ValueTask in .NET: What They Are, When to Use Each, and Why It Matters]]></title>
            <link>https://medium.com/@mahdi.com.haidar/task-vs-valuetask-in-net-what-they-are-when-to-use-each-and-why-it-matters-e8e663c3cf74?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8e663c3cf74</guid>
            <category><![CDATA[valuetask]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Wed, 20 May 2026 22:12:46 GMT</pubDate>
            <atom:updated>2026-05-21T23:25:28.191Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A deep dive into one of async/await’s most misunderstood distinctions — with real .NET examples to make it click.</em></p><h3>The Short Answer Nobody Gives You</h3><p>If you’ve been writing async C# for a while, you’ve almost certainly used Task and Task&lt;T&gt; everywhere — and that&#39;s fine. But at some point you&#39;ve probably stumbled across ValueTask or ValueTask&lt;T&gt; in .NET source code or a StackOverflow answer and wondered: <em>why does this exist, and should I be using it?</em></p><p>The short answer:</p><ul><li><strong>Task</strong> allocates a heap object every time it&#39;s returned from an async method — even if the result is already available.</li><li><strong>ValueTask</strong> is a value type (a struct) that can return a result <em>without</em> allocating when the result is already cached or synchronously available.</li></ul><p>That’s the core trade-off. But understanding <em>when</em> that matters — and when it doesn’t — is where things get interesting.</p><h3>A Quick Refresher: What Is a Task?</h3><p>Task and Task&lt;T&gt; represent asynchronous operations in .NET. They were introduced in .NET 4.0 and are the backbone of the async/await model.</p><pre>public async Task&lt;string&gt; FetchDataAsync()<br>{<br>    var client = new HttpClient();<br>    var result = await client.GetStringAsync(&quot;https://example.com&quot;);<br>    return result;<br>}</pre><p>Every time this method runs, the C# compiler and runtime create a state machine and — crucially — allocate a Task&lt;string&gt; object on the heap. This is fine for most cases. Allocation is cheap in .NET, and the GC handles it well.</p><p>But when you’re writing <strong>hot-path code</strong> — methods called millions of times per second, like in high-throughput APIs or game loops — those allocations add up. This is exactly the problem ValueTask was designed to solve.</p><h3>Enter ValueTask</h3><p>ValueTask&lt;T&gt; was introduced in .NET Core 2.0 (and the System.Threading.Tasks.Extensions NuGet package for older targets). It&#39;s a struct, not a class.</p><pre>public async ValueTask&lt;string&gt; FetchCachedDataAsync()<br>{<br>    if (_cache.TryGetValue(&quot;key&quot;, out var cached))<br>        return cached; // No allocation! Returns synchronously.<br>    var result = await SomeDatabaseCallAsync();<br>    _cache[&quot;key&quot;] = result;<br>    return result;<br>}</pre><p>Look at the above example, when the cache has the value, this method returns immediately with <strong>zero heap allocation</strong>. When it doesn’t, it falls back to the Task-like async path — which does allocate, but only when truly needed.</p><p>This is the fundamental insight: <strong>ValueTask is an optimization for the case where your method often completes synchronously.</strong></p><h3>The Simplest Example: Task.FromResult vs ValueTask</h3><p>If you’ve written synchronous-result async methods before, you’ve probably used Task.FromResult. It&#39;s the classic way to return an already-known value from a Task-returning method:</p><pre>public Task&lt;int&gt; GetAnswerAsync()<br>{<br>    return Task.FromResult(42); // Allocates a new Task&lt;int&gt; object on the heap<br>}</pre><p>This works perfectly — but it allocates a brand new Task&lt;int&gt; object on the heap every single time it&#39;s called, even though the value 42 was sitting right there. There&#39;s no async work happening at all, yet you&#39;re still paying the allocation cost.</p><h3>Microsoft’s Own Workaround: Internal Caching in Task.FromResult</h3><p>Microsoft felt this pain too. Starting with .NET 5, Task.FromResult was quietly given an internal optimization: for a small set of very common values, it returns a <strong>pre-allocated, cached </strong><strong>Task&lt;T&gt;</strong> instead of creating a new one every time:</p><pre>Task.FromResult(true)   // ✅ Returns a cached Task&lt;bool&gt; — no allocation<br>Task.FromResult(false)  // ✅ Returns a cached Task&lt;bool&gt; — no allocation<br>Task.FromResult(0)      // ✅ Returns a cached Task&lt;int&gt; — no allocation<br>Task.FromResult(-1)     // ✅ Returns a cached Task&lt;int&gt; — no allocation<br>Task.FromResult(42)     // ❌ Not cached - allocates a new Task&lt;int&gt; every time<br>Task.FromResult(&quot;hi&quot;)   // ❌ Not cached - allocates a new Task&lt;string&gt; every time</pre><p>This was a clever band-aid — but notice how limited it is. It only works for a handful of hardcoded values that Microsoft predicted would be common. The moment your result is anything outside that narrow list, you’re back to allocating on every call. It’s a workaround, not a solution. And it tells you something important: <strong>even the .NET team recognized this was a real problem worth solving at the framework level.</strong></p><h3>ValueTask: The Proper General Solution</h3><p>ValueTask&lt;T&gt; is what Task.FromResult was always trying to be — a way to return a synchronous result without paying the allocation cost, for <em>any</em> value, not just a privileged few:</p><pre>public ValueTask&lt;int&gt; GetAnswerAsync()<br>{<br>    return new ValueTask&lt;int&gt;(42); // Zero allocation — works for any value<br>}</pre><p>Both methods are awaitable. Both return 42. The difference is invisible to the caller:</p><pre>var answer = await GetAnswerAsync(); // Works identically either way</pre><p>But under the hood, the Task.FromResult(42) version creates a new heap object the GC has to eventually collect. The ValueTask version is a stack-allocated struct that simply disappears when it goes out of scope — no GC involvement at all.</p><p>The progression tells the story clearly: Task.FromResult with its internal cache was the workaround. ValueTask is the language and runtime feature that made the workaround unnecessary.</p><h3>Side-by-Side Example: Seeing the Difference</h3><h4>Caching with a Repository</h4><p>Here’s a classic scenario where the choice matters.</p><p><strong>With </strong><strong>Task (allocates even on cache hit):</strong></p><pre>public class UserRepository<br>{<br>    private readonly Dictionary&lt;int, User&gt; _cache = new();<br>    public async Task&lt;User&gt; GetUserAsync(int id)<br>    {<br>        if (_cache.TryGetValue(id, out var cached))<br>            return cached; // Still wraps in a Task under the hood<br>        var user = await _database.QueryAsync&lt;User&gt;(&quot;SELECT * FROM Users WHERE Id = @Id&quot;, id);<br>        _cache[id] = user;<br>        return user;<br>    }<br>}</pre><p>Even though return cached looks like it returns immediately, the async state machine still allocates a completed Task&lt;User&gt; to hold the result.</p><p><strong>With </strong><strong>ValueTask (zero allocation on cache hit):</strong></p><pre>public class UserRepository<br>{<br>    private readonly Dictionary&lt;int, User&gt; _cache = new();<br>    public ValueTask&lt;User&gt; GetUserAsync(int id)<br>    {<br>        if (_cache.TryGetValue(id, out var cached))<br>            return new ValueTask&lt;User&gt;(cached); // Pure struct, no heap allocation<br>        return new ValueTask&lt;User&gt;(FetchFromDatabaseAsync(id));<br>    }<br>    private async Task&lt;User&gt; FetchFromDatabaseAsync(int id)<br>    {<br>        var user = await _database.QueryAsync&lt;User&gt;(&quot;SELECT * FROM Users WHERE Id = @Id&quot;, id);<br>        _cache[id] = user;<br>        return user;<br>    }<br>}</pre><p>Notice the pattern: the synchronous fast path returns new ValueTask&lt;User&gt;(value) directly, while the slow async path delegates to a private Task-returning method. This is idiomatic ValueTask usage.</p><p>In .NET, “idiomatic” means following the recognized best practice for a specific feature. For ValueTask, this means splitting the method so that the fast path remains a normal synchronous function without the async keyword, completely avoiding the overhead of the compiler’s async state machine unless an actual asynchronous operation is required.</p><h3>The Rules: When to Use Each</h3><h3>Use Task / Task&lt;T&gt; when:</h3><ul><li>The method almost always completes asynchronously (I/O, network, database)</li><li>You’re writing public library APIs consumed by unknown callers</li><li>The method is awaited multiple times (see the caveat below)</li><li>You need to store the result, pass it around, or check .IsCompleted later</li></ul><h3>Use ValueTask / ValueTask&lt;T&gt; when:</h3><ul><li>The method frequently returns synchronously (caching, in-memory checks)</li><li>You’re implementing IAsyncEnumerator&lt;T&gt;.MoveNextAsync()</li><li>You’re writing performance-critical infrastructure code (e.g., serializers, protocol parsers, networking layers)</li><li>Benchmarking or profiling shows Task allocations are a measurable bottleneck</li></ul><h3>The Critical Caveat: ValueTask Can Only Be Awaited Once</h3><p>This is the most important rule about ValueTask — and the source of the most subtle bugs.</p><p><strong>You MUST NOT do any of the following with a </strong><strong>ValueTask:</strong></p><pre>// ❌ WRONG: Awaiting a ValueTask more than once<br>var vt = GetValueAsync();<br>var result1 = await vt;<br>var result2 = await vt; // Undefined behavior!<br>// ❌ WRONG: Awaiting concurrently<br>var vt = GetValueAsync();<br>await Task.WhenAll(vt.AsTask(), vt.AsTask()); // Don&#39;t do this either<br>// ❌ WRONG: Storing and awaiting later from multiple places<br>_storedValueTask = GetValueAsync();<br>// ... later ...<br>var r = await _storedValueTask; // May already be consumed</pre><p>Task is a class, so its result is cached on the object — you can safely await it multiple times, store it, pass it around. ValueTask is a struct that wraps either a value or a pooled IValueTaskSource. Once consumed, it may be recycled by the pool.</p><p>For a closer look at how this works under the hood, read the companion article: <a href="https://medium.com/p/a9d0e909fb5b">Understanding C# ValueTask Without the Brain Melt</a></p><p><strong>If you need to await multiple times, convert explicitly:</strong></p><pre>var vt = GetValueAsync();<br>var task = vt.AsTask(); // Now safe to await multiple times<br>var result1 = await task;<br>var result2 = await task; // ✅ Fine</pre><h3>Real-World Examples: When Does Microsoft Use ValueTask vs. Task?</h3><p>Looking at how the .NET team designed the framework gives us the perfect rule of thumb:</p><ul><li>If data is often <strong>already waiting in memory</strong>, they use ValueTask.</li><li>If data <strong>always requires a slow trip</strong> to a disk or network, they stick to Task.</li></ul><h4>Where Microsoft Uses ValueTask (High-Speed Data)</h4><p>These APIs handle massive amounts of data and need to run completely “allocation-free” whenever possible.</p><p>1. Reading Network Data (PipeReader.ReadAsync)</p><ul><li><strong>The Code:</strong> public override ValueTask&lt;ReadResult&gt; ReadAsync(...)</li><li><strong>Why </strong><strong>ValueTask?</strong> When streaming data over a network, data arrives in chunks and gets stored in a local buffer (temporary memory). When you call ReadAsync, the data is <strong>usually already sitting right there in the buffer</strong>. Because it completes instantly, ValueTask saves millions of memory allocations.</li></ul><p>2. Looping through Async Lists (IAsyncEnumerator.MoveNextAsync)</p><ul><li><strong>The Code:</strong> ValueTask&lt;bool&gt; MoveNextAsync();</li><li><strong>Why </strong><strong>ValueTask?</strong> This is the engine behind await foreach loops. If you loop through 10,000 items pulled from a database or API, a standard Task would force your computer to create 10,000 tiny tracking objects in memory just to move to the next item. ValueTask makes looping practically free.</li></ul><p>3. High-Performance Web Servers (Socket.ReceiveAsync)</p><ul><li><strong>The Code:</strong> public ValueTask&lt;int&gt; ReceiveAsync(...)</li><li><strong>Why </strong><strong>ValueTask?</strong> Web servers like Kestrel (which powers ASP.NET Core) handle millions of requests per second. They absolutely cannot afford to create a fresh Task object for every single byte of data that hits the server.</li></ul><h4>Where Microsoft Sticks to Task (The Safe Default)</h4><p>In these two scenarios, the operation <strong>never</strong> completes instantly. There is no “cached memory shortcut,” so a standard Task is the right choice.</p><p>1. Fetching Websites (HttpClient.GetStringAsync)</p><ul><li><strong>The Code:</strong> public Task&lt;string&gt; GetStringAsync(...)</li><li><strong>Why Standard </strong><strong>Task?</strong> An HTTP web request <em>always</em> has to travel across the internet, hit a remote server, and travel back. It will never finish instantly, making ValueTask useless here.</li></ul><p>2. Database Queries (DbCommand.ExecuteReaderAsync)</p><ul><li><strong>The Code:</strong> public virtual Task&lt;DbDataReader&gt; ExecuteReaderAsync();</li><li><strong>Why Standard </strong><strong>Task?</strong> Talking to a database always involves out-of-process communication or disk access. It always takes real time, so a standard Task is appropriate.</li></ul><h4>The Golden Rule Summary:</h4><blockquote><em>Use </em><strong><em>ValueTask</em></strong><em> if your async method is called enormously and will </em><strong><em>frequently find its data already waiting in memory</em></strong><em>.</em></blockquote><blockquote><em>Use </em><strong><em>Task</em></strong><em> for everything else. It is safer, easier to use, and perfectly fine for standard network/disk operations.</em></blockquote><h3>A Quick Mental Model</h3><p>Here’s how they compare across the dimensions that matter most:</p><p><strong>Task&lt;T&gt;</strong></p><ul><li>Reference type (class) — lives on the heap</li><li>Always allocates, even when the result is already available</li><li>Safe to await multiple times</li><li>Safe to store, pass around, and reuse</li><li>The right default for the vast majority of async methods</li><li>Used by IAsyncEnumerable&lt;T&gt; (the outer enumerable interface)</li></ul><p><strong>ValueTask&lt;T&gt;</strong></p><ul><li>Value type (struct) — avoids heap allocation on the synchronous path</li><li>Only allocates when the operation is genuinely async</li><li>Can only be awaited once — consuming it again is undefined behavior</li><li>Risky to store or share; treat it as single-use</li><li>For hot-path optimization only, not a blanket replacement for Task</li><li>Required by IAsyncEnumerator&lt;T&gt;.MoveNextAsync() (the per-iteration interface)</li></ul><h4>Summary</h4><p>Task is the right default. It&#39;s safe, composable, and well-understood. Use it for anything involving genuine I/O — HTTP calls, database queries, file operations.</p><p>ValueTask is a targeted optimization. Use it when your method has a fast synchronous path that&#39;s exercised frequently, especially in infrastructure code or when implementing async interfaces like IAsyncEnumerator&lt;T&gt;. Just remember: await it once, don&#39;t store it, and don&#39;t share it.</p><p>The goal isn’t to replace every Task with ValueTask. It&#39;s to use ValueTask precisely where allocations are a demonstrable problem — and to let Task carry the rest with its simplicity and safety.</p><p><em>If you found this useful, consider following for more deep dives into .NET internals and async patterns. Got questions or a scenario you’re not sure about? Drop them in the comments.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8e663c3cf74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding C# ValueTask Without the Brain Melt]]></title>
            <link>https://medium.com/@mahdi.com.haidar/understanding-c-valuetask-without-the-brain-melt-a9d0e909fb5b?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9d0e909fb5b</guid>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[valuetask]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Wed, 20 May 2026 21:52:32 GMT</pubDate>
            <atom:updated>2026-05-20T21:52:32.312Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve been diving into modern C# performance optimization, you’ve likely run into ValueTask. You’ve probably also read the terrifying warning in the official documentation:</p><blockquote><strong><em>“A ValueTask should only be awaited exactly once.”</em></strong></blockquote><p>But why? What happens behind the scenes if you break that rule? And why does a simple test method returning a hardcoded number seemingly break this law with zero consequences?</p><p>Let’s break it down using simple analogies, visual flows, and zero academic fluff.</p><h3>1. The Core Analogy: YouTube vs. Snapchat</h3><p>To understand why ValueTask exists, we first have to look at the standard Task.</p><ul><li><strong>Standard </strong><strong>Task is like a downloadable YouTube movie.</strong> Once it’s downloaded, it sits in your device&#39;s memory. You can watch it, pause it, rewind it, watch it a second time, or share it with a friend. It stays there until you explicitly delete it.</li><li><strong>ValueTask is like a Snapchat video.</strong> You open it, you watch it <em>exactly once</em>, and then <em>poof</em>—it’s gone.</li></ul><p>If you try to view a Snapchat video a second time after it disappears, the app doesn’t let you. If you try to force a ValueTask to execute a second time, your program behaves wildly unpredictably or completely crashes.</p><h3>Why did C# creators make a “Snapchat” version?</h3><p>Memory efficiency. Every time you create a regular Task, your computer has to allocate a tiny piece of tracking memory on the <strong>heap</strong> (the computer&#39;s long-term scratchpad).</p><p>If you have a high-throughput application running functions thousands of times a second, those tiny memory allocations pile up like junk mail. The computer has to pause or slow down to clean them up (a process called <strong>Garbage Collection</strong>). ValueTask was invented to be lightweight and skip that junk mail creation entirely.</p><h3>2. The Plot Twist: When the Rule Safely “Breaks”</h3><p>If you try to test this “await once” rule using a basic mock method, you might run into a confusing scenario. Consider this code:</p><pre>async ValueTask&lt;int&gt; GetConstantValueAsync() <br>{<br>    return 10; <br>}<br>// Elsewhere in your code...<br>var vTask = GetConstantValueAsync();<br>await vTask; // Await 1<br>await vTask; // Await 2<br>await vTask; // Await 3... works perfectly 100 times!</pre><p>If the golden rule says we can only await a ValueTask once, why does this code run 100 times without blowing up?</p><h3>The Synchronous Shortcut</h3><p>The rule didn’t break because your code isn’t actually using the asynchronous engine. Because the number 10 is already right there, the method completes <strong>synchronously</strong> (instantly).</p><p>When a ValueTask completes instantly, C# bypasses the complex background tracking machinery entirely. It wraps the raw integer 10 into a tiny, direct data structure and hands it to you. Awaiting a raw, completed value is always safe to do repeatedly because there is no underlying background operation to break or recycle.</p><p>The “await only once” rule specifically triggers when the operation is <strong>actually asynchronous</strong> — like waiting on a network call, a file system read, or a database query.</p><h3>3. Behind the Scenes: The Vending Machine (IValueTaskSource)</h3><p>When a ValueTask actually has to wait on real background work, it utilizes a helper interface called IValueTaskSource.</p><p>Think of <strong>IValueTaskSource</strong> as a <strong>vending machine</strong>, and <strong>ValueTask</strong> as the <strong>ticket</strong> it hands you to claim your snack.</p><p>Here is the exact lifecycle of a proper asynchronous ValueTask operation:</p><pre>[ Your Code ] ---&gt; (Calls Async Method) ---&gt; [ Reusable Vending Machine Pool ]<br>                                                            |<br>                                                   Creates Manager Engine<br>                                                   &amp; Issues Ticket (#42)<br>                                                            |<br>[ Your Code ] &lt;--- &lt;ValueTask Struct (Token:42)&gt; &lt;----------+<br>     |<br>  (Await #1) ---&gt; Hands Ticket to Manager ---&gt; Delivers Data<br>                                                    |<br>                                            Wipes &amp; Recycles Manager Engine<br>                                            Back into the Pool for Re-use!                                            Back into the Pool for Re-use!</pre><h3>The Step-by-Step Flow:</h3><ul><li><strong>The Request:</strong> Your code calls an async method. Behind the scenes, the system pulls an IValueTaskSource manager object out of a reusable object pool to track the background job.</li><li><strong>The Ticket:</strong> The method returns a ValueTask to you. As we are about to see, this structure isn&#39;t a complex object; it is just a tiny receipt pointing back to that manager.</li><li><strong>The Await:</strong> You await the ValueTask. It knocks on the manager&#39;s door, matches your receipt, extracts your data, and <strong>instantly releases the manager back to the pool</strong> so other parts of your app can reuse it.</li></ul><h3>4. What is inside a ValueTask?</h3><p>To see why you can’t just reuse that ticket to pull the data a second time, we have to look at what a ValueTask actually contains under the hood.</p><p>A ValueTask is a tiny, bare-bones struct. It isn&#39;t a living piece of code or an executable function; it’s just a dumb data container holding two things:</p><ul><li><strong>A reference pointer (</strong><strong>_obj):</strong> The address of the IValueTaskSource manager engine.</li><li><strong>A token number (</strong><strong>_token):</strong> A unique serial number for this specific asynchronous run (e.g., 42).</li></ul><p>It looks roughly like this:</p><pre>// Simplification of what&#39;s inside the ValueTask struct<br>public struct ValueTask <br>{<br>    internal object _obj;    // Points to the IValueTaskSource manager<br>    internal short _token;   // The serial number for this specific run<br>}</pre><p>Because a ValueTask is just a historical receipt holding a pointer and a number, <strong>it doesn&#39;t know how to rerun your code.</strong> It only knows how to ask its assigned manager for the result of the work that <em>already happened</em>.</p><h3>5. The Timeline of the Breakage</h3><p>When you call an async method, the system hooks up a manager, sets its internal serial number to 42, and hands you a ValueTask(_obj, token: 42).</p><p>Here is the exact play-by-play of what happens when you try to use it twice:</p><h3>Await #1 (The Legit Run)</h3><p>Your code says await myValueTask;.</p><p>The system reads the ticket, goes to the manager address, and asks: <em>“Hey, do you have the data for token #42?”</em></p><p>The manager verifies the number: <em>“Yep, here is your data!”</em> <strong>The Instant Recycle:</strong> The millisecond that data is handed to your code, the manager wipes its memory clean and resets. It goes straight back into the available pool, waiting for a completely different async method in your app to claim it.</p><h3>Await #2 (The Danger Zone)</h3><p>Your code tries to run await myValueTask; a second time.</p><p>Your ticket <strong>still points to the same manager address and still expects token </strong><strong>#42</strong>. It has no idea the method already wrapped up.</p><p>When your code knocks on the manager’s door this second time, one of two chaotic things happens:</p><ul><li><strong>Scenario A (The System Catches You):</strong> The manager has already been rented out to a completely different method, and its internal serial token has been updated to 43. The manager looks at your ticket, realizes your token (42) is expired, and throws an immediate InvalidOperationException crash.</li><li><strong>Scenario B (The Silent Disaster):</strong> The manager was just rented out to a new method, but by sheer bad luck, its internal token rolled back or matched 42. The manager looks at your ticket, sees the number matches, and unknowingly hands you <strong>the brand-new data belonging to an entirely different part of your application</strong>. Your code now has corrupted data, and no error was thrown to warn you.</li></ul><h3>6. The Summary Cheat Sheet</h3><p>To keep your code stable and high-performing, keep these quick guardrails in mind:</p><p><strong>What is Safe to DoWhat is Dangerous (The Danger Zone)</strong></p><p>await MyValueTaskAsync();</p><p><em>(Awaiting the method call instantly)</em></p><p>var t = MyValueTaskAsync();</p><p>await t;</p><p>await t; <em>(Awaiting the same variable twice)</em></p><p>var result = await t;</p><p><em>(Extracting the value once and reusing the variable)</em></p><p>Task.WhenAll(vTask1, vTask2);</p><p><em>(Passing raw ValueTasks into standard Task combinators)</em></p><h3>The 1-Second Escape Hatch</h3><p>If you ever find yourself in a complex architecture where you absolutely <em>must</em> await an operation multiple times, look at its results later, or pass it into standard task tracking arrays, use the built-in converter method:</p><pre>// Converts the Snapchat video into a permanent downloadable YouTube file<br>Task safeTask = MyValueTaskAsync().AsTask(); <br>await safeTask; // Safe<br>await safeTask; // Still safe!</pre><p>By calling .AsTask(), you explicitly tell C# to move the operation out of the high-speed recycling pool and allocate it as a standard, permanent Task. You lose the slight memory savings, but you gain absolute safety when multi-awaiting is a structural necessity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9d0e909fb5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency & Parallelism in C# — From Threads to TPL]]></title>
            <link>https://medium.com/@mahdi.com.haidar/concurrency-parallelism-in-c-from-threads-to-tpl-ac08a7a2d4c9?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac08a7a2d4c9</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[parallelism]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Wed, 13 May 2026 23:42:47 GMT</pubDate>
            <atom:updated>2026-05-14T22:22:08.224Z</atom:updated>
            <content:encoded><![CDATA[<h3>1. Process vs Thread</h3><p>A <strong>Process</strong> is the running application itself. A <strong>Thread</strong> is a path of execution running inside it.</p><p><strong>Code example:</strong></p><pre>using System;<br>using System.Diagnostics;<br>using System.Threading;<br>// This program, once running, is a Process<br>class Program {<br>    static void Main() {<br>        // Spawning a new Thread (worker) inside this Process<br>        Thread worker = new Thread(() =&gt; {<br>            Console.WriteLine(&quot;I&#39;m a Thread running inside this Process!&quot;);<br>        });<br>        <br>        worker.Start();<br>    }<br>}</pre><h3>2. Multi-threading vs Asynchronous</h3><ul><li><strong>Multi-threading:</strong> Runs code simultaneously by spawning additional OS threads.</li><li><strong>Async (smart waiting):</strong> Frees the current thread from blocking while waiting for data — no new thread needed.</li></ul><h3>Full side-by-side example</h3><pre>using System;<br>using System.Net.Http;<br>using System.Threading;<br>using System.Threading.Tasks;<br>class Program<br>{<br>    static async Task Main()<br>    {<br>        Console.WriteLine($&quot;[Main] Running on Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>        // ───────────────────────────────────────────<br>        // Approach 1: Multi-threading<br>        // ───────────────────────────────────────────<br>        // A brand-new OS thread is created - it has its own stack (~1MB) and creation cost.<br>        // Main won&#39;t freeze, but we&#39;ve &quot;spent&quot; a real thread for the entire wait duration.<br>        Thread t = new Thread(() =&gt;<br>        {<br>            Console.WriteLine($&quot;[Thread] Started on Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>            Thread.Sleep(2000); // simulate heavy work or a network wait<br>            Console.WriteLine($&quot;[Thread] Finished on Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>        });<br>        t.Start();<br>        Console.WriteLine(&quot;[Main] Keeps working while the Thread runs in the background...&quot;);<br>        t.Join(); // wait for the thread to finish<br>        Console.WriteLine();<br>        // ───────────────────────────────────────────<br>        // Approach 2: Async / Await<br>        // ───────────────────────────────────────────<br>        // No new thread! The Main thread is released and can do other work<br>        // while the OS waits for the network response - then resumes right where it left off.<br>        Console.WriteLine($&quot;[Main] Before await - Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>        var loadDataTask = LoadDataAsync(); <br>        Console.WriteLine($&quot;[Main] Do Work While Data Loading&quot;); <br>        await loadDataTask;<br>        Console.WriteLine($&quot;[Main] After await - Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>    }<br>    static async Task LoadDataAsync()<br>    {<br>        Console.WriteLine($&quot;  [Async] Sending request on Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>        using var client = new HttpClient();<br>        // At the await: the thread is released back to the thread pool - nobody is blocked!<br>        string data = await client.GetStringAsync(&quot;https://jsonplaceholder.typicode.com/todos/1&quot;);<br>        // When the response arrives, execution resumes on a thread pool thread<br>        // (may or may not be the same thread ID as before)<br>        Console.WriteLine($&quot;  [Async] Resumed on Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);<br>        Console.WriteLine($&quot;  [Async] First 50 chars: {data[..50]}...&quot;);<br>    }<br>}</pre><h3>Why is Async better than Multi-threading for I/O?</h3><p><strong>Multi-threading:</strong></p><ul><li>Threads used: one new OS thread per operation</li><li>Memory cost: ~1MB stack per thread</li><li>1,000 concurrent requests: 1,000 threads — a disaster!</li><li>Best for: CPU-heavy computation</li></ul><p><strong>Async/Await:</strong></p><ul><li>Threads used: same thread, released during the wait</li><li>Memory cost: nearly zero</li><li>1,000 concurrent requests: handled by a small pool of threads</li><li>Best for: network, database, file I/O</li></ul><blockquote><strong><em>Bottom line:</em></strong><em> Both approaches avoid freezing Main, but async is far more efficient because it doesn’t “waste” a real thread sitting idle during the wait. Imagine 10,000 HTTP requests — with threads you’d need 10,000 of them; with async, a handful is enough.</em></blockquote><h3>3. Concurrency vs Parallelism</h3><ul><li><strong>Concurrency:</strong> Rapidly switching between tasks — like a mother managing her household: washing, cooking, doing the dishes, and looking after the kids, moving between them so quickly it looks like she’s doing everything at once, when in reality she’s switching between them.</li><li><strong>Parallelism:</strong> Truly executing tasks at the exact same moment (like you cooking while a friend washes the dishes — two people, two things simultaneously).</li></ul><h3>Concurrency example — overlapping tasks on one or a few threads</h3><pre>using System;<br>using System.Threading;<br>using System.Threading.Tasks;<br>class ConcurrencyExample<br>{<br>    static async Task Main()<br>    {<br>        Console.WriteLine(&quot;=== Concurrency: launching 3 tasks together without waiting for each ===\n&quot;);<br>        // Fire all three tasks at once - we don&#39;t await each one before starting the next.<br>        // This is concurrency: coordinating tasks that overlap in time.<br>        Task task1 = SimulateWork(&quot;API Request&quot;,  delaySeconds: 3);<br>        Task task2 = SimulateWork(&quot;File Read&quot;,    delaySeconds: 1);<br>        Task task3 = SimulateWork(&quot;DB Query&quot;,     delaySeconds: 2);<br>        Console.WriteLine(&quot;[Main] All three tasks launched - now waiting for all to finish...\n&quot;);<br>        // Wait for all of them - actual execution may be on one thread or a few<br>        await Task.WhenAll(task1, task2, task3);<br>        Console.WriteLine(&quot;\n[Main] All tasks completed!&quot;);<br>        // Notice: total time is ~3s (the slowest task), not 6s (the sum of all)<br>    }<br>    static async Task SimulateWork(string taskName, int delaySeconds)<br>    {<br>        Console.WriteLine($&quot;  [{taskName}] Started... (takes {delaySeconds}s)&quot;);<br>        await Task.Delay(TimeSpan.FromSeconds(delaySeconds)); // simulate I/O wait<br>        Console.WriteLine($&quot;  [{taskName}] ✓ Done after {delaySeconds}s&quot;);<br>    }<br>}<br>// Expected output:<br>//   [API Request] Started... (takes 3s)<br>//   [File Read]   Started... (takes 1s)<br>//   [DB Query]    Started... (takes 2s)<br>// [Main] All three tasks launched - now waiting for all to finish...<br>//   [File Read]   ✓ Done after 1s    ← finishes first<br>//   [DB Query]    ✓ Done after 2s<br>//   [API Request] ✓ Done after 3s<br>// [Main] All tasks completed!         ← after ~3s total, not 6s</pre><h3>Parallelism example — truly using multiple CPU cores at once</h3><pre>using System;<br>using System.Threading;<br>using System.Threading.Tasks;<br>class ParallelismExample<br>{<br>    static void Main()<br>    {<br>        Console.WriteLine(&quot;=== Parallelism: spreading computation across CPU cores ===\n&quot;);<br>        int[] numbers = new int[12]; // 12 items to process<br>        // Parallel.For distributes iterations across available cores automatically.<br>        // Each iteration may run on a different core at the exact same moment.<br>        Parallel.For(0, numbers.Length, i =&gt;<br>        {<br>            int threadId = Thread.CurrentThread.ManagedThreadId;<br>            numbers[i] = HeavyCalculation(i);<br>            Console.WriteLine($&quot;  Item [{i:D2}] = {numbers[i]:D6}  |  Core/Thread: {threadId}&quot;);<br>        });<br>        Console.WriteLine($&quot;\nAll done! Results: {string.Join(&quot;, &quot;, numbers)[..40]}...&quot;);<br>    }<br>    // A CPU-intensive calculation<br>    static int HeavyCalculation(int n)<br>    {<br>        int result = 0;<br>        for (int i = 0; i &lt; 1_000_000; i++)<br>            result += (n + i) % 7;<br>        return result;<br>    }<br>}<br>// Expected output (order varies each run - notice different Thread IDs working simultaneously):<br>//   Item [00] = 142857  |  Core/Thread: 4<br>//   Item [01] = 142858  |  Core/Thread: 6   ← different thread, same moment!<br>//   Item [02] = 142859  |  Core/Thread: 5<br>//   Item [03] = 142860  |  Core/Thread: 4<br>//   ...</pre><h3>Key differences between the two</h3><p><strong>Concurrency:</strong></p><ul><li>What actually happens: tasks interleaved over time</li><li>Threads used: can be a single thread (with async)</li><li>Best for: I/O — network, files, database</li><li>Example: Task.WhenAll for multiple API calls</li></ul><p><strong>Parallelism:</strong></p><ul><li>What actually happens: tasks executing at the exact same instant</li><li>Threads used: multiple cores/threads</li><li>Best for: CPU — computation, image processing, compression</li><li>Example: Parallel.For for data processing</li></ul><h3>4. Task Parallel Library (TPL)</h3><p>TPL makes concurrent programming easier by working with <strong>Tasks</strong> instead of managing raw Threads directly.</p><p><strong>Code example (using Task):</strong></p><pre>using System.Threading.Tasks;<br>class TPL_Example {<br>    static async Task Main() {<br>        // Spin up a task easily<br>        Task&lt;int&gt; task = Task.Run(() =&gt; {<br>            // Some long-running work<br>            return 42; <br>        });<br>        // Do other work here while the task runs...<br>        // Get the result when it&#39;s ready<br>        int result = await task;<br>        Console.WriteLine($&quot;Result: {result}&quot;);<br>    }<br>}</pre><h3>When to use each TPL tool</h3><ul><li><strong>Parallel.ForEach</strong> — you have thousands of items and want maximum throughput (Data Parallelism)</li><li><strong>Task.Run</strong> — you need to run a heavy operation in the background to keep the UI responsive</li><li><strong>Task.WhenAll</strong> — you want to fire off multiple requests simultaneously and wait for all of them</li></ul><blockquote><strong><em>Developer tip: </em></strong><em>Always reach for TPL (Tasks) first — it’s the modern and most efficient way to manage concurrency in .NET. TPL efficiently manages work scheduling and ThreadPool usage for you, reducing the complexity of manual thread management. Only drop down to raw </em><em>Thread in rare, advanced scenarios where you need direct control over thread lifecycle or priority.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac08a7a2d4c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SNAT Port Exhaustion: When Your Machine Runs Out of Breath]]></title>
            <link>https://medium.com/@mahdi.com.haidar/snat-port-exhaustion-when-your-machine-runs-out-of-breath-03308429bc3d?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/03308429bc3d</guid>
            <category><![CDATA[snat]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[networking]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Wed, 13 May 2026 22:12:19 GMT</pubDate>
            <atom:updated>2026-05-13T22:28:16.307Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Everything looks fine — until suddenly, nothing works.</em></p><p>You’ve deployed your application. It’s humming along beautifully. Then, without warning, errors start creeping in:</p><pre>🔴 connection failed<br>🔴 socket exception</pre><p>Your first instinct? <em>The remote server must be down.</em> But you check — it’s perfectly healthy. So what’s going on?</p><p>Welcome to SNAT Port Exhaustion: the silent killer of outbound connections.</p><h3>What Is SNAT, Anyway?</h3><p>Every time your machine sends a request to an external server, it needs to pick a source port to identify that outgoing connection. This happens through a process called SNAT — Source Network Address Translation.</p><p>The operating system assigns a temporary (ephemeral) port from a limited pool. That pool is not unlimited. For any given destination (same IP + port combination), you typically get:</p><ul><li>Around 16,000 ports in a standard setup</li><li>Only ~1,024 SNAT ports per VM if you’re behind an Azure Load Balancer</li></ul><p>That’s it. That’s your budget.</p><h3>What Happens When You Run Out?</h3><p>Here’s the sequence when port exhaustion hits:</p><ol><li>Your application tries to open a new TCP connection</li><li>The OS sends a SYN packet</li><li>It searches for an available source port — and finds none</li><li>The connection fails before it ever reaches the remote server</li></ol><p>The remote server didn’t reject you. You couldn’t even say hello.</p><h3>How to Spot It</h3><p>Port exhaustion doesn’t announce itself with a clear error message. Instead, you’ll see symptoms like:</p><ul><li>Failed to establish connection</li><li>Socket exhaustion</li><li>Intermittent, seemingly random failures on outbound requests</li></ul><p>The randomness is the giveaway. If connections fail inconsistently under load, and the destination server looks healthy, your ports are likely the culprit.</p><h3>Why Cloud Environments Make It Worse</h3><p>On cloud platforms like Azure, traffic often flows through a shared Load Balancer. The LB allocates a fixed number of SNAT ports per VM — and that number can be surprisingly small.</p><p>If your service makes many concurrent outbound connections to the same destination (think: a high-throughput API client, a connection-heavy microservice, or a busy database proxy), you can exhaust your port allocation faster than you’d expect.</p><h3>How to Fix It</h3><h3>Use Connection Pooling</h3><p>Don’t open a new connection for every request. Reuse existing ones. Connection pooling is the single most effective fix — it dramatically reduces how many ports you consume at any given moment.</p><h3>Reduce Connection Timeouts</h3><p>Long-lived idle connections hold onto ports even when they’re doing nothing. Tighten your timeout settings so ports are released promptly.</p><h3>Monitor Your Open Connections</h3><p>Use netstat or ss to see what’s happening in real time:</p><pre># Count established connections by destination<br>ss -s<br># See all connections with port details<br>netstat -an | grep ESTABLISHED | wc -l</pre><p>Catching port pressure early prevents a full exhaustion event.</p><h3>Scale Your Public IPs (Azure-specific)</h3><p>If you’re on Azure and hitting LB SNAT limits:</p><ul><li>Add more Public IPs to your Load Balancer to increase the port pool</li><li>Switch to NAT Gateway — unlike a Load Balancer, which pre-assigns a fixed number of SNAT ports per VM, NAT Gateway dynamically allocates ports on demand. This is the fundamental difference: there’s no static budget per VM to exhaust. It’s Microsoft’s recommended solution for outbound connectivity at scale, and you can attach up to 16 public IPs or a /28 prefix to it if you need even more capacity</li></ul><h3>The Bigger Picture</h3><p>SNAT Port Exhaustion is dangerous precisely because it’s quiet. There’s no crash, no obvious error, no stack trace pointing at the real cause. Your application just… starts failing. Intermittently. Mysteriously.</p><p>The fix isn’t complicated — but you have to know what you’re looking for.</p><p>Three things to take away:</p><ol><li>Monitor how your application opens connections — every one costs a port</li><li>Design for port efficiency — pooling and timeouts aren’t optional, they’re hygiene</li><li>Keep an eye on your network layer — especially in cloud environments where limits are lower than you think</li></ol><h3>Summary:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hEn73SmyT41jBv7l" /></figure><p><em>Have you run into SNAT Port Exhaustion in a production system? What was the hardest part to diagnose? Share your experience in the comments.</em></p><h3>References</h3><ul><li><a href="https://learn.microsoft.com/en-us/azure/nat-gateway/nat-overview">What is Azure NAT Gateway? — Microsoft Learn</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=03308429bc3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Your Program Is Living a Lie About Memory]]></title>
            <link>https://medium.com/@mahdi.com.haidar/your-program-is-living-a-lie-about-memory-draft-9cd5d52e7eed?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cd5d52e7eed</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[memory-management]]></category>
            <category><![CDATA[virtual-memory]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Sat, 09 May 2026 01:07:23 GMT</pubDate>
            <atom:updated>2026-05-09T02:40:12.423Z</atom:updated>
            <content:encoded><![CDATA[<p>Everything you think you know about how your process uses RAM is mediated by a beautifully crafted illusion — Virtual Memory.</p><p>Open Task Manager and look at your app’s memory usage. That number is often treated as “how much RAM my app is using.”</p><p>That number is Working Set — the pages currently loaded in physical RAM. For understanding machine memory pressure, that is exactly what you want. But if you are diagnosing a memory leak or investigating an OutOfMemoryException, Working Set will mislead you. It does not show committed pages the OS has moved to disk, or shared DLL pages attributed to your process. For those diagnostic scenarios, Private Bytes gives you a more honest picture of what your process actually owns.</p><p>It is a snapshot of one corner of a much larger picture.</p><p>That larger picture is Virtual Memory — the system underneath that makes modern process isolation, paging, memory protection, and large address spaces possible.</p><p>Once you understand it, you will never look at OutOfMemoryException, memory leaks, or process isolation the same way again.</p><h3>Physical Memory vs. Virtual Memory</h3><p>Physical memory is your RAM — the actual silicon on your motherboard. It is fixed, limited, and shared between every process running on the system.</p><p>Virtual memory is different.</p><p>Virtual memory is an address space that the operating system constructs for each process individually. Each process gets the illusion that it owns a large, private, continuous memory space.</p><p>The primary purpose of virtual memory is isolation.</p><p>Each process lives in its own private address space and cannot directly see or touch another process’s memory. That is the foundation of process isolation and memory protection.</p><blockquote><em>Virtual memory’s core purpose is isolation — giving each process its own private address space so it cannot see or corrupt another process’s memory. The ability to use more memory than installed RAM is a consequence of that abstraction. Because the OS already controls the mapping between virtual addresses and physical resources, it gains the flexibility to back those addresses with RAM, disk, or nothing yet.</em></blockquote><p>Because the OS controls the mapping between virtual addresses and physical resources, it can decide where the actual data lives:</p><ul><li>In physical RAM</li><li>In the page file on Windows</li><li>In swap space on Linux/macOS</li><li>Or not backed by anything yet</li></ul><p>This is what allows the OS to move memory pages between RAM and disk without the process needing to know.</p><p>From your program’s point of view, it still has addresses. From the OS point of view, those addresses are just entries in a map.</p><h3>Why Can Virtual Memory Be Larger Than RAM?</h3><p>Because having an address does not mean having RAM.</p><p>A process can reserve a huge range of virtual address space — even gigabytes or terabytes on a 64-bit system — while using only a small amount of physical memory. The rest are just addresses: ranges in the process’s virtual address space with no real memory assigned to them yet.</p><p>This works because virtual memory has two important states: Reserved and Committed. Understanding the difference is critical.</p><h3>Reserved Memory</h3><p>Reserved memory means the OS has set aside a range of virtual addresses for the process. No physical RAM is assigned yet. No page file space is necessarily consumed yet. The process owns that address range, but it cannot safely read or write to it until the memory is committed.</p><p>Think of it like this:</p><blockquote><em>Reserved memory is like a baby name you’ve decided on but the baby hasn’t been born yet. The name is taken — nobody else in your family will use it — but there’s no person behind it yet. Reservation protects the address range from being used for something else, but it does not make the memory usable.</em></blockquote><h3>Committed Memory</h3><p>Committed memory is where the address range becomes real from the process’s point of view.</p><p>Committed memory is virtual memory that the operating system has guaranteed to back with real storage — either physical RAM or space in the page file.</p><pre>byte[] buffer = new byte[1024];<br>// Allocates the array from the current GC heap segment.<br>// The CLR may request additional committed pages from the OS if needed.</pre><blockquote><em>Committed memory does not necessarily mean the data is currently sitting in RAM.</em></blockquote><p>The OS may assign physical pages lazily, often on first access. Later, if memory pressure increases, the OS may move inactive pages out of RAM and into the page file or swap.</p><p>So, commitment is not about location — it is about guarantee.</p><ul><li><strong>Reserved</strong> says: “This address range belongs to you.”</li><li><strong>Committed</strong> says: “This address range is usable, and the OS guarantees backing storage for it.”</li></ul><h3>How Virtual Address Space Is Laid Out</h3><p>Every process gets its own virtual address space divided into regions with different purposes:</p><ul><li>Executable code</li><li>Loaded DLLs or shared libraries</li><li>Thread stacks</li><li>Native heaps</li><li>Managed heaps</li><li>Memory-mapped files</li><li>Guard pages</li><li>Reserved gaps</li></ul><p>Some regions are established at process startup. Others are created dynamically as the runtime, allocator, or application requests more memory. Think of the address space as a controlled map that grows and changes over time — not a fully-defined block that exists before the program runs.</p><p>The heap, for example, does not need to be fully backed by RAM from the beginning. The runtime can reserve address space and commit parts of it later as allocations require it. That is the reserved-to-committed transition in practice.</p><h3>Working Set and Private Bytes: What Those Numbers Really Mean</h3><p>When you inspect a process in Task Manager, Performance Monitor, or Process Explorer, you will encounter several memory metrics. Two of the most commonly confused are Working Set and Private Bytes.</p><h3>Working Set</h3><p>Working Set is the set of pages from your process that are currently resident in physical RAM. It includes both private pages (used only by your process) and shared pages (such as DLLs used by multiple processes).</p><p>This is why Working Set can be misleading. It tells you what is currently in RAM, not everything your process has committed or reserved. The OS can also trim a process’s Working Set under memory pressure, so the number can shrink even if your application freed nothing.</p><ul><li><strong>Working Set answers:</strong> “How much of this process’s memory is currently in RAM?”</li><li><strong>It does not answer:</strong> “How much memory does this process own?”</li></ul><h3>Private Bytes</h3><p>Private Bytes is memory committed exclusively for this process — shared pages are excluded. It can represent memory currently in RAM or memory that has been paged out to disk, making it a better indicator of a process’s private committed footprint than Working Set.</p><p>But Private Bytes does not tell you:</p><ul><li>Whether the memory is actively used</li><li>Whether the heap is fragmented</li><li>Whether the GC can reuse the space efficiently</li><li>Whether a large contiguous allocation can succeed</li></ul><p>It is more useful than Working Set for ownership analysis, but it is still only one piece of the story.</p><h3>Why This Matters for .NET Developers</h3><p>When the CLR allocates managed objects, it operates inside the process’s virtual address space. The managed heap is not outside the OS memory model — it is built on top of it.</p><p>After the CLR initializes the garbage collector, it allocates a segment of memory to store and manage objects — the managed heap. There is one managed heap per managed process, and all threads in the process allocate objects on the same heap.</p><p>The GC manages memory for that heap directly: it calls VirtualAlloc to reserve segments from the OS as needed, and calls VirtualFree to release segments back to the OS once they have been cleared of objects. This is not a separate CLR allocator layer — the GC itself owns segment acquisition and release.</p><p>Managed heap areas include:</p><ul><li><strong>SOH</strong> — Small Object Heap</li><li><strong>LOH</strong> — Large Object Heap</li><li><strong>POH</strong> — Pinned Object Heap (modern .NET)</li></ul><p>Objects are organized logically into generations — Gen 0, Gen 1, Gen 2 — which are GC concepts, not separate physical memory devices. They are regions and logical structures inside the process’s virtual memory space.</p><p>The layers look like this:</p><pre>Your C# code<br>   ↓<br>CLR / GC<br>   ↓<br>Managed heap segments (acquired via VirtualAlloc by the GC)<br>   ↓<br>OS virtual memory manager<br>   ↓<br>MMU + physical RAM + page file</pre><p>The OS handles virtual memory reservation, commitment, protection, and paging. The GC handles managed object allocation, reachability, collection, compaction, and segment lifecycle. The MMU handles address translation on every memory access. These layers are separate, but they interact — and that interaction is where many confusing memory issues originate.</p><h3>Most .NET Allocations Do Not Call the OS</h3><p>This point is easy to overlook. When you write:</p><pre>var buffer = new byte[1024];</pre><p>that allocation usually does not ask the OS for memory directly. Most managed allocations happen inside heap segments the GC already owns:</p><pre>GC already has a committed heap segment<br>   ↓<br>Your object is placed inside available space<br>   ↓<br>No OS call is needed for that allocation</pre><p>The OS becomes involved only when the GC needs more memory than its current segments can provide. At that point, the GC itself calls VirtualAlloc to reserve a new segment. A better mental model:</p><blockquote><em>Most allocations happen inside segments the GC already obtained from the OS. But underneath that allocator, the same virtual memory pipeline still exists.</em></blockquote><h3>Why LOH Fragmentation Is So Dangerous</h3><p>This layering becomes critically important when discussing the Large Object Heap. In .NET, objects 85,000 bytes or larger are allocated on the LOH, which behaves differently from the small object heap: it is not compacted by default during every GC cycle.</p><p>Over time, as large objects are allocated and freed, the LOH can end up looking like this:</p><pre>[large object][free][large object][free][free][large object]</pre><p>There may be enough total free space. But the free space is fragmented into disconnected pieces. When the application needs one new large array, it does not just need some free memory — it needs a sufficiently large contiguous block.</p><p>The problem is not that the machine has no RAM. The problem is that the available memory is in the wrong shape.</p><p>Plenty of space. Wrong shape. That is why LOH fragmentation can produce allocation failures that feel completely unexpected.</p><p>What you can do about it — in .NET 5+, you can opt into LOH compaction explicitly:</p><pre>GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;<br>GC.Collect();</pre><p>Use this judiciously — compaction has a cost — but it is a useful escape valve when fragmentation builds up.</p><h3>Why OutOfMemoryException Can Feel Unexpected</h3><p>OutOfMemoryException does not always mean the system has exhausted every byte of RAM. It can occur because:</p><ul><li>The process hit a configured memory limit</li><li>The system commit limit was reached</li><li>A container or job object enforced a cap</li><li>The virtual address space is fragmented</li><li>The managed heap is fragmented</li><li>The LOH cannot find a large enough contiguous block</li><li>Pinned objects prevent useful compaction</li><li>The GC cannot obtain another suitable heap segment via VirtualAlloc</li></ul><p>You may look at the machine and see free RAM. You may look at the process and see it is not close to its apparent limit. And yet a large allocation fails — because allocation success depends not just on total available memory, but on commit availability, address space shape, heap fragmentation, runtime limits, and allocation size all at once.</p><h3>Diagnosing Memory Issues: Where to Start</h3><p>Understanding the theory is only useful if it changes how you investigate problems. Tools worth knowing:</p><ul><li>dotnet-counters — live GC and memory metrics</li><li>dotnet-dump — managed heap analysis</li><li>PerfView — allocation tracking and GC events</li><li>windbg with !dumpheap — deep managed heap inspection</li><li>Process Explorer — native virtual memory view</li></ul><h3>Key Concepts at a Glance</h3><p><strong>Virtual Memory</strong> A per-process address space managed by the OS. Its primary purpose is isolation and protection, not just extending RAM.</p><p><strong>Physical Memory</strong> The actual RAM installed in the machine.</p><p><strong>Reserved Memory</strong> A virtual address range set aside for future use. Consumes address space, not necessarily physical resources.</p><p><strong>Committed Memory</strong> Virtual memory the OS has guaranteed to back with real storage (RAM or page file). Usable by the process, but not necessarily resident in RAM at that moment.</p><p><strong>Page</strong> The fixed-size unit of memory management, commonly 4 KB.</p><p><strong>Page Fault</strong> The mechanism that lets the OS react when a process accesses a page not currently mapped as needed. Often normal, sometimes expensive.</p><p><strong>Working Set</strong> Pages of a process currently resident in RAM. Includes private and shared pages.</p><p><strong>Private Bytes</strong> Private committed memory owned exclusively by the process. Useful, but not a complete diagnosis by itself.</p><p><strong>GC Heap</strong> The CLR-managed memory area where objects live. Built on top of the OS virtual memory system. Segments are acquired and released by the GC via VirtualAlloc/VirtualFree.</p><p><strong>LOH Fragmentation</strong> A condition where enough total free memory exists, but no single contiguous block is large enough for a new large object allocation.</p><h3>Final Thought</h3><p>Your program does not live directly in RAM. It lives inside a virtual address space. The GC manages object allocation and segment lifecycle inside that space — calling directly into the OS via VirtualAlloc when new segments are needed and VirtualFree when segments are returned. The OS manages the mapping. The MMU performs the translation. And physical RAM is only one part of the picture.</p><p>Not every allocation goes directly to the operating system. Most are served from segments the GC already owns. But underneath all of them is the same pipeline:</p><pre>Code → GC / Runtime → Virtual Memory (VirtualAlloc/VirtualFree) → Physical Resources</pre><p>Understanding that pipeline explains why memory problems are rarely as simple as “we ran out of RAM.” The shape, fragmentation, and commitment state of memory matter just as much as the total amount.</p><p>The map is not the territory. But in computing, it is the map that determines whether your next allocation succeeds.</p><h3>References</h3><ul><li><a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals">Fundamentals of garbage collection — .NET | Microsoft Learn</a></li><li><a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap">Large Object Heap on Windows systems | Microsoft Learn</a></li><li><a href="https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/workstation-server-gc">Workstation and server garbage collection | Microsoft Learn</a></li><li><a href="https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualalloc">VirtualAlloc function (memoryapi.h) | Microsoft Learn</a></li><li><a href="https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree">VirtualFree function (memoryapi.h) | Microsoft Learn</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cd5d52e7eed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Guessing How .NET Manages Memory]]></title>
            <link>https://medium.com/@mahdi.com.haidar/stop-guessing-how-net-manages-memory-f91b127d7dcf?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f91b127d7dcf</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[garbage-collection]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Thu, 30 Apr 2026 15:11:06 GMT</pubDate>
            <atom:updated>2026-05-01T21:44:12.888Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>A practical guide to Stack, Heap, Garbage Collection, generations, SOH, LOH, and why your app might throw OutOfMemoryException even when RAM is available.</em></blockquote><p>Every .NET developer has heard the words “Garbage Collector” thrown around. Most nod along. Few actually know what happens the moment they write new SomeBigObject(). Where does it go? When does it get cleaned up? Why does memory sometimes fragment even when you’ve freed everything?</p><p>Let’s fix that — from the ground up.</p><h3>Two kinds of memory: Stack and Heap</h3><p>When your program runs, it works with two distinct memory areas: the Stack and the Heap. They serve different purposes and behave very differently.</p><p>Think of the Stack as your desk drawer — fast to open, holds small things, and auto-clears itself when you leave the room. Local variables, value types, and method call frames live here. It’s lightning-fast, but it’s not where large or long-lived objects belong.</p><p>The Heap is the warehouse out back. It’s large, flexible, and can store complex objects that need to outlive a single method call. Classes, large arrays, anything you create with new — it all goes here. The tradeoff: managing that warehouse takes work, and that’s where the Garbage Collector comes in.</p><h3>The Garbage Collector and why it uses Generations</h3><p>Imagine throwing a party. Plates accumulate. You don’t wait until the party’s over to clean up — you periodically clear the table, focusing on what’s obviously done and leaving the good crystal alone.</p><p>The GC does exactly this. It periodically scans the Heap, identifies objects that nothing is referencing anymore, and reclaims that memory. But it’s smarter than a blanket sweep — it uses a Generational model based on a key observation: most objects die young.</p><p>Generation Description Gen 0 Newly created objects. Collected very frequently. Most die here. Gen 1 Survived one GC sweep. A buffer zone between young and old. Gen 2 Long-lived objects. Collected infrequently. Only during Full GC.</p><p>Every new object starts at Gen 0. If it’s still referenced when the GC sweeps, it gets promoted to Gen 1. Survive another sweep, and it moves to Gen 2. Gen 2 objects are assumed important — the GC leaves them alone unless a Full GC is triggered.</p><p>This hierarchy lets the GC run fast, targeted cleanups on Gen 0 (where most garbage is) without touching the stable, long-lived Gen 2 objects unnecessarily.</p><h3>SOH and LOH: two heaps inside the Heap</h3><p>Here’s where things get interesting. The managed Heap isn’t a single blob — it’s split into two distinct regions based on object size.</p><h3>Small Object Heap (SOH)</h3><p>For objects under ~85,000 bytes. This is where the vast majority of your objects live.</p><ul><li>Participates in the full Gen 0 → Gen 1 → Gen 2 lifecycle</li><li>The GC runs compaction regularly — sliding surviving objects together to eliminate gaps</li><li>Allocations are fast (pointer bump)</li><li>Fragmentation is minimal and well-managed</li></ul><h3>Large Object Heap (LOH)</h3><p>For objects over ~85KB — things like large byte arrays, big buffers, or oversized data structures.</p><pre>// This lands directly on the LOH — every time.<br>var buffer = new byte[100000]; // ~100KB &gt; 85KB threshold</pre><ul><li>Objects are placed directly into Gen 2, bypassing Gen 0 and Gen 1 entirely</li><li>Only cleaned up during a Full GC — which the CLR triggers automatically when Gen 2 or the LOH fills up, when the OS signals low memory, or when you call GC.Collect() manually. It’s not rare per se, but it’s far less frequent than Gen 0 collections — Gen 0 can run hundreds of times per minute, Full GC might run a handful of times per hour under normal load</li><li>Compaction is rare — the CLR almost never moves LOH objects because they’re too large</li><li>Fragmentation is a real and serious risk</li></ul><h3>Fragmentation: the hidden enemy</h3><blockquote>“The Heap is powerful. But power without discipline creates fragmentation, pauses, and mysterious OutOfMemoryExceptions.”</blockquote><p>When an LOH object is collected, it leaves a hole. If the next large allocation doesn’t fit perfectly into that hole, the runtime allocates fresh memory beyond it. Over time, the LOH looks like Swiss cheese — plenty of total space, but no single contiguous block large enough for the next request.</p><p>You may hit OutOfMemoryException even when you have gigabytes of RAM free. The problem isn’t total memory — it’s <em>contiguous</em> memory. The LOH is fragmented.</p><h3>SOH vs LOH at a glance</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*nGf8NMWRckP3SVHR_DKyIQ.png" /></figure><h3>The fix: Memory Pools</h3><p>If the LOH’s problem is repeated allocation of large temporary buffers, the solution is to stop allocating them repeatedly. Instead of creating a new buffer each time, rent a pre-allocated one from a shared pool, use it, then return it.</p><p>.NET provides exactly this via ArrayPool&lt;T&gt;:</p><pre>// Instead of this — which hits the LOH every time:<br>var buffer = new byte[100000];<br><br>// Do this — rent from the pool, return when done:<br>var buffer = ArrayPool&lt;byte&gt;.Shared.Rent(100000);<br>try<br>{<br>    // use buffer...<br>}<br>finally<br>{<br>    ArrayPool&lt;byte&gt;.Shared.Return(buffer);<br>}</pre><p>The buffer is reused rather than re-created. The LOH stops growing. Fragmentation stays flat. GC pressure drops significantly.</p><p>This pattern is deeply embedded in high-performance .NET code — ASP.NET Core, System.IO.Pipelines, and SignalR all use it extensively. If you’re allocating large byte arrays frequently, you should too.</p><h3>Key takeaways</h3><ul><li>Stack — fast, small, auto-managed. Value types and locals.</li><li>Heap (SOH) — objects under 85KB. Generational, compacted, efficient.</li><li>Heap (LOH) — objects over 85KB. Gen 2 only. Rarely compacted. Fragmentation-prone.</li><li>GC — cleans Gen 0 often, Gen 2 rarely. Full GC is expensive and infrequent.</li><li>Fragmentation — can cause OutOfMemoryException even with available RAM.</li><li>Memory Pools — the right tool for frequent large allocations.</li></ul><p>Understanding these layers won’t just help you debug mysterious memory issues — it’ll change how you write code. Once you know that new byte[100_000] skips the generational model entirely and lands in a rarely cleaned, non-compacting region of memory, you stop treating large allocations as “just another new.”</p><p>And that’s the kind of knowledge that separates engineers who guess from engineers who know.</p><p><em>#dotnet, #csharp, #memory-management, #garbage-collection, #performance</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f91b127d7dcf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LINQ Performance Pitfalls Every C# Developer Should Know]]></title>
            <link>https://medium.com/@mahdi.com.haidar/linq-performance-pitfalls-every-c-developer-should-know-4c3268fe32dc?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c3268fe32dc</guid>
            <category><![CDATA[linq-to-sql]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[linq]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Sat, 11 Apr 2026 15:28:08 GMT</pubDate>
            <atom:updated>2026-04-11T15:28:08.460Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Two subtle choices — </em><em>IEnumerable vs </em><em>IQueryable, and </em><em>Func vs </em><em>Expression — can mean the difference between a fast query and a silent disaster.</em></blockquote><p>LINQ is one of C#’s most expressive features. But with that expressiveness comes a trap that even experienced developers fall into: writing queries that <em>look</em> correct, compile without warnings, and silently destroy your application’s performance at scale.</p><p>In this article, we’ll look at two specific decisions that have an outsized impact on how your LINQ queries actually execute.</p><h3>Part 1: IEnumerable vs IQueryable</h3><p>Both interfaces let you write LINQ queries with the same familiar syntax. The difference is in <em>where</em> the work happens.</p><h3>IEnumerable — works in memory</h3><p>When you filter, sort, or project over an IEnumerable&lt;T&gt;, all that work happens in your application&#39;s memory. If you&#39;re using it against a database, that means Entity Framework will fetch <em>every row</em> first, then apply your filters in C#.</p><h3>IQueryable — works at the source</h3><p>IQueryable&lt;T&gt; is designed for external data sources like SQL databases. Instead of running logic in memory, it builds up a query expression that gets translated into SQL and executed inside the database — where it belongs.</p><blockquote><strong><em>Key rule:</em></strong><em> when working with a database through Entity Framework, always operate on </em><em>IQueryable&lt;T&gt;. Filtering happens in SQL, not in RAM.</em></blockquote><p>The practical consequence is significant. Suppose you call AsEnumerable() before filtering:</p><pre>// All rows fetched from DB first, then filtered in memory<br>var result = dbContext.Users<br>    .AsEnumerable()<br>    .Where(u =&gt; u.Age &gt; 30);</pre><p>The generated SQL is effectively SELECT * FROM Users — no WHERE clause. The filtering happens after all that data lands in your application.</p><p>Compare that to staying on IQueryable:</p><pre>// Filter pushed into SQL — only matching rows are returned<br>var result = dbContext.Users<br>    .Where(u =&gt; u.Age &gt; 30);</pre><p>This generates SELECT * FROM Users WHERE Age &gt; 30. The database does the heavy lifting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*e-tEMhBD_NXiSP8h_aaBtA.png" /></figure><p>A quick note on AsQueryable(): calling it on an in-memory collection like a List&lt;T&gt; does wrap it in an IQueryable, but there&#39;s no SQL provider behind it. No translation occurs — everything still runs in memory. The translation only happens when a real query provider, like Entity Framework, is in the picture.</p><h3>Part 2: Func vs Expression&lt;Func&gt;</h3><p>This is where things get subtle — and where most developers get burned.</p><p>Both Func&lt;T, bool&gt; and Expression&lt;Func&lt;T, bool&gt;&gt; look almost identical when you write a lambda. But they are fundamentally different things at runtime.</p><h3>Func — a compiled black box</h3><p>A Func is a compiled delegate. By the time it reaches Entity Framework, it&#39;s already been compiled into IL bytecode. EF has no way to look inside it. It cannot read the logic, inspect the conditions, or translate them to SQL. It just sees: &quot;here is a method, call it.&quot;</p><h3>Expression&lt;Func&gt; — a readable data structure</h3><p>An Expression&lt;Func&lt;T, bool&gt;&gt; is not compiled code — it&#39;s a <em>description</em> of code, stored as an expression tree. It&#39;s a data structure that EF can walk, inspect, and translate into a SQL WHERE clause.</p><p>Think of it like this:</p><ul><li><strong>Expression&lt;Func&gt;</strong> → a recipe written on paper. Entity Framework can read it and cook it inside the database.</li><li><strong>Func</strong> → a meal already cooked. EF can&#39;t &quot;uncook&quot; it to figure out what ingredients were used.</li></ul><h3>What this looks like in practice</h3><pre>// Problematic — Func is a compiled delegate, EF can&#39;t translate it<br>Func&lt;User, bool&gt; filter = u =&gt; u.Age &gt; 30;<br>var result = dbContext.Users.Where(filter);<br>// SQL generated: SELECT * FROM Users<br>// All rows fetched, filter runs in memory<br><br>// Correct — Expression tree, EF translates it to SQL<br>Expression&lt;Func&lt;User, bool&gt;&gt; filter = u =&gt; u.Age &gt; 30;<br>var result = dbContext.Users.Where(filter);<br>// SQL generated: SELECT * FROM Users WHERE Age &gt; 30</pre><blockquote><strong><em>Important:</em></strong><em> the compiler will not warn you about this. Both versions compile cleanly. The performance difference only shows up at runtime — and if your table has millions of rows, that difference is catastrophic.</em></blockquote><p>The reason the compiler stays silent is that IQueryable&lt;T&gt; has overloads for both Where(Func&lt;T, bool&gt;) and Where(Expression&lt;Func&lt;T, bool&gt;&gt;). Passing a Func is syntactically valid — it just forces EF to fall back to in-memory evaluation by first loading all data internally.</p><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*oRZPPQetsUQD9ZFU43LDDw.png" /></figure><h3>Quick rules to remember</h3><ul><li>When querying a database with EF, always work with IQueryable, not IEnumerable.</li><li>Pass Expression&lt;Func&lt;T, bool&gt;&gt; to Where() — never a bare Func.</li><li>AsEnumerable() is a signal that everything after it runs in memory — use it deliberately, not by accident.</li><li>If something compiles cleanly but seems slow, check whether your filters are actually reaching SQL.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c3268fe32dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monitoring and Observability in Load Testing — k6 vs JMeter vs LPS]]></title>
            <link>https://medium.com/@mahdi.com.haidar/monitoring-and-observability-in-load-testing-lps-vs-k6-vs-jmeter-b85e4548b05e?source=rss-3529f5e2cee3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b85e4548b05e</guid>
            <category><![CDATA[k6]]></category>
            <category><![CDATA[lp]]></category>
            <category><![CDATA[jmeter]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Mohamad]]></dc:creator>
            <pubDate>Fri, 03 Apr 2026 22:13:39 GMT</pubDate>
            <atom:updated>2026-04-05T08:56:00.961Z</atom:updated>
            <content:encoded><![CDATA[<p>Generating load is only half the job. What you can see while the test is running — and how easily you can diagnose what went wrong afterward — is often what separates a useful tool from a frustrating one. This article compares the built-in observability each tool offers, what requires additional setup, and where each one falls short.</p><h3>1. What You Get Out of the Box</h3><h3>LPS — Built-in live dashboard, zero setup</h3><p>LPS ships with a fully functional live dashboard enabled by default. No plugins, no external services, no configuration required to get started. The moment a test runs, the dashboard is available on port 8009 (configurable) and updates in real time.</p><p>The dashboard covers:</p><ul><li>Total requests, success/failed count, error rate</li><li>Average response time, P99</li><li>Max concurrent requests (peak simultaneous requests)</li><li>Total data received and transfer rate</li><li>Windowed and cumulative views across all metrics</li><li>Response breakdown by HTTP status code over time</li><li>Latency breakdown: Total Time, TTFB, TCP, TLS, Waiting, Server Time, Sending, Receiving</li><li>Data transfer: upstream vs downstream, windowed and cumulative throughput</li></ul><p>It also supports downloading the full dashboard as a PDF report for sharing or archiving results.</p><p>One important limitation: the dashboard does not persist data. Once the dashboard is closed, the metrics are gone. For result retention, historical comparisons, or sharing results after a test completes, InfluxDB integration is not optional — it becomes the necessary complement to the built-in dashboard.</p><p>The dashboard can be disabled for CI/CD pipelines where a UI is not needed — the REST API remains active regardless, allowing metrics to be fetched programmatically.</p><pre>&quot;LPSDashboardConfiguration&quot;: {<br>  &quot;BuiltInDashboard&quot;: true,<br>  &quot;Port&quot;: 8009,<br>  &quot;RefreshRate&quot;: 5<br>}</pre><h3><strong>k6 — Built-in web dashboard and external stack options</strong></h3><p>Since v0.49.0, k6 ships with a built-in web dashboard. It is not enabled by default — you activate it by setting an environment variable when running your test:</p><pre>K6_WEB_DASHBOARD=true k6 run script.js</pre><p>Once enabled, the dashboard is available at http://localhost:5665 and updates in real time during the test run. It also supports exporting an HTML report at the end of the test:</p><pre>K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=report.html k6 run script.js</pre><p>For long-term storage and historical comparisons, k6 supports streaming metrics to external systems via the --out flag — typically InfluxDB + Grafana.</p><p>k6 Cloud offers a fully managed dashboard, but it is a paid service and requires running tests through Grafana’s infrastructure.</p><h3>JMeter — Listeners in the GUI, heavy and limited</h3><p>JMeter’s built-in monitoring comes through Listeners — components you add to the test plan in the GUI. Common ones include the Summary Report, Aggregate Report, and Graph Results. They work, but they are known to consume significant resources and can affect test results if left enabled during a run. For serious observability, JMeter is typically paired with the Backend Listener plugin streaming to InfluxDB + Grafana, mirroring the k6 external stack approach.</p><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*mxSMl3S7GnBQd-zHdwwXqg.png" /></figure><h3>2. Windowed vs Cumulative Metrics — Why Both Matter</h3><p>This is one of the most important distinctions in load test observability, and it is where LPS’s dashboard design stands out.</p><h3>The problem with cumulative-only metrics</h3><p>Cumulative metrics calculate across all requests from the start of the test. This produces stable, reportable numbers — but stability comes at a cost. A system that performed well for 25 minutes and then degraded significantly in the final 5 minutes will still report a “good” P90 overall because the early clean data drowns out the late degradation.</p><p>This is not a theoretical concern. It is one of the most common reasons load test results look acceptable, but production incidents still happen — the test passed on aggregate while the system was quietly struggling at peak load.</p><h3>Windowed metrics for anomaly detection</h3><p>LPS divides the test into fixed time windows (default: 5 seconds, configurable via RefreshRate). At the end of each window, metrics are calculated using only the requests from that window, then the window resets. Each point on the windowed chart represents a fresh, isolated sample.</p><p>Because each window contains a small sample, any performance change stands out immediately. Consider a test running at 100 requests per second — in a 5-second window that is 500 requests. If the server slows down and 50 of those requests take 3 seconds instead of 100ms, the P90 for that window jumps noticeably. Across 50,000 cumulative requests, those same 50 slow responses would barely register.</p><p>Cumulative averages always risk masking point-in-time degradation, spikes, or intermittent errors that only appeared during a specific window of the test.</p><p>Below is an example that shows you how an anomaly could be detected with the windowed metrics while normalized in the cumulative one</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YN6CT2TH-rghpOd5_IaO_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mhGPiAyurNwKZ9K_N0wQsw.png" /></figure><h3>The configurable window size</h3><p>The default 5-second window is a sensible balance — granular enough to catch sharp spikes, stable enough to avoid excessive noise. Shorter windows increase sensitivity but also increase noise; longer windows smooth the signal but may miss brief degradation events. Aligning the window size with the nature of your system under test gives you cleaner anomaly detection.</p><h3>k6 and JMeter</h3><p>Neither k6 nor JMeter has a native windowed/cumulative split in their built-in output. k6’s terminal output shows rolling aggregates. In Grafana you can configure time window aggregations manually, but this requires query knowledge and is not built in. JMeter’s Listeners aggregate across the full run by default.</p><h3>3. Metrics Depth and Latency Breakdown</h3><h3>LPS — Full timing decomposition per iteration</h3><p>LPS tracks and exposes a detailed breakdown of every request’s lifecycle, available both in the dashboard and exported to InfluxDB:</p><ul><li><strong>TotalTime</strong> — end-to-end request duration including DNS, TCP, TLS, sending, waiting, and receiving</li><li><strong>TTFB (TimeToFirstByte)</strong> — time until the first response byte arrives; includes TCP, TLS, sending, server processing, and network latency</li><li><strong>WaitingTime</strong> — TTFB minus DNS, TCP, TLS, and upload time; a proxy for backend responsiveness under stable network conditions</li><li><strong>ServerTime</strong> — server-side processing time read from response headers (Server-Timing, X-Response-Time, or X-Runtime); requires two prerequisites: the server must send timing headers, and you must explicitly enable it before the test via lps httpclient --servertimeheader &lt;headerName&gt; --servertimeformat &lt;format&gt;</li><li><strong>TCPHandshakeTime</strong> — time for the TCP 3-way handshake; a useful indicator of network latency</li><li><strong>TLSHandshakeTime</strong> — SSL/TLS negotiation time; useful for verifying session resumption (P50 ≈ 0ms when resumption is working)</li><li><strong>SendingTime</strong> — time to write the request body to the send buffer</li><li><strong>ReceivingTime</strong> — time to read and parse the response body</li></ul><p>Each metric is available at P50, P90, P95, P99, Avg, Min, and Max — both in windowed and cumulative form.</p><p>The ServerTime metric has two prerequisites before it produces any data. First, the server must be instrumented to include timing headers in its responses. Second, you must explicitly enable collection before the test using the lps httpclient command, specifying which header to read and the format:</p><pre># Auto-detect format from Server-Timing header<br>lps httpclient --servertimeheader &quot;Server-Timing&quot; --servertimeformat Auto<br># Read from a custom header in milliseconds<br>lps httpclient -sth &quot;X-Response-Time&quot; -stf Milliseconds</pre><p>Supported formats are Auto (tries Server-Timing syntax first, then numeric), Milliseconds, Seconds, and ServerTiming. If either prerequisite is missing — the server doesn&#39;t send the header, or the command hasn&#39;t been run — ServerTime records zero. k6 can achieve the same result by reading response headers manually in the default function. The difference is that LPS handles the parsing and exposes it as a named, tracked metric automatically once configured, while k6 requires explicit code to extract and record it.</p><h3>k6 — Good built-in metrics, server-side timing requires manual extraction</h3><p>k6 tracks http_req_duration, http_req_waiting, http_req_connecting, http_req_tls_handshaking, http_req_sending, and http_req_receiving natively. These are solid and cover most of the same dimensions as LPS. k6 can also extract server-side timing from response headers, but it requires writing explicit code to read the header, parse the value, and record it as a custom metric. LPS handles this automatically once configured with lps httpclient — the difference is convenience, not capability.</p><h3>JMeter — Basic timing, breakdown requires configuration</h3><p>JMeter tracks connect time and latency (time to first byte) alongside response time. A full breakdown comparable to LPS or k6 requires additional configuration or plugins.</p><h3>4. InfluxDB Integration and Long-Term Storage</h3><h3>LPS — Native push, pre-built Grafana template</h3><p>Starting from version 3.0.3.1, LPS supports native InfluxDB export configured with a single CLI command:</p><pre>lps influxdb -ie true -iu &quot;&lt;InfluxDB_URL&gt;&quot; -it &quot;&lt;Your-Token&gt;&quot; -io &quot;&lt;ORG&gt;&quot; -ib &quot;&lt;BucketName&gt;&quot;</pre><p>LPS exports both windowed and cumulative variants of every metric — meaning you get anomaly detection data and aggregate data in InfluxDB, not just the final numbers. A pre-built Grafana dashboard template is available for immediate import, covering all measurements out of the box.</p><p>The metrics are tagged per plan_name, round_name, iteration_name, and target URL — making it straightforward to filter by specific test phases or endpoints in Grafana queries.</p><p>Exported measurements include:</p><ul><li>cumulative_duration / windowed_duration — all timing metrics at all percentiles</li><li>cumulative_requests / windowed_requests — request counts, rates, concurrency, error rate</li><li>cumulative_response_codes / windowed_response_codes — HTTP status code distribution</li><li>cumulative_data_transfer / windowed_data_transfer — throughput metrics</li><li>iteration_final_status — final outcome per iteration (Success, Failed, Cancelled, Terminated, Skipped)</li></ul><p>The iteration_final_status measurement is worth calling out specifically. It lets you query CI/CD pipelines to determine whether a specific iteration passed or failed without parsing logs or scraping output — a clean integration point for automated pipelines.</p><h3>k6 — Manual InfluxDB setup, no pre-built template</h3><p>k6 supports InfluxDB output via the --out flag. The setup works, but there is no pre-built Grafana template from k6 — the community maintains various dashboard templates of varying quality. You also only get one view of the data — there is no windowed vs cumulative split because k6 does not compute windowed metrics natively.</p><h3>JMeter — Backend Listener plugin required</h3><p>JMeter requires the Backend Listener plugin to stream metrics to InfluxDB. Configuration is done through the GUI and is more involved than k6 or LPS. Like k6, there is no native windowed/cumulative distinction.</p><h3>Summary</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*oBewcj1-yFowJFtFltK13w.png" /></figure><h3>5. Per-Iteration Observability</h3><p>This connects back to a point made in the first article in this series. Because LPS defines named iterations with their own failure and termination rules, the observability is naturally scoped to the same granularity.</p><p>In InfluxDB, every metric is tagged with the iteration_name. This means in Grafana you can filter to a specific endpoint and see its latency, error rate, and throughput independently — without that signal being mixed with other endpoints in the same test run.</p><p>In k6, metrics can be tagged per request using { tags: { name: &#39;checkout&#39; } }, and Grafana queries can filter on those tags. This works, but it requires the test author to remember to tag every request correctly, and it is not enforced by the tool. In LPS it is structural — every iteration is named and every metric inherits that name automatically.</p><p>In JMeter, sampler names appear in results but correlating them cleanly in long-term storage requires careful setup.</p><h3>6. When Each Approach Makes Sense</h3><p><strong>LPS</strong> is the strongest choice when you want observability with minimal setup for live monitoring during a test run. The built-in dashboard, configurable windowed metrics, and per-iteration metrics give you an immediate picture without assembling a stack. However, the dashboard does not persist data — for result retention and historical comparison, InfluxDB integration is required, and the pre-built Grafana template makes that straightforward. Being a free tool, there is no cloud dashboard offering, which is a real limitation for teams that need managed, hosted result storage without running their own infrastructure.</p><p><strong>k6</strong> makes sense when your team is already running a Grafana + InfluxDB stack and is comfortable building or adapting Grafana dashboards. The native metrics are solid and the ecosystem is mature. The gap is the absence of windowed metrics and the lack of a pre-built dashboard template.</p><p><strong>JMeter</strong> is the weakest option for observability out of the box. The GUI Listeners are resource-heavy and limited. For anything beyond a basic run, an external stack with the Backend Listener plugin is effectively required — which negates JMeter’s GUI-first simplicity advantage.</p><p><em>All tool behavior based on publicly available documentation as of early 2026. LPS: lpsload.io/docs</em>/readme.html<em> | k6: k6.io/docs | JMeter: jmeter.apache.org</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b85e4548b05e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>