DianaIonita/serverless-api-gateway-caching

headers caching for multi-tenant

Opened this issue · 8 comments

Hi!
We have a SaaS app and we use a custom header to define the customer-id and send that to the APIs we have.
In every request, we have that customer-id header.
When I enable caching, data from one customer is shown in another customer, like it's not respecting the customer-id key I set in every function.

For instance:

    events:
      - http:
          method: GET
          request:
            parameters:
              querystrings:
                module: true
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.customer-id
              - name: request.querystring.module

What am I doing wrong?. Thanks!

Hi @fernandohonig,

Thanks for raising the issue.
Can you please confirm via the AWS Console that caching is enabled for the API Gateway you're using in this project and that the customer-id cache key parameter is set?

For example:

image

here I set a "limit" query string parameter:
image

Hi @DianaIonita, thanks for taking the time.

Yes, it’s enabled but the difference is that the caching is not on the query string as shown in your picture, but in the request headers.

@DianaIonita any ideas about this?. Thanks.

Hi @fernandohonig,

Apologies for the delay. Here's what I tried:

  • I installed the latest version of this caching plugin
  • I enabled caching:
plugins:
  - serverless-api-gateway-caching

custom:
  apiGatewayCaching:
    enabled: true
  • I added it to my function:
functions:
  list-cats:
    handler: rest_api/cats/get/handler.handle
    events:
      - http:
          path: /cats
          method: get
          caching:
            enabled: true
            cacheKeyParameters:
              - name: request.header.customer-id
  • when listing the response, I also added a response timestamp, so it generates a fresh one each time the lambda is invoked

  • then I tested if it worked:

    • when calling this endpoint with header customer-id=123, it returns response timestamp A
    • calling it with header customer-id=456 returns response timestamp B
    • calling it again with header customer-id=123 returns response timestamp A again, which is expected
    • calling it again with header customer-id=456 returns response timestamp B, also as expected

In your configuration, can you tell if anything differs from what I tried?

Hi @DianaIonita, thanks again for taking the time. Does this only work with GET or also POST methods? (Some functions we have despite using a POST method, are used to get information.

Also, is request.header case sensitive? If I am sending "Customer-Id" the name: needs to be "Customer-Id" or it could be "customer-id" ?

Hi @fernandohonig,

All cache key parameters are case-sensitive, yes, this is how API Gateway works. If you call an endpoint with header customer-id=123, that will creates a cache entry, and if you call it with header Customer-Id=123, it will create another entry.

Caching works with both GET and POST methods.

So then I don’t understand why it doesn’t work on my end. I have it configured just like you but when I execute a get or a post with 1 Customer-Id and then try the same with another Customer-Id the results for the second one is the same as the first one.

Hey @fernandohonig,

Can you try reproducing it in a very simple public repository so that I can debug based on that?