I have a lambda function which is an api fetching data from the database. I want to add caching to the method itself, with the idea that if the lambda is being invoked it should return the cached response. I understand subsequent invocation might need the data to be fetched again from the database. But if lru_cache is helping reduce the number of hits to the database, I will implement it. Also is there any drawback to using lru_cache ?
Will implementing caching in API gateway is optimum in the scenario.
Or there is some other caching mechanism I should utilize.
from functools import lru_cache
@lru_cache()
@router.get(
"/myapi/v1",
name="My Api",
status_code=status.HTTP_200_OK,
response_model=ApiModel,
)
Lambda has a short live period. Even if you can keep a lambda in a hot state, multiple requests will trigger new lambda instances without any kind of cache.
One option is to enable API Gateway caching. This way the lambda function will be called only once and you will have the option to control how long that data will be valid before re-fetching.
Alternatively it's possible to deploy a CloudFront distribution in front of an API Gateway endpoint. The main reason is to reduce latency because of the following reasons:
Another reason to deploy CloudFront along with API Gateway is increased security capabilities. Currently, AWS Shield Advanced doesn't support enabling protection on API Gateways, but supports CloudFront.
As a summary, it's a best practice to place the CloudFront Distribution in front of the API Gateway and then enable protection on that distribution. But it will depend if you want to deal with another service complexity only for reducing the number of calls to the database.