LESSON 08 · Reliability and operations

Design the failure path

Recover from broken sockets and stalled workers without losing or duplicating messages.

SYSTEM VIEW

The message path

Diagram view
Stored copies—
Pushes sent—
Devices have it—

Watch a worker crash halfway through delivering message 1042, then see its job come back and finish without a duplicate.

  1. Message history keeps message 1042 in a replicated log, the one copy that must survive. Delivery is separate, short-lived state: a retryable job for 1042 waits in the Delivery queue for any worker to take.

    Log
    replicated, durable
    Job
    retryable

    What changed

    • Message 1042 in the log
    • Job for 1042 queued
    • Log and jobs kept apart
  2. The Delivery worker takes job 1042 under a 30 s lease, which Amazon SQS calls a visibility timeout. Until the worker acknowledges the job or the lease runs out, no other worker can see it.

    Lease
    30 s
    In SQS
    visibility timeout

    What changed

    • Job 1042 leased for 30 s
    • Hidden from other workers
  3. The Delivery worker pushes “lunch?” to Ben’s phone. The phone hasn’t seen message 1042, so it shows it and keeps its ID. Ben’s laptop has nothing yet, and the job is still unacknowledged, its lease running.

    What changed

    • Push to Ben’s phone
    • Phone shows 1042
    • Laptop still waiting
  4. The Delivery worker crashes before it reaches Ben’s laptop or acknowledges the job. Nothing is lost: 30 s after it took the job, the lease runs out and job 1042 is visible again. That is at-least-once: work may repeat, but isn’t dropped.

    Acknowledged
    no
    Delivery
    at least once

    What changed

    • Delivery worker crashes
    • Lease runs out at 30 s
    • Job 1042 visible again
  5. Worker 2 takes job 1042 from the Delivery queue under a new 30 s lease. Worker 1 never acknowledged it, so the whole job runs again, including the push that Ben’s phone already got.

    What changed

    • Worker 2
    • Job 1042 leased again
    • The whole job runs again
  6. Worker 2 pushes “lunch?” to both devices. Ben’s phone already has message 1042, so it drops the repeat; his laptop gets its first copy. That makes the push idempotent: twice gives the same result as once. Worker 2 then acknowledges the job.

    What changed

    • Phone drops the repeat
    • Laptop: first copy
    • Job 1042 acknowledged
  7. Message history writes job 1042 in the same database transaction as the message, a pattern called a transactional outbox. Both commit or neither does, so a crash can never leave a stored message with no job for a worker to deliver.

    Pattern
    transactional outbox
    One commit
    message + job

    What changed

    • Outbox in Message history
    • Message and job: one commit
    • No message without a job
  8. Monitoring reads queue depth from the Delivery queue and write latency from Message history, but users feel end-to-end delivery lag, so it watches that too, with reconnects and failed pushes. Size by connections and fanout; rate limits and backpressure protect gateways.

    Watch
    end-to-end delay
    Also
    reconnects, failed pushes
    Size by
    connections, fanout

    What changed

    • Job lease, Worker 1
    • Monitoring
    • Queue depth, write latency
    • Delivery lag: what users feel

Space plays · ← → step · Home resets

THE IDEA

How it works

Separate the durable message log from ephemeral connection state and delivery jobs. Replicate or otherwise protect the log, monitor write latency and lag, and keep a retryable queue for fanout. A worker crash should leave a job available for another worker. That is at-least-once processing, where work may repeat but is not dropped, so effects must be idempotent: running them twice gives the same result as running them once.

Follow one crash. A worker takes the job for message 1042 from a queue with a 30-second lease, which Amazon SQS calls a visibility timeout. It pushes to Ben’s phone, then dies before acknowledging. After 30 seconds the job reappears, and a second worker pushes to the phone and laptop. The phone sees a known message ID and drops the repeat; the laptop gets its first copy. Writing the job in the same database transaction as the message, a pattern called a transactional outbox, means a crash cannot leave a message with no job.

Watch socket count per gateway, reconnect rate, outbound queue depth, end-to-end delivery delay, replay backlog, failed pushes, and storage errors. Add capacity based on concurrent connections and message fanout rather than daily users alone. Rate limits and backpressure, slowing output a client cannot absorb, protect gateways from slow clients or abusive senders.

A full end-to-end encryption requirement would change key management, multi-device handling, moderation, and server-side search. Media attachments would add object storage, upload processing, scanning, and CDN delivery. Keep those as explicit next design changes rather than quietly assuming this text-only system already supports them.

DECISION POINT

A delivery worker crashes after pushing to one device but before acknowledging its job. What happens when the job runs again?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Durable storage, retryable jobs, idempotency, and replay each address a different failure.
  • Measure connections, fanout, lag, and recovery; revisit the design when scope changes.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

Which state must survive a gateway restart?

Choose an answer. You’ll see the explanation once you get it right.

Design a live customer-support inbox

Reuse the chat path for a product where people, not sockets, are the scarce resource, and customers often leave before the reply arrives.

The brief

Customers start a chat from a website widget or mobile app. Agents work several conversations at once from a shared inbox. A customer may close the page and come back hours later on another device, and should see the whole thread and hear about replies sent while they were away. Design the first version. Keep bots, voice, file attachments, and reporting outside this exercise.

  • 200,000 conversations start per day, averaging 20 messages of 500 bytes each, including metadata. Half of the messages are agent replies. Use decimal units and 86,400 seconds per day.
  • Peak traffic is 5 times the daily average. A conversation stays active for 30 minutes on average, with its customer connected during that time.
  • 4,000 agents are online at peak, each with one dashboard connection and a limit of 8 conversations at once.
  • 30% of agent replies arrive after the customer has gone offline. Those customers should get an email or push within about a minute, grouped rather than one per message.
  • Keep history for 2 years. An accepted message must survive a process crash, and two agents must not both believe they own one conversation.

Reading a design isn’t the same as producing one under a clock. Try it timed: the requirement change stays hidden until partway through.

Loading your draft…

State your scope and assumptions. Estimate message rates, peak concurrent conversations and connections, agent capacity use, the offline alert rate, and two years of storage. Keep units visible.

0 / 4,000 characters

Name the components and what each stores. Trace a customer message to an agent, an agent reply to an offline customer, and how a waiting conversation gets assigned to exactly one agent.

0 / 4,000 characters

Choose a likely bottleneck and the metric that would prove it. Explain what happens when an agent's laptop dies mid-conversation, and when a connection server or alert worker crashes.

0 / 4,000 characters

Respond to the outage surge below. Estimate how long the backlog lasts, then explain what you do about a wait that more machines cannot fix.

The requirement changesA large customer has an outage, and 50,000 of its users open chats within ten minutes. You cannot add agents quickly. Who waits, in what order, and what do they see? Name what you would protect, and what you would automate or shed.

0 / 4,000 characters

Write an initial response in all four sections to unlock the checklist and two worked approaches.

Next chapter: News Feed

0 of 3 checkpoints answered correctly to complete this lesson.