Skip to content

Detect and expose clock skew between Keycloak cluster nodes within a cluster #54284

Description

@ahus1

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:

  1. Defines the supported clock-skew assumptions and warning thresholds centrally.
  2. 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

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?

Activity

  1. added
    kind/enhancement
    area/infinispan
    on Oct 23, 2026
  2. added
    area/core
    and removed
    area/infinispan
    on Oct 23, 2026
  3. changed the issue type fromtoon Oct 23, 2026
  4. added and removed
    kind/enhancement
    on Oct 23, 2026
  5. 25 remaining items

  6. added a commit that references this issue on Nov 9, 2026
    69527b1
  7. reopened this on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/corekind/weaknesspriority/normalrelease/26.8.3release/27.0.0team/core-sharedteam/production-readiness

Fields

No fields configured for weakness.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions