June 15, 2023

Request Throttling in Minimal Api

Throttling in Minimal API refers to the practice of limiting the number of requests a client can make within a certain time period. It helps prevent abuse and ensures fair usage of API resources. By implementing throttling mechanisms, developers can control the rate at which clients can access their APIs, improving performance and protecting against potential overloading or denial of service attacks.

Typically these requests are handled via Api Gateway policies. However, .Net provides build in middleware to handle this effectively.


    builder.Services.AddRateLimiter(r => r
    .AddFixedWindowLimiter(policyName: "fixed", options =>
    {
    options.PermitLimit = 4;
    options.Window = TimeSpan.FromSeconds(12);
    }));

    var app = builder.Build();
    app.UseRateLimiter();

    app.MapGet("/", () => Results.Ok($"Hello World"))
    .RequireRateLimiting("fixed");