Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

Asp.Net Core: Fix Rate Limiting Counting Requests Per Instance

- 20.11.26 - ErcanOPAK

⚙️ Fix Rate Limiting Counting Requests Per Instance Instead of Per Deployment

The built-in rate limiting middleware’s fixed window, sliding window, and token bucket limiters all keep their counters in that process’s own memory by default. The moment the app runs behind a load balancer across more than one instance, each instance enforces the configured limit entirely independently against only the requests it personally received, so a client whose traffic happens to spread across N instances transparently gets up to N times the intended limit – with the configuration itself still looking completely correct in code.

⚙️ The Problem

builder.Services.AddRateLimiter(options =>
{
    // In-memory partition store - correct per process, but each of
    // N load-balanced instances enforces this limit independently
    // against only the requests that instance personally received.
    options.AddFixedWindowLimiter("api", opt =>
    {
        opt.PermitLimit = 100;
        opt.Window = TimeSpan.FromMinutes(1);
    });
});

✅ Fix: Move the Counter Somewhere Every Instance Can See It

  • For any limit meant to apply per client or API key across the whole deployment, move the counter state to a shared store – a Redis-backed partition counter, or a distributed-cache-backed custom limiter – instead of the default in-memory partitions, which are explicitly documented as per-process.
  • If true cross-instance exactness genuinely matters, such as a billing-grade quota rather than general abuse mitigation, enforce the hard limit at a layer that actually sees all instances’ traffic together, like an API gateway or reverse proxy in front of the cluster, and treat the in-app limiter as a secondary, best-effort guard.
  • Load-test with traffic deliberately spread across every running instance in staging before trusting a configured limit number in production, since a single-instance load test can never reveal this gap.

🚨 Why This Is Dangerous

  • Shipping a “100 requests per minute” limit that’s quietly 100 times N instances in production can leave an API wide open to the exact abuse pattern the limiter was added to stop, while the code review sees nothing wrong with the configuration at all.

A rate limit enforced per process isn’t a weaker rate limit – behind more than one instance, it’s a different one than the number in the code suggests.

— Backend Architect

Related posts:

.NET Core: Handling Errors Gracefully with Middleware

.NET Core: Handling Errors Gracefully with Middleware

.NET Core: Use Entity Framework Core for Database Access

.NET Core: Use Entity Framework Core for Database Access

.NET Core: Use Source Generators for Compile-Time Code Generation

.NET Core: Use Source Generators for Compile-Time Code Generation

Views: 6

Post navigation

Docker: Fix a Build Cache Ignoring a Changed ARG Value
Photoshop: Fix the Magic Wand Selecting Across Hidden Layers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com