The author decided to test how performant is one of his WEB APIs and how he can improve its performance by using HybridCache.Next he used k6 - an open-source tool and cloud service that makes load testing easy for developers and QA engineers.He set rate: 30000 as the base number for desired requests per second for the tested endpoint.
The API is now much faster, but he was curious if he could push the boundaries even further. Being aware of the cache stampede problem, he decided to add caching. He decided to use a 'Read Through Cache' pattern that works as follows:
When working with cache, we must be aware of the cache stampede problem: when multiple requests may receive a cache miss and will all call the database to retrieve the entity. Instead of calling the database once to get the entity and write it into cache.
He used a new caching library HybridCache available in .NET 9 that prevents a cache stampede problem. HybridCache is great replacement of old IMemoryCache and IDistributedCache as it combines them both and can work with in-memory cache and distributed cache like Redis.
Finally, he used Redis and saw how it can perform better than InMemory caching. While the increase was not as huge as previous improvements, Redis provides additional benefits like persistence and scalability. This in total gives an 18x performance improvement compared to the first version.
To increase the performance, he used the following steps: Use AsNoTracking in EF Core, replaced EF Core with Dapper, used InMemory cache using HybridCache, and used Redis cache using HybridCache.
By methodically analyzing and addressing performance bottlenecks, he transformed his ASP.NET Core Web API from handling 1,370 RPS to an impressive 25,798 RPS — 18x times increase.