Quick revision
The short answer
Lost sessions on a node restart point at the clustered caches: since version 17 Keycloak runs on Quarkus and keeps sessions in distributed Infinispan caches, so I would check the cache configuration, the number of owners and whether the nodes still discover each other after the upgrade. Slow logins point at the database and at password hashing cost, which upgrades can raise. To run it reliably we use several nodes behind a load balancer with health and metrics enabled, rolling restarts that wait for cache rebalancing, upgrades rehearsed in staging with production-like data, backups, and realms managed as code.
What to remember
In this answer, we investigated a Keycloak cluster after an upgrade. Lost sessions pointed at the distributed Infinispan caches of the Quarkus-based Keycloak: whether nodes discover each other, how many owners each entry has, and whether restarts wait for rebalancing. Slow logins pointed at the database pool and queries and at password hashing cost raised by the upgrade. To operate it reliably we run several nodes with health and metrics, roll restarts carefully, rehearse upgrades in staging, back up the database and manage realms as code.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Lost sessions on a node restart point at the clustered caches: since version 17 Keycloak runs on Quarkus and keeps sessions in distributed Infinispan caches, so I would check the cache configuration, the number of owners and whether the nodes still discover each other after the upgrade. Slow logins point at the database and at password hashing cost, which upgrades can raise. To run it reliably we use several nodes behind a load balancer with health and metrics enabled, rolling restarts that wait for cache rebalancing, upgrades rehearsed in staging with production-like data, backups, and realms managed as code.
Why this question is asked
A Keycloak cluster that serves 120 applications is a shared dependency: when logins slow down, every product looks broken, and when sessions are lost, users across the company are logged out at once. Upgrades are the most common trigger, because major Keycloak versions change how clustering, sessions and hashing work. Interviewers ask this to see whether we can debug a distributed system we did not write and then turn it into something we run calmly. In this answer, we will recap how Keycloak keeps sessions, investigate the lost sessions, investigate the slow logins, and set up the operating practices that prevent both.
1A Quick Recap of How Keycloak Keeps Sessions
Since version 17, Keycloak is built on Quarkus and configured through keycloak.conf, environment variables and command-line options, rather than the old WildFly XML files. When a user logs in, Keycloak creates a user session and client sessions. In a cluster these live in Infinispan, an in-memory data grid embedded in each node, in distributed caches: each entry is stored on a configured number of nodes, called owners, usually two. Nodes find each other through JGroups using a discovery mechanism, such as a shared database table or DNS on Kubernetes.
So a session survives a node restart only if another node holds a copy. That needs owners of at least two, nodes that actually form one cluster, and time for the cache to rebalance, which means copying entries to restore the number of copies, before the next node goes down. Recent versions can also persist user sessions in the database, which changes this picture again, so check the defaults of your version.
2Investigating the Lost Sessions
The first question is whether the nodes form one cluster at all. If an upgrade changed the discovery stack, for example its default, the nodes may each start a cluster of one. Everything appears to work, because the load balancer's sticky sessions send each user to the same node, until that node restarts and its sessions vanish. The Keycloak logs at startup show the cluster view with its members; with three nodes we expect a view of three.
The second question is the cache configuration. A custom cache configuration file carried over from the old version may set owners to 1, or may not be read at all if its option name changed. The third is the restart procedure: if Kubernetes restarts pods faster than caches rebalance, two nodes holding the same entries can be gone at once.
- Does the startup log show all nodes in one cluster view?YesCheck cache ownersNoFix discovery (stack, ports, DNS, DB table)
- Are distributed caches set to 2 or more owners?YesCheck restart timingNoSet owners, confirm the config file is read
- Do restarts wait for rebalancing to finish?YesCheck session persistence settingsNoSlow the rollout, use readiness checks
- Confirm with a testlog in, restart one node, session still valid
4 steps in order. Play them, or step through them one at a time.
Most lost-session incidents after upgrades stop at the first or second question.
3Investigating the Slow Logins
A login does three expensive things: it reads the user and realm from the database, it verifies the password hash, and it writes sessions to the caches. So we measure each. On the database side we look at the connection pool, whose size is a Keycloak option and may need raising for 120 applications, and at slow queries, since migrations can add tables or change indexes. The Keycloak metrics endpoint and the database's own statistics show both.
Password hashing is the subtle one. Keycloak deliberately makes hashing slow to resist cracking, and recent versions raised the default cost, for example by moving to PBKDF2-SHA512 with far more iterations and later to Argon2; check the release notes of your versions. Existing passwords are usually re-hashed with the new policy when users log in, so after an upgrade every login costs noticeably more CPU, and a node sized for the old cost saturates. The signs are high CPU on Keycloak nodes during login peaks while the database is calm. The fixes are more CPU or nodes, or a deliberate hashing policy agreed with security, not simply lowering it to the old value.
4Operating Keycloak Reliably
With the incident understood, we make it unlikely to recur. We run at least three nodes behind a load balancer across zones, with readiness checks so traffic only reaches a node that has joined the cluster. Health and metrics endpoints are enabled and wired into monitoring, with alerts on login latency, error rates and the cluster size. Restarts are rolling, one node at a time, and the next node waits until the previous one is ready and caches have rebalanced.
Upgrades are rehearsed. We keep a staging cluster with the same topology, import production-like realm exports, run a load test of logins and token refreshes, and read the migration notes for every version we skip, since major versions change configuration options, defaults, themes and sometimes the database schema. We back up the database before each upgrade and know how to restore it, because a schema migration cannot always be reversed.
Finally, realms are managed as code. Client definitions, roles, mappers and flows for 120 applications are too many to change by hand in the admin console. We keep them in Git and apply them with the admin REST API or a tool such as keycloak-config-cli, so every change is reviewed, repeatable in staging, and recoverable after a mistake.
5Follow-up questions
Why did everything work before the restart if the cluster was split?
Because sticky sessions at the load balancer sent each user to one node, which held their session locally. The split only becomes visible when a node goes away and its users land on a node that never had a copy.
Should we just store all sessions in the database?
Recent Keycloak versions can persist user sessions in the database, which makes restarts safe even without cache replication. It shifts load to the database, so we size it for that and test it, and we check whether our version enables it by default.
How often should we upgrade Keycloak?
Regularly and in small steps, staying within supported versions, because security fixes arrive in new releases and jumping several majors at once multiplies the migration risk. A quarterly upgrade rehearsed in staging is a common rhythm.
6Conclusion
In this answer, we investigated a Keycloak cluster after an upgrade. Lost sessions pointed at the distributed Infinispan caches of the Quarkus-based Keycloak: whether nodes discover each other, how many owners each entry has, and whether restarts wait for rebalancing. Slow logins pointed at the database pool and queries and at password hashing cost raised by the upgrade. To operate it reliably we run several nodes with health and metrics, roll restarts carefully, rehearse upgrades in staging, back up the database and manage realms as code.
Open with
For the lost sessions, I start from how Keycloak works since version 17: it runs on Quarkus and keeps user and client sessions in distributed Infinispan caches, each entry on a number of owner nodes.
Then, if they want more
A session survives a restart only if another node has a copy. So I check the startup logs for the cluster view, because an upgrade that changed the discovery stack can leave each node in a cluster of one, hidden by sticky sessions until a restart.
Then I check that the cache configuration still has two or more owners and is actually being read, and that the rollout waits for rebalancing before restarting the next node.
For slow logins, I look at the database pool and slow queries, and at CPU during login peaks, because recent versions raised the default password hashing cost and re-hash on login, which can saturate nodes sized for the old cost.
To operate it reliably, I run at least three nodes across zones behind a load balancer, with health and metrics enabled and alerts on login latency and cluster size, rolling restarts with readiness checks, upgrades rehearsed in staging with production-like realm exports and load, database backups before every upgrade, and realms managed as code with the admin API or keycloak-config-cli.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RFC 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You run a Keycloak cluster for 120 applications. After a Keycloak upgrade, logins slow...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/900dc1b428/