Boosting API Performance and Scalability: Best Practices for C# & ASP.NET Core Developers with Examples

Mohamed Hendawy
4 min readJul 17, 2023

--

Building high-performance and scalable APIs is crucial for delivering exceptional user experiences. As C# developers, there are various techniques and best practices that can significantly enhance the performance and scalability of your APIs. In this article, we will delve into specific strategies such as caching, response compression, load balancing, optimizing database queries, and leveraging caching mechanisms, accompanied by practical examples. By incorporating these practices into your C# API development, you can ensure optimal performance even under heavy loads.

  1. Caching for Performance:
    Caching plays a pivotal role in improving API response times by storing frequently accessed data and avoiding unnecessary trips to the server. Let’s consider an example of caching using the ASP.NET Core MemoryCache:
public class UserController : Controller
{
private readonly IMemoryCache _cache;

public UserController(IMemoryCache cache)
{
_cache = cache;
}

[HttpGet("users")]
public async Task<IActionResult> GetUsers()
{
if (_cache.TryGetValue("users", out List<User> users))
{
return Ok(users);
}

users = await _userService.GetUsers(); // Retrieve users from a data source

var cacheOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
};

_cache.Set("users", users, cacheOptions);

return Ok(users);
}
}

In this example, the API endpoint /users first checks if the users list is available in the cache. If found, it is returned immediately, avoiding the need to hit the data source. Otherwise, the data is fetched from the source, stored in the cache, and returned to the client.

2. Response Compression:

Compressing API responses reduces the amount of data transmitted over the network, resulting in faster response times. Let’s see how to enable response compression in an ASP.NET Core API:

public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<GzipCompressionProvider>();
});

services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
}

By configuring response compression, the API will automatically compress responses using Gzip. The compression level can be adjusted based on performance needs.

3. Optimizing Database Queries:
Optimizing database queries is crucial for reducing response times and preventing database bottlenecks. Consider the following example using Entity Framework Core to optimize a database query:


public IEnumerable<Product> GetProductsByCategory(string category)
{
return _dbContext.Products
.Where(p => p.Category == category)
.OrderByDescending(p => p.Price)
.ToList();
}

In this example, we filter products by category and order them by price. By creating appropriate indexes on the Category and Price columns, we can significantly improve the query performance.

also, you can use Eager Loading Related Data: If the Product entity has related entities that are commonly accessed together, such as ProductCategory or ProductSupplier, you can use eager loading to fetch all related data in a single query, reducing the number of roundtrips to the database.

public IEnumerable<Product> GetProductsByCategory(string category)
{
return _dbContext.Products
.Where(p => p.Category == category)
.Include(p => p.ProductCategory)
.Include(p => p.ProductSupplier)
.OrderByDescending(p => p.Price)
.ToList();
}

By using the Include method, we instruct Entity Framework Core to fetch the related entities ProductCategory and ProductSupplier along with the products, resulting in more efficient query execution.

also, you can use Projection: If you only need specific properties of the Product entity, consider projecting the query results into a custom DTO (Data Transfer Object) instead of retrieving the entire entity.

public IEnumerable<ProductDTO> GetProductsByCategory(string category)
{
return _dbContext.Products
.Where(p => p.Category == category)
.OrderByDescending(p => p.Price)
.Select(p => new ProductDTO
{
Id = p.Id,
Name = p.Name,
Price = p.Price
})
.ToList();
}

By projecting the query results into a custom DTO, you retrieve only the necessary data, reducing the amount of data transferred between the database and the application.

4. Leveraging Caching Mechanisms:

In addition to caching at the API level, leveraging caching mechanisms at the data access layer can further enhance performance. Let’s explore an example using Redis caching:

public async Task<IEnumerable<Product>> GetProductsByCategory(string category)
{
var cacheKey = $"products:{category}";
var cachedProducts = await _cache.GetAsync<IEnumerable<Product>>(cacheKey);

if (cachedProducts != null)
{
return cachedProducts;
}

var products = await _dbContext.Products
.Where(p => p.Category == category)
.ToListAsync();

await _cache.SetAsync(cacheKey, products);

return products;
}

In this example, the API first checks if the desired products are available in the Redis cache. If found, they are returned immediately. Otherwise, the products are fetched from the database, stored in the cache, and then returned.

5. Load Balancing:

Load balancing distributes incoming API requests across multiple servers or instances, ensuring efficient resource utilization and improved scalability.

Conclusion: By implementing caching, response compression, load balancing, optimizing database queries, and leveraging caching mechanisms, C# developers can significantly improve the performance and scalability of their APIs. The examples provided demonstrate how these practices can be applied in real-world scenarios. Remember to continuously measure, monitor, and fine-tune your APIs to identify new optimization opportunities. With these best practices in place, you can ensure that your C# APIs deliver optimal performance even under heavy loads, resulting in a seamless user experience.

--

--

No responses yet