The connection between the client and the server is based on a TCP persistent connection. The client connects to the server's default port 2181, which is the session.
From the establishment of the first connection, the client begins the session lifecycle. The client sends ping packets to the server. Each session can have a timeout period set.
Session Creation
sessionIDSession ID: used to uniquely identify a session. Each time a client creates a session, Zookeeper allocates a globally unique sessionID to it. Zookeeper creates the sessionID in the source code of the SessionTrackerImpl class.

TimeoutSession timeout: When constructing a Zookeeper instance, the client sends the configured timeout to the server, and the server will ultimately confirm the session timeout based on its own timeout constraints.
TickTimeNext session timeout point, default 2000 ms. It can be configured in the zoo.cfg configuration file to facilitate the server side in implementing sessionbucket strategy management.。
isClosingThis attribute marks whether a session has been closed. When the server detects that the session has timed out and become invalid, the session is marked as "closed", and new requests from this session will no longer be processed.
Session States
The following describes several important states:
-
connectingConnecting: once a session is established, the state is "connecting", and it lasts for a very short time.
-
connectedConnected: the state after a successful connection.
-
closedClosed: occurs when a session expires, usually due to network failure, client reconnection failure, server downtime, or the client actively disconnecting.
Session Timeout Management (Bucket Strategy + Session Activation)
During operation, the Zookeeper leader server periodically performs session timeout checks. The interval is ExpirationInterval, in milliseconds, with a default value of tickTime. A session timeout check is performed every tickTime.

ExpirationTime calculation method:
ExpirationTime = CurrentTime + SessionTimeout; ExpirationTime = (ExpirationTime / ExpirationInterval + 1) * ExpirationInterval;
During Zookeeper operation, the client will send requests (including reads and writes) or ping requests to the server within the session timeout expiration range, commonly known asheartbeat detection.This completes session activation, thereby maintaining the validity of the session.
Session activation process:

After activation, the session migration process is carried out, and then a new round begins:
