systemdebt
systemdebt

Reputation: 4941

How to invalidate the cache of an API gateway based on path parameters and query parameters

I have a GET endpoint that looks like this:

GET /tenant/:tenantId/groups?user=userName  

Now, there is a corresponding POST endpoint as well. When the POST endpoint is used to update groups for the given username, I would like to defeat the cache for this GET endpoint with the username.

POST request could be made by any user and not necessarily just this user so that the front-end would not know to call the API with the

Cache-Control: max-age=0

How do we invalidate the cache of an API gateway based on path parameters and query parameters?

Upvotes: 0

Views: 2772

Answers (2)

Mike Dubs
Mike Dubs

Reputation: 729

Are you using DynamoDB to store your data?

If so, you could you use a DynamoDB Stream to trigger a Lambda function that clears the API Gateway cache for that specific record's cache key parameters.

See this AWS resource if you need to set up an IAM policy or create a signed request. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html

Upvotes: 1

lloyd
lloyd

Reputation: 1811

api gw caching is very straight forward

Cache invalidation on the other hand is normally passed in as a header to that endpoint. If you can live with an eventually consistent model then the default of 300 seconds is normally fine.

Assuming that's not good enough and you require the POST to invalidate the other api's you could:

  1. Use elasticache and then you have a single cache that you can invalidate for everything
  2. Run a query against each api gw cache entry for the other endpoints to invalidate them, maybe through a lambda that the POST is handling
  3. Disable caching, performance will probably be impacted
  4. Contact AWS Support and ask what's your options as there's others that I probably missed
  5. Flush the entire cache : aws apigateway flush-stage-cache help
  6. Make everything a POST/PUT, sec guidelines
  7. Use Aws AppSync which eliminates the requirement to handle the caching (Assumes GraphQL instead of REST)

Upvotes: 1

Related Questions