Search code examples
sessionkeycloakopenid-connectinfinispan

How to make keycloak sessions survive server restarts or upgrades?


Keycloak configuration and data is stored in a relational database, which is usally persisted to the hard disk. This includes data like realm settings, users, group- and role-memberships, auth flows and so on. But the user sessions will only be stored in an ephemeral in-memory infinispan cache. Therefore the session data in this cache is lost, when the keycloak server restarts.

There are many reasons why a restart of the Keycloak server is required. Major OS upgrades, Keycloak server upgrades to new versions, applying changes to keycloak e-mail templates or re-scheduling keycloak pods to other worker nodes in kubernetes or other cloud-based environments.

How to persist the session data to survive restarts. Ideally without having to maintain a custom infinspan server or using keycloak "offline sessions".


One solution could be to simply use so-called keycloak "offline sessions", but these sessions also have huge disadvantages:

  • they remain valid, even if the user logs out
  • logging out users with the keycloak admin console is no longer possible

See: https://www.keycloak.org/docs/latest/server_admin/#_offline-access


Will this problem still be present when keycloak > 17 is out and uses the all new quarkus distribution? Because in the following articles claim goals like Container-First Approach, Zero-Downtime Upgrade and Storage re-architecture.


Solution

  • With Keycloak 25, there is a preview feature “persistent user sessions”, which stores the user sessions in its database. If a session is not found in memory, it is loaded from the database, and the user can continue to use their session without the need to re-authenticate.

    Source: https://www.keycloak.org/2024/06/persistent-user-sessions-in-preview.html