When the database you are using has slow access you may use a cache to speed up the execution. In this blog I’ll share how you can speed up your application if you are using a NoSQL document database.
You can use Redis to resolve the same problem. In my case, I preferred to have finer control on how the cache is working.
You can find the code here: https://github.com/tachev/DBCache
Using it is easy, just replace the calls to use the cache. Here are some examples:
var document = new Document(); await database.UpsertItemAsync(document); var resultDocument = await database.ReadItemByIdAsync(id);
The cache is used for both reads and writes, that way you can get the control back to the application quickly. It’s memory based, so if that’s not enough you can scale it with sharding the data and deploying it to a cluster.
You can play with the unit tests and see what are the performance gains for your database.
- Use the upsert functionality from my other blog post
- Switch to use MemoryCache instead of ConcurrentDictionary – not sure yet what will be the gains from that.
- Add sharding logic (That way it will be possible to execute cache on a cluster)
- Optimize the search results to use cache
- Add runtime ability to disable/enable cache
Let me know if you have any questions or comments on how I can improve the blog.