serverless-api-gateway-caching icon indicating copy to clipboard operation
serverless-api-gateway-caching copied to clipboard

headers caching for multi-tenant

Open fernandohonig opened this issue 1 year ago • 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!

fernandohonig avatar Apr 26 '24 16:04 fernandohonig

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

DianaIonita avatar Apr 29 '24 14:04 DianaIonita

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.

fernandohonig avatar Apr 29 '24 18:04 fernandohonig

@DianaIonita any ideas about this?. Thanks.

fernandohonig avatar May 14 '24 08:05 fernandohonig

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?

DianaIonita avatar May 14 '24 18:05 DianaIonita

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" ?

fernandohonig avatar May 15 '24 09:05 fernandohonig

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.

DianaIonita avatar May 17 '24 16:05 DianaIonita

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.

fernandohonig avatar May 19 '24 09:05 fernandohonig

Hey @fernandohonig,

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

DianaIonita avatar May 19 '24 18:05 DianaIonita