By default, the OpenID Connect plugin caches access tokens until they expire. This means Kong can serve a cached token that is only seconds from expiring, which may cause the downstream request to fail. You can use config.cache_ttl_max to set an upper limit on how long a token stays in cache, so Kong fetches a fresh token before the cached one expires.
How to avoid the OpenID Connect plugin obtaining a token from cache that is almost expired
How do I avoid the OpenID Connect plugin obtaining a token from cache that is almost expired?
Set config.cache_ttl_max on the OpenID Connect plugin to a value lower than the access token’s actual lifetime (as configured in the IDP). Kong will then request a new token from the IDP once the cached token’s age passes that threshold, instead of reusing a token that is about to expire. For example, with a 180s access token lifetime, setting config.cache_ttl_max=165 gets a new token 15s before expiry.
Overview
Steps
Whether Kong fetches a new token from the IDP (such as Okta, Keycloak, or Azure AD) depends on two things:
- The access token lifetime configured in the IDP
- The
config.cache_ttl_maxvalue set on the OpenID Connect plugin
To configure early token refresh:
-
Set
config.cache_tokens=trueon the OpenID Connect plugin to enable token caching. -
Check the access token lifetime in your IDP configuration.
-
Set
config.cache_ttl_maxto the token lifetime minus the buffer you want. For example, with a 180-second token lifetime and a 15-second buffer, setconfig.cache_ttl_max=165(180 - 15 = 165).
With this configuration, Kong fetches a new token from the IDP once the cached token’s age exceeds config.cache_ttl_max, rather than waiting for the token to fully expire.
Example
Using a 180-second token lifetime and config.cache_ttl_max=165:
-
At 0s: The first request reaches Kong. The plugin fetches a new access token from the IDP, caches it, and forwards it to the upstream. The token is valid for 180s.
-
At 150s: Another request reaches Kong. The cached token is 150s old, which is under the 165s limit, so the plugin returns the cached token. The token is still valid for 30s (180 - 150 = 30).
-
At 166s: Another request reaches Kong. The cached token is 166s old, which exceeds
config.cache_ttl_max. The plugin fetches a new access token from the IDP. The new token is valid for 180s.