Below the Fold

A designer’s path to creative technologist. Thirty days, ten minutes each, on how software actually gets made.

Boxes on a conveyor A conveyor belt carrying a line of boxes that will each be handled eventually, the next one out marked in red.

Async work

After today, you can explain how queues, workers, and webhooks let a product answer now and finish later, and design the states in between.

Dip your toes 1 min

Some work in a product finishes after the screen says it’s done. Post a photo on Instagram, and you’re back in your feed in a moment. Behind that, the photo still has to be copied into several sizes, checked against the rules, and sent toward your followers’ feeds. Instead of making you wait, Instagram’s servers write each task down as a job in a waiting line, called a queue. Separate programs work through that line. That keeps the app fast, but it creates in-between moments: a post your friend can’t see yet, or a video that still says “processing.” So when Instagram says your post is shared, it means the photo is safe, not that every follower can see it yet.

The full lesson 10 min

Why this exists

Post a photo on Instagram. You tap Share, and a moment later you’re back in your feed. Behind that moment, a lot still has to happen. In a simplified version, the photo gets stored and copied into several sizes for different screens. It gets checked against the rules, and it starts making its way into your followers’ feeds.

If the app made you wait for all of that, you’d stare at a spinner for the slowest step. If any one step failed, the whole post would fail, even though the photo itself was safely stored. And on a busy night, every upload would slow down together, because each one would hold a server until its last step finished. The app’s speed would be tied to its slowest step, which is rarely the part the person cares about.

Waiting for everything, or answering firstTwo timelines for a photo upload. In the first, upload, resize, check, and notify run back to back before you can move on. In the second, you can move on right after the upload, and resize, check, and notify run later in the background.Illustrative timingDo it all, then answerUploadResizeCheckNotifyYou move onAnswer, then queue the restUploadYou move onResizeCheckNotifyLater, in the background
Figure 1. The same upload handled two ways: doing everything before answering, and answering first while the slow work waits its turn. Look at the line where you move on in the second row. It comes as soon as the upload is safe, and everything after it happens without you watching.

The fix is to split the work in two. The part the person has to wait for happens now, inside the request. Everything else gets written down as a to-do and handled shortly after, by something else. Engineers call that second kind async work, short for asynchronous: work that finishes after the response has already gone back. The opposite, synchronous work, is everything the request waits for before it answers.

What it is

A restaurant kitchen runs this way. The server takes your order, clips the ticket to a rail, and goes back to the dining room. Cooks take tickets off the rail in order. The server never stands at the stove, and a rush means a longer rail, not a server stuck in the kitchen. On a busy night, the kitchen can add cooks without changing how the servers work.

Software has the same three parts. A queue is the rail: a waiting line of jobs, stored so they can be picked up in order. Each job is a background job, a unit of work that runs outside the request that created it. A worker is the cook: a program that takes jobs off the queue one at a time and carries them out. The code that adds jobs is often called the producer, and it can move on the moment the job is in line.

A producer, a queue, and three workersYour upload reaches an upload server, which adds a job to a queue. Three workers take jobs from the queue: one is resizing a photo, one is checking a photo, and one is free and will take the next job.Your uploadUpload serverQueueWorkerWorkerWorkerAdds a jobJobs wait hereResizingCheckingFree, takes nextJob
Figure 2. A simplified version of Instagram’s upload work: the server adds jobs to a queue, and workers take them one at a time. Look at the queue in the middle. The upload server never waits for the workers, and the workers never wait for the server.

Instagram has talked about this in public. In a 2013 conference talk, Messaging at Scale at Instagram, one of its engineers described its queue system. Instagram built it as activity grew from a few thousand events a day to hundreds of millions.

The exact tools have surely changed since. The shape hasn’t, and almost every large product has some version of it. You use queues every day without noticing them, which is the point.

The queue earns its place in three ways. It absorbs spikes: when uploads double on New Year’s Eve, the line gets longer, and the team adds workers instead of slowing every upload. It survives failures: if a worker crashes halfway through a job, the job goes back in line and another worker retries it. And it decouples the two sides, so the upload server and the workers can be changed, scaled, and deployed separately.

A queue also makes delay visible. Engineers watch how long the line is and how long the oldest job has waited. If the line keeps growing, workers can’t keep up, and every background step runs late: thumbnails, notifications, receipts. That’s often the first sign of trouble during a big event, well before anything shows an error.

Retries have a catch. A job that half-finished and then ran again might do its work twice, like sending the same notification two times. Engineers design jobs so that running twice has the same effect as running once, a property called idempotency. You don’t need the word often, but you’ll see its absence as duplicate emails.

A job doesn’t have to run right away, either. Many queues let a job wait until a set time, which is how a reminder goes out tomorrow at nine. From the outside, Gmail’s Undo Send works like this: your email waits up to 30 seconds before it really leaves, and undo cancels it first. A delay like that is a tool you can ask for in a design.

Async work also crosses company lines. Say a video app rents its processing from another company, which won’t make the app’s server wait while it converts a long video. Instead, the service calls the app’s server back later with a webhook, a message one system sends to another’s server when something happens. The receiver registers an address for those messages in advance, like leaving a phone number for a callback. Webhooks are how most third-party services tell you something finished.

