A system is a set of agents and a set of connections. Each agent runs in its own thread of execution, and each agent has its own private state. Agents share no memory. The only way in which agents can interact is that an agent can send a message to another agent.
A system is also referred to an office because you can think of a system as an office of agents.
There are five types of agents: sources, sinks, routers, coordinators, and basic agents. Processing occurs in basic agents. When you design a system you will spend most of your time building basic agents.
Inboxes and outboxes.
An agent receives messages from its inboxes and puts outgoing messages
in its outboxes.
Inboxes and outboxes are queues of messages.
Most agents have exactly one inbox, in, and exactly one
outbox, out. There are two exceptions, and they are the
only places in a system where an agent has a choice of boxes:
org_news_filter, the office in the micro-course).
al_jazeera and bbc_world are
sources; console_printer and discard are sinks.
Felix is a basic agent; two connections end at its
inbox. Rex, a router, has two outboxes, yes and
no.Connections: message channels. A connection is a message channel from an outbox of an agent to an inbox of an agent. A connection is specified by a 4-tuple: sending agent, sending agent's outbox, receiving agent, receiving agent's inbox. Each message channel is a queue of messages. The words connection and channel are used interchangeably throughout.
Message propagation. The system removes a message from a nonempty outbox and puts a copy of the message in every channel to which that outbox is connected. The system also removes a message from a nonempty channel and puts the message in the inbox to which the channel is connected.
The diagram shows the following actions, which proceed asynchronously.
The next diagram illustrates an outbox connected to two channels. The following diagram shows two channels connected to the same inbox.
Source Actions. A source receives messages from an external message source such as a social media stream. The source puts messages generated by the external source in its outbox.
Sink Actions. A sink removes a message from its inbox and sends messages to external devices such as actuators, databases, or files.
Coordinator Actions. A coordinator has more than one inbox, and waits on exactly one of its inboxes at a time. The coordinator's state determines which inbox the coordinator waits on. When the inbox that the coordinator is waiting for is non-empty, the coordinator removes one message from the inbox and puts the message on its outbox.
There are different types of coordinators. One of them is the
gate, which has inboxes in and done.
A gate repeatedly carries out the following two operations.
in to become non-empty and then remove
a message from in and put the message in outbox out.
done to become non-empty and then remove the
message from done and discard the message.
The signal done tells the gate that it can allow another message from
inbox in to pass through.
Router Actions A router waits for its inbox to be non-empty and then removes one message from the inbox. The router places the message in the outbox which is specified by a field in the message.
Basic Agent Actions There are two types of basic agents: annotator and author. An author receives a message from its inbox and puts a list (possibly empty) of messages on its outbox. An annotator receives a message from its inbox and adds fields to the message and puts the annotated message - the input message with the additional fields - in its outbox.
The system has three types of actions: agent actions, outbox actions, and channel actions. A computation proceeds by selecting any agent, outbox or channel and executing its action. We have described agent actions. Next we describe outbox and channel actions.
The computation of a system proceeds as follows: for ever execute any action selected nondeterministically.
The figure below illustrates how a computation proceeds. Think of actions of the system (see Figure 2),represented as balls in a jar. Each step of a computation draws any one ball from the jar, executes that action, and puts the ball back in the jar. Do that step forever.
You have probably already asked yourself at least two questions: (1) Can some actions be never selected? (2) If actions are selected infinitely often then how is program termination represented? We give answers to both questions later.
In this course, the specification of a distributed system does not include anything to do with time, and so we do not prove properties about system performance.
We use a weak model of computation because our reasoning applies to all systems that satisfy the model. Our model allows us to reason about systems in which agents execute in Python threads or processes, or Go subroutines, or Erlang processes, and which don't require RTOS (real-time operating systems). We discuss system performance informally without using a computation model.
An example of how we treat performance is a type of agent called an alarm. An alarm has an inbox and an outbox; its action is to remove the message from the inbox, wait for a time specified in the alarm, and then send a message on the outbox. We assume that the implementation attempts to wait for the specified time but we don't rely on that assumption. So, when we reason about a system we specify the action of an alarm as: Remove a message from the inbox and put a message on the outbox. The specification doesn't mention the delay between the agent receiving the set-alarm message and its sending the wakeup message.
A network (office) has three agents X, Y, Z, and a router R. It also has sources and sinks which are not specified here. The connections are:
X -> R R.y -> Y R.z -> Z Z -> Y
R sends a message M1 to Y, and later sends a message M2 to Z. When Z receives M2 it sends M3 to Y. Can Y receive M3 before it receives M1?
Yes. M1 and M3 reach Y's inbox along two different connections,
(R, y, Y, in) and (Z, out, Y, in). That is the
merge of Figure 4: each connection keeps its own order, and the
order between them is unspecified. M1 may still be in its connection while
M2 travels to Z, Z processes it, and M3 travels to Y. Sent first is not
delivered first. You can know what each action does and still
not know which one happens next. Distributed systems are exactly this:
local behaviour that is completely determined, composed into a global
behaviour that is not.
In Figure 2, set connection takes from the outbox to 0. Watch X's outbox fill up and stop. Now answer: at that moment, what does X know about Y?
Nothing. X knows its own outbox is full. It does not know whether the connection is slow, whether Y is slow, whether Y is running at all, or whether every message it ever sent has already been read. A full outbox is consistent with all of those. An agent's knowledge is the contents of its own boxes and nothing else — and that single sentence is the reason the algorithms in this course are hard rather than obvious.
In Figure 2, try to get m5 into Y's inbox before
m4. Use any combination of the sliders and Step you like.
You cannot. Every stage is first-in-first-out, so order is preserved along one connection. Be careful about how far that generalises: it is a property of this connection, not of the office. If X reaches Y by two different paths, or two agents both send to Y, nothing whatever is guaranteed about the order in which those messages arrive. Almost every wrong intuition about distributed systems is this local guarantee being quietly promoted into a global one.
In Figure 5, wait until the gate is waiting on done, then set messages arriving at done to 0 and leave in running. Watch for half a minute. Now answer two questions: is the gate broken, and is anything in its own state different from a moment ago?
The gate is doing exactly what it was told: waiting on one inbox for a
message that is not going to come. Nothing in its state is wrong, nothing
is different from any other wait, and in goes on filling. In an
office, done is sent by an agent further down the line; if
one path never reaches Gate.done, this is what you see. This is the
shape of almost every office that hangs — not a crash, not an error, but
an agent correctly waiting for something that will never arrive. Notice
also that you can see the problem only because you can see both
inboxes at once. The agent cannot. Neither can any other agent.
Section 1 stated the model; the four tasks let you watch it. These four facts are what you should now have seen, and they are the form in which the model gets used. Write them down: everything later in the term is derived from them, and when a proof feels like magic it is usually because one of these was used without being named.
Here is the office of Figure 2 again, frozen. X's outbox is empty. Y's inbox is empty. Both agents have finished everything they were given.
Your office is running. You would like to know whether it has finished, so that you can print the results and stop. No agent can see the whole system — axiom 4 — so you do the obvious thing and ask each agent whether it is idle. Press the button.
The message is in the connection: the one place in the model that belongs to no agent, and that no agent can inspect. The predicate you actually needed is not every agent is idle. It is:
every agent is idle AND every connection is empty
and the second conjunct mentions exactly the state that no agent has access to. That is not a detail of this framework. It is a property of the model, and it would be true of any system built on axioms 1 to 4.
It gets worse, and the second problem is sharper than the first. Suppose every connection is empty. You still have to ask the agents one at a time, because asking is itself a message. Ask X at 10:00 — idle, true. Ask Y at 10:01 — idle, true. Between those two moments Y may have sent X a message and X may have gone back to work. Both answers were true when given. Their conjunction was never true of any single state of the office.
Two of this term's central algorithms are answers to that sentence. Termination detection answers "has it finished?" without a global observer. The distributed snapshot answers "what state was it in?" without ever stopping it — and the reason a snapshot algorithm is subtle at all, rather than being four lines of code, is precisely that it must record the contents of the connections. Look at the picture again. That is where the difficulty lives, and you can see it.
There is a postscript worth having. Getting termination detection wrong does not produce a wrong answer; it produces an office that runs for ever. That happened in this framework: an agent's thread ended without reaching the shutdown protocol, detection waited for an agent that was gone, and the whole office hung. Checking the wiring before running could not have caught it: the wiring was fine. It was a fact about the execution, not about the structure.