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
Engineering a MANUAL_UPDATE DB Migration Strategy for Keycloak
#47015
While integrating Keycloak into a high-availability environment, I identified a significant friction point in the official migration strategies.
Keycloak currently offers:
UPDATE: Automates execution but requires that the initial node experiences a significant delay while executing migration scripts.
MANUAL: Generates scripts but requires external human or tool intervention to execute them, which is difficult to scale.
The Problem: There is no native way to tell Keycloak: "Execute the pending migrations now, and exit successfully so my pipeline can proceed to the next stage."
The Conclusion: The Need for MANUAL_UPDATE
To resolve these issues, I concluded that a new migration strategy, MANUAL_UPDATE, is necessary to support "migration-as-code" within containerized environments.
This strategy would decouple the database migration phase from the application runtime phase.
Proposed Logic:
Start the dedicated migration mode: The Keycloak instance is launched with a specific flag (e.g., --spi-connections-jpa--quarkus--migration-strategy=manual_update).
Execution: Keycloak identifies missing changesets and executes the SQL directly against the target DB.
Exit: Upon completion, the process returns a 0 exit code, signaling to the orchestrator (like Kubernetes or Jenkins) that the environment is ready for the actual server nodes to spin up.
Technical Reflection
By proposing this change, I aim to move away from "workaround" scripts that are fragile and version-dependent.
The MANUAL_UPDATE strategy honors Keycloak’s internal Liquibase logic while providing the exit-code signals required by modern infrastructure-as-code practices.
Something like this would have been needed for the description of #41002, but see the comments below as the preferred direction has moved towards refining the server startup behavior instead.
The closest workaround we have to perform a migration, but not start the server are currently:
run an export of some realm that should exist, such as master
or explicitly set the nonserver profile (this should not be considered supported, and could be affected by the resolution of Allow long-running DB migrations on Kubernetes #41002): ./kc.sh --profile=nonserver start ...
We don't need a lot of complexity, but users will require that the workaround remains valid and possibly documented. If at some point in the future for example we throw an exception when nothing is imported via a directory import, that will break this usage.
oh 😢
I assume the import directory behavior was intentional, as it makes total sense to me. I didn't consider my suggestion as a workaround but the proper way to do it.
@shawkins I want to study the idea you have proposed further.
However, I would say that I tested the workaround you suggested locally, and with it, I can achieve the goal I had set.
Also, I would say that I would prefer the export command over the import command for starting the server for the workaround.
Thank you for the quick response.
@arisDir - thank you for raising this topic. I agree that the pipeline you describe could benefit from an additional command.
Still, I'd like to introduce you to the update-compatibaility command that does something related, still different: It tells you if the new version and the new configuration allows for a rolling update, or not.
This can be used to figure if out you should scale down your deployment to zero first, before starting a new instance, or if you can do a rolling update.
Starting with release 26.6, rolling updates for patch releases will be supported.
Let me know if this change of perspective helps you to have a pipeline that fits your needs.
If not, please describe the different steps you have in your pipeline, so we can understand why this might not be a good fit.
I recently noticed this new feature. I want to study it to see how it could benefit me.
But I would definitely like a part related to the database to be included in the rolling updates.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
The Architectural Gap
While integrating Keycloak into a high-availability environment, I identified a significant friction point in the official migration strategies.
Keycloak currently offers:
UPDATE: Automates execution but requires that the initial node experiences a significant delay while executing migration scripts.MANUAL: Generates scripts but requires external human or tool intervention to execute them, which is difficult to scale.The Problem: There is no native way to tell Keycloak: "Execute the pending migrations now, and exit successfully so my pipeline can proceed to the next stage."
The Conclusion: The Need for
MANUAL_UPDATETo resolve these issues, I concluded that a new migration strategy,
MANUAL_UPDATE, is necessary to support "migration-as-code" within containerized environments.This strategy would decouple the database migration phase from the application runtime phase.
Proposed Logic:
--spi-connections-jpa--quarkus--migration-strategy=manual_update).Technical Reflection
By proposing this change, I aim to move away from "workaround" scripts that are fragile and version-dependent.
The
MANUAL_UPDATEstrategy honors Keycloak’s internal Liquibase logic while providing the exit-code signals required by modern infrastructure-as-code practices.All reactions
Replies: 3 comments · 5 replies
Uh oh!
There was an error while loading. Please reload this page.
Something like this would have been needed for the description of #41002, but see the comments below as the preferred direction has moved towards refining the server startup behavior instead.
The closest workaround we have to perform a migration, but not start the server are currently:
./kc.sh --profile=nonserver start ...cc @ahus1 @pruivo
All reactions
Uh oh!
There was an error while loading. Please reload this page.
Why not run the import command with an empty directory? From the source code, it does nothing if no json file is found
keycloak/model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java
Lines 117 to 134 in 56c2721
I tested with an freshly started DB and it created the tables just fine.
@aschei does this work for you?
@shawkins @ahus1 do we need to add more complexity?
All reactions
Uh oh!
There was an error while loading. Please reload this page.
@pruivo yes, this is another valid workaround.
We don't need a lot of complexity, but users will require that the workaround remains valid and possibly documented. If at some point in the future for example we throw an exception when nothing is imported via a directory import, that will break this usage.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
oh 😢
I assume the import directory behavior was intentional, as it makes total sense to me. I didn't consider my suggestion as a workaround but the proper way to do it.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
@shawkins I want to study the idea you have proposed further.
However, I would say that I tested the workaround you suggested locally, and with it, I can achieve the goal I had set.
Also, I would say that I would prefer the export command over the import command for starting the server for the workaround.
Thank you for the quick response.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
@arisDir - thank you for raising this topic. I agree that the pipeline you describe could benefit from an additional command.
Still, I'd like to introduce you to the
update-compatibailitycommand that does something related, still different: It tells you if the new version and the new configuration allows for a rolling update, or not.https://www.keycloak.org/server/update-compatibility
This can be used to figure if out you should scale down your deployment to zero first, before starting a new instance, or if you can do a rolling update.
Starting with release 26.6, rolling updates for patch releases will be supported.
Let me know if this change of perspective helps you to have a pipeline that fits your needs.
If not, please describe the different steps you have in your pipeline, so we can understand why this might not be a good fit.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
I recently noticed this new feature. I want to study it to see how it could benefit me.
But I would definitely like a part related to the database to be included in the rolling updates.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
Should also link in #11678 as it discusses some of the potential relationship with update-compatibility that @ahus1 is referencing.
All reactions