‫So now let's talk about
‫how we can enable caching of our responses
‫in the API Gateway.
‫So what is caching?
‫What we know it.
‫Caching is to reduce the number of calls
‫made into the back-end.
‫So that means that our clients
‫when they talk to the API Gateway,
‫the API Gateway will first check the cache
‫and if a result is already in cache,
‫return that result.
‫Otherwise if there's a cache miss,
‫then it will go and talk to the back-end
‫to get the response.
‫So the idea is that we really leverage a lot of the cache
‫to reduce the amount of pressure on our back-end.
‫So the default TTL
‫which is how long the results live in the cache
‫is 300 seconds.
‫So five minutes, but the min is zero.
‫That means no caching and the max is one hour.
‫The caches are defined at the stage level.
‫So you define one cache per stage
‫and it is possible for you to overwrite
‫the cache settings per method.
‫So if you want a method to not use a cache,
‫you can specify it as well at the method level.
‫The cache can be encrypted
‫and the cache size is between 0.5 gigabytes
‫all the way to 237 gigabytes.
‫The cache is really expensive
‫so it makes sense only if you're in production.
‫They may not make sense if you're in development
‫or test environment, for example,
‫because there's not that many requests done
‫on your back-end anyway.
‫So caching is really something
‫you wanna use in production or in pre-production.
‫Now, how do we invalidate the cache?
‫So you are able to invalidate
‫the entire cache immediately from the UI.
‫Or clients can invalidate the cache
‫with the header placed into a query
‫into the API Gateway called Cache-Control max H equals zero.
‫And for the clients to do this,
‫they need proper IAM authorization.
‫So this is an IAM policy
‫that would allow a client to invalidate
‫the cache on a specific resource.
‫And if you don't impose an InvalidCache policy
‫or if you don't require authorization in the consult
‫then any clients can invalidate the API cache
‫and that could be a disaster.
‫So now you know everything about caching.
‫Let's go in the hands-off to see how that works.
‫So let's go into the prod stage and in here
‫I can enable the API cache.
‫Now, if you do enable caching
‫just know that it increases costs
‫and it's not covered by the free tier.
‫So this is not something you may wanna do
‫if you wanna remain with a very low cost API Gateway
‫and account billing.
‫So, cache capacity can be from anywhere
‫between 0.5 gigabyte all the way to 237.
‫I'm not sure why I don't see it in here
‫but it can go to all the way to 237 gigabytes.
‫You can encrypt the cache data.
‫And we can set up a default time-to-live in the cache.
‫For example 60 seconds.
‫Then we can do per cache invalidations.
‫So, do we allow the clients to invalidate the cache?
‫And if so, do we want to require authorization?
‫If not, then any client can invalidate
‫the cache which can be a big problem.
‫Or if you require authorization
‫then what to do if a request is unauthorized.
‫So I can say for now
‫you don't need to require authorization,
‫but it's up to you.
‫And so when this is done,
‫the cache is going to be created for you
‫and you would save the changes and that would be it.
‫So now the cache is enabled for my stage.
‫And so what I can do
‫is I can go and edit the cache setting
‫at each method level.
‫So for example, for the houses
‫if I didn't want to have caching for the houses method
‫then I could override this method
‫and in here I can scroll down
‫and for the cache setting I can, for example,
‫disable the cache method.
‫Or we could change the TTL or just choose to not encrypt
‫or choose to not require authorization.
‫So really it's up to you to wanna customize everything
‫but each method can be customized for the cache.
‫So that's very good.
‫Will say this and this is how we enable caching
‫on our API Gateway
‫and this is seamless for us
‫so if we go into the production and just open it.
‫As we can see right now,
‫we get Hello from Lambda v2.
‫If I refresh, it's going to get it from the cache.
‫And again, get it from the cache
‫and not invoke our Lambda function this time
‫because this result is cached thanks to the cache setting.
‫Now, I don't to incur any charges
‫so I'm going to disable the API cache and save my changes.
‫And here you go.
‫Very simple, but the API Gateway caching
‫is quite a powerful feature.
‫All right, that's it.
‫I will see you in the next lecture.