A Step-by-Step Guide for the Cache-Aside Pattern + Stampede Protection
Ғылым және технология
Join a community of 1000+ .NET developers: / milanjovanovic
Master the Clean Architecture: bit.ly/3PupkOJ
The ultimate Modular Monolith blueprint: bit.ly/3SXlzSt
Caching is one of the simplest techniques to significantly improve your application's performance. It's the process of temporarily storing data in a faster access location. You will typically cache the results of expensive operations or frequently accessed data. ASP.NET Core offers several types of caches, such as IMemoryCache, IDistributedCache, and the upcoming HybridCache (.NET 9).
In this video, I'll show you how to implement the most popular caching pattern - the Cache-Aside pattern. We will also explore a potential solution for the cache stampede problem.
Caching in ASP.NET Core: Improving Application Performance
www.milanjovanovic.tech/blog/...
Check out my courses:
www.milanjovanovic.tech/courses
Read my Blog here:
www.milanjovanovic.tech/blog
Join my weekly .NET newsletter:
www.milanjovanovic.tech
Chapters
0:00 What is the Cache-Aside pattern?
1:43 Implementing Cache-Aside in .NET
8:10 Making the implementation reusable
13:23 The Cache Stampede problem
17:32 Pros and Cons of Caching
Пікірлер: 49
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
Thank you, Milon, you are a light on my way. I found you when I was searching for what is the Repository pattern. Now I can build powerful and scalable distributed systems, and I understand well almost everything about DDD, modular monolithic and microservices. I got to know many libraries thanks to you and learned from a lot... I am in the first year of the College of Computer and Electronics Engineering and I did not dream of learning about all this now... I hope we meet one day. Thank you for everything.
@MilanJovanovicTech
26 күн бұрын
You are crushing it my man 💪 Keep up the good work!
Whenever I watch your videos, I feel how poorly i write code. Thank you for all the unique and very, very practical, and useful videos.
@MilanJovanovicTech
20 күн бұрын
You're most welcome! :)
Thank you for sharing this on you channel. lot of new learning on you channel.
@MilanJovanovicTech
Ай бұрын
You are so welcome
Thank you! As always -- great presentation, great coding style and well worth a watch even if there was little new knowledgewise. I look forward to the .Net 9 Caching video!
@MilanJovanovicTech
Ай бұрын
Much appreciated!
Great tutorial thanks!
@MilanJovanovicTech
Ай бұрын
You're welcome!
It was awesome Thanks for sharing 👍
@MilanJovanovicTech
Ай бұрын
Glad you liked it
nice content
@MilanJovanovicTech
Ай бұрын
Thanks!
Hello Milan! Excellent video! Very useful information! Congrats! By the way, do you happen to have a video that deep dives into Distributed Locking (what it is, real-world scenarios, how to implement in Redis or another cache technology)? It would be really great! See ya!
@MilanJovanovicTech
Ай бұрын
Sadly, I don't have anything touching distributed locking (so far). But I've been meaning to make some content around that 😅 While you wait, you can checkout Redlock: github.com/samcook/RedLock.net
@vamvdotnet
Ай бұрын
@@MilanJovanovicTech No problem, my lad :) You just did help a whole bunch by giving me this pointer! Thank you once again :)
Hey Milan, can you do a video about EF Core and Temporal tables?
@MilanJovanovicTech
Ай бұрын
At some point, yeah
Maybe it's worth to mention that the key is important in multi-user/tenant applications which have different security concerns per user. A "NormalUser" could have different rights as an "Administrator" for example, where a "NormalUser" would be restricted in the list of entities he/her could see. When using a multi-tenant environment, an "Organization"-part should also be added to the key (or maybe a completely separate Redis Instance). When the key doesn't take anything of that into consideration, users could get strange/forbidden results. I'm just starting to implement caching now, so am I right here, any tips?
@MilanJovanovicTech
13 күн бұрын
Yes, caching and authorization (who can do/see what) is tricky. The implementation you suggested is something I've done in the past in similar situations.
hey Milan, can you please let us know the theme you're using?
@MilanJovanovicTech
Ай бұрын
It's ReSharper
How to use factory pattern for large property?
@MilanJovanovicTech
Ай бұрын
What do you mean by larger property?
@DesaiBharat702
Ай бұрын
If a class have more than property around 20 so in entity we mark property as private and create a factory method it difficult to set all twenty property using factory
@Locustfiretree
Ай бұрын
@@DesaiBharat702 Builder pattern is one option, or you could consider the class is doing too much if there are too many properties.
Would you cache the dto or the entity?
@MilanJovanovicTech
Ай бұрын
Preferably DTO
Which project should I put `CacheAside` class in Clean-Architecture?
@MilanJovanovicTech
Ай бұрын
Where do we place external concerns?
@phw1009
Ай бұрын
@@MilanJovanovicTech Oh, I guess Infrastructure...? Then, should I put it inside Persistence project?
@MilanJovanovicTech
Ай бұрын
@@phw1009 I would usually place it inside Infrastructure
What if the data being cached gets updated within the 2 mins window? Since we are not updating the cache on write wouldn't we get a stale value from the cache? While I understand that we shouldn't be caching all writes but can't we do this? When writing to DB, check if the value is present in the cache. If it is present, update the value in cache, otherwise do nothing.
@MilanJovanovicTech
22 күн бұрын
Invalidate the cache and update (write through). Or just invalidate the cache and let it be cached again on next request.
@GameSteals
22 күн бұрын
@@MilanJovanovicTech Thank you! That makes sense.
Hi, can I ask something, why don't use in memory cache with redis, we should check in memory first if not found then goto redis, in that way we can save network call. I don't know, may be I am missing something because I have never seen this approach. Could you please advise?
@MilanJovanovicTech
18 күн бұрын
HybridCache will take care of this
@InshuMussu
17 күн бұрын
@MilanJovanovicTech yes thank you, since HyridCache is introduced in .net 9, before .net 9 it should be using by manually combining both inmemory and redis for the low latency, this approch seems very powerful to me but it's not commonly discussed in internet don't know if there is drawback..
I need a sample Blazer project with a report generate. pls
@MilanJovanovicTech
Ай бұрын
Won't find it in this video
@andersjuul8310
Ай бұрын
😂@@MilanJovanovicTech
You missed a return if your data was fetched from cache
@MilanJovanovicTech
Ай бұрын
Where?
@JoaoSilva-rz4js
Ай бұрын
When first applying the cache on the minimal API. It works on the example because you press continue. Yourre just calling results.ok not actually returning
@JoaoSilva-rz4js
Ай бұрын
Minute 7:57
@MilanJovanovicTech
Ай бұрын
@@JoaoSilva-rz4js Oh lol, you're right. Typo.