2014-02-20 00:58:15 +00:00
---
layout: "docs"
page_title: "Consul Architecture"
sidebar_current: "docs-internals-architecture"
2014-10-19 23:40:10 +00:00
description: |-
Consul is a complex system that has many different moving parts. To help users and developers of Consul form a mental model of how it works, this page documents the system architecture.
2014-02-20 00:58:15 +00:00
---
# Consul Architecture
Consul is a complex system that has many different moving parts. To help
users and developers of Consul form a mental model of how it works, this
page documents the system architecture.
2014-10-19 23:40:10 +00:00
~> **Advanced Topic!** This page covers technical details of
2014-02-20 00:58:15 +00:00
the internals of Consul. You don't need to know these details to effectively
operate and use Consul. These details are documented here for those who wish
to learn about them without having to go spelunking through the source code.
## Glossary
Before describing the architecture, we provide a glossary of terms to help
clarify what is being discussed:
* Agent - An agent is the long running daemon on every member of the Consul cluster.
2014-11-26 12:31:38 +00:00
It is started by running `consul agent` . The agent is able to run in either *client*
2014-02-20 00:58:15 +00:00
or *server* mode. Since all nodes must be running an agent, it is simpler to refer to
2014-11-26 12:31:38 +00:00
the node as being either a client or server, but there are other instances of the agent. All
2014-02-20 00:58:15 +00:00
agents can run the DNS or HTTP interfaces, and are responsible for running checks and
keeping services in sync.
2014-04-17 21:45:53 +00:00
* Client - A client is an agent that forwards all RPCs to a server. The client is relatively
2014-11-26 12:31:38 +00:00
stateless. The only background activity a client performs is taking part in the LAN gossip
pool. This has a minimal resource overhead and consumes only a small amount of network
bandwidth.
2014-02-20 00:58:15 +00:00
2014-11-26 12:31:38 +00:00
* Server - A server is an agent with an expanded set of responsibilities including
participating in the Raft quorum, maintaining cluster state, responding to RPC queries,
2015-03-30 22:07:58 +00:00
exchanging WAN gossip with other datacenters, and forwarding queries to leaders or
remote datacenters.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
* Datacenter - While the definition of a datacenter seems obvious, there are subtle details
that must be considered. For example, in EC2, are multiple availability zones considered
to comprise a single datacenter? We define a datacenter to be a networking environment that is
2014-04-18 01:35:59 +00:00
private, low latency, and high bandwidth. This excludes communication that would traverse
2015-03-30 22:07:58 +00:00
the public internet, but for our purposes multiple availability zones within a single EC2
region would be considered part of a single datacenter.
2014-02-20 00:58:15 +00:00
* Consensus - When used in our documentation we use consensus to mean agreement upon
the elected leader as well as agreement on the ordering of transactions. Since these
2015-03-30 22:07:58 +00:00
transactions are applied to a
2016-01-13 22:44:01 +00:00
[finite-state machine ](https://en.wikipedia.org/wiki/Finite-state_machine ), our definition
2015-03-30 22:07:58 +00:00
of consensus implies the consistency of a replicated state machine. Consensus is described
2016-01-13 22:44:01 +00:00
in more detail on [Wikipedia ](https://en.wikipedia.org/wiki/Consensus_(computer_science )),
2014-11-26 12:31:38 +00:00
and our implementation is described [here ](/docs/internals/consensus.html ).
2014-02-20 00:58:15 +00:00
2016-08-08 16:44:27 +00:00
* Gossip - Consul is built on top of [Serf ](https://www.serf.io/ ) which provides a full
2016-01-13 22:44:01 +00:00
[gossip protocol ](https://en.wikipedia.org/wiki/Gossip_protocol ) that is used for multiple purposes.
2015-03-30 22:07:58 +00:00
Serf provides membership, failure detection, and event broadcast. Our use of these
2014-02-20 00:58:15 +00:00
is described more in the [gossip documentation ](/docs/internals/gossip.html ). It is enough to know
2014-11-26 12:31:38 +00:00
that gossip involves random node-to-node communication, primarily over UDP.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
* LAN Gossip - Refers to the LAN gossip pool which contains nodes that are all
2014-10-14 05:56:41 +00:00
located on the same local area network or datacenter.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
* WAN Gossip - Refers to the WAN gossip pool which contains only servers. These
2014-10-14 05:56:41 +00:00
servers are primarily located in different datacenters and typically communicate
over the internet or wide area network.
2014-02-20 00:58:15 +00:00
2014-11-26 12:31:38 +00:00
* RPC - Remote Procedure Call. This is a request / response mechanism allowing a
client to make a request of a server.
2014-02-20 00:58:15 +00:00
## 10,000 foot view
From a 10,000 foot altitude the architecture of Consul looks like this:
2014-10-06 23:12:17 +00:00
< div class = "center" >
2016-08-02 07:43:06 +00:00
[![Consul Architecture ](/assets/images/consul-arch.png )](/assets/images/consul-arch.png)
2014-10-06 23:12:17 +00:00
< / div >
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
Let's break down this image and describe each piece. First of all, we can see
that there are two datacenters, labeled "one" and "two". Consul has first
class support for [multiple datacenters ](/docs/guides/datacenters.html ) and
expects this to be the common case.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
Within each datacenter, we have a mixture of clients and servers. It is expected
2014-05-03 22:23:16 +00:00
that there be between three to five servers. This strikes a balance between
2014-02-20 00:58:15 +00:00
availability in the case of failure and performance, as consensus gets progressively
slower as more machines are added. However, there is no limit to the number of clients,
and they can easily scale into the thousands or tens of thousands.
All the nodes that are in a datacenter participate in a [gossip protocol ](/docs/internals/gossip.html ).
2014-04-16 04:01:12 +00:00
This means there is a gossip pool that contains all the nodes for a given datacenter. This serves
2014-11-26 12:31:38 +00:00
a few purposes: first, there is no need to configure clients with the addresses of servers;
2014-02-20 20:26:50 +00:00
discovery is done automatically. Second, the work of detecting node failures
2014-11-26 12:31:38 +00:00
is not placed on the servers but is distributed. This makes failure detection much more
2014-02-20 00:58:15 +00:00
scalable than naive heartbeating schemes. Thirdly, it is used as a messaging layer to notify
when important events such as leader election take place.
The servers in each datacenter are all part of a single Raft peer set. This means that
2015-03-30 22:07:58 +00:00
they work together to elect a single leader, a selected server which has extra duties. The leader
is responsible for processing all queries and transactions. Transactions must also be replicated to
all peers as part of the [consensus protocol ](/docs/internals/consensus.html ). Because of this
requirement, when a non-leader server receives an RPC request, it forwards it to the cluster leader.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
The server nodes also operate as part of a WAN gossip pool. This pool is different from the LAN pool
as it is optimized for the higher latency of the internet and is expected to contain only
2014-02-20 00:58:15 +00:00
other Consul server nodes. The purpose of this pool is to allow datacenters to discover each
2015-03-30 22:07:58 +00:00
other in a low-touch manner. Bringing a new datacenter online is as easy as joining the existing
2018-01-04 21:44:07 +00:00
WAN gossip pool. Because the servers are all operating in this pool, it also enables cross-datacenter
2015-03-30 22:07:58 +00:00
requests. When a server receives a request for a different datacenter, it forwards it to a random
server in the correct datacenter. That server may then forward to the local leader.
2014-02-20 00:58:15 +00:00
2014-02-20 01:05:57 +00:00
This results in a very low coupling between datacenters, but because of failure detection,
2014-04-09 18:06:27 +00:00
connection caching and multiplexing, cross-datacenter requests are relatively fast and reliable.
2014-02-20 00:58:15 +00:00
2017-08-04 23:14:39 +00:00
In general, data is not replicated between different Consul datacenters. When a
request is made for a resource in another datacenter, the local Consul servers forward
an RPC request to the remote Consul servers for that resource and return the results.
If the remote datacenter is not available, then those resources will also not be
available, but that won't otherwise affect the local datacenter. There are some special
situations where a limited subset of data can be replicated, such as with Consul's built-in
[ACL replication ](/docs/guides/acl.html#outages-and-acl-replication ) capability, or
external tools like [consul-replicate ](https://github.com/hashicorp/consul-replicate ).
2014-02-20 00:58:15 +00:00
## Getting in depth
2014-11-26 12:31:38 +00:00
At this point we've covered the high level architecture of Consul, but there are many
2015-03-30 22:07:58 +00:00
more details for each of the subsystems. The [consensus protocol ](/docs/internals/consensus.html ) is
documented in detail as is the [gossip protocol ](/docs/internals/gossip.html ). The [documentation ](/docs/internals/security.html )
2014-04-16 04:01:12 +00:00
for the security model and protocols used are also available.
2014-02-20 00:58:15 +00:00
2015-03-30 22:07:58 +00:00
For other details, either consult the code, ask in IRC, or reach out to the mailing list.