Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headers caching for multi-tenant #137

Open
fernandohonig opened this issue Apr 26, 2024 · 8 comments
Open

headers caching for multi-tenant #137

fernandohonig opened this issue Apr 26, 2024 · 8 comments

Comments

@fernandohonig
Copy link

fernandohonig commented Apr 26, 2024

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!

@DianaIonita
Copy link
Owner

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

@fernandohonig
Copy link
Author

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
Copy link
Author

@DianaIonita any ideas about this?. Thanks.

@DianaIonita
Copy link
Owner

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?

@fernandohonig
Copy link
Author

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

@DianaIonita
Copy link
Owner

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.

@fernandohonig
Copy link
Author

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.

@DianaIonita
Copy link
Owner

Hey @fernandohonig,

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants