#26624·hazelcast

Lifecycle event when a client reconnects to a cluster with a different cluster id

Author: konfiliosCreated Sep 7, 2026Updated Sep 7, 2026

Please describe the problem you are trying to solve

A Java client configured with a single cluster cannot tell when the cluster it reconnected to is a different cluster from the one it was connected to before.

This happens whenever all members are stopped and started again on the same addresses, for example a full redeploy of the cluster. The new members form a cluster with a new cluster id. The client reconnects, fires CLIENT_CONNECTED, and carries on. From the application's point of view this looks exactly like a short network interruption, but it is not: everything the application registered against the old cluster is gone.

Our setup: a long-running service keeps several QueryCache instances on a client. The cluster is redeployed as a unit on the same hosts and ports, so every redeploy is "same addresses, new cluster id". After some redeploys the caches silently stop receiving updates, because the listener re-registration on reconnect can fail while the new cluster is still starting. The only fix is restarting the service. Today we catch this by reading the cluster id through internal classes on every CLIENT_CONNECTED and comparing it with a cached value.

With a public event we would log the change at INFO as a marker for investigations, and react: recreate the QueryCache instances, or, for state that cannot be rebuilt safely in-process, exit and let the orchestrator restart the process. Which one is an application decision, but both need the event first.

The client already knows. In TcpClientConnectionManager.onAuthenticated it compares the cluster id from the authentication response with ClientClusterService.getClusterId(), logs Switching from current cluster: X to new cluster: Y, calls client.onConnectionToNewCluster() to drop its own state, and initializes itself on the new cluster again. None of that is visible to the application. ClientClusterService is internal API.

CLIENT_CHANGED_CLUSTER does not cover this. It is fired only from destroyCurrentClusterConnectionAndTryNextCluster, when the client has exhausted every address of the current CandidateClusterContext and successfully connected to the next one from a ClientFailoverConfig. A client built from a plain ClientConfig has a single candidate, so tryNextCluster never has anything to try and the event can never fire, however many times the cluster behind the addresses is rebuilt. ClientStateListener also deliberately ignores CLIENT_CHANGED_CLUSTER in stateChanged, which confirms it is not meant as a connection-state signal.

Please describe the desired behavior

Two small additions to the public API:

  1. A new LifecycleEvent.LifecycleState.CLIENT_CLUSTER_ID_CHANGED, fired for a client without failover configuration, after the client is fully initialized on the new cluster, when the new cluster id differs from the previous one. Never fired on the first connection.

    It is complementary to CLIENT_CHANGED_CLUSTER and the two never fire together. The fire site already knows whether the connection came through the failover path (switchingToNextCluster), and a client with a ClientFailoverConfig is already forced through that path on every cluster id change by checkClientStateOnClusterIdChange. So a failover client keeps getting CLIENT_CHANGED_CLUSTER exactly as today, including when the restarted cluster it reaches is a new instance of the one it was on, and a client without failover configuration gets CLIENT_CLUSTER_ID_CHANGED. An application that must react to every cluster identity change, whatever the client configuration, handles both states, with the same listener code if it wants, and never has to reason about event order or keep state between them. The Javadoc of both states says so explicitly. For Community Edition, where failover is not available and CLIENT_CHANGED_CLUSTER therefore never fires, the new state is the only cluster identity signal a client can get.

    ClientStateListener ignores the new state the same way it ignores CLIENT_CHANGED_CLUSTER, so isConnected() and isStarted() keep their meaning.

    The event does not carry the previous cluster id. An application that wants it can cache the id on CLIENT_CONNECTED.

  2. Cluster.getClusterId() returning the current cluster id, or null before the first connection, so a listener can read the new identity. ClientClusterProxy delegates to ClientClusterService; the member side ClusterServiceImpl already has this method.

A listener that reacts to every cluster identity change would then look like this:

java
client.getLifecycleService().addLifecycleListener(event -> {
    switch (event.getState()) {
        case CLIENT_CLUSTER_ID_CHANGED, CLIENT_CHANGED_CLUSTER -> recreateQueryCaches();
        default -> { }
    }
});

I have a working implementation with tests (single-cluster client reconnecting to a rebuilt cluster fires the event, initial connection does not) and can open a pull request if this is welcome.

Describe alternatives you've considered

  • Reading the cluster id through internals (HazelcastClientProxy -> HazelcastClientInstanceImpl -> ClientClusterService) and comparing it on every CLIENT_CONNECTED. This is what we do today. It works, but it depends on internal classes and reflection and breaks whenever those move.
  • Parsing the Switching from current cluster log line. Fragile and not an API.
  • Firing CLIENT_CHANGED_CLUSTER in this case as well. Rejected because its documented meaning is a failover between configured clusters, and existing listeners may react to it by switching credentials or configuration.
  • A dedicated event class carrying both cluster ids and a failover flag, with its own listener type. More API for the same decision; the exclusive lifecycle state gives the distinction with no new types.
  • Restarting the client process whenever the cluster restarts. That is the current workaround and it needs manual intervention.