Use it through IDistributedCache (as shown above). ✅ Redis supports features like eviction, expiration, pub/sub, and persistence. 📦 5. How to use Response Caching Middleware?
Response caching caches the entire HTTP response at the server level — useful for
static or rarely changing API responses.
Setup:
builder.Services.AddResponseCaching();
var app = builder.Build();
app.UseResponseCaching();
Controller Example:
[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Any)]
Follow :
[HttpGet]
public IActionResult GetTime() => Ok(DateTime.Now);
✅ The response is cached for 60 seconds — subsequent requests are served faster.
Make sure to set appropriate cache headers in the response.
🚀 6. What is Output Caching in .NET 8?
Output caching (introduced in .NET 8) is an advanced version of response caching that
stores rendered outputs at the middleware level — and supports vary-by, policies, and
authorization.
Setup:
builder.Services.AddOutputCache();
var app = builder.Build();
app.UseOutputCache();
Usage:
[OutputCache(Duration = 60, VaryByQueryKeys = new[] { "category" })]
[HttpGet]
public IActionResult GetProducts(string category)
return Ok(new { Category = category, Time = DateTime.Now });
✅ Benefits:
- Works with authenticated users.
- Supports vary-by parameters, headers, and policies.
- Integrates well with minimal APIs and Razor pages.
Follow :
🗜 7. How to compress responses?
Use response compression middleware to reduce payload size for clients.
Setup:
builder.Services.AddResponseCompression(options =>
options.EnableForHttps = true;
options.Providers.Add<GzipCompressionProvider>();
});
builder.Services.Configure<GzipCompressionProviderOptions>(opt =>
opt.Level = CompressionLevel.Fastest);
var app = builder.Build();
app.UseResponseCompression();
✅ Compresses responses using Gzip or Brotli automatically.
Common for APIs serving large JSON or text content.
⚙ 8. What is async/await and why is it important in
web apps?
async/await enables non-blocking I/O operations, allowing your web server to handle
thousands of concurrent requests efficiently.
Example:
[HttpGet]
public async Task<IActionResult> GetData()
var data = await _service.GetDataAsync(); // Non-blocking call
return Ok(data);
Follow :
✅ Benefits:
- Frees threads while waiting for I/O (e.g., DB, HTTP calls).
- Improves scalability and throughput.
- Essential for high-traffic APIs and cloud-native apps.
❌ Avoid Task.Result or .Wait() — they block threads and reduce performance.
⚡ 9. How to optimize startup performance?
To improve startup and cold-boot performance:
✅ Tips: