The idea: think like a vertex

A graph with billions of vertices does not fit on one machine, and graph algorithms touch it again and again. Pregel (Google, 2010) splits the graph over many workers and asks the programmer for one function, compute(), written from the point of view of a single vertex. In each round a vertex reads the messages sent to it, updates its own value, sends messages along its edges with sendMessageTo, and may call voteToHalt(). The framework runs that function on every vertex, on every worker, in parallel.

Timeline of three workers over supersteps 0, 1 and 2. In each superstep every worker runs compute() for its vertices, for different lengths of time. A red dashed barrier line ends each superstep. Blue arrows show messages sent in one superstep arriving in the next.
Every superstep is compute, then messages, then a barrier. Messages sent in superstep S are read in superstep S + 1.

Supersteps and the barrier

The rounds are called supersteps. In superstep S every active vertex runs compute() once. The messages it sends are not delivered at once: they wait in the worker's buffer until every worker has finished superstep S. That wait is the barrier. Then the messages are delivered, and superstep S + 1 begins. This model is bulk synchronous parallel (BSP). Its rule is simple: inside a superstep nobody sees anyone else's change, so there are no locks and no races. The price is that the slowest worker sets the pace.

The page runs 8 vertices (ids 0 to 7) on 3 workers. Each vertex lives on worker id mod 3, the default hash partition. The undirected edges are pairs of directed edges, one each way. The graph has two parts that are not connected: {0, 1, 2, 3, 4} and {5, 6, 7}.

Vote to halt

In superstep 0 every vertex is active. A vertex that calls voteToHalt() goes to sleep (grey on the canvas). It is not called again until a message arrives for it; then it wakes up and computes in the next superstep. The job ends when every vertex is halted and no message is in flight. No master has to decide that the answer is ready: the vertices decide it together.

Two states of a vertex. Active goes to halted when the vertex calls voteToHalt(). Halted goes back to active when a message arrives. The job ends when every vertex is halted and no message is in flight.
A vertex sleeps after voteToHalt() and a message wakes it. The job ends when all sleep and no message moves.

Shortest paths, one superstep at a time

