You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Review remaining considerations around whether import needs to be performed with the cluster shutdown #40798
In the discussion around #39377 there is still concern that any import should be performed with the cluster shutdown - whether it is overwriting an existing realm or adding a new one. The general concern is that caching of the master realm or of the old realm may need to be cleared.
Since the import may be a long running operation and being shutdown introduces an outage window, we should work towards providing an alternative or at least documenting what issues may be seen if the cluster is not shutdown.
Currently the operator does not shut the cluster down, but instead when it completes the import of a new realm, will restart the cluster. This creates a reduction in service, but not a full outage. It's not clear if this stategy is valid given that it does create a window of time for possibly inconsistent behavior.
All events created seem to be related to the new realm, except for the one event ClientAddedEvent that is about the new client created in the new realm
Looking at the code here, it then creates one invalidation:
, which eventually constructs an invalidation for the key realm + REALM_CLIENTS_QUERY_SUFFIX
Interesting enough, this cache key is only created during invalidations, but never used to store anything in it.
There used to be a cached list of all clients in a realm, but we don't have that any more (possibly once we switched to streaming).
A quick glance showed me that we never cache negative lookups, so we should be good here.
So TL;DR: It should be safe to import a realm as long as it doesn't exist and you do not choose to overwrite an existing realm. We can also do some cleanup to the code above.
So this can then be used as an optimization with the Operator, as that AFAIK will not overwrite an existing realm. So we can skip the KC restart afterwards.
The CLI import states:
By default, the --override option is set to true so that realms are always overridden with the new configuration.
So the documentation would need to detail when it is safe to keep Keycloak running, an when not.
Or we eventually go one step further and connect KC running an import from the CLI to a running KC cluster to share its invalidations when importing a realm that overwrites an existing realm. We would then need to do some tuning to prevent rebalancing of caches, but still be able to populate the invalidations in the worker cache. AFAIK that concept is known as an "empty node". Maybe @ryanemerson and @pruivo can comment on this more.
Or we eventually go one step further and connect KC running an import from the CLI to a running KC cluster to share its invalidations when importing a realm that overwrites an existing realm. We would then need to do some tuning to prevent rebalancing of caches, but still be able to populate the invalidations in the worker cache. AFAIK that concept is known as an "empty node". Maybe @ryanemerson and @pruivo can comment on this more.
The mode is called zero-capacity. When true, it allows a new node to join the cluster as a "passive" member which stores no entries. This means no rebalances are performed when the node joins/leaves the cluster, but the zero-capacity node is able to perform cache operations (gets/puts etc).
The Infinispan Operator utilises zero-capacity nodes to perform Backup/Restore operations, with a new pod joining the cluster as a zero-capacity node to initiate the respective action.
@ahus1 thank you for the analysis. I'll create a sub-issue about the operator handling and docs around importing a new realm.
Can you elaborate on what if anything is different if you use the admin api to delete, then create the same realm? Does the generation of admin events make this safe to do on running cluster, or is this still not a safe way to manage realms?
@shawkins - when using the admin API, doing a delete of a realm and then create the realm is safe: The delete will not only delete the data from the database, but will also send notifications via the work cache to all nodes to clear their caches of the respective nodes.
So using the admin API is always safe on a running cluster.
Maybe we can rethink the import also in a different way, that an import using the admin CLI API is the preferred way for a running cluster.
Maybe we can rethink the import also in a different way, that an import using the admin CLI API is the preferred way for a running cluster.
Until #40875 is addressed it may still be advisable to use the import command. Without zero-capacity we could initially recommend deleting the target realms via the admin api rather than overwriting.
After looking things over with #40875 / #41196 it still doesn't appear like a great idea for users to submit large single file imports via the admin api. Unless we change the logic to create a copy of the incoming stream and do two passes, that style of import can easily exhaust the server heap.
There also is no current admin api mechanism for a realm overwrite like the import command does. We have instead update realm (excludes users) and partial import (which lacks federated users). Also the admin api create realm expects the realm to not exist, so it would need to be deleted first to succeed.
Possible next steps:
Provide the import command with admin connection information so that overwrite can issue admin deletes rather than directly deleting the existing realm. Update code and documentation for import of a new realm #40874 suggested manually doing this if the user wants to do overwrite and keep the server online.
Continue to improve memory safety - I need to double check, but it certainly looks like a sufficiently large realm sent to any one of the end points accepting an inputstream could exhaust server memory.
Even if we allow partial import to provide federated users it still seems like we'll have to contend with issue of post user creation handling (importRealmAuthorizationSettings and some organization logic is also dependent upon users existing).
Allow a full memory safe import of single realm file via the admin api.
Activity
ahus1 commentedon Jul 1, 2025
I did some debugging, and this is what I found:
keycloak/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/CacheManager.java
Lines 216 to 219 in 0188d27
ClientAddedEventthat is about the new client created in the new realmkeycloak/model/infinispan/src/main/java/org/keycloak/models/cache/infinispan/events/ClientAddedEvent.java
Lines 43 to 45 in 18a6c79
realm + REALM_CLIENTS_QUERY_SUFFIXSo TL;DR: It should be safe to import a realm as long as it doesn't exist and you do not choose to overwrite an existing realm. We can also do some cleanup to the code above.
So this can then be used as an optimization with the Operator, as that AFAIK will not overwrite an existing realm. So we can skip the KC restart afterwards.
The CLI import states:
So the documentation would need to detail when it is safe to keep Keycloak running, an when not.
Or we eventually go one step further and connect KC running an import from the CLI to a running KC cluster to share its invalidations when importing a realm that overwrites an existing realm. We would then need to do some tuning to prevent rebalancing of caches, but still be able to populate the invalidations in the worker cache. AFAIK that concept is known as an "empty node". Maybe @ryanemerson and @pruivo can comment on this more.
ryanemerson commentedon Jul 2, 2025
The mode is called zero-capacity. When true, it allows a new node to join the cluster as a "passive" member which stores no entries. This means no rebalances are performed when the node joins/leaves the cluster, but the zero-capacity node is able to perform cache operations (gets/puts etc).
The Infinispan Operator utilises zero-capacity nodes to perform Backup/Restore operations, with a new pod joining the cluster as a zero-capacity node to initiate the respective action.
shawkins commentedon Jul 2, 2025
@ahus1 thank you for the analysis. I'll create a sub-issue about the operator handling and docs around importing a new realm.
Can you elaborate on what if anything is different if you use the admin api to delete, then create the same realm? Does the generation of admin events make this safe to do on running cluster, or is this still not a safe way to manage realms?
ahus1 commentedon Jul 2, 2025
@shawkins - when using the admin API, doing a delete of a realm and then create the realm is safe: The delete will not only delete the data from the database, but will also send notifications via the work cache to all nodes to clear their caches of the respective nodes.
So using the admin API is always safe on a running cluster.
Maybe we can rethink the import also in a different way, that an import using the admin
CLIAPI is the preferred way for a running cluster.ahus1 commentedon Jul 2, 2025
Changing this from "task" to "enhancement" as it will otherwise not show up in the release notes.
shawkins commentedon Jul 3, 2025
Until #40875 is addressed it may still be advisable to use the import command. Without zero-capacity we could initially recommend deleting the target realms via the admin api rather than overwriting.
shawkins commentedon Jul 16, 2025
After looking things over with #40875 / #41196 it still doesn't appear like a great idea for users to submit large single file imports via the admin api. Unless we change the logic to create a copy of the incoming stream and do two passes, that style of import can easily exhaust the server heap.
There also is no current admin api mechanism for a realm overwrite like the import command does. We have instead update realm (excludes users) and partial import (which lacks federated users). Also the admin api create realm expects the realm to not exist, so it would need to be deleted first to succeed.
Possible next steps:
cc @vmuzikar @mabartos @ahus1
shawkins commentedon Aug 11, 2025
Marking as resolved given the current subtasks are addressed. See the previous comment for possible next steps.