The alternative is polling: asking the service every few seconds whether the video is ready yet. Polling wastes requests and still learns the news late. A webhook flips the direction, so the news arrives once, when it’s ready. Because webhooks can arrive late, twice, or out of order, the receiving server checks each one before acting on it.

How it shows up in your work

A screen that says your post is shared is making a claim, and it’s worth knowing which of the steps behind it have actually happened. Usually the important part is safe, and the rest is in the queue. That’s a fine thing to tell people, and some products show it directly, like a processing badge on a video that’s still being prepared. Ask engineers which steps happen before the response and which happen after, then design for both.

Outside services raise the same question, as in that video app. You post a video, the app says it’s shared, and you close the app. Meanwhile the video service is still converting your file into versions for every screen. The app’s server only learns the video is ready when the webhook arrives, seconds or occasionally minutes later. Polling would learn the same news later, after more requests.

Polling compared with a webhookTwo side-by-side sequences between the app’s server and an outside video service. On the left, polling: the server asks whether the video is ready, hears not yet, asks again, and only on its third request hears yes, after the video was already ready. On the right, a webhook: the server asks the service once to process the video, and the service calls back the moment it’s ready.PollingWebhookApp’s serverVideo serviceApp’s serverVideo serviceReady yet?Not yetReady yet?YesProcess thisWebhook: readyReady
Figure 3. The app’s server learning that a video is ready, by polling and by webhook. Look at the webhook. It arrives once, as soon as the video is ready, while polling spends three requests and still hears the news late.

So the app has to design for a gap. Your own profile might show the video with a “processing” badge, while followers don’t see it until the webhook lands. If the webhook is delayed, you refresh your profile, see “processing,” and wonder whether the upload worked. That state needs words, and you’re the one who writes them. Day 20 goes deeper on renting services like this one.

Notifications are the third place. Push notifications, emails, and digests are almost always background jobs. They can arrive late when queues are busy, and occasionally twice when a retry wasn’t idempotent. When a spec says “notify the user,” ask what happens if the notification is late, and whether it still makes sense an hour later. A “your driver is here” message that arrives ten minutes late is worse than none.

The one thing people get wrong

“It’ll catch up eventually” is easy to hear as an excuse for something broken. A follower doesn’t see your new post for a few seconds, and the fan-out from Day 14 is why. You see your own comment on a post before a friend does. Each looks like a bug, and each is usually the system working as designed.

Engineers have a name for it: eventual consistency. Two copies of the same data can briefly disagree after a change, and are designed to catch up. Your post was saved first, then copied toward followers’ feeds by background jobs, so for a moment some feeds have it and some don’t. Products choose this on purpose, because making every part agree before answering would make every action slow. The trade is a short window where two screens tell slightly different stories.

Where to accept that window is a product decision. Some things can’t be eventual, like a username two people claim at once, or the last ticket to a concert. Others can, like a like count, a follower’s feed, or a view counter. Telling engineers which kind a number is tells them how much speed they can trade for agreement.

An upload’s states over timeThree states in order: uploading, while the file is sent; processing, while the video is converted; and live, once the webhook arrives. Under each is what the screen says: uploading, 60 percent; processing video; and your video is live. Under the processing state, a note says not upload failed.One upload, over a few seconds to minutesUploadingProcessingLiveSending the fileBeing convertedWebhook arrivedWhat the screen says“Uploading, 60%”“Processing video”“Your video is live”Not “Upload failed”
Figure 4. One video upload moving through its states, and what the screen should say at each. Look at the processing state. It’s a normal step that every upload passes through, so its screen needs calm, specific words, not an error.

What is broken is when “eventually” never comes, because a job failed every retry or a webhook got lost. Good teams watch for that, so failed jobs land somewhere a person can see them and act. There’s more here about how; you don’t need it yet.

The design question is what the person sees in that case, and how long the product waits before it says so. “Still processing” is honest for a minute. After an hour, it’s a polite way of hiding a failure.

So design every in-between state as a real state, like a video that still says “processing” after the upload finished. Give it words that say what’s happening, not that something went wrong. Decide how long each one can reasonably last, and what changes when that time runs out. Write those states into the spec next to the happy path, so they get built and tested like everything else. And when you hear “it’ll show up eventually,” ask how long eventually usually is.

Ask an engineer this

“For this feature, what’s the longest normal wait between “done” on screen and the work really finishing, and what should the screen say if it runs past that?”

Ask while the feature is still on the whiteboard, because the reply changes which states you design. The engineer will usually split the work in two: what happens before the response, and what goes to a queue or waits on a webhook. For the second group, find out how failures surface: a retry, an alert, or nothing at all. The longest normal delay becomes the time limit for your in-between state. Past that limit, the screen should stop saying “processing” and say what happened instead.

Terms from today

Queue
A waiting line of jobs, stored so they can be picked up and done in order, even if the program that added them has moved on.
Background job
A unit of work that runs outside the request that created it, like resizing a photo after an upload.
Worker
A program that takes jobs off a queue one at a time and carries them out.
Webhook
A message one system sends to another’s server when something happens, at an address the receiver registered in advance.
Eventual consistency
A property of systems where two copies of the same data can briefly disagree after a change, and are designed to catch up.
Next

Day 19: Caching and CDNs

Why the same request can be fast or slow, how a CDN works, and why changing a cached thing is hard.

18

Async work

All 30 days