Description
Description
Keycloak relies on node-local wall clocks in multiple sensitive paths, including token and assertion validation, cache entry lifetimes, single-use/revoked-token retention, sessions, scheduled cleanup, and cluster or cross-site event handling. The expected clock-synchronization guarantees and the safety margins that code should apply are not defined in one place. Individual implementations therefore use local constants and different approaches to clock skew.
The system should warn when the clock-skew if off.
Add a shared cluster clock-skew facility that:
- Defines the supported clock-skew assumptions and warning thresholds centrally.
- Estimates clock differences between active Keycloak nodes and reports excessive skew.
Publishing timestamp samples through the cluster work cache could provide a low-overhead first implementation. It would allow a node to log warnings when it detects that other nodes are ahead. The opposite (other nodes being behind) is difficult to estimate due to network latencies.
Value Proposition
Clock skew can create subtle correctness and security failures that are difficult for operators to diagnose. For example, a marker created on a fast node can expire while a slow node still considers the corresponding token or SAML assertion valid. Excessive skew has also affected cross-cluster invalidation targeting in #52649.
Detection and observability would let operators correct NTP or infrastructure problems before they cause intermittent login failures, replay windows, premature expiry, missed invalidations, or delayed cleanup. Shared constants and expiry helpers would also prevent each subsystem from independently implementing incomplete or inconsistent safety margins.
Goals
- Periodically estimate clock offset between current cluster members with bounded overhead.
- Focus on node-ahead time warnings and one-way measurements for simplity.
- Work with the supported embedded and remote Infinispan cluster configurations, degrade cleanly for a single node
- Add deterministic tests using controlled node time offsets.
Optional:
- Log a rate-limited warning when observed skew exceeds the supported threshold. If we actively probe every 60 seconds, this might be implicit
Non-Goals
- Multi-Cluster v1 setups
- Detecting time differences across multiple cluster in multi-cluster v2 setups
- Adjusting an operating system clock or replacing NTP/chrony.
- Compensating for any clock drift.
- Treating approximate application-level measurements as a trusted time source.
- Making authentication or cluster availability depend on a single transient measurement by default.
- Making the clock skew part of the health probe
- Automatically changing protocol-level clock-skew settings such as an identity provider's allowed clock skew.
- Solving database-server clock differences unless the selected design explicitly uses database time as a reference.
Discussion
- Clock skew beyond staleness cutoff suppresses cross-cluster invalidation targeting #52649 documented a concrete cross-cluster invalidation failure caused by node-local timestamps and established an NTP requirement for multi-site deployments.
- Enforce SAML OneTimeUse Condition to Prevent Assertion Replay #54097 highlighted the related need to retain SAML replay markers until no cluster node can still accept an assertion.
Notes
Design with the work cache to detect clocks that are off
- Each member periodically writes its timestamp in millisecond accuracy to the work cache every X seconds
- All members listen to updates in the work cache, and compare the updated values with their internal clock
- If another member is ahead in the time for more than X ms, the receiving node will log a warning
This approach will not do round-trips time measurements, and will not report false positive findings.
Design with database to detect clocks that are off
- Each member periodically selects the current timestamp from the database in millisecond accuracy every X seconds
- If the time received is more than X ms earlier than the time before the request started, and more than X ms later than when the response was received, the node will log a warning
Request started: 12:00:30 (Keycloak time)
Request ended: 12:00:35 (Keycloak time)
Maximum clock skew: 100 ms (as an example)
If Server time returned:
12:00:28.000 -> log a warning
12:00:29.900 -> on the boundary
12:00:30.000 -> OK
12:00:35.000 -> OK
12:00:35.100 -> on the boundary
12:00:37.000 -> log a warning
This approach will do round-trips time measurements, and will not report false positive findings due to network latency.
Open Questions
How much clock skew to allow: Start warnings 100ms, non-configurable. Optionally: Disable it, and log a warning when disabled, or log a warning if configured with a different value.
Should we use the time difference towards the database (Keycloak is never using the database time) or the work cache to establish node-is-ahead-in-time?
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
25 remaining items
Detect and expose clock skew between Keycloak cluster nodes within a …
Document purpose of ClockSkewTimestampSample in this version
Update proto.lock for ClockSkewTimestampSample
Use explicit ProtoField numbers in ClockSkewTimestampSample
Add marshalling for clock skew timestamp sample for mixed cluster com…