Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invalidate AWS APIGateway cache

We have a service which inserts into dynamodb certain values. For sake of this question let's say its key:value pair i.e., customer_id:customer_email. The inserts don't happen that frequently and once the inserts are done, that specific key doesn't get updated.

What we have done is create a client library which, provided with customer_id will fetch customer_email from dynamodb.

Given that customer_id data is static, what we were thinking is to add cache to the table but one thing which we are not sure that what will happen in the following use-case

  1. client_1 uses our library to fetch customer_email for customer_id = 2.
  2. The customer doesn't exist so API Gateway returns not found
  3. APIGateway will cache this response
  4. For any subsequent calls, this cached response will be sent
  5. Now another system inserts customer_id = 2 with its email id. This system doesn't know if this response has been cached previously or not. It doesn't even know that any other system has fetched this specific data. How can we invalidate cache for this specific customer_id when it gets inserted into dynamodb
like image 586
Em Ae Avatar asked Jul 05 '18 15:07

Em Ae


People also ask

How do I clear my API gateway cache?

To flush the API stage cache, you choose the Flush entire cache button under the Cache Settings section in the Settings tab in a stage editor of the API Gateway console. After the cache is flushed, responses are serviced from the integration endpoint until the cache is built up again.

What must a developer customer do to return a result that is not cached from the API gateway?

What must a developer customer do to return a result that is not cached from the API Gateway? Sign their request with a user or role that has the required execute-api:InvalidateCache permissions to invalidate the cache.

How do I REST API cache?

Caching in REST APIs POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

How does API gateway cache work?

When you enable caching for a stage, API Gateway caches responses from your endpoint for a specified time-to-live (TTL) period, in seconds. API Gateway then responds to the request by looking up the endpoint response from the cache instead of making a request to your endpoint.


2 Answers

You can send a request to the API endpoint with a Cache-Control: max-age=0 header which will cause it to refresh.

This could open your application up to attack as a bad actor can simply flood an expensive endpoint with lots of traffic and buckle your servers/database. In order to safeguard against that it's best to use a signed request.

In case it's useful to people, here's .NET code to create the signed request:

https://gist.github.com/secretorange/905b4811300d7c96c71fa9c6d115ee24

like image 105
Lee Gunn Avatar answered Sep 30 '22 16:09

Lee Gunn


We've built a Lambda which takes care of re-filling cache with updated results. It's a quite manual process, with very little re-usable code, but it works.

Lambda is triggered by the application itself following application needs. For example, in CRUD operations the Lambda is triggered upon successful execution of POST, PATCH and DELETE on a specific resource, in order to clear the general GET request (i.e. clear GET /books whenever POST /book succeeded).

Unfortunately, if you have a View with a server-side paginated table you are going to face all sorts of issues because invalidating /books is not enough since you actually may have /books?page=2, /books?page=3 and so on....a nightmare!

I believe APIG should allow for more granular control of cache entries, otherwise many use cases aren't covered. It would be enough if they would allow to choose a root cache group for each request, so that we could manage cache entries by group rather than by single request (which, imho, is also less common).

like image 27
Alberto Dallaporta Avatar answered Sep 30 '22 16:09

Alberto Dallaporta