SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
  • Challenges of Distribution
  • Failure Types: Crash vs Byzantine
  • Network Partitions
  • Split Brain Problem
  • Heartbeats
  • Failure Detectors
  • Handling Failures
  • Consistency Models
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Distributed Systems/Distributed System Fundamentals

Challenges of Distribution

Why many machines are harder than one: slow networks, partial failures and no shared clock.

⏱ 13 min readBeginnerMust KnowHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A distributed system is many computers that work together and look like one system to the user. It gives you more power and no single machine to lose, but it is much harder to get right than one computer.

  • The machines talk over a network, and a network can be slow, lose messages or break.
  • Some machines can fail while others keep working. This is called a partial failure.
  • Every machine has its own clock and its own memory. There is no single place that knows the whole truth.

02Concept: What is it?

On one computer, things are simple. A function call either works or crashes. Memory is shared. There is one clock. If the machine dies, everything dies together, and you know it.

When you spread the work over many machines, you lose these comforts. You can still get the benefits: more capacity, no single point of failure, and servers close to users. But a new set of problems appears, and they are the reason this whole Part exists.

One machine versus many machines
One machine
πŸ“¦Function A
→↓local call
πŸ“¦Function B
Ways the call can failworks, or the whole program crashes
Many machines
πŸ–₯️Service A
→↓network call
πŸ–₯️Service B
Ways the call can faillost, slow, duplicated, half done...
Moving from a call inside one program to a call over a network adds many new ways to go wrong.

The main challenges are:

  1. The network is unreliable. Messages can be lost, delayed, repeated or arrive out of order. You can not tell the difference between "slow" and "dead".
  2. Partial failure. One machine crashes while the others carry on. A request may be done on some machines and not on others.
  3. No shared clock. Each machine's clock drifts a little. You can not always say which of two events happened first.
  4. No shared memory. Each machine only knows its own data. To agree on something, machines must send messages.
  5. Concurrency. Many machines change the same data at the same time.
  6. Consistency. Keeping copies of data the same, when updates take time to travel.
🧠You can not tell slow from dead
If you send a request and get no reply, three things could be true: the request was lost, the machine crashed, or the machine did the work and the reply was lost. From your side they all look the same: silence. This single fact causes most of the hard problems in distributed systems.

03Real-Life Example

🍡Organising a Dinner by Post
Imagine you plan a dinner with five friends, but you may only talk by posting letters. A letter can arrive late, get lost, or arrive twice. A friend may get sick and stop answering. Everyone has their own watch and their watches do not match. You ask, "Is Friday okay?" Two friends say yes, one says nothing, and two letters never come back. Do you book the table or not? And if you decide, how do you tell everybody the decision, when the letters may be lost again?
Dinner by post
Distributed system
A letter
A network message
A letter that is lost or late
Packet loss and network delay
A friend who does not answer
A machine that crashed, or is just slow
Everyone's watch shows a different time
Clock drift between machines
One friend says yes, one says no
Machines that disagree about the state
Letters that arrive twice
Duplicate messages (so we need idempotency)
Deciding together without meeting
Consensus
The silence problem
πŸ™‹Clientsends request
→↓request
πŸ–₯️Serverdid it work?
⇒⇣reply ???
❓No answerlost, slow or dead?

04Why Do We Need It?

πŸ“ˆOne machine has a limit
Past some size, a single computer can not hold the data or handle the traffic, whatever you pay for it.
πŸ›‘οΈSurvive failures
Machines, disks and whole data centres fail. With copies on many machines, the system can carry on.
🌍Be close to users
Servers in many regions give users lower latency and meet rules about where data may live.
πŸ’°Cost
Many cheap machines are often cheaper than one huge one.
🧠Know the traps
If you know the challenges, you design for them early and ask the right questions in an interview.
πŸ”§Pick the right tools
Retries, timeouts, replication, consensus and idempotency exist because of these problems.
We use many machines because we have to. We pay for it with a network that lies, failures that are only partial and clocks that do not agree.

05Where Do We Use It?

🌐Almost every web service
Even a small site has an app server, a database and a cache. That is already a distributed system.
πŸ—„οΈReplicated databases
Copies of data in several places need rules for staying in sync.
🧩Microservices
Every call between services is a network call that can fail.
πŸ“¨Messaging systems
Queues and brokers move data between machines with at-least-once or at-most-once delivery.
☁️Cloud platforms
Storage, compute and networking are all spread over thousands of machines.
πŸ’³Payments and bookings
Money and seats must not be lost or sold twice, even when parts fail.

