DisSysLab

Model of a distributed system

Time ~45 minutes Part 1 this page only

SECTION 1A model of a distributed system

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.

Types 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:

The news filter: an office of six agents al_jazeera and bbc_world are sources; console_printer and discard are sinks. Two connections end at Felix's inbox. Rex, a router, has two outboxes, yes and no. Felix is a basic agent which plays the role of topic filter. al_jazeera source agent out bbc_world source agent out Felix topic_filter in out Rex router in yes no console_printer sink agent in discard sink agent in
inbox outbox
Figure 1. The news filter: an office of six agents (the gallery's 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.

Figure 2. Message propagation.

The diagram shows the following actions, which proceed asynchronously.

  1. X puts messages on its outbox.
  2. Messages from X's outbox are put on the channel.
  3. Messages from the channel are put in Y's inbox.
  4. Y gets messages from its inbox.

The next diagram illustrates an outbox connected to two channels. The following diagram shows two channels connected to the same inbox.

Figure 3. Broadcast: one outbox, two connections.
Figure 4. Merge: two connections, one inbox. Messages from channel A keep their order relative to each other, and so do messages from B. The relative order by which messages from channels A and B are placed in the inbox is unspecified.

Agent Actions.

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.

  1. Wait for inbox in to become non-empty and then remove a message from in and put the message in outbox out.
  2. Wait for inbox 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.

Figure 5. What a gate does. The gate waits on exactly one inbox at a time. While it waits on done, messages arriving at in accumulate and are not processed. A message from in is put on out; a message from done puts nothing anywhere, and only changes which inbox the gate waits on next. A done comes back only after the message the gate passed on has left out, so there is at most one at a time.

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.

Figure 6. What a router does. The field the router reads, here on_topic, was written by an agent before it: the router only reads it. Each message goes on, unchanged, on exactly one outbox: yes or no.

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.

Figure 7. What an annotator does. Each message goes on with one field added, here on_topic. Exactly one message leaves for each message taken, and it is the same message: everything it carried, it still carries.
Figure 8. What an author does. Each message taken goes no further. The author puts zero, one or more new messages (green) on its outbox; nothing of the message it received leaves unless the author writes it into a new one.

SECTION 2A Model of Computation

The Actions of a System

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.

  1. Outbox action If the outbox is empty then the action does nothing. If the outbox is not empty then the action removes one message from the outbox and put copies of the messages in all the channels to which the outbox is connected.
  2. Channel action If the channel is empty then the action does nothing. If the channel is not empty the action removes one message from the channel and puts a copy of the message in the inbox to which the channel is connected.

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.

Figure 9. Selecting any action. Actions of the office of Figure 2,represented as balls in a jar. The bars record how many steps have passed since each action was last drawn. Press never the channel and watch what an unfair selection looks like for a finite number of steps — one bar climbing for ever while the office does nothing. Press still producing to stop the source, and eventually every action becomes a skip: the terminated state -- a state that remains unchanged as computation proceeds.

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.

SECTION 3Correctness and Performance

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.

SECTION 4Exercises

Task 1 · Message Ordering

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?

What you should have found

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.

Task 2 · What does X know?

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?

What you should have found

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.

Task 3 · Can a message overtake another?

In Figure 2, try to get m5 into Y's inbox before m4. Use any combination of the sliders and Step you like.

What you should have found

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.

Task 4 · Make an agent wait for ever

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?

What you should have found

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 5Four facts, which are really axioms

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.

  1. Every message is somewhere definite. A message is in exactly one outbox, one connection, or one inbox, and nowhere else. Broadcast is the one thing that makes more of them: an outbox connected to two channels yields one copy in each, and from that moment they are two messages with two independent fates. Nothing is ever shared, and no message is in two places at once.
  2. Messages on one connection keep their order. Per connection. Not across connections, and not across paths.
  3. There is no clock the agents share. Each action happens when it happens. "At the same time" is not a statement you are entitled to make about two different agents.
  4. An agent knows only what is in its own boxes. It cannot see inside a connection, and it cannot see inside another agent. Anything else it wants to know, it must be told — by a message, which is subject to axioms 1 to 3.

SECTION 6Now the question

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.

Ask them.
Figure 10. Asking every agent whether it is idle. The office of Figure 2, frozen: both agents have finished everything they were given, and one message is still in the connection between them.
Every agent is idle. Every agent's answer is true. The office is not finished.

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.

You cannot photograph a system whose parts are all still moving by photographing them one at a time.

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.

SECTION 7Before the first class

  1. The four axioms written down somewhere you will find again.
  2. One sentence: the thing I would like to have watched. It will become your own office. It does not have to be right — it has to be yours.