For single-source shortest paths every vertex starts at distance ∞. In superstep 0 only the source finds something better (0) and sends its distance + edge weight along every edge. A vertex that receives a smaller distance keeps it and tells its neighbours; then every vertex votes to halt. From vertex 0 the job runs 6 supersteps and sends 2, 6, 7, 4, 2 and 0 messages, 21 in all (Demo: shortest paths from 0). Vertex 2 first hears 9 (via 1), then 8 and 7 (via 4, each time 4's own distance has improved): unlike Dijkstra, Pregel does not process vertices in order of distance. It relaxes all edges in parallel, like Bellman-Ford, and a value can improve several times.

From vertex 5 the vertices 0 to 4 compute only in superstep 0. No message ever reaches them, so they stay halted at ∞ while 5, 6 and 7 finish in 4 supersteps with 4 messages (Demo: source 5, the rest never wakes). Work is done only where messages go.

Connected components by min label

For connected components each vertex starts with its own id as its label and sends it to its neighbours. A vertex that hears a smaller label takes it and passes it on. After 4 supersteps every vertex holds the smallest id in its part of the graph: label 0 for {0, 1, 2, 3, 4} and label 5 for {5, 6, 7} (Demo: connected components by min label). It takes 16, 12, 5 and 0 messages. The number of supersteps grows with the longest distance a label has to travel, not with the size of the graph. On one machine a BFS or DFS does the same job.

Workers, messages and combiners

A message to a vertex on the same worker (green) is a memory copy. A message to a vertex on another worker (blue) crosses the network, and on a big graph the network is the bottleneck. Here only the edge 0–3 is local, so 30 of the 33 messages of the components job are remote.

A combiner merges the messages one worker sends to the same target vertex into one before they leave. It only works when the order of the messages does not matter: for distances and labels the vertex only wants the minimum, for PageRank only the sum. In the components job worker 0 holds vertices 0 and 3, and both send to vertex 1 in superstep 0. The min combiner sends min(0, 3) = 0 once. Remote messages per superstep drop from 14, 11, 5 to 11, 9, 5: 25 on the wire instead of 30, with the same labels (Demo: min combiner cuts network messages). With the shortest-paths job from 0 the combiner saves nothing: no worker ever sends two messages to one vertex in the same superstep.

Two panels. Left, without a combiner: vertices 0 and 3 on worker 0 each send a message (0 and 3) to vertex 1 on worker 1, two messages on the wire. Right, with a min combiner: the two messages pass through a min box on worker 0 and one message, 0, goes to vertex 1.
The combiner runs on the sending worker: two messages for vertex 1 become one.

An aggregator is the other global tool. Every vertex can give it a value in superstep S; the framework combines them (sum, min, max) and every vertex can read the result in S + 1. The page counts changed values with a sum aggregator, and for PageRank it sums the ranks (always 1.000). A real job uses an aggregator to stop when the total change is small.

PageRank in Pregel

The PageRank program never halts on its own messages. In superstep 0 every rank is 1/8 = 0.125. From superstep 1 on, a vertex sets its rank to 0.15/8 + 0.85 × sum(messages). While S < 3 it sends rank / degree on every edge; in superstep 3 it votes to halt. Every vertex is active in every superstep, so each of the 3 rounds sends 16 messages, 14 of them remote, and the sum combiner cuts that to 11 on the wire (33 instead of 42). Vertex 7, the hub of the small part, ends highest with 0.218 (Demo: PageRank, 3 rounds, sum combiner). The Pregel paper runs 30 supersteps.

ProgramSuperstepsMessages sentRemoteOn the wire
shortest paths from 06211818 (combiner saves 0)
shortest paths from 54444
connected components4333030, or 25 with min combiner
PageRank, 3 rounds4484242, or 33 with sum combiner

Partitioning

Hashing ids spreads vertices evenly but ignores the edges: 7 of the 8 edges here cross workers. A partition that keeps neighbours together, for example {0, 1, 2}, {3, 4}, {5, 6, 7}, leaves only 3 edges crossing, and superstep 0 of the components job would send 6 remote messages instead of 14. Finding such a partition for a huge graph is a hard problem of its own, so most systems start with hashing. Hash partitioning of keys is the same idea as in sharding.

Why not MapReduce?

A graph algorithm is a loop. Written as MapReduce jobs, every round reads the whole graph from disk, shuffles it, and writes it back. Pregel keeps each vertex, its value and its edges in the memory of its worker for the whole job; only the messages move. Spark keeps data in memory too, and its GraphX library offers a Pregel API on top of RDDs. Apache Giraph is the open-source Pregel.

See also Jacobi with Halo Exchange, the same compute, exchange, barrier rhythm on a grid instead of a graph.

What the page leaves out

Fault tolerance (workers write checkpoints at the start of some supersteps, and a failed worker's partitions are reloaded and the supersteps since the checkpoint replayed), the master that assigns partitions and runs the barrier, graph changes during a job (adding and removing vertices and edges), directed graphs with dangling vertices, messages that arrive out of order, asynchronous models such as GraphLab that skip the barrier, and vertex-cut partitioning for vertices with huge degree.

On the mind map

Open the Distributed Systems mind mapDistributedSystemsReplication: copies of data · what reads seeReplicationPartitioning: split data over nodesPartitioningTime and membership: order events, know who is aliveTime and membershipConsensus: agree on one value or logConsensusTransactions: one change across many servicesTransactionsMessaging: send events between systemsMessagingKafka internals: a replicated append-only logKafka internalsRedis internals: one thread, data in memoryRedis internalsKubernetes: reconcile desired stateKubernetesResilience and speed: stay up and fast under loadResilience and speedPartitioning vs shardingPartitioning vs shardingone server vs many; shard key mattersConsistent hashingConsistent hashingring: adding a node moves few keysChord DHTChord DHTfingers halve the distance: O(log N) hopsBitTorrent and the DHTBitTorrent and the DHTEvery downloader also uploadsRange vs hash partitioningRange vs hash partitioningrange: fast scans; hash: no hot spotRebalancing partitionsRebalancing partitionsmod N vs fixed vs dynamic: keys movedRedis ClusterRedis Cluster16384 hash slots, MOVED and ASKMapReduceMapReducemap, shuffle by key, reducePregel superstepscompute, message, barrier, vote to haltSpark RDD lineageSpark RDD lineageLazy recipe; shuffle cuts stagesMapReduce joinsMapReduce joinssort-merge, skew, broadcast and merge joinsS3 object storageS3 object storagedata plus parity shards rebuild a fileRendezvous hashingRendezvous hashinghighest hash score of key+node winsHDFS / GFS file layoutHDFS / GFS file layoutNamenode maps blocks; datanodes pipelineNFS vs AFS cachingNFS vs AFS cachingNFS polls GETATTR; AFS gets callbacksNFS vs iSCSI vs NVMe-oFNFS vs iSCSI vs NVMe-oFFile level: server runs the filesystemOpen the Distributed Systems mind mapDistributed Systemssplit data over nodesPartitioningPartitioning vs shardingPartitioning vs shardingone server vs many; shard key mattersConsistent hashingConsistent hashingring: adding a node moves few keysChord DHTChord DHTfingers halve the distance: O(log N) hopsBitTorrent and the DHTBitTorrent and the DHTEvery downloader also uploadsRange vs hash partitioningRange vs hash partitioningrange: fast scans; hash: no hot spotRebalancing partitionsRebalancing partitionsmod N vs fixed vs dynamic: keys movedRedis ClusterRedis Cluster16384 hash slots, MOVED and ASKMapReduceMapReducemap, shuffle by key, reducePregel superstepscompute, message, barrier, vote to haltSpark RDD lineageSpark RDD lineageLazy recipe; shuffle cuts stagesMapReduce joinsMapReduce joinssort-merge, skew, broadcast and merge joinsS3 object storageS3 object storagedata plus parity shards rebuild a fileRendezvous hashingRendezvous hashinghighest hash score of key+node winsHDFS / GFS file layoutHDFS / GFS file layoutNamenode maps blocks; datanodes pipelineNFS vs AFS cachingNFS vs AFS cachingNFS polls GETATTR; AFS gets callbacksNFS vs iSCSI vs NVMe-oFNFS vs iSCSI vs NVMe-oFFile level: server runs the filesystem
This page on the Distributed Systems mind map · 104 pages in 10 branches · open it to see the whole map and test yourself in recall mode.