Making Your APIs Blazingly Fast in .NET 8!
HTML-код
- Опубликовано: 16 авг 2023
- Use code ARCH15 and get 15% off the brand new Solution Architecture course on Dometrain: dometrain.com/course/getting-...
Become a Patreon and get source code access: / nickchapsas
Hello, everybody, I'm Nick, and in this video, I will show you how you can use the brand you Redis provider to introduce scalable, distributed output caching in .NET and make your APIs and web apps extremely fast!
Workshops: bit.ly/nickworkshops
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet
I have a crow that now says "Hello everybody, i'm Nick". Thanks for all the videos love your channel.
I was very confused how this differentiated from the ResponseCache middleware. Maybe I missed it during your video, but I found the answer in the MS docs for Response Cache:
"Is typically not beneficial for UI apps such as Razor Pages because browsers generally set request headers that prevent caching. Output caching, which is available in ASP.NET Core 7.0 and later, benefits UI apps. With output caching, configuration decides what should be cached independently of HTTP headers."
I know you could probably create a custom policy for it, but it'd be nice if there was an easier way (e.g. a fluent method call) to specify what types of response to cache. For example I'd like to cache Ok responses for the city "London" for 5 minutes, but cache the NotFound responses for the city "asdf123" for several hours or days.
This Is What I Do I Sit On You! , Sit On You! , Sit On You!....
I love to see your passion explaining those things, ty for sharing
Amazing video. Now that I'm working with Docker I just realize how easy it makes development without the need of installing 3rd party sotfware. Obsessed with container technology now
Agree. I was amazed with Docker and it's possibilities, when it comes to additional software or, for instance, integration tests.
Good video.
Side note, the full redis-stack image (omit -server on the end of the image name) also runs Redis Insight in case someone doesn't have Another Redis Desktop Manager.
In Copenhagen we cache the weather output for no more than 10 seconds 🤣
Waited so long for this. Greate job.
Perfect timing! Thanks 🙏 I was just looking at a high latency request today which would benefit from some caching. Question: can you invalidate certain keys? For example if you wanted to fetch fresh weather for London but not Milan, can you use tagging to say “I want to invalidate the cache for ?city=London but keep ?city=Milan”?
Hey, thanks for the video. Can you please explain how to create own sample service with certain options just like any other services we use in startup and configure their options?
there are 3 things in computer science that are hard: 1) cache invalidation 2) off by one errors
Forgot naming things. And scope creep.😂
Is it possibile to evict by key? For example if the weather for London gets updated (let's pretend that it's my API that's updating the weather), could I just delete the cache entry for that specific key (maybe using the query string)?
Great vídeo Nick. I wonder if it works along with Authorization. Once i tried ourput caching in .NET 6 with authorized controllers and it didn't work. It would be great to have the opportunity to vary by Authorization header
If multiple requests come in before the first one is returned, so the rest still call the weatherservice or do they wait for the first one to finish?
Will this be updated in the API course?
Hey Nick. Thanks for the video. Here you are using a simple location string as parameter. How about complex object as parameter? Will the caching work correctly, as the object may differ on every api call?
It don't. You can't even invalidate by param name. It always invalidates everything.
Caching is actually a very difficult problem as soon as the complexity of your API increases even a little bit. Like what about caching endpoints that return lists, what about objects with different keys that can be used for fetching (I guess just duplicate the cache entries per query right), what if those endpoints return an object with lazy loaded fields? I look at caching more as a necessary evil to be used sparingly when absolutely all else fails.
What console extension do you use?
when the cache expires, and you get 100 calls at that endpoint at the same time, will they all go in trying to get data (i.e., requires you to thread lock the access to the data refresh call) or redis has a built-in option to control that access? (i.e., only one call goes in to refresh the data and the remaining 99 wait for the cached data after the first call gets served).
And if redis has such multi-threaded control option built-in, is it scalable or only per instance?
It seems that by default locking is enabled which should prevent the issue you're describing
it is specifically designed for that
As this is intended to facilitate scaling up, and the key used looks to contain the port number as well - this can result in different keys for each instance of the service depending on the infrastructure.
I am wondering how can that be excluded?
And in general: how can I format the key that is created?
Exactly my concern too. With the approach in video, service instances will not share the cache keys at all.
Now that is useful
The problem with caching is to determine how long we can store cached value.
In case of token it is cool because if token would become obsolete then we get error. In case of weather we could return wrong data.
Really COOL!
I do not using it but just wonder if this could useful for translations for example.
I was hoping you will show us the distributed part :)
he did ?
Redis is the distributed cache
This is a nice one, but I'M just wondering where you gat .NET 8 RC1?
It also works with net 7.
Nice feature, How nice could be doing caching from the httpclient? To avoid call the API, is it anything like that?
A Redis IDistributedCache implementation would be your friend here
I remember your video implementing redis along with output cache yourself, microsoft listenned you ;;))
As someone who lives in Saint-Petersburg I can say that the weather could change several times within 5 minutes XDXD
the same in Almaty😂
Can you invalidate for a specific entry? Like only invalidate for the London weather?
Each call is per city in this case, so yes. It depends on how the API's request parameters are set up. A cache entry is recorded per api method and parameter set.
What if you want to return the cached data ordered in a different way? How would you do it?
it is an output cache and depends on request.
if request has an instruction of ordering as parameter (e.g. in query string such as ..&order=desc&...), the cache middleware will scan for such key.
And if not found the mapped delegate will be called and the result will be stored with key that accounts this parameter.
there are two ways - or cache each ordered response (the same tag will help on evicting) or cache unordered and then order on the client side.
also you may decouple fetching and presentation of data, cache just data that fetched from storage by predicate and prepare it to present from api doing necessary ordering always.
What happened if multiple people hit the endpoint at the same moment when it is not cache anymore ? Because the http call is long may it be possible that there is multiple people entering the controller and so make multiple http call ?
Same question
Hey Nick, have you thought of creating a subscription model for dometrain? I’ve take courses from you in the past and I’m wondering if this is something we could expect in a near future now that you are inviting external experts
hi there. could you please share your experience with completed course? if you don't mind to share what courses you have taken and how useful found them. thanks in advance!
I've taken the Minimal API one. It was very helpful at the time because Nick covered the topic in depth.@@insteor
can we add specific redis db based on language culture instead of 0 by default like i need 0 for english 1 for spanish
?
Hey Nick I use output cache in my application, but I would like to have option to invalidate cache by some dynamic value. For example when user with specific id is updated, I would like to evict his entry from cache. With this approach you evicted all 'weather' tagged entries. What if we would like to evict only Milan and leave London in cache? This issue stops me from putting output cache in my whole applicaiton. Is it worth of follow up video?
Instead of using this minimal API approach you want to use the service with dependency injection and cache it by UUID for example. Use the service like he did invalidation.
@@DaminGamerMC I am not using minimal APIs at all. Currently there is no out of the box functionality to invalidate specific entries in this Output cache functionality that I am aware of. IOutputCacheStore has only EvictByTag method. And with tags defined as in the video, you are not able to evict single entry. Only all of entries related to this tag
@@Velociapcior couldn't you just use a different tag for every entry (i.e. the key/id of the user)? Then when you want to invalidate a user, just evictbytag(userId)?
@@EikeSchwass how would you like to do that in attribute?
Would love to have an out of box solution for this!
Trying to implement redis cache right now, because api that I'm creating is being deployed into k8s.
I did it little different and experiencing timeouts when testing with thousands of requests at once.
I believe its somehow thread related? Like threads are waiting to be completed?
I will definettely try this way you showed in video, to see if I'll experience same problem.
If anybody have some experience with this problem, or just have any ideas, please, let me know.
Dude lives in GB and dares to ask 'how much can the weather change in 5 minutes?'! Brilliant! 😂
Hi Nick, but what about Multitenant application ? Does it persist per (token?)
You can customise it yes
@@nickchapsas thanks :)
What if I want to use MS SQL? Is there a nuget package for that?
I don't get the idea of distributed cache since such data as in example can be easily cached in memory to avoid additional network call + cache DB overhead
Today memory is fairly cheap and caching of small sets of data in external system looks redundant
Because you might run multiple instances of your service for scalability. If you cache in memory they'll each end up with different caches and give inconsistent responses.
I memory is the right answer if you only have one instance. It's the wrong answer if you have more.
If you have more than one instance of your application running behind a load balancer, with in-memory caching the work will be need to be ran on each instance for it to be cached locally to that instance. If you use a distributed cache only one of the instances needs to perform the work.
@@kasperstergaard1592 well, it's not that bad, even YT or Twitter does this. It depends on nature of the data
Which IDE are you using ?
JetBrains Rider.
JetBrains Rider
For those like me scratching their heads why output caching doesn't work with authenticated endpoints: you'll need to either redefine base policy or write your custom one.
I've never been a fan of caching results at the endpoint level. What happens if you want to use your Weather service in another endpoint? Each one would be caching different values whereas if your cache logic was in the service it would be available for any consumer.
your API key is visible in the video, just so you know
In older videos he's said "don't bother trying to use it cause it will be invalid by the time I release the video" and I think he stopped bothering to say that now
Yeah they are always invalidated after shooting
@@nickchapsasHey, at least you get extra engagement from these types of comments
@@reikooters ah I see, thanks
@@nickchapsas thanks for clarifying!
The weather doesn't change in 5 minutes? I thought you lived in the UK, so you should know better. :D
lol I wrote this years ago... oh well guess i'll trash it now haha
œ
That looks a good approach to keep sensitive data of users from authorization module, better than use browser's localstorage or session storage.
I don't think it makes sense
.NET8 has nothing to do with redis. Thumb down for that click bait.