2014-05-20 00:05:35 +00:00
|
|
|
---
|
2020-04-07 18:55:19 +00:00
|
|
|
layout: docs
|
|
|
|
page_title: Sessions
|
|
|
|
description: >-
|
|
|
|
Consul provides a session mechanism which can be used to build distributed
|
|
|
|
locks. Sessions act as a binding layer between nodes, health checks, and
|
|
|
|
key/value data. They are designed to provide granular locking and are heavily
|
|
|
|
inspired by The Chubby Lock Service for Loosely-Coupled Distributed Systems.
|
2014-05-20 00:05:35 +00:00
|
|
|
---
|
|
|
|
|
2014-05-20 19:22:16 +00:00
|
|
|
# Sessions
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2014-11-05 04:01:45 +00:00
|
|
|
Consul provides a session mechanism which can be used to build distributed locks.
|
2014-05-20 00:05:35 +00:00
|
|
|
Sessions act as a binding layer between nodes, health checks, and key/value data.
|
2015-04-15 18:47:15 +00:00
|
|
|
They are designed to provide granular locking and are heavily inspired by
|
|
|
|
[The Chubby Lock Service for Loosely-Coupled Distributed Systems](http://research.google.com/archive/chubby.html).
|
2014-05-20 00:05:35 +00:00
|
|
|
|
|
|
|
## Session Design
|
|
|
|
|
|
|
|
A session in Consul represents a contract that has very specific semantics.
|
2015-04-15 18:47:15 +00:00
|
|
|
When a session is constructed, a node name, a list of health checks, a behavior,
|
|
|
|
a TTL, and a `lock-delay` may be provided. The newly constructed session is provided with
|
|
|
|
a named ID that can be used to identify it. This ID can be used with the KV
|
|
|
|
store to acquire locks: advisory mechanisms for mutual exclusion.
|
|
|
|
|
2014-05-20 00:05:35 +00:00
|
|
|
Below is a diagram showing the relationship between these components:
|
|
|
|
|
2020-04-07 23:56:08 +00:00
|
|
|
![Consul Sessions](/img/consul-sessions.png)
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2014-11-05 04:01:45 +00:00
|
|
|
The contract that Consul provides is that under any of the following
|
2020-04-06 20:27:35 +00:00
|
|
|
situations, the session will be _invalidated_:
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
- Node is deregistered
|
|
|
|
- Any of the health checks are deregistered
|
|
|
|
- Any of the health checks go to the critical state
|
|
|
|
- Session is explicitly destroyed
|
|
|
|
- TTL expires, if applicable
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2015-01-07 23:38:11 +00:00
|
|
|
When a session is invalidated, it is destroyed and can no longer
|
|
|
|
be used. What happens to the associated locks depends on the
|
|
|
|
behavior specified at creation time. Consul supports a `release`
|
|
|
|
and `delete` behavior. The `release` behavior is the default
|
2015-04-15 18:47:15 +00:00
|
|
|
if none is specified.
|
2015-01-07 23:38:11 +00:00
|
|
|
|
|
|
|
If the `release` behavior is being used, any of the locks held in
|
2015-04-15 18:47:15 +00:00
|
|
|
association with the session are released, and the `ModifyIndex` of
|
|
|
|
the key is incremented. Alternatively, if the `delete` behavior is
|
|
|
|
used, the key corresponding to any of the held locks is simply deleted.
|
|
|
|
This can be used to create ephemeral entries that are automatically
|
|
|
|
deleted by Consul.
|
2014-05-20 00:05:35 +00:00
|
|
|
|
|
|
|
While this is a simple design, it enables a multitude of usage
|
2015-04-15 18:47:15 +00:00
|
|
|
patterns. By default, the
|
2022-01-10 23:36:16 +00:00
|
|
|
[gossip based failure detector](/docs/architecture/gossip)
|
2014-05-20 00:05:35 +00:00
|
|
|
is used as the associated health check. This failure detector allows
|
2015-04-15 18:47:15 +00:00
|
|
|
Consul to detect when a node that is holding a lock has failed and
|
2014-05-20 00:05:35 +00:00
|
|
|
to automatically release the lock. This ability provides **liveness** to
|
2015-04-15 18:47:15 +00:00
|
|
|
Consul locks; that is, under failure the system can continue to make
|
2014-05-20 00:05:35 +00:00
|
|
|
progress. However, because there is no perfect failure detector, it's possible
|
|
|
|
to have a false positive (failure detected) which causes the lock to
|
|
|
|
be released even though the lock owner is still alive. This means
|
|
|
|
we are sacrificing some **safety**.
|
|
|
|
|
|
|
|
Conversely, it is possible to create a session with no associated
|
2015-04-15 18:47:15 +00:00
|
|
|
health checks. This removes the possibility of a false positive
|
2014-05-20 00:05:35 +00:00
|
|
|
and trades liveness for safety. You can be absolutely certain Consul
|
|
|
|
will not release the lock even if the existing owner has failed.
|
|
|
|
Since Consul APIs allow a session to be force destroyed, this allows
|
|
|
|
systems to be built that require an operator to intervene in the
|
2015-04-15 18:47:15 +00:00
|
|
|
case of a failure while precluding the possibility of a split-brain.
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2015-01-07 23:38:11 +00:00
|
|
|
A third health checking mechanism is session TTLs. When creating
|
2015-04-15 18:47:15 +00:00
|
|
|
a session, a TTL can be specified. If the TTL interval expires without
|
2015-01-07 23:38:11 +00:00
|
|
|
being renewed, the session has expired and an invalidation is triggered.
|
|
|
|
This type of failure detector is also known as a heartbeat failure detector.
|
|
|
|
It is less scalable than the gossip based failure detector as it places
|
2015-04-15 18:47:15 +00:00
|
|
|
an increased burden on the servers but may be applicable in some cases.
|
|
|
|
The contract of a TTL is that it represents a lower bound for invalidation;
|
|
|
|
that is, Consul will not expire the session before the TTL is reached, but it
|
2015-01-07 23:38:11 +00:00
|
|
|
is allowed to delay the expiration past the TTL. The TTL is renewed on
|
|
|
|
session creation, on session renew, and on leader failover. When a TTL
|
2015-04-15 18:47:15 +00:00
|
|
|
is being used, clients should be aware of clock skew issues: namely,
|
2015-01-07 23:38:11 +00:00
|
|
|
time may not progress at the same rate on the client as on the Consul servers.
|
2015-04-15 18:47:15 +00:00
|
|
|
It is best to set conservative TTL values and to renew in advance of the TTL
|
2015-01-07 23:38:11 +00:00
|
|
|
to account for network delay and time skew.
|
|
|
|
|
2014-05-20 00:05:35 +00:00
|
|
|
The final nuance is that sessions may provide a `lock-delay`. This
|
2014-11-26 12:49:53 +00:00
|
|
|
is a time duration, between 0 and 60 seconds. When a session invalidation
|
2014-05-20 00:05:35 +00:00
|
|
|
takes place, Consul prevents any of the previously held locks from
|
2014-11-26 12:49:53 +00:00
|
|
|
being re-acquired for the `lock-delay` interval; this is a safeguard
|
2014-05-20 00:05:35 +00:00
|
|
|
inspired by Google's Chubby. The purpose of this delay is to allow
|
|
|
|
the potentially still live leader to detect the invalidation and stop
|
|
|
|
processing requests that may lead to inconsistent state. While not a
|
|
|
|
bulletproof method, it does avoid the need to introduce sleep states
|
2015-04-15 18:47:15 +00:00
|
|
|
into application logic and can help mitigate many issues. While the
|
2014-05-20 00:05:35 +00:00
|
|
|
default is to use a 15 second delay, clients are able to disable this
|
|
|
|
mechanism by providing a zero delay value.
|
|
|
|
|
2015-04-15 18:47:15 +00:00
|
|
|
## K/V Integration
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
Integration between the KV store and sessions is the primary
|
2015-04-15 18:47:15 +00:00
|
|
|
place where sessions are used. A session must be created prior to use
|
2014-11-26 12:49:53 +00:00
|
|
|
and is then referred to by its ID.
|
2014-05-20 00:05:35 +00:00
|
|
|
|
2017-04-04 16:33:22 +00:00
|
|
|
The KV API is extended to support an `acquire` and `release` operation.
|
2015-04-15 18:47:15 +00:00
|
|
|
The `acquire` operation acts like a Check-And-Set operation except it
|
2015-11-14 18:28:14 +00:00
|
|
|
can only succeed if there is no existing lock holder (the current lock holder
|
|
|
|
can re-`acquire`, see below). On success, there is a normal key update, but
|
|
|
|
there is also an increment to the `LockIndex`, and the `Session` value is
|
|
|
|
updated to reflect the session holding the lock.
|
|
|
|
|
|
|
|
If the lock is already held by the given session during an `acquire`, then
|
|
|
|
the `LockIndex` is not incremented but the key contents are updated. This
|
|
|
|
lets the current lock holder update the key contents without having to give
|
|
|
|
up the lock and reacquire it.
|
2014-05-20 00:05:35 +00:00
|
|
|
|
|
|
|
Once held, the lock can be released using a corresponding `release` operation,
|
2018-01-04 21:44:07 +00:00
|
|
|
providing the same session. Again, this acts like a Check-And-Set operation
|
2014-05-20 00:05:35 +00:00
|
|
|
since the request will fail if given an invalid session. A critical note is
|
2014-05-20 19:22:16 +00:00
|
|
|
that the lock can be released without being the creator of the session.
|
2018-01-04 21:44:07 +00:00
|
|
|
This is by design as it allows operators to intervene and force-terminate
|
2014-05-20 00:05:35 +00:00
|
|
|
a session if necessary. As mentioned above, a session invalidation will also
|
2015-01-07 23:38:11 +00:00
|
|
|
cause all held locks to be released or deleted. When a lock is released, the `LockIndex`
|
2015-04-15 18:47:15 +00:00
|
|
|
does not change; however, the `Session` is cleared and the `ModifyIndex` increments.
|
2014-05-20 00:05:35 +00:00
|
|
|
|
|
|
|
These semantics (heavily borrowed from Chubby), allow the tuple of (Key, LockIndex, Session)
|
|
|
|
to act as a unique "sequencer". This `sequencer` can be passed around and used
|
|
|
|
to verify if the request belongs to the current lock holder. Because the `LockIndex`
|
|
|
|
is incremented on each `acquire`, even if the same session re-acquires a lock,
|
|
|
|
the `sequencer` will be able to detect a stale request. Similarly, if a session is
|
|
|
|
invalided, the Session corresponding to the given `LockIndex` will be blank.
|
|
|
|
|
2020-04-06 20:27:35 +00:00
|
|
|
To be clear, this locking system is purely _advisory_. There is no enforcement
|
2014-05-20 00:05:35 +00:00
|
|
|
that clients must acquire a lock to perform any operation. Any client can
|
|
|
|
read, write, and delete a key without owning the corresponding lock. It is not
|
|
|
|
the goal of Consul to protect against misbehaving clients.
|
|
|
|
|
2014-05-20 19:22:16 +00:00
|
|
|
## Leader Election
|
|
|
|
|
|
|
|
The primitives provided by sessions and the locking mechanisms of the KV
|
|
|
|
store can be used to build client-side leader election algorithms.
|
2022-09-06 15:35:01 +00:00
|
|
|
These are covered in more detail in the [Leader Election guide](https://learn.hashicorp.com/tutorials/consul/application-leader-elections).
|
2015-11-14 20:45:34 +00:00
|
|
|
|
|
|
|
## Prepared Query Integration
|
|
|
|
|
|
|
|
Prepared queries may be attached to a session in order to automatically delete
|
|
|
|
the prepared query when the session is invalidated.
|