BlogKeycloak

Keycloak Upgrade Failure Modes and Database Rollback

Scott Sternloff

By Scott Sternloff, Senior Enterprise Architect

LinkedIn · Updated · 5 min read

Keycloak Upgrade Failure Modes and Database Rollback
On this page

Short answer

A Keycloak upgrade fails in four predictable places: the database schema migration, the lack of a database rollback, rolling updates that are not allowed for your change, and custom providers, themes or cache files that were not carried over. Your rollback is a restore from backup, so you should test that restore before the upgrade.

A Keycloak upgrade fails in four predictable places: the database schema migration, the lack of a database rollback, rolling updates that are not allowed for your change, and custom providers, themes or cache files that were not carried over. Your rollback is a restore from backup, so you should test that restore before the upgrade.

The upgrade procedure in the guide runs in order: review the migration changes and breaking changes in the release notes, upgrade the Keycloak server, then upgrade the adapters and client libraries. This post sticks to what Keycloak's upgrade guide, update compatibility guide and Operator guide document. If Keycloak runs your logins in production, our Keycloak support page explains how we help when an upgrade goes wrong.

What happens to the database when you upgrade Keycloak?

By default, Keycloak migrates the database schema automatically the first time you start the new version against it. The upgrade guide says to shut down all nodes running the old version first, and that migration is not supported on the default H2 dev-file database.

  • The default migration timeout is 30 minutes. Raise it with the transaction-setup-timeout option if your database is large.
  • Above 300,000 records, automatic migration skips index creation. On databases with non-blocking index support, Keycloak creates the missing indexes in the background after startup; otherwise it logs the SQL for you to apply.
  • Setting the connections-jpa migration strategy to manual lets a DBA apply the schema changes, with downtime.

The database guide adds engine-specific traps: MySQL 8.0.30 and later can fail with error 1068 unless invisible primary keys are disabled, and MySQL or MariaDB tables must share one charset and collation. Details are in the Keycloak database guide.

Can you roll back a Keycloak upgrade?

Not in place. The upgrade guide states that after the upgrade the database schema is no longer compatible with the old server, and that Keycloak does not support rolling back the database changes. To go back, restore the old installation first, then restore the database from your backup.

That makes the pre-upgrade backup the only rollback you have. The guide's preparation steps are to back up the old installation (configuration, themes and so on), back up the database using your database vendor's instructions, and, if XA transactions are enabled, handle open transactions and delete the data/transaction-logs/ directory.

Two side effects matter to users. If persistent-user-sessions is disabled, the guide says all sessions except offline sessions are lost on upgrade. Authentication flows in progress live in internal caches that are cleared at shutdown, so those users start again.

Can you upgrade Keycloak without downtime?

Only for some changes. Zero-downtime upgrades are limited to rolling updates. Keycloak's update compatibility guide defines a rolling update as zero downtime for a deployment of at least two nodes, and says patch releases within the same major.minor stream can use it. The upgrade guide says patch-release rolling updates are supported starting with 26.6.0. Minor and major upgrades need a shutdown.

QuestionWhat the docs say
Check if rolling is possiblekc.sh update-compatibility metadata on the old version, then check on the new one
Exit code 0Rolling update possible
Exit code 3Not possible; shut the deployment down first
Exit code 4Not possible because the rolling-updates feature is disabled
Changes that always need a recreateToggling features such as login-failures, persistent-user-sessions or stateless; changes to --db or --cache options

On Kubernetes, the Operator rolling updates guide lists three strategies. The default, RecreateOnImageChange, scales down the StatefulSet when the image changes. Auto decides from the version and configuration; Explicit follows spec.update.revision. Rolling updates avoid downtime only with two or more replicas, graceful shutdown on TERM and a load balancer that honors readiness. For the surrounding setup, see our production setup on Kubernetes guide.

What breaks in custom providers, themes and cache files?

Anything you copied by hand. The guide tells you to copy providers/ and themes/ from the old installation and all of conf/ except cache-ispn.xml. If you changed the cache file, re-apply your changes to the new version's file instead of copying yours over.

  • Providers: the guide's migration notes list API changes. For example, the internal UserSessionProvider gained new streaming methods, and custom providers should implement them to avoid performance penalties. Read the migration changes for every version you cross.
  • Themes: custom themes must be copied over, and any changed built-in templates, messages or styles need comparing against the new base theme.
  • Mixed versions: one migration note says not to run Keycloak 26.6 alongside older versions, because older ones do not populate a new column and may hit constraint violations.

The 26.8.2 notes also say a rolling upgrade is not supported for one client registration policy change, so a patch-level upgrade still needs a compatibility check.

How do you prove a Keycloak rollback works before you need it?

Restore the backup into a scratch environment and start the old version against it. Test the restore before you rely on it. The upgrade guide documents the order (old installation first, then database), so rehearse exactly that order.

  1. Take the database backup and the copy of the old installation as the guide describes.
  2. Restore both into an isolated environment and start the old version. Confirm logins work for a test realm.
  3. Run the upgrade there: start the new version, watch the migration against the 30-minute timeout and the 300,000-record index threshold, and record how long it took.
  4. Restore the backup again and start the old version to prove the rollback path.
  5. Run update-compatibility if you plan a rolling update, and write down the result.

The timing from step 3 sets your maintenance window; the timing from step 4 sets how long a failed upgrade costs you. For ongoing signals during and after the change, see what to alert on.

When should you bring in help?

Bring in a second pair of hands when the rehearsal fails, when your database is large enough that the migration may exceed the timeout, or when custom providers sit in the login path. AceMQ supports Keycloak operations on community or Red Hat build installs, 24/7 across the Americas, EMEA and APAC, and a senior engineer can review your upgrade plan and rollback test. We do not sell Red Hat subscriptions, and defects in Red Hat's build go to Red Hat. If you want planning rather than incident help, see our Keycloak consulting page.

Sources

Frequently Asked Questions

Does Keycloak support downgrading after an upgrade?

No. The upgrade guide says Keycloak does not support rolling back database changes. To go back, restore the old installation first, then restore the database from the backup you took before upgrading.

How long does a Keycloak database migration take?

It depends on hardware, release and data volume. Keycloak sets a 30-minute migration timeout by default, adjustable with transaction-setup-timeout. Rehearse on a restored copy of production to measure your own number.

Can I do a rolling update for a minor Keycloak upgrade?

The docs describe rolling updates for patch releases in the same major.minor stream, supported since 26.6.0. Minor and major upgrades need the deployment shut down. Run the update-compatibility check to confirm for your change.

Scott Sternloff

Author

Scott SternloffSenior Enterprise Architect

Senior Enterprise Architect at AceMQ. Writes the technical coverage here: RabbitMQ, Kafka and Redis architecture, clustering and migrations.

Talk to an engineer

Need a hand with Keycloak?

Senior engineers who run it in production. Get a quote or a free consultation, no sales pitch.

Free Consultation

Get Expert Eyes on Your Keycloak Environment

Whether you're troubleshooting a production incident, planning a migration, or want a second opinion on your architecture — our team is ready. No pitch, just answers.

Email Us