Plugin development for High Availability (active/active)

Identify when Jenkins plugins work correctly in High Availability (HA) controllers without changes. Review common problem areas, such as writing to $JENKINS_HOME and inspecting the queue, to develop plugins that are compatible with HA.

4 minute read

Many plugins work correctly in a High Availability (HA) controller without changes. In other cases, you should avoid certain Jenkins APIs or adopt different idioms to ensure correct behavior in an HA controller. If your organization maintains custom Jenkins plugins, or Jenkins plugins not in the Cloudbees Assurance Program (CAP), review the following guidelines to understand how the plugins behave in HA mode. For background on how HA works, refer to High Availability (active/active) architecture; for the compatibility status of specific non-CAP plugins, refer to Non-CAP plugin compatibility for High Availability (active/active) controllers.

Common problem areas

Review the following potential problem areas to identify issues your plugin might encounter in HA mode.

This list is not exhaustive. Avoiding these potential issues does not guarantee that your plugin will function correctly in HA mode. Always test your plugin in an HA controller to look for unexpected behaviors.

File writes inside $JENKINS_HOME

If your plugin writes to a file inside $JENKINS_HOME, be aware that multiple replicas might try to write to it at the same time, causing file corruption. This does not apply to settings files, item definitions, or files in a build directory.

Some plugins use on-disk storage in $JENKINS_HOME to buffer data before transmitting it to external services. In an HA setup, multiple replicas might attempt to read from or write to this storage concurrently, leading to potential data corruption. If such data does not need to be collated or aggregated across replicas, the simplest fix is to buffer it in a temporary file or directory instead of inside $JENKINS_HOME, because replicas do not share the temporary storage.

Settings stored outside standard configuration idioms

Some plugins use XML files inside $JENKINS_HOME to store configuration, yet do not use standard Jenkins configuration idioms such as GlobalConfiguration or Descriptor. In an HA setup, direct reads and writes to these files bypass the configuration synchronization hooks, leading to inconsistent state across replicas.

Lookups of in-progress Run objects

If your plugin looks up an in-progress Run object outside a build step, a listener, or other code called from that build’s control loop, the result depends on the replica. On a replica that does not own the build, the lookup may return null, making it appear as though the build does not exist.

Queue inspection

Since the Jenkins queue is divided between replicas, code that inspects it from a single replica may incorrectly assume it has a complete view of all queued jobs. A plugin that schedules a queue item may also fail if it assumes the same replica handles the execution of that item; it may instead have been load-balanced to another replica, with the local copy canceled.

Background tasks

If your plugin runs background tasks, be aware that they might be run on multiple replicas interleaved or even concurrently, so make sure they are idempotent.

Incomplete lists of computers or nodes

If your plugin expects that a list of computers or nodes is exhaustive, it may behave incorrectly when ephemeral agents are created on other replicas.

For example, some cloud plugins attempt to garbage-collect unused external resources, such as VMs, by enumerating Jenkins Slave objects, with the presumption that a resource is unused if there is no matching Slave. If the agent was created by another replica, it may not have a corresponding Slave on the current replica, leading the plugin to mistakenly consider it unused. The correct approach is to track the ownership of external resources in the external system itself, rather than relying on Jenkins-internal metadata. For example, a plugin can update a timestamp on the external resource periodically to indicate it is still in use.

In-memory state

Plugins that maintain significant in-memory state, or locks, may behave incorrectly in HA mode, because each replica runs in its own Java process with its own memory space.

Functions that should work transparently

Many standard Jenkins APIs or plugin functions work correctly in an HA controller without changes.

This is not an exhaustive list. Many other Jenkins APIs work as-is in an HA controller.

Build steps

Typical build steps and publishers, such as a Pipeline Step, generally work correctly in an HA controller, because a single replica owns a build at any given time and all step code is run inside that replica’s Java process. Reading or writing files inside the build directory is safe.

HTML pages and REST endpoints

A plugin may define URLs such as HTML pages or REST endpoints that each replica serves normally. Requests from a web browser are routed via sticky sessions to a single replica as long as it remains available.

REST endpoints and CLI commands are handled by an arbitrary replica, which is fine because these requests are typically stateless.

A webhook is likewise processed by one arbitrary replica, which works for triggering a build (scheduling a queue item) and immediately reporting an HTTP status.

Outbound connections

Each replica may make its own outbound connections to various services independently, provided these are used to send rather than receive information, or that any received information is idempotent.

Items and settings

The list of Item objects, such as Job objects, can be safely inspected from any replica. The same applies to global settings, such as GlobalConfiguration, or more generally to any state saved by a Descriptor.

Occasional user-initiated changes to global and job settings should also work, because they are propagated to other replicas.

Completed builds

Run objects representing completed builds can be safely inspected from any replica. Floating references, such as the last successful build, also work correctly from any replica as long as they refer to a completed build.

Cloud agents

Provisioning single-executor ephemeral agents from a Cloud generally works correctly in an HA controller, as each replica can independently request and manage agents from the cloud. The Slave is automatically marked as owned by the replica that created it and transferred to another replica if the original replica exits and a build using the agent is adopted.

On-disk caches

On-disk caches in a temporary directory are local to each replica and do not interfere with other replicas.