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

Cache Remote JWKS Fetch #334

Open
danielduan opened this issue Nov 18, 2024 · 3 comments
Open

Cache Remote JWKS Fetch #334

danielduan opened this issue Nov 18, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@danielduan
Copy link

danielduan commented Nov 18, 2024

In the Ruby SDK, WorkOS::UserManagement.load_sealed_session seems to fetch the remote JWKS on every load, adding about 100ms to each authenticated request.

We use Rails and for every authenticated request, we call authorize_request which does the following:

session = WorkOS::UserManagement.load_sealed_session
result = session.authenticate
@user = result[:user]

After implementing WorkOS, we realized all of our authenticated endpoints now take 100ms longer on the backend. It took some digging through our Sentry profiles to find that create_remote_jwk_set was called repeatedly:

@jwks = create_remote_jwk_set(URI(@user_management.get_jwks_url(client_id)))

We host our services on Render.com on GCP's oregon-1 region.

Please advise if there is a better way to authenticate the token or if improvements can be made to the SDK. Thanks!

Screenshot 2024-11-18 at 5 12 29 PM
@PaulAsjes PaulAsjes added the enhancement New feature or request label Nov 18, 2024
@PaulAsjes
Copy link
Contributor

I see the problem, I think we should able to move that to when you initialize the SDK instead of on every session load. Will look into fixing this soon.

@danielduan
Copy link
Author

I see the problem, I think we should able to move that to when you initialize the SDK instead of on every session load. Will look into fixing this soon.

thanks for looking into this! would appreciate if you can prioritize this for us.

@adam-h
Copy link

adam-h commented Dec 3, 2024

We ran into this when we noticed the JWKS call was taking up 30-45% of our request processing time.

For now we've worked around it by adding the following to our initializer (config/initializers/workos.rb):

  # We load and authenticate the sealed session each request.
  # Just loading the JWKS to verify the token was taking up 30% of our server request time!
  #
  # Cache this for 5 minutes to stop having to do that on every request.
  # Ideally we'd cache longer and just update it if we triggered a validation error
  module CacheJWKSet
    private

    def create_remote_jwk_set(uri)
      Rails.cache.fetch("workos-jwk-set", expires_in: 5.minutes) do
        super
      end
    end
  end

  WorkOS::Session.prepend(CacheJWKSet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

3 participants