COMPUTER ENGINEER

Jump to examples
Blog
Next.js / ASP.NET Core / SQL6 min study
37/ 41

Resilience and observability

Rate limits, timeouts, retries and circuit breakers

Protecting the system from abuse and from slow dependencies.

UnderstandStudy an examplePractise

Understand the concept

A rate limit restricts how many requests a client may make in a period. It is the first defence against password guessing, form spam and abuse of expensive endpoints. ASP.NET Core’s built-in rate limiter offers fixed window, sliding window, token bucket and concurrency algorithms. When the limit is exceeded it returns 429 Too Many Requests and, where possible, Retry-After. In this project sign-in, the contact form and analytics endpoints have separate policies.

A timeout means not waiting for a dependency forever. Waiting on an unresponsive service consumes threads, connections and memory, and slowness spreads along the chain. Every external call needs a sensible timeout.

A retry overcomes transient failures (a momentary network drop, a 503) by trying again, but only for safely repeatable (idempotent) operations and with increasing delays. Otherwise it adds load to a service already struggling. A circuit breaker protects both the dependency and the caller by sending no requests to a constantly failing dependency for a while. In .NET these are configured together with Microsoft.Extensions.Http.Resilience (Polly).

WHEN TO USE IT

In an unprotected system, one dependency’s failure can stop the whole application.

COMMON PITFALL

Sending a non-repeatable POST such as a payment twice through automatic retries.

LIVE ANIMATION

A letter’s journey through the city

Think of a request as a letter and the server as a city. Pick a scenario, press play and watch which gates the letter passes and where it gets stopped.

CLIENTEDGEWEB SERVERAPI · ASP.NET COREDATABrowserThe user’s screenNetworkDNS · TCP · TLSReverse proxyTLS termination, compressionNext.js serverSSR and route handlersKestrelHTTP serverException handlerMiddlewareRoutingEndpoint selectionRate limiterMiddlewareAuthenticationWho you areAuthorizationWhat you may doModel bindingHTTP → C# + validationControllerEndpoint codeServiceBusiness rules (DI)RedisDistributed cacheEF CoreLINQ → SQLSQL ServerQuery plan, indexes
OUTBOUND →

A bot sends the same form for the sixth time in a minute.

BrowserYour screen. It writes the request and finally turns the answer into a page.
1 / 5
01 / PRACTICE

One topic. Three levels of depth.

Illustrative example
builder.Services.AddRateLimiter(o =>
{
    o.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
    o.AddFixedWindowLimiter("contact", w => { w.PermitLimit = 5; w.Window = TimeSpan.FromMinutes(1); });
});

[HttpPost, EnableRateLimiting("contact")] public Task<IActionResult> Send(...)

Five submissions per minute; beyond that the request gets 429 before any business logic.

This page connects to no real server or database. Code samples are illustrative for .NET 8, Next.js and SQL Server; timings are example values in the simulation, not measurements. Try samples only in your own development environment; never share real connection strings, tokens or passwords.

02 / YOUR TURN

Try it yourself first.

An external exchange-rate service occasionally stops answering for 20 seconds and your whole site slows down at those moments. Propose three layers of protection.

Show the solution and explanation
Worked solution
1) Zaman aşımı: çağrı başına ör. 2 saniye.
2) Devre kesici: art arda hatalarda 30 sn boyunca servisi çağırma.
3) Yedek: son başarılı kuru önbellekten sun ve 'güncellenemedi' notu göster.
(+ isteğe bağlı: arka planda periyodik güncelleme, istek yolundan çıkarma)

The further a slow dependency is kept from the request path, the less users are affected.