06Types / Parts / How It Works

The eight famous false beliefs

Engineers at Sun Microsystems listed the wrong guesses that new distributed-system builders make. They are called the fallacies of distributed computing. Every one is false.

Wrong beliefThe truthWhat to do about it
The network is reliablePackets are lost, cut or blockedTimeouts, retries, acknowledgements
Latency is zeroEvery call takes time, and sometimes a lotFewer calls, caching, async work
Bandwidth is infiniteLinks have a limit and cost moneyCompress, send less, batch
The network is secureTraffic can be read or changedTLS, authentication on every call
Topology does not changeMachines come and go all the timeService discovery, no hard-coded addresses
There is one administratorMany teams and companies run partsClear contracts, monitoring, versioning
Transport cost is zeroSerialising and sending data costs CPU and moneyEfficient formats, fewer round trips
The network is homogeneousDifferent hardware, software and versionsStandard protocols, tolerant readers

The main problem areas

Challenges and where this course answers them
Network
βœ‚οΈPartitions
πŸ’“Failure detection
Time
πŸ•’Clock drift
πŸ”’Ordering events
Agreement
πŸ—³οΈConsensus
πŸ”’Locks and leaders
Data
πŸ”Consistency
πŸ’ΈTransactions
Network Time Agreement Data
Each one has its own topic in this Part.
VisualizerSmall failures add up: one request, many machinesSlide the number of machines a request needs. Raise the failure chance, then try retries and see what they fix. Switch between calls one after another and calls all at once.
Retries per call
How the calls are made
Chance that a request works, by number of machines it needs (cyan bar = your setting, now 10). Taller is better.
1 machine1530 machines
1
2
3
4
5
6
7
8
9
10
Each square is one machine the request has to talk to.
One machine works
99.0%
for a single call
Whole request works
90.4%
needs 10 calls
Failures per 1000
96
requests
Average time
396 ms
calls added up
Hit a slow call
18%
at least one slow call
Each call works 99.00% of the time, but a request needs 10 calls. Together only 90.4% of requests work: about 96 in every 1000 fail.

A simple model: every call fails or is slow on its own, with the chances you set (no random numbers, just the maths). A normal call takes 20 ms and a slow one 1000 ms. Real failures are often linked, for example when one network switch breaks many calls at once, which is worse than shown here.

07How to Use It / Steps / Implementation

Designing with the challenges in mind

  1. 1
    Assume every call can fail
    Add a timeout to every network call. A call without a timeout can wait forever and hold a thread and a connection.
  2. 2
    Retry carefully
    Retry only when it is safe, with a growing wait (exponential backoff) and some randomness. Too many retries at once can finish off a struggling service.
  3. 3
    Make operations idempotent
    If a message may arrive twice, doing it twice must not hurt. Use a request ID, so a repeated payment is recognised.
  4. 4
    Keep fewer machines in the critical path
    Every extra machine a request needs lowers its chance of success. Cache, combine calls or move work to the background.
  5. 5
    Do not trust clocks for order
    Use sequence numbers, logical clocks or a log to order events. Use real time only for things like expiry, and allow for drift.
  6. 6
    Plan for partial failure
    Decide what the user sees when one part is down: an error, old data or a reduced feature. Use circuit breakers and bulkheads.
  7. 7
    Test the bad days
    Inject failures on purpose (kill machines, add delay, cut links). A failure that you have only imagined is not handled.
πŸ’‘Ask: what if it is slow, lost or doubled?
For every call in a design, ask three questions: what if it is slow, what if it is lost, and what if it arrives twice? Most bugs hide in these three answers.
⚠️Do not hide the network
Libraries that make a remote call look like a local function call are handy, but they hide the failures. Treat remote calls as different, and always write the error path.

08Key Terms to Know

TermMeaning in easy words
Distributed systemMany computers that work together and look like one system
NodeOne machine (or process) in the system
Partial failureSome parts fail while the others keep running
LatencyThe time a message takes to travel and be answered
TimeoutA limit on how long to wait for a reply before giving up
Network partitionThe network splits so groups of nodes can not talk to each other
Clock driftTwo clocks slowly show different times
IdempotentDoing it twice has the same result as doing it once
ConsensusSeveral nodes agreeing on one value, even when some fail
Fallacies of distributed computingEight wrong assumptions about networks, such as 'the network is reliable'
Fault toleranceThe ability to keep working when parts fail

