Unit 5 · Distributed Computing¶
1. Setting the scene¶
Scenario: One server is not enough
SensorGrid produces about 35 GB of raw readings per day and 12.6 TB per year. The data science team wants to train on three years of data. The best single server the city can buy has 1 TB of RAM and 64 cores.
Worse: at 2 a.m. last Tuesday that server's power supply failed. For 40 minutes, no alerts were processed at all.
The team must leave the single machine behind: same goal, bigger scale, more constraints.
Guiding questions
- What changes when workers no longer share memory?
- Why is the network, not the CPU, usually the bottleneck?
- How does a system keep computing when machines fail?
2. Intended learning outcomes¶
| # | Outcome | Bloom | CLO |
|---|---|---|---|
| ILO 5.1 | Distinguish parallel from distributed computing, and shared from distributed memory. | UNDERSTAND | 3 |
| ILO 5.2 | Identify the core challenges of distributed systems. | UNDERSTAND | 6 |
| ILO 5.3 | Compare master worker, client server, peer to peer and microservice architectures. | ANALYSE | 6 |
| ILO 5.4 | Compute communication cost with the latency bandwidth model and justify batching. | APPLY | 6 |
| ILO 5.5 | Select OpenMP, MPI or Spark for a given workload. | EVALUATE | 3 |
3. Where this unit sits in the module¶
This module focuses primarily on parallel computing (threads, OpenMP, MPI, CUDA, performance thinking). Distributed computing is introduced here to show how computation scales beyond one machine and to connect parallelism with real systems and data science. We then return to parallel computing with a deeper perspective.
Mental model
Parallelism = many workers. Distribution = many locations.
Parallel computing speeds up work using multiple compute units. Distributed computing uses compute units that are separate machines connected by a network. The key difference: communication is explicit and expensive (network instead of memory bus).
4. Shared memory versus distributed memory¶
flowchart TB
subgraph SM["Shared memory · one machine"]
direction LR
T1["Thread 1"] --> RAM[("One address space")]
T2["Thread 2"] --> RAM
T3["Thread 3"] --> RAM
end
subgraph DM["Distributed memory · cluster"]
direction LR
N1["Node 1<br/>CPU + own RAM"] <-->|"messages"| N2["Node 2<br/>CPU + own RAM"]
N2 <-->|"messages"| N3["Node 3<br/>CPU + own RAM"]
end
SM ~~~ DM
| Shared memory (OpenMP) | Distributed memory (MPI, Spark) | |
|---|---|---|
| Machines | one, one address space | many, separate memories |
| Communication | shared variables | messages over the network |
| Main cost | cache coherence, contention | latency and bandwidth |
| Typical scale | up to dozens of cores | hundreds to millions of cores |
Same goal: speed. Different bottleneck: memory versus network.
In practice the two nest: a cluster is a distributed system whose nodes are themselves shared memory parallel computers.
5. Why distribute?¶
- Scale out when one machine cannot hold the data or the workload.
- Big data: storage and compute must grow together.
- Reliability: machines fail; the system must keep going.
- Geography: users and data are spread across cloud regions and edge devices.
Rule of thumb
If the data or the workload does not fit on one machine, you distribute.
| Scale up (vertical) | Scale out (horizontal) | |
|---|---|---|
| How | bigger machine | more machines |
| Limit | hardware ceiling, cost grows faster than capacity | practically unbounded, commodity hardware |
| Failure | one failure stops everything | tolerated by replication |
| Complexity | low | high |
6. Core challenges¶
mindmap
root((Distributed<br/>systems))
Communication
network slower than RAM
Partial failures
some nodes fail
No global clock
ordering is hard
Consistency
replicas disagree
Debugging
timing dependent
Distributed computing trades simplicity for scale.
No global clock¶
Each machine has its own clock, and clocks drift. If node A logs an event at 10:00:00.003 and node B at 10:00:00.001, we cannot conclude that B's event happened first. Distributed systems therefore reason with logical time: Lamport clocks assign counters such that if event \(a\) causes event \(b\), then \(L(a) < L(b)\).
sequenceDiagram
participant A as Node A
participant B as Node B
Note over A: event a, L = 1
A->>B: message (L = 1)
Note over B: receive: L = max(0, 1) + 1 = 2
Note over B: event b, L = 3
7. Common architectures¶
flowchart TB
M["Coordinator"] -->|"task"| W1["Worker 1"]
M -->|"task"| W2["Worker 2"]
M -->|"task"| W3["Worker 3"]
W1 & W2 & W3 -->|"result"| M
One coordinator assigns tasks, workers execute and return results. Simple scheduling, but the coordinator can become a bottleneck and a single point of failure. Examples: Hadoop YARN, Spark, Kubernetes.
flowchart LR
C1["Client"] --> S["Server"]
C2["Client"] --> S
C3["Client"] --> S
Many clients request services from servers. Typical for APIs and databases.
flowchart LR
P1["Peer 1"] --- P2["Peer 2"] --- P3["Peer 3"] --- P4["Peer 4"] --- P1
P1 --- P3
No central coordinator; every node is client and server. Better resilience, more complex coordination. Examples: BitTorrent, IPFS, blockchains, Cassandra (Unit 6).
flowchart LR
G["API gateway"] --> I["Ingest service"]
G --> A["Alert service"]
G --> R["Report service"]
I --> Q[("Queue")]
Q --> A
Many small services communicate over the network; each is deployed and scaled independently.
8. Two programming models¶
| Message passing (MPI style) | Dataflow / batch (MapReduce, Spark) |
|---|---|
explicit send and receive |
computation expressed as transformations of datasets |
| fine grained control of communication | the system schedules tasks and moves data |
| HPC simulations, linear algebra | analytics, ETL, machine learning pipelines |
MPI gives you absolute control, but you pay for it. Spark gives you productivity and hides the complexity.
sequenceDiagram
participant R0 as Rank 0
participant R1 as Rank 1
participant R2 as Rank 2
Note over R0,R2: MPI: every step is explicit
R0->>R1: MPI_Send(chunk 1)
R0->>R2: MPI_Send(chunk 2)
R1->>R1: compute partial sum
R2->>R2: compute partial sum
R1->>R0: MPI_Send(partial 1)
R2->>R0: MPI_Send(partial 2)
R0->>R0: combine
9. Performance basics: latency and bandwidth¶
- Latency \(\alpha\): the fixed start up cost of a message.
- Bandwidth: how fast bytes flow; \(\beta\) is the time per byte (\(1/\text{bandwidth}\)).
Worked example: batching messages
\(\alpha = 50\ \mu\text{s}\) (network latency), bandwidth \(1\ \text{GB/s}\) so \(\beta = 1\ \text{ns/byte}\). Send 1 MB of sensor readings.
1,000 messages of 1 KB: \(1000 \times (50\ \mu\text{s} + 1\ \mu\text{s}) = 51\ \text{ms}\).
1 message of 1 MB: \(50\ \mu\text{s} + 1\ \text{ms} \approx 1.05\ \text{ms}\).
Same data, about 49× faster. Many small messages are worse than fewer large ones.
The effective bandwidth of a message of size \(n\) is
where \(n_{1/2}\) is the message size that reaches half of the peak bandwidth: here \(50\ \mu\text{s} / 1\ \text{ns} = 50\) KB.
xychart-beta
title "Effective bandwidth vs message size (α = 50 µs, 1 GB/s)"
x-axis "Message size (KB)" [1, 10, 50, 100, 500, 1000]
y-axis "Effective bandwidth (GB/s)" 0 --> 1
line [0.02, 0.17, 0.5, 0.67, 0.91, 0.95]
10. Partitioning: the number one skill¶
Split work into partitions across nodes and aim for data locality: compute where the data already is, and avoid shuffles (massive data exchange across the network).
| Pattern | Communication | Example |
|---|---|---|
| Embarrassingly parallel | none | normalise each reading |
| Reduce / aggregate | small: local compute, then global combine | average per district |
| Join / group by | large: shuffle | join readings with sensor metadata by key |
flowchart LR
subgraph N1["Node 1"]
D1[("Partition 1")] --> L1["local sum, count"]
end
subgraph N2["Node 2"]
D2[("Partition 2")] --> L2["local sum, count"]
end
subgraph N3["Node 3"]
D3[("Partition 3")] --> L3["local sum, count"]
end
L1 & L2 & L3 -->|"only 2 numbers each"| G["global mean"]
Each node sends two numbers instead of millions of readings.
11. Fault tolerance: computing when machines fail¶
In distributed systems, failure is normal, not exceptional. With \(N\) machines, each failing independently with probability \(p\) on a given day,
For \(p = 0.001\) and \(N = 1000\): \(1 - 0.999^{1000} \approx 63\,\%\). On most days, something breaks.
Keep copies of data or services. If each of \(r\) replicas is unavailable with probability \(p\), the data is unavailable only if all are:
Fast recovery, higher resource cost.
Save progress periodically; after a failure, re run failed tasks from the last checkpoint.
gantt
title Checkpoint and restart
dateFormat X
axisFormat %s
section Job
compute :0, 4
checkpoint :milestone, 4, 4
compute :4, 7
failure :crit, milestone, 7, 7
restart from checkpoint :crit, 7, 10
compute :10, 12
A classic rule for the checkpoint interval is Young's approximation \(\tau \approx \sqrt{2\,\delta\,M}\), with \(\delta\) the time to write a checkpoint and \(M\) the mean time between failures.
HPC assumes failures are rare and costly, so it saves state. Big data assumes failures are normal, so it recomputes.
12. A data science example: hybrid parallel and distributed training¶
Scenario: train a model on a dataset too large for one machine.
flowchart TB
subgraph DL["Distributed level · cluster"]
direction LR
N1["Node 1<br/>data chunk 1"]
N2["Node 2<br/>data chunk 2"]
N3["Node 3<br/>data chunk 3"]
end
subgraph PL["Parallel level · inside each node (OpenMP threads)"]
direction LR
F["feature extraction"] --> P["preprocessing"] --> G["local gradients"]
end
DL --> PL
PL --> AR["all reduce: average gradients"]
AR --> U["update model on every node"]
Each node \(p\) computes a gradient \(g_p\) on its chunk; the global gradient is the average
Modern systems use hybrid parallelism: cluster + threads (+ GPUs).
13. When to use what?¶
flowchart TB
Q1{"Does the data fit in<br/>one machine's memory?"} -->|"yes"| Q2{"Shared data structures<br/>convenient, low effort?"}
Q2 -->|"yes"| OMP["OpenMP / threads"]
Q2 -->|"no"| Q3
Q1 -->|"no"| Q3{"Well defined communication,<br/>peak performance needed?"}
Q3 -->|"yes · scientific computing"| MPI["MPI"]
Q3 -->|"no · ETL, analytics, ML pipelines"| SP["Spark / distributed dataflow"]
14. Classwork: discussion prompts¶
Discuss in pairs (20 min)
- Why is network communication a bigger bottleneck than memory access?
- What is data locality, and how does it affect performance?
- A node fails mid job. Should the system restart the whole job, recompute only the failed tasks, or keep replicas? When is each right?
- Give one example where OpenMP is enough and one where a cluster is required.
Hints
- Network latency is microseconds to milliseconds versus nanoseconds for RAM, and network bandwidth is lower. 2. Moving computation to data avoids transferring large volumes. 3. Restart for short jobs; recompute failed tasks for long batch jobs with deterministic tasks (Spark lineage); replicas for services that must not pause. 4. OpenMP: averaging one day of SensorGrid data (35 GB fits in RAM). Cluster: training on three years (38 TB).
15. Check your understanding¶
Using α = 20 µs and β = 0.5 ns/byte, how long does a 400 KB message take?
\(20\ \mu\text{s} + 400{,}000 \times 0.5\ \text{ns} = 20 + 200 = 220\ \mu\text{s}\).
Why is a group by more expensive than a sum in a distributed system?
A sum needs only a small partial result per node. A group by must move all records with the same key to the same node: a shuffle across the network.
16. Key takeaways¶
- Parallel = many workers; distributed = many locations with explicit, expensive communication.
- Shared memory costs are coherence and contention; distributed costs are latency and bandwidth: \(T = \alpha + \beta n\).
- Partition for locality; avoid shuffles; batch messages.
- Failures are normal: replicate or recompute.
- Real systems are hybrid: clusters of multicore nodes.
17. Link to assessment¶
For any distributed component in your portfolio, document the partitioning scheme, the expected communication volume, and the failure strategy.
Further reading¶
- M. van Steen, A. S. Tanenbaum, Distributed Systems, 4th ed., ch. 1 and 5.
- M. Kleppmann, Designing Data Intensive Applications, ch. 5, 6 and 8.
- L. Lamport (1978), Time, clocks, and the ordering of events in a distributed system, CACM 21(7).