09Pros, Cons and Trade-offs

βœ… Pros
  • Can grow past the limit of one machine
  • Survives machine and data-centre failures
  • Servers can sit close to users
  • Parts can be updated and scaled separately
  • Often cheaper at large size
⚠️ Cons
  • Networks lose, delay and repeat messages
  • Partial failures are hard to detect and handle
  • No shared clock or memory
  • Harder to test, debug and reason about
  • More moving parts to run and monitor

Trade-offs

ChoiceWe gainWe lose
One big machineSimple, fast calls, easy to reason aboutA hard size limit and one thing that can fail
Many machinesScale and fault toleranceNetwork problems, complexity and cost to run
Retry on failureHides short glitches from usersDuplicates and extra load, unless idempotent
Long timeoutFewer false alarmsSlow failure detection, users and threads wait
Short timeoutFast failure detectionHealthy but slow machines look dead
Strong agreementEveryone sees the same dataSlower, and may refuse requests during failures

10Common Problems and Mistakes

Common mistakes

  1. Trusting the network. Code that assumes every call succeeds works in testing and fails in production.
  2. No timeouts. One slow dependency makes every caller slow, and then the failure spreads.
  3. Retrying without idempotency. The customer is charged twice because a reply was lost.
  4. Using wall-clock time to order events. Clocks drift, so the "later" event may have an earlier time.
  5. Too many calls per request. Twenty services at 99.9% each give only about 98% for the whole request.
  6. Thinking "it failed" means "it did not happen". A timed-out call may still have been done.
  7. Only testing the happy path. Failures that are never practised are handled badly.

Things to watch

🌊Cascading failure
One slow service makes others slow, and the whole system falls like dominoes.
πŸ”Retry storm
Everyone retries at once and overloads the service that was trying to recover.
🐒Gray failure
A machine is not dead, only very slow. It is harder to detect than a crash.
πŸ”€Split brain
Two groups both think they are in charge after a network cut.

11Real-World Examples

πŸ’Chaos Monkey
Netflix built tools that kill servers at random during work hours, so engineers are forced to handle failures.
☁️Cloud outages
Large cloud providers have had outages that started with a small network or config problem and spread.
πŸ“¨Google Spanner
Uses special atomic clocks and GPS to make time between data centres more precise.
πŸ—„οΈAmazon Dynamo
Chose to stay available during failures and to repair differences later.
πŸ“šJepsen tests
A well-known project that tests databases under network cuts and often finds lost writes.
πŸ§ͺFault injection
Many companies run game days where they break parts of production on purpose.
πŸ“Note
Public outage reports from cloud companies are a good way to learn. Most are stories about the challenges on this page.
Mini case: a checkout that calls 6 services
πŸ›’Checkout
→↓6 calls
🧩Six services99.5% each
→↓0.995⁢
πŸ“‰About 97%works end to end
Entry point Dependencies Combined result
Each call is 99.5% reliable, yet the whole checkout is noticeably less reliable.

12Related Topics to Read Next

Network Partitions→

The most famous way the network goes wrong.

Heartbeats→

How machines notice that another one stopped.

Handling Failures→

Timeouts, retries and other tools to survive failures.

CAP Theorem→

The trade-off you face when the network splits.

Idempotency→

Makes retries and duplicate messages safe.

Clock Synchronization Problem→

Why clocks can not be trusted to order events.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: many machines that act like one system, linked by an unreliable network.
  • βœ”Big truth: you can not tell a slow machine from a dead one. Silence has many causes.
  • βœ”Partial failure: some parts fail while others work, so work can be half done.
  • βœ”No shared clock or memory: machines must agree by sending messages.
  • βœ”Fallacies: the network is reliable, latency is zero, bandwidth is infinite... all false.
  • βœ”Maths: n calls at p success each give p^n for the whole request. Keep the chain short.
  • βœ”Tools: timeouts, retries with backoff, idempotency, replication, circuit breakers, consensus.
  • βœ”One line: β€œAssume anything that can fail, be slow or repeat will, and design for it.”
← Previous
Idempotent Consumer and Inbox Pattern
Next β†’
Failure Types: Crash vs Byzantine