Literally Everything There Is To Know About Backend Engineering (For Sleep) — Transcript
Full transcript
- 0:00Welcome to this exploration of back-end
- 0:03engineering, designed for learning at
- 0:05your own pace. Whether you're actively
- 0:08listening or letting the information
- 0:10wash over you as you fall asleep, at
- 0:13some point today, you tapped a button
- 0:16that said order or send or pay. And
- 0:20before you'd even put your phone down, a
- 0:23quiet machine somewhere had read your
- 0:26request, checked it, saved it, and told
- 0:30half a dozen other machines what you'd
- 0:32done. But what really happens behind the
- 0:35screen when you press that button? How
- 0:38does one request find its way through
- 0:41all those machines, get handled exactly
- 0:44once, and come back in less than a
- 0:47second? Even on a night when millions of
- 0:50other people are pressing buttons too.
- 0:53The answer is more surprising than you
- 0:56might expect. The back end is a kitchen
- 0:59you never see, but you taste its cooking
- 1:02every day. If you find this helpful,
- 1:05consider subscribing for more content on
- 1:08science and technology. And we'd love to
- 1:10know, where are you listening from
- 1:13tonight? Drop your city or country in
- 1:16the comments below. Now, find a
- 1:18comfortable position. Let your body
- 1:21settle and let's discover how the hidden
- 1:24side of every app actually works.
- 1:29Chapter one, the kitchen behind the
- 1:32restaurant. It's 1890 in London, and a
- 1:36new hotel on the Strand has a problem in
- 1:39its basement. The hotel is the Savoy. It
- 1:43has electric lights and lifts and a
- 1:46dining room where the fashionable people
- 1:49of the city come to be seen. Upstairs,
- 1:53everything is calm. Waiters glide
- 1:55between tables. Silver gleams. Nobody
- 1:59raises voice. Downstairs is the kitchen,
- 2:03and the kitchen is where the evening is
- 2:05won or lost. The owner has hired a chef
- 2:09named Auguste Escoffier to run it.
- 2:12Escoffier is in his forties, a small,
- 2:15neat man from the south of France, and
- 2:18he has spent years thinking about a
- 2:20question that sounds simple. How to get
- 2:23dozens of cooks to produce hundreds of
- 2:26different dishes, all at the right
- 2:29moment without chaos. His answer, worked
- 2:32out over years in different kitchens and
- 2:35brought with him to London, came partly
- 2:38from his time as an army cook. He
- 2:40organized the kitchen like a brigade.
- 2:43Every cook had one station and one job.
- 2:46One cook made the sauces, one handled
- 2:49the roasts, one did the vegetables and
- 2:52soups, one kept the cold larder, the
- 2:56salads and the patés, one made the
- 2:59pastry. Over all of them stood the head
- 3:02chef, and between the kitchen and the
- 3:04dining room stood a person whose job was
- 3:07to receive each order from the waiters
- 3:10and call it out to the right stations so
- 3:13that every plate for a table would be
- 3:16ready together. That system, the brigade
- 3:19de cuisine, spread to restaurant
- 3:22kitchens around the world. If you've
- 3:25ever peeked through a kitchen door and
- 3:27heard someone calling out an order and
- 3:30someone else answering, yes, chef,
- 3:33you've heard its echo. What matters for
- 3:36us tonight is the shape of the thing. A
- 3:39calm room where guests sit and choose,
- 3:42and a busy room out of sight where the
- 3:45real work happens. Restaurant people
- 3:48have names for those two rooms, the
- 3:51front of house and the back of house.
- 3:54Software engineers ended up with almost
- 3:56exactly the same words. The part of an
- 3:59app you see and touch, the buttons and
- 4:03screens and scrolling lists is called
- 4:06the front end. The part you never see
- 4:09running on computers in a data center
- 4:12somewhere is called the back end.
- 4:14Tonight is about the back of house. If
- 4:17you heard our lecture on web
- 4:19development, you may remember the
- 4:21chapter about the other side of the wire
- 4:24where web servers first learned to cook
- 4:27a fresh page for every visitor. Tonight
- 4:30we'll walk all the way into that kitchen
- 4:33and stay a while. So let's look at what
- 4:36a back end actually does all day long.
- 4:40Strip away the details and almost every
- 4:43request it handles goes through four
- 4:46steps. It receives, it validates, it
- 4:50stores, it responds. Let's follow one
- 4:54order through those steps start to
- 4:57finish. It's a quiet evening and you're
- 5:00ordering from a small coffee shop that
- 5:02lets you order ahead on your phone. You
- 5:05choose two flat whites and a blueberry
- 5:08muffin and you press the button that
- 5:10says place order. At that moment your
- 5:14phone writes a short message. It's a
- 5:16request and it says in effect this
- 5:20customer wants two of item number seven
- 5:23and one of item number 12 for pickup at
- 5:27the shop on Market Street. Your phone
- 5:30sends that message across the internet
- 5:32to an address belonging to the coffee
- 5:35shop's back end. Step one, the back end
- 5:38receives it. Somewhere in a data center
- 5:42a program has been waiting listening for
- 5:45exactly this kind of message. It accepts
- 5:48the request and reads it. Before it does
- 5:51anything else it wants to know who's
- 5:54asking. Your phone included a small
- 5:57token that proves you signed in earlier
- 6:00and the back end checks that token. Yes,
- 6:04this is a real customer with a real
- 6:06account. Now the request is in the
- 6:09kitchen. The ticket has arrived. Step
- 6:12two, the back end validates it. This is
- 6:16the step people outside the kitchen tend
- 6:19to forget, and it's the one good back
- 6:21ends are most careful about. The back
- 6:24end never simply believes what it's
- 6:27told. It checks. It checks that item
- 6:30number seven exists. It does, and it's a
- 6:34flat white. Item 12 exists too, a
- 6:38blueberry muffin. It checks that the
- 6:40quantity is sensible. Two is fine. If
- 6:44the request had asked for minus three
- 6:46coffees or 9,000, the back end would
- 6:50stop right here and say no. It checks
- 6:53that the Market Street shop is open and
- 6:56that it still has muffins. And then it
- 6:59turns to the price. Here's a subtle
- 7:02point. Your phone showed you a price,
- 7:05but the back end doesn't use the price
- 7:07your phone sent. Even if it sent one, it
- 7:11looks the prices up for itself in its
- 7:14own records. A flat white is $4.50.
- 7:19A muffin is $3.25.
- 7:23Two flat whites come to $9. Add the
- 7:26muffin and the total is $12.25.
- 7:31The back end works that out on its own
- 7:34because anything that arrives from
- 7:36outside could have been changed along
- 7:39the way. A phone can have a bug. An old
- 7:42version of the app might be showing
- 7:45yesterday's prices, and occasionally
- 7:47someone will try to send a request by
- 7:50hand that says a coffee costs 1 cent. A
- 7:55good kitchen doesn't let the customer
- 7:57write the bill. Step three, the back end
- 8:01stores it. The order now gets written
- 8:03into the shop's database, the back end's
- 8:07long-term
- 8:08A new row appears, recording who you
- 8:11are, what you ordered, which shop, the
- 8:15total of $12.25
- 8:18and the time. The order is given a
- 8:20number. Let's say it's order 4,018.
- 8:24Until that row is safely written, the
- 8:27order hasn't really happened. If the
- 8:30power went out a moment before, there
- 8:33would be no order at all, and the honest
- 8:36thing would be to tell you so. Once it's
- 8:38written, the order exists, and it will
- 8:42still exist tomorrow and next year. Step
- 8:45four, the back end responds. It sends a
- 8:49message back to your phone. Your order
- 8:52is confirmed. Number 4,018,
- 8:56$12.25,
- 8:59ready in about 10 minutes. Your screen
- 9:02shows a small green tick. That whole
- 9:05round trip might take a quarter of a
- 9:07second. Most of that is the request
- 9:10crossing the internet and coming back.
- 9:13The work in the kitchen itself, the
- 9:15checking and the saving might take only
- 9:18a few thousandths of a second. But
- 9:21notice that the back end isn't finished.
- 9:24There's still the barista's tablet
- 9:26behind the counter, which needs to show
- 9:29the new order. There's a receipt to
- 9:31email you. There's a loyalty program
- 9:34that wants to add points to your
- 9:35account. There's the payment to arrange.
- 9:39There's a dashboard somewhere where the
- 9:41owner likes to watch sales come in. A
- 9:44sensible back end doesn't make you wait
- 9:47for all of that. It answers you as soon
- 9:50as the order is safely stored, and it
- 9:52hands the other jobs off to be done in
- 9:55the background a moment later. That
- 9:58trick, answering quickly and doing the
- 10:00rest afterward, gets a whole chapter of
- 10:03its own later tonight. Receive,
- 10:06validate, store, respond. Most of what
- 10:10backends do from the smallest coffee
- 10:13shop to the largest bank is some version
- 10:17of those four steps repeated many
- 10:19thousands of times a second. And there's
- 10:22a reason those steps live in the back of
- 10:25house rather than on your phone. The
- 10:27first reason is trust. Your phone is in
- 10:31your pocket. It belongs to you and you
- 10:34can change what it does. The backend
- 10:37belongs to the business and it's the
- 10:40only place where the rules can be
- 10:42enforced for everyone. The price of a
- 10:44coffee, the number of muffins left,
- 10:47whether you've really paid. Those
- 10:50decisions have to be made somewhere the
- 10:52customer can't reach in and adjust. The
- 10:56second reason is sharing. The muffins on
- 10:59the shelf are the same muffins for
- 11:01everyone. If 10 people order at once,
- 11:05something has to keep a single true
- 11:08count. Your phone only knows about you.
- 11:11The backend sees everyone. And third,
- 11:15there's memory. Your phone could be lost
- 11:18tomorrow or dropped in a river. Your
- 11:21order history, your account, your
- 11:24loyalty points all need to live
- 11:26somewhere more permanent. So the backend
- 11:30is where the rules live, where the
- 11:32shared things live, and where the memory
- 11:35lives. It's also where the hard problems
- 11:38live and they're the subject of the rest
- 11:41of tonight. What happens when 10,000
- 11:44people order at the same moment instead
- 11:47of one? What happens when the payment
- 11:50service takes too long to reply and your
- 11:53phone gives up and tries again and you
- 11:56might be charged twice? How does the
- 11:59kitchen keep cooking when one of its
- 12:02ovens breaks in the middle of the dinner
- 12:04rush? And how do the engineers who run
- 12:07it even know that an oven has broken
- 12:10when the whole kitchen is
- 12:13spread across thousands of machines in
- 12:16buildings they may never visit. Back end
- 12:19engineering is the craft of answering
- 12:22those questions. It's less about any one
- 12:25clever trick and more about a set of
- 12:28habits. Check everything, write things
- 12:31down before you promise them. Expect
- 12:34parts to fail. Never let one slow job
- 12:38hold up the whole line. Escoffier would
- 12:41have recognized every one of those
- 12:43habits. His kitchens were famous, not
- 12:46because any single cook was a genius,
- 12:49though some were, but because the system
- 12:52kept working on the busiest nights. A
- 12:55cook could fall ill, a delivery could
- 12:58arrive late, and the plates still went
- 13:01out hot and in order. Back ends are
- 13:04judged the same way. Nobody praises a
- 13:07back end for a good day. A good back end
- 13:11is one you never think about at all. You
- 13:13press the button, the green tick
- 13:15appears, and you go on with your
- 13:18evening. There's one more thing to
- 13:20notice about our coffee order before we
- 13:23leave it. Your phone and the back end
- 13:26had to agree on a great deal before any
- 13:29of this could work. Your phone had to
- 13:32know where to send the order and how to
- 13:35write it and what to expect back. It
- 13:38said item number seven, and the back end
- 13:41understood that to mean a flat white.
- 13:45The back end said order confirmed, and
- 13:48your phone knew to show a green tick.
- 13:50None of that was an accident. Someone
- 13:53wrote down the rules of that
- 13:55conversation carefully in advance, and
- 13:59both sides agreed to follow them.
- 14:01Engineers call that written agreement an
- 14:04interface. And at the turn of this
- 14:07century, a young computer scientist in
- 14:11California wrote a dissertation that
- 14:13changed how much of the world's software
- 14:16would design those agreements. He wasn't
- 14:19trying to invent anything new. He was
- 14:21trying to describe as precisely as he
- 14:25could something that already existed and
- 14:28already worked better than almost anyone
- 14:31had expected.
- 14:34Chapter two, a contract between
- 14:37programs. It's the late 1990s
- 14:41at the University of California Irvine
- 14:44and a graduate student named Roy
- 14:47Fielding has a slightly unusual problem
- 14:50for a student. He has already helped
- 14:52build the thing he's meant to be
- 14:54studying. Through the middle of the
- 14:56decade, Fielding had been one of the
- 14:59principal authors of the specifications
- 15:02for HTTP,
- 15:04the language that web browsers and web
- 15:07servers use to talk to each other. He
- 15:09was also one of the people who started
- 15:12the Apache web server project, which
- 15:15went on to run a large share of the
- 15:17world's websites. So he knew the web
- 15:20from the inside. He'd sat in the
- 15:22discussions where its rules were argued
- 15:25over line by line and he'd noticed
- 15:28something. The web was growing at a rate
- 15:31no one had planned for. Millions of
- 15:34machines, all built by different people,
- 15:38most of whom would never meet. And yet
- 15:41it mostly worked. A browser written in
- 15:44one country could talk to a server
- 15:47written in another and neither needed to
- 15:50know anything about the others' insides.
- 15:53Fielding wanted to explain why. He
- 15:57wanted to pin down the rules that made
- 15:59it hold together and to see whether
- 16:02other software could be designed to
- 16:04follow them on purpose. In 2000 he
- 16:08finished his doctoral dissertation. Its
- 16:11title is long and dry, Architectural
- 16:14Styles and the Design of Network-Based
- 16:18architectures, but its fifth chapter
- 16:21gave a name to the style he had found in
- 16:24the web's design. He called it
- 16:26representational state transfer, REST
- 16:30for short. To see why that idea mattered
- 16:33so much to back-end engineers, we need
- 16:36to go back to the coffee order from the
- 16:39last chapter. Your phone and the back
- 16:42end had to agree on how to talk. That
- 16:45agreement is called an API, short for
- 16:49application programming interface. It's
- 16:52the menu and the ordering rules written
- 16:55down for programs instead of people.
- 16:58Before REST caught on, many of these
- 17:01agreements were built around a simple
- 17:03idea. One program would call a function
- 17:06inside another as if it were sitting
- 17:09right next door. Get customer balance,
- 17:13update shipping address, cancel order
- 17:16with reason. Every system invented its
- 17:19own list of commands with its own names
- 17:22and its own rules. That worked in a way,
- 17:26but every API was a different language.
- 17:29To use a new one, you had to learn all
- 17:32of its commands from scratch. It was a
- 17:35bit like walking into a library where
- 17:37every librarian had their own private
- 17:40set of phrases and you had to memorize
- 17:43them before you could borrow anything.
- 17:46Fielding's description of the web
- 17:48pointed to a different way of organizing
- 17:51things and it rests on a few plain
- 17:54ideas. The first idea is that everything
- 17:57worth talking about is a resource and
- 18:00every resource has a name. On the web,
- 18:04the name is an address. A page about
- 18:07soup has one address. A photo of a cat
- 18:10has another. In a REST API, the same is
- 18:13true of the things a business cares
- 18:16about. A customer is a resource. An
- 18:19order is a resource. A book in a library
- 18:23is a resource. Each one gets its own
- 18:26address, and the address stays the same.
- 18:29The second idea is that you use the same
- 18:32small set of verbs for every resource.
- 18:36HTTP
- 18:37already had these verbs built in. Get
- 18:41means please show me this. Post means
- 18:45here's something new. Please add it. Put
- 18:49means please replace this with what I'm
- 18:52sending. And delete means please remove
- 18:55this. That's nearly the whole
- 18:58vocabulary. Four verbs applied to any
- 19:01resource at all. The third idea is that
- 19:04what travels back and forth isn't the
- 19:07thing itself, but a representation of
- 19:10it. A book on a shelf can't travel down
- 19:13a wire. What travels is a description of
- 19:16the book, its title, its author, whether
- 19:20it's on loan, written out in an agreed
- 19:23format. These days that format is very
- 19:26often a simple text layout called JSON,
- 19:31which a programmer named Douglas
- 19:33Crockford described and popularized in
- 19:36the early 2000s. It looks much like a
- 19:39tidy list of labels and values. And the
- 19:43fourth idea is that each request should
- 19:46carry everything needed to understand
- 19:48it. The server shouldn't have to
- 19:51remember what you asked a moment ago.
- 19:54That one turns out to be so important
- 19:56for running large systems that we'll
- 19:59give it a whole chapter of its own a
- 20:01little later tonight. Let's put these
- 20:04ideas to work by building an API for a
- 20:08small town library. The library has
- 20:11about 1,200 books, and it wants members
- 20:14to be able to search the catalog and
- 20:17borrow books from an app on their
- 20:19phones. The collection of all the books
- 20:22gets one address. We'll call it books.
- 20:25Each individual book gets its own
- 20:27address made from that one plus a
- 20:30number. Book number 42 lives at
- 20:34books/42.
- 20:36Now let's use the verbs. A member opens
- 20:40the app and wants to see book 42. The
- 20:44app sends a get request to books/42.
- 20:49The back end looks it up and sends back
- 20:52a representation. Title, Moby Dick.
- 20:55Author, Herman Melville. Published,
- 20:581851.
- 21:00Status, available. Then the librarian
- 21:04receives a new donation, a copy of a
- 21:07cookbook, and adds it through her own
- 21:09screen. Her app sends a post request to
- 21:13books, the address for the whole
- 21:15collection, carrying the cookbook's
- 21:17title and author. Post means add
- 21:21something new here. The back end creates
- 21:24the record, gives it the next free
- 21:26number, which happens to be 1,201,
- 21:31and replies, created. And you'll find it
- 21:34at books/1,201.
- 21:38A week later, she notices she spelled
- 21:41the author's name wrong. Her app sends a
- 21:44put request to books/1,201,
- 21:50carrying the corrected record in full.
- 21:53Put means replace what lives at this
- 21:56address with this. And a few years on,
- 22:00when the cookbook falls apart from use,
- 22:03a delete request to that same address
- 22:06removes it from the catalog. Notice that
- 22:09nobody had to invent a command called
- 22:12add new book or fix book author or
- 22:15remove damaged book. The library's API
- 22:19is just addresses and four verbs, the
- 22:22same four verbs that every other rest
- 22:25API uses. Borrowing is where it gets
- 22:28clever. You might expect a special
- 22:31command called borrow, but in rest you
- 22:34usually turn the action into a resource
- 22:37of its own. A loan is a thing. It has a
- 22:40start date, a due date, a book, and a
- 22:44member. So the library adds one more
- 22:46collection called loans. On the 5th of
- 22:50November, member number seven wants to
- 22:53borrow Moby Dick. Her app sends a post
- 22:56request to loans saying member seven
- 23:00book 42. The back end checks that the
- 23:03book is available and that member seven
- 23:06doesn't have overdue fines. Then it
- 23:08creates loan number 503
- 23:12due back 14 days later on the 19th of
- 23:15November. Now if anyone sends a get
- 23:19request to books/42,
- 23:22the status says on loan. And when member
- 23:25seven brings it back, a request to
- 23:28update loan 503
- 23:31marks it returned. Every reply also
- 23:34comes with a short number that says how
- 23:37things went and those numbers are part
- 23:39of the same agreement. 200 means fine.
- 23:43201 means created. 404 means there's
- 23:48nothing at that address. And 500 means
- 23:52the kitchen itself went wrong. This
- 23:55style brought some real gifts. One was
- 23:59predictability.
- 24:00Once a developer had used one rest API,
- 24:04the next one felt familiar. Collections
- 24:07and items for verbs, a handful of status
- 24:11numbers. You could often guess how an
- 24:14API worked before reading its
- 24:16documentation.
- 24:18Another gift came from the verbs
- 24:20themselves. A get request is only
- 24:23allowed to look, never to change
- 24:26anything. That sounds like a small
- 24:29but it means a get can be repeated
- 24:32safely, and its answer can be saved and
- 24:35reused by anything along the way. Put
- 24:38and delete have a special quality of
- 24:41their own too. Doing them twice leaves
- 24:44things exactly as doing them once would.
- 24:48That quality has a name, and it turns
- 24:51out to be the key to never charging
- 24:54anyone twice, which is a story for later
- 24:57tonight. Through the 2000s, as companies
- 25:01began opening their systems to outside
- 25:04developers, rest became the usual way to
- 25:07do it. Photo sites, maps, payment
- 25:11companies, and social networks all
- 25:13published rest APIs, and a whole
- 25:17generation of programmers learned the
- 25:19pattern of addresses and verbs. Fielding
- 25:22himself has said more than once that
- 25:25many of the APIs calling themselves
- 25:27restful don't follow all of his
- 25:30constraints. By his strict definition,
- 25:33that's fair. In everyday use, the word
- 25:37drifted toward meaning something looser.
- 25:40Resources with addresses, the standard
- 25:43verbs, JSON going back and forth. But
- 25:46even that looser version did a great
- 25:49deal of good. There's one more thing
- 25:51worth knowing about any API, rest or
- 25:55otherwise. It's a contract, and
- 25:57contracts are hard to change once people
- 26:00rely on them. Say the library's app has
- 26:04been in use for two years. Thousands of
- 26:07members have it on their phones, and
- 26:09many of them never update anything. If
- 26:12the backend team decides to rename the
- 26:16field called title to name, every one of
- 26:19those old apps will suddenly find no
- 26:22titles at all. Their screens will show
- 26:25blank lines where the books should be.
- 26:28So backend engineers learn a careful
- 26:31habit. You can add new fields to a
- 26:33contract because old apps will simply
- 26:37ignore what they don't recognize, but
- 26:39you almost never remove or rename one.
- 26:43When a big change is truly needed, you
- 26:46publish a second version of the API
- 26:49alongside the first, often with the
- 26:52version number right in the address, and
- 26:54you keep the old one running until the
- 26:57last of the old apps has faded away. An
- 27:00API is a promise made to people you'll
- 27:03never meet. Good back-ends keep their
- 27:06promises for years. For a long time,
- 27:10rest seemed like the end of the story.
- 27:13Clean addresses, simple verbs, a
- 27:16contract everyone could read. But around
- 27:192012, at a company with hundreds of
- 27:22millions of people looking at it through
- 27:24small phones on slow connections,
- 27:27engineers started to feel that rest was
- 27:30making their app do too much work. The
- 27:33app kept asking for whole resources when
- 27:36it only needed a few small pieces of
- 27:39each. It was like ordering the entire
- 27:42menu just to get one dish.
- 27:47Chapter three. Ask for exactly what you
- 27:50need. It's 2012 in Menlo Park,
- 27:54California, and Facebook has decided to
- 27:57rebuild its iPhone app. The first
- 28:00versions of the app had leaned heavily
- 28:02on web pages shown inside a frame on the
- 28:05phone. That had made it quick to update,
- 28:08but it felt slow and sticky in the hand.
- 28:12Later that year, Mark Zuckerberg would
- 28:15say publicly that betting so heavily on
- 28:18that approach had been one of the
- 28:20company's biggest mistakes. So a team
- 28:23set out to write the app again,
- 28:26natively, as a proper phone program. The
- 28:29hardest screen was news feed, the
- 28:32endless scroll of posts from friends.
- 28:36And the trouble with newsfeed wasn't
- 28:38really on the phone, it was in how the
- 28:41phone got its data. One of the engineers
- 28:44in the middle of this was Lee Byron, who
- 28:47was managing the iPhone team. As he
- 28:50later told the story, the old app had
- 28:53been pulling newsfeed through an aging
- 28:56interface that was never designed for
- 28:59it, and posts were going missing. The
- 29:02team concluded that they needed a new
- 29:04way for the app to ask for data, built
- 29:07from scratch. Byron worked with two
- 29:10other engineers, Nick Schrock and Dan
- 29:13Schäfer. Schrock built a prototype he
- 29:16called Supergraph. Over a month or two
- 29:19of steady improvement, it grew into
- 29:22something new. The team called it
- 29:24GraphQL. To understand what problem they
- 29:28were solving, let's look at what a REST
- 29:31interface makes a phone do. Say the app
- 29:34needs to draw a small screen showing 10
- 29:37of your friends, each with a name and a
- 29:40small photo. In a typical REST design,
- 29:44the app first asks for your list of
- 29:46friends. Back comes a list of 10 friend
- 29:49numbers. That's one request. Then, for
- 29:53each friend, it asks for that person's
- 29:56record. Get user 104,
- 30:00get user 2009,
- 30:03and so on, 10 more times. That's 11
- 30:06requests in all, and each of those user
- 30:10records is the full representation
- 30:13because REST hands you the whole
- 30:15resource. Name and photo, yes, but also
- 30:19birthday, hometown, workplace, schools,
- 30:23relationship status, the date the
- 30:26account was made, a dozen links and
- 30:29settings. Say each record comes to about
- 30:326 kilobytes. 10 of them is 60 kilobytes
- 30:36plus a little for the friend list. Call
- 30:39it 61 kilobytes to draw a screen that
- 30:43needs only names and photos. There are
- 30:45two problems here and they have names.
- 30:49The first is overfetching. The app
- 30:52ordered whole records when it wanted two
- 30:54fields from each. It's like walking into
- 30:57a restaurant wanting only the soup and
- 31:00being told the kitchen only serves the
- 31:03entire menu, every course, every time
- 31:07you eat your soup and leave the rest.
- 31:10The second is underfetching. No single
- 31:14request gave the app everything it
- 31:16needed, so it had to keep going back.
- 31:19Each trip across a mobile network in
- 31:222012 might easily take 3/10 of a second.
- 31:27If the app made its 11 requests one
- 31:30after another, that's about 3 and 1/3
- 31:33seconds of waiting with a spinning wheel
- 31:36on the screen. A desktop browser on a
- 31:40fast office connection could shrug that
- 31:43off. A phone on a train couldn't.
- 31:46GraphQL changed who decides the shape of
- 31:49the answer. Instead of the server
- 31:51deciding what each resource contains,
- 31:54the app writes a small description of
- 31:57exactly the data it wants and sends that
- 32:00description in one request. In plain
- 32:04words, the app's request says, starting
- 32:07from me, give me my friends, the first
- 32:1010 of them, and for each one, only their
- 32:13name and their photo. The server reads
- 32:17that description, gathers exactly those
- 32:20pieces, and sends back one answer shaped
- 32:24exactly like the question, 10 names, 10
- 32:28photo addresses, about 2 kilobytes, one
- 32:32trip, 61 kilobytes in 11 trips or 2
- 32:36kilobytes in one. It's like ordering à
- 32:39la carte with a very precise waiter.
- 32:42Soup, no croutons, and a half portion of
- 32:46bread, and it all comes out together on
- 32:49one tray. Behind the scenes, a GraphQL
- 32:53server starts with a written description
- 32:56of everything it can offer, called a
- 32:58schema. The schema says that a user has
- 33:01a name, which is text, and a photo,
- 33:05which is an address, and friends, which
- 33:08is a list of other users. Every field
- 33:11has a type, so the app can't ask for
- 33:14something that doesn't exist, and tools
- 33:17can check a request for mistakes before
- 33:19it's ever sent. For each field in the
- 33:22schema, the backend has a small piece of
- 33:25logic that knows how to fetch it. When a
- 33:27request arrives, the server walks
- 33:30through the description, calls the right
- 33:33piece for each field, and assembles the
- 33:36result. The phone never needs to know
- 33:39where each piece came from. A friend's
- 33:42name might live in one database and
- 33:45their photo address in another. The
- 33:48answer arrives as one neat bundle.
- 33:51Facebook used GraphQL internally for a
- 33:55few years. In 2015, it published the
- 33:59idea and a reference version as open
- 34:02source. Other companies picked it up,
- 34:05and in 2018, Facebook moved the project
- 34:09into a new independent foundation so
- 34:12that no single company would control it.
- 34:15GraphQL isn't free, though. It moves a
- 34:17lot of power to the person writing the
- 34:20request, and the backend has to be ready
- 34:23for that. A request can ask for friends
- 34:26of friends of friends, nested 10 levels
- 34:29deep and turn into millions of lookups.
- 34:33So GraphQL servers usually measure how
- 34:36expensive a request will be before
- 34:39running it and refuse ones that go too
- 34:42far. Saving answers for reuse also gets
- 34:46harder. With rest, a get request for
- 34:49books/42
- 34:51always means the same thing, so a copy
- 34:55of the answer can be kept and handed out
- 34:58again. With GraphQL, almost every
- 35:01request is a slightly different
- 35:03question, often sent to the same single
- 35:07address, so those simple saved copies
- 35:10don't work as well. Many teams use both,
- 35:14a public rest interface for outside
- 35:16developers and GraphQL behind their own
- 35:20apps, where the screens change every
- 35:23month and the phone needs exactly what
- 35:26it needs. Now, let's leave the phone
- 35:29behind and go deeper into the kitchen.
- 35:32Inside a large company, most requests
- 35:36aren't coming from phones at all.
- 35:38They're coming from other parts of the
- 35:40back end. One program asks another to
- 35:44check a price. That one asks a third to
- 35:47look up a stock level. A single tap on
- 35:50your screen might set off dozens of
- 35:53these internal conversations.
- 35:56For those conversations,
- 35:58speed and strictness matter far more
- 36:01than readability. Google had faced this
- 36:04early. For more than a decade, its
- 36:07internal programs had spoken to each
- 36:10other through a system of its own, known
- 36:13inside the company as Stubby. In
- 36:16February 2015,
- 36:18Google released a new open version of
- 36:21the same ideas built for the outside
- 36:24world. It was called gRPC.
- 36:28The RPC part stands for remote procedure
- 36:32call. That's the old idea from the last
- 36:35chapter, calling a function inside
- 36:38another program as if it were next door.
- 36:41Rest had moved away from it for public
- 36:44interfaces, but for programs inside the
- 36:47same building owned by the same company,
- 36:50calling functions directly turned out to
- 36:53be exactly right as long as both sides
- 36:57agreed precisely on what those functions
- 36:59were. That precise agreement is written
- 37:03in a small contract file. It lists each
- 37:06function, what it takes in and what it
- 37:09gives back, and the exact shape of every
- 37:12message. From that one file, tools
- 37:16generate matching code for both sides in
- 37:19many different programming languages. A
- 37:22service written in one language can call
- 37:25a service written in another, and if
- 37:28either side breaks the contract, the
- 37:30mistake shows up while the code is being
- 37:33built, not at 3:00 in the morning. The
- 37:35messages themselves travel in a compact
- 37:39binary format called protocol buffers,
- 37:42which Google had used internally for
- 37:44years. Here's where the savings come
- 37:47from. Say one program sends another a
- 37:51temperature reading of 21 degrees. In
- 37:54JSON, the message is written out in
- 37:57text, an opening brace, the word
- 38:00temperature in quotation marks, a colon,
- 38:04the digits two and one, and a closing
- 38:07brace. That's 18 characters, so 18
- 38:13bytes. In protocol buffers, the word
- 38:16temperature never travels at all. Both
- 38:19sides already have the contract, which
- 38:22says that field number one is the
- 38:25temperature. So the message is one byte
- 38:29that means field number one followed by
- 38:32one byte holding the number 21, two
- 38:35bytes instead of 18. It's the same trick
- 38:39a busy kitchen uses on its tickets.
- 38:42Nobody writes two flat whites with oat
- 38:45milk for the customer at table seven.
- 38:48They write T7 to FW oat. Everyone at the
- 38:53pass knows the shorthand, so the
- 38:56shorthand is all that needs to travel.
- 38:59On a single message, 16 bytes saved is
- 39:03nothing. But by Google's own account in
- 39:052015, its data centers were handling
- 39:09tens of billions of these calls every
- 39:12second. At that scale, small savings in
- 39:15every message add up to a great many
- 39:18machines. gRPC also rides on HTTP
- 39:23version two, which can carry many
- 39:26conversations at once over a single
- 39:29connection and lets either side stream a
- 39:32long run of messages without starting a
- 39:35new request each time. So by the middle
- 39:38of the 2010s, backend engineers had
- 39:42three main ways for programs to talk.
- 39:45REST with its clean addresses and
- 39:48familiar verbs for the wide outside
- 39:51world. GraphQL for apps that want to ask
- 39:55for exactly the shape of data they need.
- 39:58And gRPC,
- 40:00fast and strict for the busy internal
- 40:03corridors of the backend. None of them
- 40:06replaced the others. Each fits a
- 40:08different kind of conversation, but an
- 40:11interface is only the doorway. Someone
- 40:15still has to write everything behind it.
- 40:18The logic that checks orders and saves
- 40:20rows and sends replies over and over for
- 40:25every new product. In 2004,
- 40:29a young programmer in Copenhagen,
- 40:31working remotely for a small company in
- 40:34Chicago, got tired of writing the same
- 40:37pieces over and over. So he pulled them
- 40:40out of the product he was building, gave
- 40:43them a name, and gave them away.
- 40:47Chapter four, frameworks that shipped.
- 40:51It's 2003
- 40:53in Copenhagen, and a young Danish
- 40:55programmer named David Heinemeier
- 40:58Hansson is building a web application
- 41:01for a small design company in Chicago.
- 41:04The company is called 37signals.
- 41:07One of its founders, Jason Fried, wants
- 41:10a simple tool for managing projects with
- 41:13clients, a place to post messages, keep
- 41:17to-do lists, and track milestones. It
- 41:20will be called Basecamp. Heinemeier
- 41:23Hansson, who would become known to
- 41:25almost everyone in the field by his
- 41:27initials, DHH,
- 41:30was working remotely, part-time, across
- 41:34an ocean and seven time zones. He'd
- 41:37chosen to write Basecamp in Ruby, a
- 41:40programming language created in Japan by
- 41:43Yukihiro Matsumoto in the mid-1990s
- 41:47and still little known in the West at
- 41:49the time. DHH liked it because it read
- 41:53almost like English and because it let a
- 41:56programmer bend the language to suit the
- 41:59problem. As he built Basecamp, he
- 42:02noticed something every back-end
- 42:04programmer notices sooner or later. Most
- 42:08of the work wasn't about project
- 42:10management at all. It was plumbing,
- 42:13taking a request that arrives at an
- 42:15address and deciding which piece of code
- 42:18should handle it, reading the fields
- 42:21from a form, checking that an email
- 42:24address looks like an email address,
- 42:26turning a row from the database into an
- 42:29object the program can use and turning
- 42:33it back again, filling in a page
- 42:35template with the results. Every web
- 42:38application needed all of this, and
- 42:41every team was writing it again by hand
- 42:45from scratch. So DHH wrote his plumbing
- 42:49carefully in a way that wasn't tied to
- 42:52Basecamp. Basecamp launched in early
- 42:552004
- 42:56and that July, he pulled the plumbing
- 42:59out, gave it a name, and released it for
- 43:02anyone to use free. He called it Ruby on
- 43:06Rails. It's a little like a chef who has
- 43:09spent years setting up kitchens and
- 43:12finally writes down the layout that
- 43:14works, where the stoves go, where the
- 43:17knives hang, which station is next to
- 43:20which. Any new restaurant can start with
- 43:23that layout on day one and get straight
- 43:27to cooking. Rails was built around two
- 43:30short phrases that its fans repeated
- 43:33often. The first was don't repeat
- 43:36yourself. Every piece of knowledge in a
- 43:38program should live in exactly one
- 43:41place. If the rule that a price can't be
- 43:44negative is written in one place, then
- 43:47fixing it means changing one place. The
- 43:51second phrase was convention over
- 43:53configuration,
- 43:55and that's where Rails felt different
- 43:57from almost everything before it. Many
- 44:00frameworks of the time asked you to
- 44:03describe everything in long
- 44:06configuration files. This class of
- 44:09object is stored in that table. This
- 44:12column holds that field. This address
- 44:15goes to that piece of code. Rails took a
- 44:18different approach. It made sensible
- 44:21guesses and only asked you to speak up
- 44:24when you wanted something different.
- 44:27Let's watch those guesses at work. Say
- 44:30you're building a small shop and you
- 44:32tell Rails you have a kind of thing
- 44:34called a product. Without being told,
- 44:37Rails assumes the products are stored in
- 44:40a database table called products, the
- 44:43plural. Each column in that table
- 44:46becomes a property of a product
- 44:49automatically. If the table has a column
- 44:51called price, then every product simply
- 44:54has a price. Rails knows English
- 44:58plurals, even the awkward ones. Tell it
- 45:01about a person and it looks for a table
- 45:04called people. Now say each order
- 45:07belongs to a customer. By convention,
- 45:11the orders table has a column named
- 45:14customer _ id. Rails sees that name and
- 45:20understands the link on its own. Ask an
- 45:23order for its customer and it goes and
- 45:26fetches the right one. Then there are
- 45:29addresses. One short line in Rails
- 45:33declares that products are a resource,
- 45:36the very idea from Roy Fielding's
- 45:39dissertation two chapters ago. From that
- 45:42one line, Rails sets up seven standard
- 45:45actions. List all the products, show one
- 45:49product, show a form for a new product,
- 45:53create it, show a form to edit a
- 45:56product, update it, and delete it. One
- 46:00line, seven actions, each wired to the
- 46:03right address and the right verb.
- 46:05Underneath, Rails organized every
- 46:08application into three kinds of pieces,
- 46:12an arrangement called model, view,
- 46:15controller, which traces back to work by
- 46:18Trygve Reenskaug at Xerox Park in the
- 46:22late 1970s.
- 46:24Let's follow one request through them. A
- 46:27customer's browser asks for products /
- 46:3042. First, the router reads the address
- 46:34and sees the pattern, products, then a
- 46:37number. That means the show action of
- 46:40the products controller with the number
- 46:4242. Next, the controller is the
- 46:46expediter of this little kitchen. It
- 46:49doesn't cook. It asks the model for
- 46:51product 42. The model is the part that
- 46:55knows about data and rules. It fetches
- 46:59row 42 from the products table, a
- 47:02teapot, $24.
- 47:04Last, the controller hands that teapot
- 47:08to the view, a page template with gaps
- 47:11in it, and the view fills in the gaps.
- 47:14Teapot, $24.
- 47:17Add to cart. Router, controller, model,
- 47:21view. Every request in every Rails
- 47:24application takes the same path. A
- 47:28programmer joining a new Rails project
- 47:31already knows where everything lives
- 47:34because it lives where it always lives.
- 47:37In 2005,
- 47:39DHH recorded a short screencast, a video
- 47:43of his own screen as he worked, in which
- 47:46he built a simple blog in about 15
- 47:49minutes. Posts, comments, a page to
- 47:52write new ones. At several points, as
- 47:56the story is usually told, he paused to
- 47:59point out all the things he wasn't
- 48:01having to do. For programmers who had
- 48:04spent weeks wiring up their last
- 48:06project, it was startling to watch. The
- 48:10video spread quickly, and many people
- 48:13later said it was the reason they tried
- 48:16Rails at all. Rails reached version 1.0
- 48:20in December 2005.
- 48:23The following year, Apple announced that
- 48:25it would include Rails with the next
- 48:28version of its desktop operating system,
- 48:31Mac OS X Leopard, which shipped in 2007.
- 48:36In the years after, a long line of
- 48:38companies started life on Rails. Among
- 48:42them, GitHub and Shopify and a small
- 48:45messaging service called Twitter, which
- 48:47we'll meet again in the next chapter. At
- 48:50almost exactly the same time, in a very
- 48:53different setting, the same idea was
- 48:56being discovered again. It's autumn 2003
- 49:00in Lawrence, Kansas, a university town
- 49:04on the edge of the prairie. The local
- 49:06newspaper is the Lawrence Journal-World,
- 49:09and it's unusually ambitious about the
- 49:12web. It has a small team of web
- 49:14developers working right in the
- 49:16newsroom. Two of them are Adrian
- 49:19Holovaty, a young journalist who also
- 49:22programmed, and Simon Willison, an
- 49:25intern who had come over from England.
- 49:28They were building sites for the paper
- 49:30in the Python language, and they were
- 49:33building them on newspaper time. A
- 49:35newsroom runs on deadlines. A big local
- 49:39story breaks in the afternoon, and the
- 49:42editors want a page with a searchable
- 49:44database of results or a map or a
- 49:48collection of readers' photos live by
- 49:51the evening edition. There's no room for
- 49:54weeks of setup. So, like DHH, Holovaty
- 49:58and Willison found themselves writing
- 50:01the same plumbing again and again and
- 50:04pulled it out into a framework of their
- 50:06own. Another developer, Jacob
- 50:09Kaplan-Moss, joined the team soon after.
- 50:12In July 2005,
- 50:15the newspaper released it to the world
- 50:17free. Holovaty plays jazz guitar in the
- 50:21swinging gypsy jazz style, and he named
- 50:25the framework after the most famous
- 50:27player of that style, the Belgian-born
- 50:30Romani guitarist Django Reinhardt. So,
- 50:34the framework was called Django. Its
- 50:36tagline described it as the web
- 50:39framework for perfectionists with
- 50:42deadlines. Django shared many of Rails's
- 50:45ideas, models and templates and tidy
- 50:49addresses, but it had one feature that
- 50:52came straight from the newsroom, and it
- 50:54amazed people when they first saw it,
- 50:57the admin. In a newspaper, the people
- 51:00who enter the
- 51:02aren't programmers. They're reporters,
- 51:05editors, and clerks. So Django could
- 51:08look at the models you described and
- 51:10build a complete editing screen for them
- 51:13automatically with no extra work. Say
- 51:16the paper wants a page listing local
- 51:19restaurant inspections. A developer
- 51:22describes one model, an inspection with
- 51:25five fields, the restaurant's name, its
- 51:29address, the date, the score, and the
- 51:32inspector's notes. That's all. From that
- 51:35single description, Django's admin
- 51:38produces a list of every inspection with
- 51:41search and filters, a form to add a new
- 51:44one that checks each field is filled in
- 51:47properly, a form to edit, and a way to
- 51:50delete. A reporter could start typing in
- 51:53inspection results that same afternoon
- 51:56while the developer got on with the
- 51:58public page. Django spread from
- 52:01newsrooms into everywhere else. By 2008,
- 52:05it had its own independent foundation to
- 52:08look after it. And over the years, it
- 52:11carried some very large sites, including
- 52:14Instagram in its early days. Rails and
- 52:18Django had the same deeper effect. They
- 52:21turned the back end from a craft that
- 52:24every team reinvented into a set of good
- 52:27habits that came in the box. Validate
- 52:30the input, keep the rules in the model,
- 52:33make database changes in small recorded
- 52:37steps, so every copy of the application
- 52:40can be brought up to date the same way.
- 52:43Follow a predictable shape so the next
- 52:46person can find their way around. They
- 52:49didn't make back end engineering easy,
- 52:52but they made the first version of an
- 52:54idea fast. A small team could go from a
- 52:57sketch on a napkin to a working site in
- 53:01days. And sometimes that worked a little
- 53:04too well. A small site built in days
- 53:08could find itself a year or two later
- 53:12with millions of people pressing its
- 53:14buttons at once. The framework would
- 53:16still route the requests and fill in the
- 53:19templates exactly as designed, but one
- 53:23server or even a handful could no longer
- 53:27keep up. In 2008, one of the most talked
- 53:31about sites on the internet kept falling
- 53:34over. And each time it did, its visitors
- 53:37were shown the same small, cheerful
- 53:40drawing.
- 53:43Chapter Five, Remembering Nothing. It's
- 53:46the spring of 2008 and a drawing of a
- 53:50whale is about to become famous. A young
- 53:53designer named Yi Ying Lu had made the
- 53:57picture as a greeting for friends who
- 54:00lived far away. It shows a large,
- 54:03smiling whale being lifted out of the
- 54:06sea by a flock of small orange birds,
- 54:10eight of them. She called it Lifting a
- 54:12Dreamer, and she posted it on a website
- 54:16where artists sell their images for
- 54:18other people to use. Somewhere on the
- 54:21other side of that website, one of the
- 54:24founders of Twitter, Biz Stone, was
- 54:27looking for something to show people
- 54:29when the service was overloaded. He
- 54:31found the whale. Around May of 2008,
- 54:35Twitter began to use it. And that year,
- 54:39people saw it a great deal. Twitter was
- 54:41only two years old, a service for
- 54:44posting short messages to anyone who
- 54:47chose to follow you. It had grown far
- 54:50faster than anyone had planned. Every
- 54:53time a big event happened, a keynote, a
- 54:56sports final, a piece of breaking news,
- 54:59huge numbers of people rushed to post
- 55:02about it at the same moment. And again
- 55:05and again, the site couldn't keep up.
- 55:09Instead of your messages, you'd see the
- 55:11whale held aloft by its little birds and
- 55:15a short note saying Twitter was over
- 55:19capacity. Users gave it a nickname. They
- 55:22called it the fail whale. It turned up
- 55:25on t-shirts and in jokes, and it
- 55:28gathered a fan following of its own.
- 55:31It's one of the very few error messages
- 55:34people have ever felt fond of. Behind
- 55:37that gentle drawing was a hard
- 55:39engineering problem, and it's one almost
- 55:42every successful back end meets sooner
- 55:45or later. The problem is how to handle
- 55:4810 times as many people as last month,
- 55:52and then 10 times that. There are two
- 55:55basic answers. The first is to buy a
- 55:58bigger machine, more processors, more
- 56:02memory, faster disks. Engineers call
- 56:05this scaling up or vertical scaling.
- 56:08It's simple, and for a while it works
- 56:11beautifully, but there's always a
- 56:13biggest machine you can buy, and the
- 56:16price climbs steeply as you approach it.
- 56:19Worse, if that one machine fails,
- 56:23everything fails with it. The second
- 56:25answer is to use more machines side by
- 56:29side. Engineers call this scaling out or
- 56:34horizontal scaling. In a kitchen, the
- 56:37first answer is like hiring one
- 56:39superhuman cook who chops twice as fast.
- 56:43The second is hiring more cooks.
- 56:46Superhuman cooks are rare and expensive,
- 56:50and now and then they fall ill. Ordinary
- 56:53cooks are easy to find, and if one goes
- 56:56home sick, the others keep cooking.
- 56:59Almost every large back end in the world
- 57:02today scales out, but scaling out only
- 57:05works if the cooks are interchangeable.
- 57:08Any order must be able to go to any
- 57:11cook. And that leads to one of the most
- 57:14important habits in all of back end
- 57:17engineering. The servers should remember
- 57:19nothing. Let's see why. Say you're
- 57:22shopping on a site that runs on two
- 57:25servers, A and B. You add a kettle to
- 57:28your cart, and that request happens to
- 57:31go to server A, which writes kettle in a
- 57:34note in its own memory. Your next click
- 57:38to add a rug happens to go to server B.
- 57:41Server B has never heard of you. It has
- 57:44no note. As far as it knows, your cart
- 57:48is empty. You could try to fix this by
- 57:50always sending the same customer to the
- 57:53same server. Engineers call that sticky
- 57:56sessions, and it's sometimes used, but
- 58:00it's fragile. If server A needs a
- 58:03restart, every cart it was holding is
- 58:06lost. And if one server ends up with a
- 58:09crowd of very busy customers, it can't
- 58:12hand any of them off to its quieter
- 58:14neighbor. The cleaner answer is to make
- 58:17every server stateless. The server keeps
- 58:21nothing about you between requests.
- 58:24Anything that needs to be remembered,
- 58:26your cart, your login, your half
- 58:29finished order goes into a shared store
- 58:33that every server can reach, usually a
- 58:36database or a fast shared memory of the
- 58:39kind we'll meet in the next chapter. If
- 58:42you heard our lecture on web
- 58:44development, you'll remember the
- 58:46session, the number on a slip of paper
- 58:49matched to a notebook behind the
- 58:51counter. A stateless back end simply
- 58:55moves that notebook out from behind any
- 58:58one counter and puts it in a back office
- 59:01where every counter can read it. Now
- 59:04each request carries what it needs, its
- 59:07session number, its token, the details
- 59:10of what it wants. Any server can pick it
- 59:13up, look up what it needs in the shared
- 59:16store, do the work, and forget all about
- 59:19it. It's the fourth of Roy Fielding's
- 59:22ideas from earlier tonight put to work.
- 59:25Each request should carry everything
- 59:27needed to understand it. The servers
- 59:30become like cooks working from tickets.
- 59:33Any cook can pick up any ticket because
- 59:36everything about the order is written on
- 59:39it or in the shared order book on the
- 59:41wall. No cook needs to remember who
- 59:44ordered what an hour ago. Once the
- 59:47servers are interchangeable, you need
- 59:50someone at the door to share out the
- 59:52work. That's the load balancer. A load
- 59:55balancer is a program or sometimes a
- 59:59dedicated device that sits in front of
- 1:00:02all the servers. Every request from the
- 1:00:05outside world arrives at the load
- 1:00:07balancer first. It chooses a server,
- 1:00:11passes the request along, and passes the
- 1:00:14answer back. To the customer, it all
- 1:00:17looks like one single, very capable
- 1:00:21machine. It's like the host at the front
- 1:00:23of a busy restaurant who greets each
- 1:00:26party and seats them in a different
- 1:00:29waiter's section so that no single
- 1:00:31waiter is buried while another stands
- 1:00:34idle. The simplest way to choose is to
- 1:00:38take turns. First, request to server
- 1:00:41one, next to server two, next to server
- 1:00:44three, and round again. That's called
- 1:00:47round robin. A slightly smarter way is
- 1:00:51to send each new request to whichever
- 1:00:54server is currently handling the fewest.
- 1:00:57Whatever the method, the load balancer
- 1:00:59also keeps checking on its server's
- 1:01:02health and stops sending work to any
- 1:01:04that aren't answering. And because the
- 1:01:07load balancer is now the one door that
- 1:01:10every request passes through, it's
- 1:01:12usually set up as a pair. If one of the
- 1:01:15pair fails, its twin takes over within
- 1:01:19moments, and the customers waiting
- 1:01:21outside never know there was a change of
- 1:01:24host. Let's put some numbers on this.
- 1:01:28Say a shop's back end receives 3,000
- 1:01:31requests every second at its busiest
- 1:01:34time of day. And say one server can
- 1:01:37comfortably handle 500 requests a second
- 1:01:41before it starts to slow down. 3,000
- 1:01:44divided by 500 is 6. So six servers in
- 1:01:49theory. But running every server flat
- 1:01:52out leaves no room for surprises, a
- 1:01:55sudden rush, a slow database, a server
- 1:01:59that falls over. So engineers usually
- 1:02:02aim to run each server at a comfortable
- 1:02:05fraction of its limit, say 60%. That's
- 1:02:09300 requests a second per server. 3,000
- 1:02:14divided by 300 is 10. So the shop runs
- 1:02:1810 servers. Now one server fails. Its
- 1:02:22power supply gives out or its software
- 1:02:25freezes. The load balancer has been
- 1:02:27checking each server every 5 seconds
- 1:02:31with a small request that amounts to,
- 1:02:34are you alive? After three checks in a
- 1:02:36row with no answer, it decides the
- 1:02:39server is gone. That takes about 15
- 1:02:42seconds. During those seconds, some
- 1:02:46requests sent to the broken server fail
- 1:02:49and are tried again. Then the load
- 1:02:52balancer simply stops sending it
- 1:02:54anything. The 3,000 requests a second
- 1:02:58are now shared among nine servers
- 1:03:01instead of 10. That's about 333
- 1:03:05each, two-thirds of what a server can
- 1:03:08handle. Everything keeps working.
- 1:03:11Customers might notice nothing at all.
- 1:03:14Meanwhile, an automatic system notices
- 1:03:17that the remaining servers are busier
- 1:03:19than usual and starts a fresh one to
- 1:03:22replace the lost one. A couple of
- 1:03:25minutes later, there are ten again. No
- 1:03:28one had to wake up. That's the quiet
- 1:03:30gift of stateless servers because none
- 1:03:34of them holds anything precious, any one
- 1:03:36of them can be thrown away and replaced
- 1:03:39at any moment like a paper cup. This
- 1:03:42also makes growth almost mechanical. If
- 1:03:46the shop's busiest moment doubles next
- 1:03:49year to 6,000 requests a second, the
- 1:03:53answer is 20 servers instead of 10. Many
- 1:03:56systems now do this automatically
- 1:03:59through the day, adding servers as the
- 1:04:02morning rush arrives and removing them
- 1:04:04again at night. Statelessness doesn't
- 1:04:07make the hard problems disappear though.
- 1:04:10It moves them. All that remembering now
- 1:04:13happens in the shared stores, the
- 1:04:16databases and caches, and those have to
- 1:04:19be fast and reliable in their own right.
- 1:04:22Much of the rest of back-end engineering
- 1:04:25is about looking after them. For
- 1:04:27Twitter, the road out of the fail whale
- 1:04:30years was long. Over the following
- 1:04:33years, its engineers rebuilt large parts
- 1:04:36of the system, spreading the work across
- 1:04:39many more machines and separating out
- 1:04:42the pieces that were under the most
- 1:04:44strain. Bit by bit, the whale appeared
- 1:04:47less often and eventually it was retired
- 1:04:51altogether. Yi Ying Lu's drawing
- 1:04:54outlived its job. It became a small
- 1:04:57piece of internet history, a reminder
- 1:05:00that even the busiest services began as
- 1:05:04a few servers struggling to keep up and
- 1:05:07that a little kindness on an error page
- 1:05:10can turn frustration into affection. But
- 1:05:13there was one lesson in all of this that
- 1:05:16every growing site learned, Twitter
- 1:05:18included. When you look closely at what
- 1:05:21those overloaded servers were actually
- 1:05:23doing, a surprising amount of their work
- 1:05:27was the same work repeated. The same
- 1:05:30popular profile fetched from the
- 1:05:32database 10,000 times in a minute. The
- 1:05:36same front page rebuilt from scratch for
- 1:05:40every single visitor. When the answer
- 1:05:43hasn't changed, there's no need to work
- 1:05:46it out again. You can keep it close at
- 1:05:48hand instead, on a shelf you can reach
- 1:05:52without moving your feet. In 2009
- 1:05:55in Sicily, a programmer who was
- 1:05:58struggling to keep up with a flood of
- 1:06:00small writes decided to build exactly
- 1:06:03that kind of shelf.
- 1:06:07Chapter six, the fast shelf. It's early
- 1:06:112009
- 1:06:12in Catania, on the eastern coast of
- 1:06:15Sicily, where the city sits at the foot
- 1:06:18of Mount Etna and the volcano's long
- 1:06:21slope fills the horizon. A programmer
- 1:06:25named Salvatore Sanfilippo, known online
- 1:06:29by the nickname Antirez, is working on a
- 1:06:32small startup. One of its products is a
- 1:06:35tool called Loog. Website owners put a
- 1:06:39tiny snippet on their pages and Loog
- 1:06:42shows them live who is visiting right
- 1:06:46now, which page each visitor is on,
- 1:06:49where they came from, what they click
- 1:06:51next, scrolling past on the screen as it
- 1:06:55happens. It's a charming idea and it has
- 1:06:58an awkward property. Every single page
- 1:07:01view on every site that uses it has to
- 1:07:05be written down immediately. Thousands
- 1:07:08of tiny writes all day long and readers
- 1:07:11who want to see the latest ones at once.
- 1:07:14Loog kept its data in a conventional
- 1:07:17database, MySQL, which stores things on
- 1:07:21disk, and disks, even good ones, are
- 1:07:25slow compared with a computer's memory.
- 1:07:28Each small write had to wait its turn on
- 1:07:31the disk. As more sites signed up, the
- 1:07:34database fell further and further
- 1:07:37behind. San Filippo tried the usual
- 1:07:40remedies, tuning and indexing and adding
- 1:07:43hardware, and none of them really
- 1:07:45changed the picture. The problem was the
- 1:07:48shape of the tool. He was asking a
- 1:07:51careful, disk-based filing cabinet to
- 1:07:54behave like a whiteboard, so he built a
- 1:07:57whiteboard. His first prototype was only
- 1:08:00a few hundred lines long, written in a
- 1:08:03small scripting language called TCL. It
- 1:08:07kept all of its data in memory, where
- 1:08:10reading and writing take a tiny fraction
- 1:08:13of the time a disk needs. And it didn't
- 1:08:16just store simple values. It understood
- 1:08:19lists, so Logue could push each new page
- 1:08:23view onto the front of a list and trim
- 1:08:25the old ones off the back. It worked. He
- 1:08:29rewrote it in the faster language C, and
- 1:08:32in February 2009
- 1:08:35he shared it with the world as a free,
- 1:08:38open project. He called it Redis, short
- 1:08:42for Remote Dictionary Server. A
- 1:08:45dictionary in programming is just a
- 1:08:48collection of labels and values. Look up
- 1:08:51a label, get back its value. The label
- 1:08:55might be user 404
- 1:08:58profile, and the value might be that
- 1:09:01user's name, photo address, and bio.
- 1:09:04Remote means the dictionary lives on its
- 1:09:08own server, where many other programs
- 1:09:10can reach it over the network. So Redis
- 1:09:14is a big shared dictionary kept in
- 1:09:16memory and very fast. At first it was a
- 1:09:20side project from Sicily. About a A
- 1:09:23later, a large software company, VMware,
- 1:09:27hired San Filippo to work on it
- 1:09:30full-time, and Redis went on to become
- 1:09:33one of the most widely used pieces of
- 1:09:36back-end software in the world. He
- 1:09:38stepped back from leading it in 2020 and
- 1:09:42returned to work on it again at the end
- 1:09:45of 2024.
- 1:09:47It wasn't the first tool of its kind. If
- 1:09:50you heard our lecture on database
- 1:09:52engineering, you'll remember Memcached
- 1:09:55D, written in 2003
- 1:09:58to keep a busy blogging site from
- 1:10:00drowning. Memcached was a plain shared
- 1:10:04memory for simple values. Redis took the
- 1:10:07same idea and gave it richer shapes and
- 1:10:11the ability to save a copy of itself to
- 1:10:14disk so it could come back after a
- 1:10:17restart. To see why a shared memory like
- 1:10:20this matters so much, let's go back into
- 1:10:23the kitchen. In a busy restaurant
- 1:10:25kitchen, the main stores are in a
- 1:10:28walk-in cooler down the hall. Everything
- 1:10:31is there, carefully labeled, kept at the
- 1:10:34right temperature, but walking to the
- 1:10:36cooler takes time. If a cook went to the
- 1:10:39cooler for every pinch of chopped
- 1:10:42parsley, the dinner service would grind
- 1:10:45to a halt. So before service, each cook
- 1:10:48fills a small shelf right beside their
- 1:10:51station. Chopped herbs, sauces, butter,
- 1:10:56the things they reach for again and
- 1:10:58again. Most of the time, what they need
- 1:11:02is right there, within arms reach. Only
- 1:11:05now and then do they walk down the hall.
- 1:11:09The walk-in cooler is the database. The
- 1:11:12little shelf is the cache. A cache is a
- 1:11:16place to keep copies of answers you're
- 1:11:18likely to need again, somewhere much
- 1:11:21faster to reach than the original.
- 1:11:24Backends use Redis as a cache more than
- 1:11:27for anything else. The pattern goes like
- 1:11:30this. When a request needs a piece of
- 1:11:33data, the back end checks the cache
- 1:11:36first. If the answer is there, that's
- 1:11:39called a hit, and the back end uses it
- 1:11:42straight away. If it isn't, that's
- 1:11:44called a miss. The back end goes to the
- 1:11:47database, gets the answer, puts a copy
- 1:11:51on the shelf for next time, and carries
- 1:11:53on. Let's put some numbers on that. Say
- 1:11:57a shop's back end needs to look up
- 1:12:00product details 10,000 times a second,
- 1:12:03and say a trip to the database takes
- 1:12:06about 10,000ths of a second, while a
- 1:12:09trip to the cache takes about half of
- 1:12:121/1000th. Without a cache, all 10,000
- 1:12:17lookups go to the database every second,
- 1:12:20each taking 10,000ths of a second. Now
- 1:12:24add a cache and say 95% of lookups are
- 1:12:28hits. Then 9,500
- 1:12:31lookups a second are answered from the
- 1:12:34shelf. Only 500 a second go to the
- 1:12:37database. The database's load has
- 1:12:40dropped from 10,000 to 500, a 20th of
- 1:12:44what it was. The average time per lookup
- 1:12:47drops too. 95% of lookups take half a
- 1:12:51thousandth of a second. The other 5%
- 1:12:55first check the cache, find nothing, and
- 1:12:59then go to the database. So they take 10
- 1:13:02and 1/2 thousandths. Blend those
- 1:13:04together, and the average comes to
- 1:13:07almost exactly 1/1000th of a second,
- 1:13:101/1000th instead of 10, 10 times faster
- 1:13:14on average. With a database that's
- 1:13:17barely breaking a sweat, the hit rate
- 1:13:20matters more than you might expect. If
- 1:13:23it slips from 95% to 80, the database's
- 1:13:27load goes from 500 lookups a second to
- 1:13:312000, four times as much, from a drop of
- 1:13:3515 percentage points. That's why back
- 1:13:38end engineers watch their hit rates
- 1:13:41closely. a catch, of course, and it's
- 1:13:44the one every cache brings with it. A
- 1:13:47copy can go out of date. Say the shop
- 1:13:50drops the price of its teapot from $24
- 1:13:54to 20. The database now says 20, but the
- 1:13:58cache still holds a copy that says 24.
- 1:14:03Until that copy is replaced, some
- 1:14:05customers will be shown the old price.
- 1:14:09Removing or refreshing stale copies is
- 1:14:12called cache invalidation, and back ends
- 1:14:15use two main approaches, usually
- 1:14:18together. The first is to give every
- 1:14:21copy an expiry time. When the back end
- 1:14:25puts the teapot on the shelf, it says,
- 1:14:28keep this for 60 seconds. After that,
- 1:14:31Redis quietly throws it away, and the
- 1:14:34next lookup goes to the database and
- 1:14:37fetches the fresh price. So a stale copy
- 1:14:40can never live longer than a minute. The
- 1:14:43second is to clear the shelf
- 1:14:45deliberately. When the price changes in
- 1:14:48the database, the code that changed it
- 1:14:51also deletes the teapot from the cache.
- 1:14:55The very next lookup misses and fetches
- 1:14:58$20.
- 1:15:00Even with both, there can be brief
- 1:15:02moments when the shelf and the cooler
- 1:15:05disagree. So careful back ends follow a
- 1:15:08simple rule. Use the cache for showing
- 1:15:12and the database for deciding. The
- 1:15:15product page can show a price from the
- 1:15:18cache, but at checkout, when real money
- 1:15:21changes hands, the back end looks the
- 1:15:24price up in the database itself. That's
- 1:15:27the validation step from our coffee
- 1:15:30order in the first chapter. A good
- 1:15:32kitchen doesn't let a sticky note on the
- 1:15:35shelf write the bill. Redis does more
- 1:15:39than keep copies. Because it understands
- 1:15:42shapes of data, it gets used for all
- 1:15:45sorts of jobs where speed matters. It
- 1:15:48can be the shared notebook from the last
- 1:15:51chapter, holding every customer's
- 1:15:53session so that any server can find it.
- 1:15:57It can count things. Add one to the
- 1:15:59number of likes on this photo. Add one
- 1:16:03to the number of requests from this
- 1:16:05customer in the last minute, which, as
- 1:16:08we'll see later tonight, is how many
- 1:16:11back ends decide when someone is asking
- 1:16:14too often. And it keeps something called
- 1:16:16a sorted set, a collection that stays in
- 1:16:20order by score all the time, however
- 1:16:23often it changes. Say an online game has
- 1:16:27a million players, each with a score. A
- 1:16:30player finishes a round and their score
- 1:16:33rises from 820
- 1:16:35to 905.
- 1:16:37Redis slides them into their new place
- 1:16:40in the ranking straight away. Asking for
- 1:16:43the top 10 players or for one player's
- 1:16:46exact rank out of a million takes a
- 1:16:49moment because the order is already kept
- 1:16:52rather than worked out from scratch each
- 1:16:55time. That's the general gift of Redis.
- 1:16:58It takes the structures a programmer
- 1:17:01already thinks in, lists and counters
- 1:17:04and rankings, and makes them shared and
- 1:17:07fast for every server in the kitchen.
- 1:17:10And there's something fitting about
- 1:17:12where it began. Its very first job was
- 1:17:15to hold a stream of events as they
- 1:17:18happened. Page views arriving one after
- 1:17:22another, too fast for a filing cabinet
- 1:17:25to keep up. Not every job in a back end
- 1:17:28needs to be fast, though. Some only need
- 1:17:31to happen eventually and reliably
- 1:17:34without making anyone wait for them.
- 1:17:37When you sign up for a new account,
- 1:17:40someone has to send you a welcome email.
- 1:17:43When you upload a photo, someone has to
- 1:17:45make smaller copies of it for different
- 1:17:48screens. When a shop takes 10,000 orders
- 1:17:52before lunch, someone has to print
- 1:17:5410,000 labels. None of that should
- 1:17:57happen while you stand at the counter.
- 1:18:00It should go on a list to be done
- 1:18:03properly in order by someone in the
- 1:18:06back. Anyone who has waited at a busy
- 1:18:09deli counter already knows how that list
- 1:18:12works. You pull a paper ticket from a
- 1:18:15little red machine and you wait for your
- 1:18:18number to be called.
- 1:18:22Chapter seven, take a number. It's 1909
- 1:18:26in Copenhagen and a quiet mathematician
- 1:18:30named Agner Krarup Erlang is watching
- 1:18:33telephone calls. He works for the
- 1:18:36Copenhagen Telephone Company. The city's
- 1:18:39telephone exchanges are rooms full of
- 1:18:42switchboards with operators who plug
- 1:18:45cables into sockets to connect one
- 1:18:47caller to another. The company has a
- 1:18:50practical question and it's an expensive
- 1:18:53one. It needs to know how many lines and
- 1:18:56how many operators an exchange really
- 1:18:59requires. Too few and callers hear
- 1:19:02nothing but a busy signal or wait and
- 1:19:05wait for an operator to answer. Too many
- 1:19:09and the company is paying for equipment
- 1:19:11and people who sit idle most of the day.
- 1:19:14Calls don't arrive on a timetable. They
- 1:19:17come in random clumps, 10 in one minute,
- 1:19:21two in the next, 15 in the one after.
- 1:19:25Erlang set out to describe that
- 1:19:27randomness with mathematics and in 1909
- 1:19:31he published a paper on the theory of
- 1:19:34probabilities and telephone
- 1:19:36conversations. Over the following years,
- 1:19:39his work became the foundation of a
- 1:19:42whole field, the study of waiting lines,
- 1:19:45which mathematicians call queuing
- 1:19:48theory. Telephone engineers honored him
- 1:19:51by naming their basic unit of traffic,
- 1:19:54the Erlang, and much later, a
- 1:19:57programming language built at the
- 1:19:59Swedish telephone company Ericsson for
- 1:20:02systems that must juggle huge numbers of
- 1:20:05simultaneous calls was named after him
- 1:20:08too. Keep that language in mind. It will
- 1:20:12turn up again in a moment. A queue is
- 1:20:15about the most ordinary thing in the
- 1:20:17world. You've stood in hundreds of them.
- 1:20:20At a busy deli counter, there's often a
- 1:20:23little red machine by the door that
- 1:20:26dispenses paper tickets with numbers on
- 1:20:29them. You pull a ticket. You're number
- 1:20:3147. Above the counter, a display says
- 1:20:3539. And then something nice happens. You
- 1:20:40don't have to stand in a line. You can
- 1:20:42wander off and look at the cheeses. When
- 1:20:46your number is called, you step up and
- 1:20:49someone serves you. That ticket machine
- 1:20:52separates two things that would
- 1:20:54otherwise be tangled together. The
- 1:20:57moment you ask for something and the
- 1:20:59moment someone does it, back ends use
- 1:21:02exactly the same trick, and it's one of
- 1:21:05the most useful ideas in the whole
- 1:21:07craft. Remember in the first chapter
- 1:21:11that our coffee order wasn't finished
- 1:21:14when the green tick appeared. There was
- 1:21:16a receipt to email, loyalty points to
- 1:21:19add, the barista's screen to update. A
- 1:21:23good back end doesn't do those jobs
- 1:21:25while you wait. It writes each one on a
- 1:21:28ticket and puts it in a queue. Separate
- 1:21:31programs, called workers, take tickets
- 1:21:34from the queue one at a time and do the
- 1:21:37work. If the queue gets long, you add
- 1:21:40more workers, like opening another till
- 1:21:43at the counter. The program that puts
- 1:21:46tickets in the queue is called the
- 1:21:48producer. The programs that take them
- 1:21:50out are called consumers, and in between
- 1:21:54sits a piece of software whose only job
- 1:21:57is to hold the tickets safely and hand
- 1:22:00them out in order. It's called a message
- 1:22:03broker. For a long time, message brokers
- 1:22:07were expensive commercial products and
- 1:22:09everyone spoke its own private language.
- 1:22:13That bothered a man named John O'Hara,
- 1:22:16who worked on technology at the bank JP
- 1:22:19Morgan Chase in London. Banks are full
- 1:22:22of messages, trades, payments, prices,
- 1:22:26confirmations, all flowing between
- 1:22:29systems that must never lose a single
- 1:22:32one. In 2003,
- 1:22:34O'Hara began work on an open standard
- 1:22:38for message brokers so that any program
- 1:22:41could talk to any broker in the same way
- 1:22:44and no bank would be tied to one
- 1:22:46supplier forever. It became the Advanced
- 1:22:50Message Queuing Protocol or AMQP, a
- 1:22:54standard needs software that speaks it.
- 1:22:57In 2007,
- 1:22:59a small joint venture between two
- 1:23:01companies, the London software firm L
- 1:23:05Shift and a firm called Cohesive FT,
- 1:23:09released a free message broker that
- 1:23:12spoke AMQP. They called it RabbitMQ
- 1:23:16and they wrote it in Erlang, the
- 1:23:18telephone language named for the
- 1:23:20mathematician from Copenhagen. A
- 1:23:23language designed for juggling vast
- 1:23:25numbers of calls without dropping any
- 1:23:28turned out to be a natural fit for
- 1:23:30juggling messages. Let's watch a queue
- 1:23:33earn its keep. Say a new app runs a
- 1:23:36television advertisement and in the next
- 1:23:39few minutes, 10,000 people sign up.
- 1:23:43Every one of them should get a welcome
- 1:23:45email. Sending an email means talking to
- 1:23:48an email service across the internet,
- 1:23:51and say that takes about a fifth of a
- 1:23:53second. Without a queue, the sign up
- 1:23:56request itself would send the email
- 1:23:59before answering. Each new user would
- 1:24:02wait an extra fifth of a second, which
- 1:24:05isn't terrible, but if the email service
- 1:24:07has a bad moment and takes 30 seconds to
- 1:24:11reply, every sign up takes 30 seconds.
- 1:24:14If the email service goes down
- 1:24:16altogether, nobody can sign up at all. A
- 1:24:20problem in a small side job has broken
- 1:24:23the front door. With a queue, the sign
- 1:24:25up request does only the essential work.
- 1:24:28It checks the details, stores the new
- 1:24:31account, and drops a small ticket in the
- 1:24:34queue that says send a welcome email to
- 1:24:37this person. Then it answers in perhaps
- 1:24:40a 20th of a second. The new user is in.
- 1:24:44Meanwhile, the workers chew through the
- 1:24:47tickets. 10,000 emails at a fifth of a
- 1:24:50second each is 2,000 seconds of sending.
- 1:24:54If a single worker did them all, one
- 1:24:57after another, the last person would get
- 1:25:00their welcome email about 33 minutes
- 1:25:03after signing up. With 20 workers
- 1:25:06sharing the queue, each takes its own
- 1:25:09share, and the whole pile is cleared in
- 1:25:12about 100 seconds, a minute and 40
- 1:25:15seconds. And if the email service goes
- 1:25:18down for 10 minutes in the middle of it
- 1:25:21all, nothing is lost. The tickets simply
- 1:25:24wait in the queue. When the service
- 1:25:27comes back, the workers pick up where
- 1:25:29they left off. In this way, a queue
- 1:25:33works like a reservoir behind a dam.
- 1:25:36Water may rush in after a storm, but it
- 1:25:39flows out through the turbines at a
- 1:25:42steady rate. The workers never feel the
- 1:25:45storm. They only see a pile of tickets
- 1:25:48and they work through it at their own
- 1:25:50pace. Erlang's mathematics has something
- 1:25:53important to say here too. Say the
- 1:25:56signups settle down to a steady 100
- 1:26:00every second all day. Each worker can
- 1:26:04send five emails a second. To keep up,
- 1:26:07you need at least 20 workers because 20
- 1:26:10* 5 is 100. If you run only 15 workers,
- 1:26:15they can handle 75 a second. 25 more
- 1:26:19arrive than leave every second and the
- 1:26:23queue grows without stopping. After 10
- 1:26:26minutes, 15,000 tickets are waiting. But
- 1:26:30here's the subtler lesson and it's the
- 1:26:32one Erlang's descendants spelled out.
- 1:26:36Even when the workers can just about
- 1:26:38keep up, waiting times don't grow
- 1:26:41gently. They climb steeply as the
- 1:26:44workers get close to fully busy. In the
- 1:26:47simplest model of a single worker with
- 1:26:51random arrivals, the average time a
- 1:26:54ticket spends waiting in line is the
- 1:26:57time a job takes multiplied by a ratio.
- 1:27:01The ratio is how busy the worker is
- 1:27:05divided by how idle it is. If the worker
- 1:27:08is busy half the time, the ratio is a
- 1:27:11half divided by a half, which is one. A
- 1:27:15ticket waits on average about as long as
- 1:27:19one job takes. At 80% busy, the ratio is
- 1:27:2380 divided by 20, which is four. At 90%,
- 1:27:28it's nine. At 99%,
- 1:27:31it's 99. So a worker that's nearly
- 1:27:35always busy, which might look efficient
- 1:27:38on paper, keeps people waiting a very
- 1:27:41long time. That's why the deli always
- 1:27:44seems to have one more person behind the
- 1:27:47counter than strictly
- 1:27:50on a Saturday morning. And it's why back
- 1:27:53end engineers rarely run anything close
- 1:27:56to flat out. A good message broker adds
- 1:27:59one more careful step and it's the
- 1:28:01reason queues are trusted with important
- 1:28:04work. When a worker takes a ticket, the
- 1:28:07broker doesn't throw the ticket away. It
- 1:28:10sets it aside and waits. Only when the
- 1:28:13worker reports back done does the broker
- 1:28:17delete it. That report is called an
- 1:28:19acknowledgement. If the worker crashes
- 1:28:22halfway through, the acknowledgement
- 1:28:25never comes. After a while, the broker
- 1:28:28puts the ticket back in the queue and
- 1:28:31another worker picks it up. No job is
- 1:28:34silently lost because one worker fell
- 1:28:37over. And if a ticket fails again and
- 1:28:41again, say five times in a row because
- 1:28:44the email address is broken, the broker
- 1:28:47moves it to a separate holding area,
- 1:28:50often called a dead letter queue, where
- 1:28:53a person can look at it later. One bad
- 1:28:56ticket shouldn't block the counter
- 1:28:58forever. But notice what that safety net
- 1:29:01implies. Say a worker sends the email
- 1:29:05and then crashes a split second before
- 1:29:08it can say done. The broker never heard
- 1:29:11the acknowledgement, so it hands the
- 1:29:13ticket to another worker who sends the
- 1:29:16email again. The customer gets two
- 1:29:19welcome emails. Engineers call this at
- 1:29:22least once delivery. Every job will be
- 1:29:26done, but some may be done twice. For a
- 1:29:29welcome email, that's a small
- 1:29:32embarrassment. For a payment, it would
- 1:29:34be a serious problem. Doing things twice
- 1:29:38safely is such an important puzzle that
- 1:29:41it gets its own chapter later tonight.
- 1:29:44So by the late 2000s, back ends had a
- 1:29:47sturdy way to hand off work. Producers
- 1:29:51drop tickets
- 1:29:52brokers keep them safe, workers take
- 1:29:55them out, do the job, and say done. And
- 1:29:59then the ticket is gone. That last part,
- 1:30:03the ticket being gone, turned out to
- 1:30:05matter. At LinkedIn, around 2010,
- 1:30:09engineers were drowning in a different
- 1:30:12kind of problem. They didn't just need
- 1:30:14to hand jobs to workers. They had dozens
- 1:30:18of systems that all wanted to know about
- 1:30:21the same events. Every profile view,
- 1:30:24every click, every new connection. And
- 1:30:27they wanted to be able to go back and
- 1:30:30read those events again, hours or days
- 1:30:33later. A queue that forgets each ticket
- 1:30:37once it's served couldn't do that. They
- 1:30:40needed something more like a book where
- 1:30:42every event is written down in order and
- 1:30:45stays written down for anyone to read.
- 1:30:51Chapter eight, the log that remembers
- 1:30:54everything. It's around 2010 in Mountain
- 1:30:58View, California, and a software
- 1:31:00engineer at LinkedIn named Jay Kreps is
- 1:31:04looking at a diagram that has gotten out
- 1:31:07of hand. LinkedIn, the social network
- 1:31:10for working life, had started with one
- 1:31:13main database, like most young websites.
- 1:31:17Everything lived in it. But as the site
- 1:31:20grew, the team added more and more
- 1:31:23specialized systems around it. A search
- 1:31:26engine so you could look people up. A
- 1:31:29system for suggesting people you might
- 1:31:32know. A store for the activity feed. A
- 1:31:36big warehouse where analysts studied how
- 1:31:39the site was used. Monitoring systems
- 1:31:42that watched the servers. Each of these
- 1:31:45needed data from the others. The search
- 1:31:48engine needed to know when someone
- 1:31:50changed their job title. The suggestion
- 1:31:53system needed to know who viewed whose
- 1:31:56profile. The warehouse wanted to know
- 1:31:59about everything. So, engineers had
- 1:32:02built pipelines between them. A custom
- 1:32:05pipe from the main database to search,
- 1:32:08another from the activity store to the
- 1:32:11warehouse, another from the warehouse
- 1:32:14back to the suggestion system. Each pipe
- 1:32:17was written separately. Each one
- 1:32:20formatted the data its own way, and each
- 1:32:23one could break in its own particular
- 1:32:25way. When you draw that on a whiteboard,
- 1:32:28it looks like a plate of spaghetti.
- 1:32:30Let's count the pipes. Say there are 10
- 1:32:33systems, and in the worst case, every
- 1:32:36one of them needs to send data to every
- 1:32:39other. Each system sends to nine others.
- 1:32:4210 * 9 is 91 way pipes. Add an 11th
- 1:32:47system and you need 20 more. Krebs,
- 1:32:50working with two colleagues, Neha
- 1:32:53Narkhede and Jun Rao, came at the
- 1:32:55problem from a different angle. Instead
- 1:32:58of connecting every system to every
- 1:33:01other, they would connect every system
- 1:33:03to one shared thing, a single central
- 1:33:07record of everything that happened. Each
- 1:33:10system would write its events into that
- 1:33:12record once. Each system that wanted to
- 1:33:15know about events would read from it. 10
- 1:33:18systems, each with one connection for
- 1:33:21writing and one for reading. 20
- 1:33:24connections instead of 90. The central
- 1:33:27record would be a log. That word has a
- 1:33:30particular meaning for engineers, and
- 1:33:33Krebs later wrote a long, much-shared
- 1:33:36essay about it in 2013.
- 1:33:39A log, in this sense, is a sequence of
- 1:33:43records that can only be added to at the
- 1:33:46end. Nothing in the middle is ever
- 1:33:48changed. Each new record goes after the
- 1:33:51last one, and each gets the next number
- 1:33:54in line. It's one of the oldest kinds of
- 1:33:57record there is. Consider the log book
- 1:34:00on a ship. Every few hours, the officer
- 1:34:03of the watch writes an entry, the time,
- 1:34:06the position, the speed, the weather,
- 1:34:10anything worth noting. Entries go in
- 1:34:12order, one after another down the page.
- 1:34:16Nobody tears out an old page or rewrites
- 1:34:19yesterday's weather. If a mistake was
- 1:34:22made, a new entry corrects it. The log
- 1:34:25book is a faithful history of the voyage
- 1:34:28in the order things happened, and many
- 1:34:31people can read it. The captain checks
- 1:34:33the last few entries each morning. The
- 1:34:36navigator reads back through a week of
- 1:34:39positions to plot the course. Back on
- 1:34:42shore, months later, someone might read
- 1:34:45the whole thing from the first page to
- 1:34:47the last. Each reader keeps their own
- 1:34:50place. The captain's bookmark is near
- 1:34:53the end. The shore clerk's is somewhere
- 1:34:55near the start. Reading doesn't use up
- 1:34:58an entry, and one reader's bookmark has
- 1:35:01nothing to do with another's. That's the
- 1:35:04heart of what the LinkedIn team built.
- 1:35:07They called it Kafka. The name came from
- 1:35:10Krebs. As he later explained, the system
- 1:35:13was optimized for writing, so it seemed
- 1:35:16fitting to name it after a writer, and
- 1:35:19he liked the work of Franz Kafka.
- 1:35:22LinkedIn released Kafka as open source
- 1:35:25in early 2011. It moved to the Apache
- 1:35:29Software Foundation, a home for shared
- 1:35:32open projects, and became a full Apache
- 1:35:35project in 2012. By the time of Krebs's
- 1:35:39essay in 2013,
- 1:35:41LinkedIn was writing more than 60
- 1:35:44billion messages a day into its Kafka
- 1:35:47logs. Part of what made Kafka so fast
- 1:35:50was that very simplicity. Adding to the
- 1:35:53end of a file and reading straight
- 1:35:56through it from front to back are the
- 1:35:58things disks, and operating systems do
- 1:36:01best. There's no searching and no
- 1:36:04rearranging, just a steady stream like
- 1:36:07reeling out a ribbon. Let's look at how
- 1:36:10it works with numbers. In Kafka, a log
- 1:36:15is called a topic. Say LinkedIn has a
- 1:36:18topic called profile views. Every time
- 1:36:22anyone looks at anyone's profile, a
- 1:36:24small record is added to the end of it.
- 1:36:27Who looked, whose profile, and when?
- 1:36:31Each record gets a number called its
- 1:36:34offset. The very first record ever
- 1:36:36written is offset zero. The next is
- 1:36:40offset one. After a busy few days, the
- 1:36:43newest record might be at offset 1
- 1:36:46million and 200. Now three different
- 1:36:49systems are reading this topic. The
- 1:36:51first is the notification system, which
- 1:36:54tells people someone viewed your
- 1:36:57profile. It wants to be quick, so it
- 1:37:00reads right at the end. Its bookmark is
- 1:37:03at offset 1 million and 200. When a new
- 1:37:07view arrives, it reads it within a
- 1:37:10moment. The second is the analytics
- 1:37:12warehouse. It doesn't need to be quick.
- 1:37:16Once an hour it wakes up and reads
- 1:37:18everything since its last visit. Right
- 1:37:20now its bookmark is back at offset
- 1:37:23980,000.
- 1:37:26It has about 20,000 records to catch up
- 1:37:28on, and it will get through them in a
- 1:37:30few seconds when it wakes. And the third
- 1:37:34is brand new. A team has just built a
- 1:37:37system to spot fake accounts, which tend
- 1:37:40to view large numbers of profiles very
- 1:37:43quickly. They want to train it on
- 1:37:46history, so it starts its bookmark at
- 1:37:49the oldest record still kept and reads
- 1:37:52forward through days of views at its own
- 1:37:55pace until it catches up with the
- 1:37:58present. Three readers, three bookmarks,
- 1:38:01one log. None of them gets in the others
- 1:38:04way, and the system writing the views
- 1:38:07doesn't even know they exist. Compare
- 1:38:10this with the queue from the last
- 1:38:12chapter. In a queue, once a worker has
- 1:38:15taken a ticket and said done, the ticket
- 1:38:18is gone. That's right for jobs that
- 1:38:21should happen once. A log keeps every
- 1:38:24record for a set period, often a week,
- 1:38:28whether anyone has read it or not. Any
- 1:38:31number of readers can come along and
- 1:38:33read the same records, and a new reader
- 1:38:36can go back and replay the past. Of
- 1:38:39course, one log on one machine couldn't
- 1:38:42possibly hold 60 billion records a day,
- 1:38:45so Kafka splits each topic into pieces
- 1:38:49called partitions and spreads them
- 1:38:52across many machines. Say profile views
- 1:38:56are split into six partitions. When a
- 1:38:58new view comes in, Kafka needs to decide
- 1:39:02which partition it goes into. A common
- 1:39:05rule is to choose by the person being
- 1:39:08viewed. All views of the same person
- 1:39:11always land in the same partition, so
- 1:39:15they stay in order relative to one
- 1:39:17another. Different people's views are
- 1:39:20spread across the six, so six machines
- 1:39:23can share the work, and six readers can
- 1:39:26read in parallel, and each partition is
- 1:39:29copied to several machines, often three.
- 1:39:33If one machine fails, another already
- 1:39:36holds an identical copy and carries on.
- 1:39:40The log idea has one more consequence,
- 1:39:44and it's quietly beautiful. If you keep
- 1:39:47every event in order, then you can
- 1:39:50rebuild the current state of anything
- 1:39:53from scratch just by reading the events
- 1:39:55again. Say a simple bank account begins
- 1:39:59at zero. The log for that account has
- 1:40:02three entries, deposit $100,
- 1:40:06withdraw 30, deposit 50. Read them in
- 1:40:10order. 0 + 1 00 is 100 - 30 is 70 + 50
- 1:40:18is 120.
- 1:40:19The balance is $120,
- 1:40:23but the log holds more than the balance.
- 1:40:26It holds the whole story of how the
- 1:40:28balance got there. If a bug in the
- 1:40:31system once calculated a balance
- 1:40:33wrongly, you can fix the bug, replay the
- 1:40:37log, and arrive at the right answer. If
- 1:40:40a new team wants to know how often
- 1:40:42people withdraw money on Fridays, the
- 1:40:45history is all there, waiting. Engineers
- 1:40:49call this way of building systems event
- 1:40:52sourcing. The events are the truth.
- 1:40:55Everything else, every table and screen
- 1:40:57and summary is just a view built by
- 1:41:00reading them. If you heard our lecture
- 1:41:03on database engineering, you may
- 1:41:05remember that databases had used this
- 1:41:08trick inside themselves for years,
- 1:41:11writing every change to a log before
- 1:41:14touching anything else so they could
- 1:41:16recover from a crash. Kafka took that
- 1:41:19private diary out of the database and
- 1:41:22made it a shared log book for the whole
- 1:41:24company. In 2014, Krebs, Narkhede, and
- 1:41:29Rao left LinkedIn to start a company
- 1:41:32built around Kafka, and the project went
- 1:41:35on to be used at a great many large
- 1:41:37organizations, carrying everything from
- 1:41:41ride requests to bank payments to the
- 1:41:44readings of sensors on factory floors.
- 1:41:47The deeper change was in how engineers
- 1:41:49drew their diagrams. Instead of a plate
- 1:41:52of spaghetti, there was a spine, one
- 1:41:56shared log running down the middle and
- 1:41:59every system hanging off it, writing
- 1:42:01what happened and reading what it
- 1:42:04needed. A system could be added or
- 1:42:06removed without rewiring anything else,
- 1:42:10which raised an obvious next question.
- 1:42:13If systems could be that independent of
- 1:42:16each other, connected only by logs and
- 1:42:19interfaces, then someone had to decide
- 1:42:22how big each one should be, one large
- 1:42:25program or many small ones. At Amazon,
- 1:42:30nearly a decade before Kafka, that
- 1:42:32question had been answered by decree in
- 1:42:35a memo that is passed into legend.
- 1:42:40Chapter nine, one big program or many
- 1:42:44small ones. It's sometime around 2002
- 1:42:49in Seattle, and according to a story
- 1:42:51that engineers still retell, Jeff Bezos
- 1:42:55has sent an instruction to everyone
- 1:42:57building software at Amazon. We should
- 1:43:00be careful with this one because nobody
- 1:43:03outside Amazon has ever published the
- 1:43:05original. Almost everything we know
- 1:43:08about it comes from one source, written
- 1:43:11nearly a decade later. In October 2011,
- 1:43:15an engineer named Steve Yegge, who had
- 1:43:19spent about six and a half years at
- 1:43:21Amazon before moving to Google, wrote a
- 1:43:24very long post about the differences
- 1:43:27between the two companies. He meant it
- 1:43:30to be read only by his colleagues inside
- 1:43:33Google. By accident, he shared it with
- 1:43:36the whole world. He took it down
- 1:43:38quickly, but by then it had been copied
- 1:43:42everywhere. In that post, Yegge
- 1:43:45described the mandate as he remembered
- 1:43:47it, placing it around 2002,
- 1:43:50give or take a year. In his telling, it
- 1:43:53went roughly like this. From now on,
- 1:43:57every team will expose its data and its
- 1:44:00functions only through service
- 1:44:03interfaces. Teams will talk to one
- 1:44:06another only through those interfaces,
- 1:44:09no reaching directly into another team's
- 1:44:12database. No shared memory, no
- 1:44:15backdoors, no exceptions. It doesn't
- 1:44:18matter what technology each team uses.
- 1:44:22Every interface must be designed from
- 1:44:25the start as if outside developers might
- 1:44:28one day use it. And anyone who doesn't
- 1:44:31do this will be fired. Yegge added a
- 1:44:34final line, which he presented as a
- 1:44:37joke. Thank you and have a nice day.
- 1:44:40Whether every word is accurate, we can't
- 1:44:43say, but there's little doubt about the
- 1:44:45direction Amazon took. Over the
- 1:44:48following years, its giant online store
- 1:44:51was rebuilt from one large program into
- 1:44:55many separate services, each owned by a
- 1:44:58small team, each talking to the others
- 1:45:01only through its published interface. To
- 1:45:04see why that was such a big decision,
- 1:45:07let's first look at what Amazon was
- 1:45:09moving away from. The simplest way to
- 1:45:12build a backend is as one program. All
- 1:45:16the code for the shop, the product
- 1:45:18pages, the cart, the checkout, the stock
- 1:45:22counts, the recommendations
- 1:45:24lives together in one codebase, is built
- 1:45:28together and runs together on each
- 1:45:31server. Engineers call this a monolith,
- 1:45:35from the Greek for a single stone. A
- 1:45:38monolith has real virtues, and it's
- 1:45:41worth being fair to them. It's simple to
- 1:45:44understand. There's one program to
- 1:45:47build, one to test, one to deploy. When
- 1:45:51the checkout needs a stock count, it
- 1:45:54just calls a function right there in the
- 1:45:56same program. That call takes a few
- 1:46:00billionths of a second, and it never
- 1:46:02gets lost on a network. And when an
- 1:46:05order and its stock change need to be
- 1:46:08saved together, both can go into the
- 1:46:11same database in a single transaction.
- 1:46:15Either both happen or neither does. Most
- 1:46:18backends start as monoliths and many
- 1:46:22very successful ones stay that way for
- 1:46:25years. The Rails and Django applications
- 1:46:28from earlier tonight were almost always
- 1:46:32monoliths. The trouble comes with size
- 1:46:35and especially with the number of
- 1:46:37people. Picture a single large kitchen
- 1:46:41with one long counter where every cook
- 1:46:44works on every dish. With five cooks,
- 1:46:47it's friendly and efficient. With 50,
- 1:46:51they're reaching across each other for
- 1:46:53the same pans. Nobody can rearrange
- 1:46:56their own station without disturbing
- 1:46:58three neighbors. And if one cook drops a
- 1:47:02pan of oil on the floor, the whole
- 1:47:04kitchen stops. A monolith with hundreds
- 1:47:08of engineers works the same way.
- 1:47:11Everyone's changes go into the same
- 1:47:13program, so everyone's changes have to
- 1:47:16be tested together and released
- 1:47:19together. A mistake in the
- 1:47:21recommendations code can crash the
- 1:47:23checkout. And if one part needs far more
- 1:47:27computing power than the rest, you still
- 1:47:29have to run more copies of the whole
- 1:47:32thing. The alternative is to split the
- 1:47:35kitchen up. Instead of one long counter,
- 1:47:38there's a food hall, separate stalls,
- 1:47:41each with its own small team, its own
- 1:47:44menu, and its own equipment. The noodle
- 1:47:47stall can change its recipes, buy a new
- 1:47:50stove, or close for an afternoon without
- 1:47:53asking the dumpling stall for
- 1:47:55permission. If one stall has a problem,
- 1:47:59the others keep serving. In software,
- 1:48:02each stall is a service. A service owns
- 1:48:05one area of the business and its own
- 1:48:08data. The cart service owns carts. The
- 1:48:12stock service owns stock counts, and no
- 1:48:15one else is allowed to read its database
- 1:48:18directly. Other services ask it through
- 1:48:21its interface. That's exactly what the
- 1:48:24mandate demanded. Splitting things up
- 1:48:27also lets each part grow on its own. Say
- 1:48:31a big holiday sale is coming. The
- 1:48:34checkout is about to get 10 times
- 1:48:36busier, but hardly anyone will be
- 1:48:39writing reviews. In a monolith, you'd
- 1:48:42have to run 40 copies of the whole
- 1:48:44program to give the checkout 40 copies
- 1:48:48worth of power. With services, you run
- 1:48:5140 copies of the checkout service and
- 1:48:54leave the reviews service at four. It's
- 1:48:57the food hall putting extra cooks on the
- 1:48:59busiest stall and leaving the quiet ones
- 1:49:03alone. In March 2014,
- 1:49:06two consultants, James Lewis and Martin
- 1:49:09Fowler, published a widely read article
- 1:49:13that gave this style the name that
- 1:49:15stuck. They called it microservices.
- 1:49:19Their description was of a single
- 1:49:21application built as a suite of small
- 1:49:24services, each running on its own and
- 1:49:27each deployable independently. They also
- 1:49:30pointed back to an observation made in
- 1:49:331968
- 1:49:35by a programmer named Melvin Conway.
- 1:49:38Organizations, he noticed, tend to
- 1:49:41design systems that mirror their own
- 1:49:43communication structure. In other words,
- 1:49:47if four teams build a program together,
- 1:49:50it tends to come out in four parts.
- 1:49:53Microservices turned that around and
- 1:49:56used it on purpose. Decide how you want
- 1:49:59your teams to work, small and
- 1:50:02independent, and shape the software to
- 1:50:04match. Amazon has often been described
- 1:50:08as organizing around small teams,
- 1:50:11sometimes said to be no bigger than two
- 1:50:13pizzas could feed. Each one could own a
- 1:50:16service, change it, and release it
- 1:50:20whenever it was ready, without waiting
- 1:50:22for anyone else. But splitting things up
- 1:50:25isn't free, and here's where we should
- 1:50:28put some numbers down. Say a product
- 1:50:31page needs information from five
- 1:50:34services. Product details, price, stock,
- 1:50:38reviews, and recommendations.
- 1:50:41In a monolith, those would be five
- 1:50:44function calls, taking almost no time at
- 1:50:47all. With microservices,
- 1:50:50they're five trips across the network.
- 1:50:52Say each trip takes 20,000ths of a
- 1:50:55second. If the page asks the five
- 1:50:58services one after another, that's
- 1:51:01100,000ths of a second, a tenth of a
- 1:51:04second just in waiting. Reliability gets
- 1:51:08more subtle, too. Say each service is
- 1:51:11working correctly 99.9%
- 1:51:15of the time. That sounds excellent, but
- 1:51:18if the page needs all five, it only
- 1:51:22works when all five work at once. You
- 1:51:25find that by multiplying 0.999
- 1:51:30multiplied by itself five times comes to
- 1:51:34about 0.995,
- 1:51:3799 and 1/2 percent. So, instead of
- 1:51:41failing about one time in a thousand,
- 1:51:44the page now fails about one time in
- 1:51:47200, five times as often, just from
- 1:51:50adding up small chances. The food hall
- 1:51:54has the same problem. If your set meal
- 1:51:57needs a dish from each of five stalls,
- 1:52:00then any one stall running late holds up
- 1:52:03your whole tray. Engineers have built up
- 1:52:06a toolkit of habits for this. Ask the
- 1:52:09five services at the same time, rather
- 1:52:12than one after another. So the page
- 1:52:14waits for the slowest single answer
- 1:52:17instead of the sum. Set a firm time
- 1:52:20limit on every call, so one slow stall
- 1:52:24can't hold up the tray forever. And
- 1:52:26decide in advance which parts are
- 1:52:29essential. If the recommendation service
- 1:52:32doesn't answer in time, show the page
- 1:52:35without recommendations.
- 1:52:37Second customers will never notice. If
- 1:52:40the price service doesn't answer though,
- 1:52:43the page really can't be shown. There
- 1:52:46are other costs. Data that used to sit
- 1:52:50in one database is now scattered, so
- 1:52:53keeping an order and its stock count in
- 1:52:55step needs more care. A single
- 1:52:58customer's request might pass through
- 1:53:01dozens of services, so finding out what
- 1:53:04went wrong becomes detective work, which
- 1:53:07is a subject for later tonight. And
- 1:53:10someone has to run all those services,
- 1:53:13each with its own servers and its own
- 1:53:16releases. So the honest answer to the
- 1:53:19chapter's question is it depends, and
- 1:53:22mostly it depends on people. A small
- 1:53:26team with a young product usually does
- 1:53:29best with a well-organized monolith.
- 1:53:32Many experienced engineers advise
- 1:53:35starting there and splitting off
- 1:53:37services only when a part of the system
- 1:53:40clearly needs to grow or change on its
- 1:53:43own. Some large and successful companies
- 1:53:47have deliberately kept big, carefully
- 1:53:50organized monoliths for years, and DHH
- 1:53:54himself has written in their defense. A
- 1:53:57large organization with hundreds of
- 1:54:00teams that need to move independently
- 1:54:03often finds that services are the only
- 1:54:06way to stop everyone tripping over each
- 1:54:09other. Neither is a mistake. They're
- 1:54:11different shapes of kitchen suited to
- 1:54:14different numbers of cooks. The mandate
- 1:54:17had one more consequence that Jager's
- 1:54:19post pointed out, and it may have been
- 1:54:22the most important of all. One of its
- 1:54:25rules said every interface must be built
- 1:54:29as if outsiders might use it. And as
- 1:54:32Jäger and many others have told the
- 1:54:34story, once Amazon had turned its own
- 1:54:38infrastructure into services, its
- 1:54:40storage, its computing, its queues, each
- 1:54:44behind a clean interface, a thought
- 1:54:47followed naturally. If its own teams
- 1:54:50could rent computing through an
- 1:54:52interface, so could anyone. That thought
- 1:54:56became Amazon Web Services. And in 2014,
- 1:55:01it led to an offer that would have
- 1:55:02sounded strange only a few years
- 1:55:05earlier. Don't rent a server at all,
- 1:55:08just hand over a function and let
- 1:55:11someone else worry about where it runs.
- 1:55:16Chapter 10, Servers You Never See. It's
- 1:55:20Thursday, the 13th of November, 2014,
- 1:55:25in Las Vegas, and a large hall is full
- 1:55:29of people who build software for a
- 1:55:31living. They've come to reinvent Amazon
- 1:55:35Web Services yearly conference. On stage
- 1:55:38is Werner Vogels, Amazon's Chief
- 1:55:41Technology Officer, a Dutchman who had
- 1:55:45been a computer science researcher at
- 1:55:47Cornell University before he came to
- 1:55:50Amazon. Over the course of his keynote,
- 1:55:53he introduces a new service with a
- 1:55:55short, slightly mysterious name, AWS
- 1:56:00Lambda. The idea was easy to state and
- 1:56:03strange to hear. You wouldn't rent a
- 1:56:06server anymore. You wouldn't choose a
- 1:56:08machine or install an operating system
- 1:56:12or decide how many copies to run. You
- 1:56:15would simply hand Amazon a function, a
- 1:56:18small piece of code that does one job
- 1:56:21and tell it when that function should
- 1:56:23run. When a photo is uploaded, run this.
- 1:56:27When a new row appears in this table,
- 1:56:30run that. Amazon would find a machine,
- 1:56:33run your code, and charge you for the
- 1:56:36time it actually spent running, measured
- 1:56:39in slices of a tenth of a second. When
- 1:56:42nothing was happening, you'd pay nothing
- 1:56:44at all. At launch, Lambda was a preview
- 1:56:48with some firm limits. Functions could
- 1:56:51only be written in one language,
- 1:56:54JavaScript running on Node, and each one
- 1:56:57could run for at most a minute. It
- 1:57:00opened to everyone in April 2015,
- 1:57:04and over the following years, it grew
- 1:57:07more languages and much longer time
- 1:57:10limits. It wasn't the very first service
- 1:57:13of its kind. Google, for instance, had
- 1:57:16offered a platform for running web
- 1:57:19applications without managing servers
- 1:57:22since 2008.
- 1:57:24But Lambda's pure form, a single
- 1:57:27function woken by an event, billed by
- 1:57:31the slice, caught the imagination of the
- 1:57:34industry. People started calling the
- 1:57:36approach serverless. The name is a
- 1:57:40little cheeky. There are servers, of
- 1:57:42course, racks and racks of them humming
- 1:57:45in Amazon's data centers. They're just
- 1:57:48not yours to think about. To feel the
- 1:57:51difference, let's compare three ways to
- 1:57:54get somewhere. You can own a car. It's
- 1:57:57always in the driveway, ready to go at a
- 1:58:00moment's notice. But you pay for it
- 1:58:03whether you drive it or not. Insurance,
- 1:58:06parking, the loan all month long. That's
- 1:58:10like owning or renting a server. It's
- 1:58:13there all the time, and the bill arrives
- 1:58:16all the time. You can hire a car for a
- 1:58:19week. That's like renting cloud servers
- 1:58:22by the hour. More flexible, but still,
- 1:58:26if you hire it and leave it parked,
- 1:58:29you're paying for a parked car. Or you
- 1:58:31can take a taxi. You pay for each trip
- 1:58:35and only for the trip. When you're not
- 1:58:37going anywhere, it costs nothing. That's
- 1:58:41serverless. As with real taxis, the
- 1:58:45question of which is cheaper depends
- 1:58:47entirely on how much you travel. Let's
- 1:58:51put numbers on it. These are made-up
- 1:58:54round prices chosen to make the
- 1:58:57arithmetic easy. Real prices vary and
- 1:59:00change over time. Say a small server
- 1:59:04costs 10 cents an hour. There are 720
- 1:59:08hours in a 30-day month, so the server
- 1:59:11costs $72 a month, whether it's busy or
- 1:59:15idle. And say a serverless function for
- 1:59:19a typical small request works out to $2
- 1:59:23for every million requests. Now consider
- 1:59:26three different apps. The first is a
- 1:59:29little app for a local sports club. It
- 1:59:32gets about a million requests a month,
- 1:59:35mostly on match days. On the server,
- 1:59:38that's $72.
- 1:59:40As functions, it's a million requests at
- 1:59:44$2 per million. $2 $2 instead of 72. For
- 1:59:50a quiet app, serverless is a gift, and
- 1:59:53there's a hidden saving on top of that.
- 1:59:56Nobody at the club has to install
- 1:59:58updates on a server, renew it, or worry
- 2:00:02about it overnight. For a club run by
- 2:00:05volunteers, that might matter even more
- 2:00:08than the $70.
- 2:00:10The second app gets 36 million requests
- 2:00:14a month. 36 * 2 is $72,
- 2:00:19exactly the same as the server. That's
- 2:00:22the break-even point. The third app is
- 2:00:25busy with 100 million requests a month.
- 2:00:29As functions, that's $200
- 2:00:32and 100 million requests a month is only
- 2:00:35about 39 requests a second on average,
- 2:00:39which a single modest server could
- 2:00:41likely handle. On the server, it's still
- 2:00:44$72.
- 2:00:46So the taxi is a fine choice for
- 2:00:48occasional trips and expensive as a
- 2:00:51daily commute. Many companies end up
- 2:00:54with both functions for spiky,
- 2:00:57occasional or unpredictable work and
- 2:01:00ordinary servers for the steady, heavy
- 2:01:03traffic. The price isn't the only
- 2:01:05difference. There's also a matter of
- 2:01:08waking up. When a function hasn't run
- 2:01:10for a while, the service doesn't keep it
- 2:01:13loaded on a machine waiting. So when a
- 2:01:16request arrives, the service has to find
- 2:01:19a machine, set up a fresh, isolated
- 2:01:23space for your code, load the code, and
- 2:01:26start it running. Only then can it
- 2:01:29handle the request. That extra delay is
- 2:01:32called a cold start. Say a warm
- 2:01:35function, one that's already loaded
- 2:01:38because it ran a moment ago, answers in
- 2:01:4120,000ths of a second. A cold start
- 2:01:44might add most of a second on top,
- 2:01:47depending on the language and how much
- 2:01:49the code has to load. The first visitor
- 2:01:52of the morning waits nearly a second.
- 2:01:55The next visitor, a moment later, gets
- 2:01:58the warm function and waits a 50th of a
- 2:02:02second. It's like a small cafe where the
- 2:02:05barista goes home between customers.
- 2:02:09When you arrive, someone has to call her
- 2:02:11in. She has to put on her apron and warm
- 2:02:14up the machine, and then she makes your
- 2:02:17coffee. The next customer who arrives
- 2:02:21while she's still there gets served
- 2:02:24right away. After a quiet spell, she
- 2:02:27goes home again. For a background job,
- 2:02:30like making thumbnail copies of an
- 2:02:33uploaded photo, a cold start doesn'
- 2:02:35matter at all. Nobody is watching the
- 2:02:38clock. For a page a person is waiting
- 2:02:41on, it can matter a lot, and engineers
- 2:02:44have developed tricks to soften it,
- 2:02:47keeping code small so it loads quickly,
- 2:02:50paying a little extra to keep a few
- 2:02:52copies always warm, or simply choosing
- 2:02:55ordinary servers for the parts where
- 2:02:58every moment counts. There's a deeper
- 2:03:00lesson in serverless, and it's one
- 2:03:03you've heard before tonight. A function
- 2:03:06can't remember anything between one call
- 2:03:08and the next. The machine it ran on
- 2:03:11might be a different machine next time,
- 2:03:14or might not exist at all. So everything
- 2:03:17that matters has to be kept somewhere
- 2:03:20outside, in a database, a cache, or a
- 2:03:24queue. That's exactly the stateless
- 2:03:27habit from the chapter on the fail
- 2:03:29whale. Serverless simply takes it to its
- 2:03:32natural end. The cooks don't just forget
- 2:03:36the last customer. They go home between
- 2:03:39orders, and serverless fits beautifully
- 2:03:42with the queues and logs from the last
- 2:03:45few chapters. A message lands in a
- 2:03:48queue, and a function wakes up to handle
- 2:03:51it. A new event is written to a log, and
- 2:03:54a function wakes up to react. A file
- 2:03:58appears in storage, and a function wakes
- 2:04:01up to process it. Events in, small jobs
- 2:04:05out, no machine to look after. It also
- 2:04:08scales in both directions without anyone
- 2:04:11deciding anything. If one photo arrives,
- 2:04:14one copy of the function runs. If 10,000
- 2:04:18arrive in the same minute, the platform
- 2:04:21runs many copies side by side, up to a
- 2:04:24limit set on the account, which, as of
- 2:04:272026,
- 2:04:29is usually 1,000 at once by default, and
- 2:04:33lower for some new accounts. When the
- 2:04:35rush is over, the copies simply vanish.
- 2:04:39Nobody had to add servers in the morning
- 2:04:42or remove them at night. The kitchen
- 2:04:44grows and shrinks with the orders.
- 2:04:47Behind the scenes, keeping strangers
- 2:04:50code apart on shared machines is serious
- 2:04:53work. Each function runs inside its own
- 2:04:56sealed compartment, so one customer's
- 2:04:59code can never peek at another's. In
- 2:05:022018, Amazon released the technology it
- 2:05:06had built for this, a way of starting
- 2:05:09tiny, stripped-down virtual machines in
- 2:05:12a fraction of a second so that every
- 2:05:15function could have walls of its own
- 2:05:18without a long wait. Let's follow one
- 2:05:21through. You're on holiday and you
- 2:05:23upload a photo from your phone to a
- 2:05:26photo sharing app. The photo arrives in
- 2:05:29the app storage. That arrival is an
- 2:05:32event. It wakes a function which reads
- 2:05:35the full-size photo and makes three
- 2:05:38smaller copies, one for thumbnails, one
- 2:05:42for phones, and one for tablets. It
- 2:05:45saves them and goes back to sleep. The
- 2:05:48whole thing takes a couple of seconds.
- 2:05:51You never knew it happened. The app's
- 2:05:53owners paid for those couple of seconds
- 2:05:56and nothing else. Serverless has its
- 2:05:59critics and they make fair points. Every
- 2:06:03provider's version works a little
- 2:06:05differently, so code written for one can
- 2:06:08be hard to move to another. Running a
- 2:06:11function far from its database can be
- 2:06:14slow and opening a fresh database
- 2:06:17connection on every cold start can
- 2:06:20strain the database itself. And when a
- 2:06:23system is built from hundreds of small
- 2:06:25functions, each triggered by others, it
- 2:06:29can be hard to see the whole picture at
- 2:06:31once. But the core idea has lasted. Pay
- 2:06:35for what runs, not for what waits. Let
- 2:06:38someone else keep the machines alive.
- 2:06:42There's one thing all these systems have
- 2:06:44in common though, and it causes trouble
- 2:06:47no matter how they're built. When a
- 2:06:49function fails, the platform will often
- 2:06:52run it again automatically. When a queue
- 2:06:55isn't sure a job was done, it hands it
- 2:06:58out again. When a phone doesn't hear
- 2:07:01back from a server, it tries again.
- 2:07:04Retrying is how distributed systems
- 2:07:07survive. But retrying has a dark side.
- 2:07:11If the thing being retried was a
- 2:07:13payment, the customer might be charged
- 2:07:16twice. Fortunately, there's a
- 2:07:18mathematical idea named in 1870 by a
- 2:07:23professor at Harvard that turns out to
- 2:07:26be exactly the right tool. You've used
- 2:07:28it yourself many times in an elevator.
- 2:07:34Chapter 11, Charging Only Once. It's
- 2:07:381870
- 2:07:40in Washington, and a Harvard
- 2:07:42mathematician named Benjamin Peirce is
- 2:07:45presenting a long, demanding piece of
- 2:07:48work to the National Academy of
- 2:07:51Sciences. Peirce was one of the most
- 2:07:54respected American mathematicians of his
- 2:07:57century. He taught at Harvard for
- 2:07:59decades, worked on astronomy and on the
- 2:08:03survey of the American coast, and was
- 2:08:06known for lectures so dense that only
- 2:08:09his best students could follow them. His
- 2:08:12son, Charles Sanders Peirce, would
- 2:08:15become a famous philosopher and logician
- 2:08:18in his own right. The work he presented
- 2:08:21that year was called Linear Associative
- 2:08:24Algebra. It was a survey of strange new
- 2:08:27number systems, algebras in which the
- 2:08:30familiar rules of arithmetic didn't
- 2:08:33always hold. It was printed that same
- 2:08:36year in a small run of lithographed
- 2:08:38copies and later reprinted in a
- 2:08:41mathematics journal by his son. To
- 2:08:43describe the creatures he found in those
- 2:08:46algebras, Pierce needed new words. One
- 2:08:50of them was nilpotent for a quantity
- 2:08:53that becomes zero when you multiply it
- 2:08:56by itself enough times. Another
- 2:08:59described a quantity with a different
- 2:09:01and rather calm property. Multiply it by
- 2:09:04itself and you get exactly itself back.
- 2:09:08A times A equals A. He called that
- 2:09:12quality idempotent. It comes from the
- 2:09:15Latin roots for same and power. Raise it
- 2:09:19to any power and it stays the same. For
- 2:09:23a long time, that word lived quietly in
- 2:09:27algebra textbooks. Then computer
- 2:09:29scientists borrowed it and gave it a
- 2:09:32slightly broader meaning. An action is
- 2:09:35idempotent if doing it twice has exactly
- 2:09:39the same effect as doing it once. You
- 2:09:42know this from everyday life better than
- 2:09:45you might think. When you call an
- 2:09:47elevator, you press the button and it
- 2:09:50lights up. If you're in a hurry, you
- 2:09:52might press it again and again. Five
- 2:09:56presses, ten presses, only one elevator
- 2:10:00comes. Pressing the button doesn't mean
- 2:10:03send another elevator. It means make
- 2:10:06sure an elevator is coming. Once that's
- 2:10:09true, pressing it again changes nothing.
- 2:10:12Compare a light switch that toggles.
- 2:10:15Each press flips the light from on to
- 2:10:18off or off to on. Press it twice and
- 2:10:22you're back where you started. That's
- 2:10:24not idempotent at all. The number of
- 2:10:27presses matters. Or think about a
- 2:10:29thermostat. Set the temperature to 20
- 2:10:32degrees is idempotent. Say it once or
- 2:10:36say it five times, the room ends up at
- 2:10:3920. Raise the temperature by 1 degree is
- 2:10:43not. Say it five times and the room ends
- 2:10:47up five degrees warmer. That's why
- 2:10:50careful designers of interfaces,
- 2:10:53wherever they can, prefer requests that
- 2:10:56state the result they want over requests
- 2:10:59that describe a change. Set the quantity
- 2:11:03in the basket to two survives being
- 2:11:06repeated. Add one more to the basket
- 2:11:08does not. You may remember from the
- 2:11:11chapter on rest that get, put, and
- 2:11:14delete were designed to be idempotent.
- 2:11:17Fetching book 42 twice changes nothing.
- 2:11:21Replacing it with the same record twice
- 2:11:24leaves the same record. Deleting it
- 2:11:27twice leaves it deleted. But post, which
- 2:11:31means add something new, isn't. Post an
- 2:11:35order twice and you might have two
- 2:11:37orders. And that's where payments come
- 2:11:40in. Here's the situation that keeps
- 2:11:43payment engineers up at night. Your
- 2:11:46phone sends a request to charge your
- 2:11:48card $25.
- 2:11:51It waits for a reply. 10 seconds pass
- 2:11:55and nothing comes back. The phone gives
- 2:11:58up and shows an error. Consider what
- 2:12:01might actually have happened. There are
- 2:12:04three possibilities. Maybe the request
- 2:12:07never reached the server at all, lost
- 2:12:10somewhere on a weak connection. Maybe it
- 2:12:13reached the server which charged your
- 2:12:15card perfectly and then the reply was
- 2:12:18lost on its way back. Or maybe the
- 2:12:21server is still working on it, held up
- 2:12:24by a slow bank. From the phone's side,
- 2:12:27all three look exactly the same.
- 2:12:30Silence. If the phone doesn't retry and
- 2:12:34the first possibility was the true one,
- 2:12:37you never paid and your order never
- 2:12:39happens. If the phone does retry and the
- 2:12:43second possibility was true, you've just
- 2:12:46been charged twice, $50 instead of 25.
- 2:12:51There's no way for the phone to know
- 2:12:53which. This isn't a matter of better
- 2:12:56programming. It's built into the nature
- 2:12:59of sending messages across an unreliable
- 2:13:02network. When you don't hear back, you
- 2:13:05can't tell whether your message was lost
- 2:13:08or the answer was. So, instead of trying
- 2:13:11to know, engineers make the retry safe.
- 2:13:15They turn a non-idempotent action,
- 2:13:18charge this card, into an idempotent
- 2:13:21one. The tool for this is called an
- 2:13:24idempotency key, and the payments
- 2:13:26company Stripe, founded in 2010
- 2:13:30by two brothers from Ireland, Patrick
- 2:13:33and John Collison, helped make it
- 2:13:35familiar to a whole generation of
- 2:13:38developers. Here's how it works. Before
- 2:13:41the phone sends the payment for the
- 2:13:43first time, it creates a long, random
- 2:13:47string of letters and numbers, so long
- 2:13:50that no two phones will ever create the
- 2:13:52same one by chance. That string is the
- 2:13:56idempotency key. It means in effect this
- 2:14:00particular attempt to pay for this
- 2:14:02particular order. The phone sends the
- 2:14:05key along with the request. If it ever
- 2:14:08needs to retry, it sends the same key
- 2:14:12again. On the server's side, the first
- 2:14:15time a key arrives, the server does the
- 2:14:18work and writes down the key along with
- 2:14:21the result. When a request arrives with
- 2:14:24a key it has seen before, the server
- 2:14:27doesn't do the work again. It simply
- 2:14:30looks up the saved result and sends that
- 2:14:33back. Let's walk through it with times.
- 2:14:36At 9:00 and no seconds, your phone sends
- 2:14:40the payment request. $25
- 2:14:44with a key we'll call key K. 1 second
- 2:14:48later, the server receives it. Key K is
- 2:14:52new. It charges your card $25
- 2:14:56and writes down key K, payment number
- 2:14:59881
- 2:15:01succeeded. At two seconds past nine, the
- 2:15:05server sends its reply. Somewhere in a
- 2:15:08tunnel between two phone masts, the
- 2:15:11reply is lost. By 10 seconds past nine,
- 2:15:15your phone has given up waiting. It
- 2:15:18waits a second, then tries again with
- 2:15:21the same key K. At 11 seconds past nine,
- 2:15:26the server receives the retry. It looks
- 2:15:29up key K and finds it. This payment has
- 2:15:33already been made, so it doesn't charge
- 2:15:36your card again. It sends back the saved
- 2:15:39answer. Payment 881
- 2:15:43succeeded. Your phone shows a green
- 2:15:46tick. Your card has been charged $25
- 2:15:51exactly once. Stripe's own documentation
- 2:15:55describes the details. The key travels
- 2:15:58in a header on the request called
- 2:16:01idempotency key. The server saves the
- 2:16:04result of the first request, even if it
- 2:16:07was an error, and returns that same
- 2:16:10result to any retry with the same key.
- 2:16:14Stripe also checks that a retry asks for
- 2:16:17the same thing as the original and
- 2:16:20refuses it if someone tries to reuse a
- 2:16:23key for a different payment. And as of
- 2:16:262026,
- 2:16:28it keeps keys for at least a day, long
- 2:16:31enough to cover any sensible run of
- 2:16:33retries. On the server's side, there's a
- 2:16:36detail that matters more than it looks.
- 2:16:39The note that says key K was used and
- 2:16:43the record of the payment itself should
- 2:16:46be saved together in one step so that
- 2:16:49one can never exist without the other.
- 2:16:52If the server charged the card and then
- 2:16:55crashed before writing down the key, the
- 2:16:58retry would find no key and charge
- 2:17:01again.
- 2:17:03both at once, the way the database
- 2:17:05lecture described transactions, closes
- 2:17:09that gap. There's one more subtle case.
- 2:17:12Two copies of the same request might
- 2:17:15arrive at almost exactly the same moment
- 2:17:19before either has finished. A careful
- 2:17:21server notices that the key is already
- 2:17:24being worked on and tells the second
- 2:17:27copy to wait and try again rather than
- 2:17:30doing the work twice in parallel. The
- 2:17:33same idea rescues the welcome emails
- 2:17:36from our queue chapter. Recall the
- 2:17:39worker that sent an email and then
- 2:17:41crashed before it could say done, so the
- 2:17:44email went out twice. If each ticket
- 2:17:47carries a key and the worker writes down
- 2:17:51which keys it has finished, then a
- 2:17:53ticket that comes back a second time can
- 2:17:56be recognized and set aside. At least
- 2:17:59once delivery, combined with idempotent
- 2:18:02handling, adds up to an effect that
- 2:18:05happens just once. It's one of the
- 2:18:07quietest and most useful tricks in the
- 2:18:10whole craft. Retrying well has one more
- 2:18:14ingredient and it's about patience. If a
- 2:18:18server is struggling, the worst thing
- 2:18:21every client can do is retry instantly,
- 2:18:25all together. Thousands of phones, all
- 2:18:29failing at the same moment and all
- 2:18:31retrying at the same moment, hit the
- 2:18:34struggling server with a second wave
- 2:18:37just when it can least cope. So
- 2:18:39well-behaved clients back off. After the
- 2:18:42first failure, wait 1 second. After the
- 2:18:46second, 2 seconds. Then 4, then 8. Each
- 2:18:50wait is double the last. Four retries
- 2:18:54spread across 15 seconds of waiting,
- 2:18:57which gives a troubled server room to
- 2:18:59recover. And they add a little
- 2:19:01randomness to each wait, a touch of
- 2:19:04jitter, so that thousands of phones
- 2:19:07don't all come back at precisely the
- 2:19:09same instant. Instead of a crowd
- 2:19:12charging the door together, they drift
- 2:19:14back in ones and twos. So the elevator
- 2:19:18button teaches the whole lesson. Make
- 2:19:21your actions safe to repeat, then repeat
- 2:19:24them gently until you're sure. But even
- 2:19:28gentle retries can add up. One phone
- 2:19:31politely backing off is fine. A million
- 2:19:34phones doing it at once, along with all
- 2:19:37the ordinary traffic, can still be more
- 2:19:41than a backend can handle. And not every
- 2:19:43client is polite. Some programs, through
- 2:19:47bugs or bad intentions, send requests as
- 2:19:51fast as their machines allow. A backend
- 2:19:54needs a way to say kindly, but firmly,
- 2:19:58that's enough for now. And one of the
- 2:20:01neatest ways to do that involves an
- 2:20:04imaginary bucket slowly filling with
- 2:20:07tokens.
- 2:20:10Chapter 12, not too many at once. It's
- 2:20:141986
- 2:20:16and an engineer named Jonathan Turner, a
- 2:20:19professor at Washington University in
- 2:20:21St. Louis, is thinking about the
- 2:20:23networks of the future. He's been
- 2:20:26working on designs for fast packet
- 2:20:28networks, the kind that might one day
- 2:20:31carry voice, video, and data all
- 2:20:35together over the same lines. And he can
- 2:20:38see a problem coming. In a shared
- 2:20:40network, one greedy user sending far
- 2:20:44more than their fair share could swamp
- 2:20:47everyone else. The network needs a way
- 2:20:50to hold each user to a promise, an
- 2:20:53agreed average rate, while still
- 2:20:55allowing a little burst now and then.
- 2:20:58That year, in an article in a magazine
- 2:21:01for communications engineers, Turner
- 2:21:04described a simple mechanism for doing
- 2:21:07exactly that. He called it a leaky
- 2:21:10bucket. Picture an ordinary bucket with
- 2:21:13a small hole in the bottom. Water drips
- 2:21:16out of the hole at a steady rate, no
- 2:21:19matter what. You can pour water in as
- 2:21:21fast as you like, in splashes or in a
- 2:21:25trickle. If you pour slowly, the bucket
- 2:21:28never fills. If you pour a quick splash,
- 2:21:31the bucket holds it for a moment and
- 2:21:34lets it drip away. But if you keep
- 2:21:36pouring faster than the hole can drain,
- 2:21:39the bucket fills to the brim, and
- 2:21:42anything more spills over the side and
- 2:21:45is lost. In Turner's version, the water
- 2:21:49was packets. The size of the hole set
- 2:21:52the average rate a user was allowed. The
- 2:21:55size of the bucket set how big a burst
- 2:21:58could be absorbed.
- 2:22:00And the water that spilled over was
- 2:22:02traffic the network would refuse. In the
- 2:22:05machine itself, of course, there was no
- 2:22:08water at all. Turner's bucket was just a
- 2:22:11counter, nudged up by one with every
- 2:22:14packet that arrived and down by one at a
- 2:22:18steady tick, with a line drawn near the
- 2:22:21top. The idea spread through
- 2:22:23telecommunications.
- 2:22:25A version of it became part of the
- 2:22:28standards for a type of high-speed
- 2:22:30network in the 1990s.
- 2:22:33And a close cousin with the same
- 2:22:35behavior seen from the other side became
- 2:22:38the favorite tool of backend engineers.
- 2:22:42It's called the token bucket. The token
- 2:22:45bucket turns the picture upside down.
- 2:22:48Instead of water leaking out, tokens
- 2:22:51drip in. Say there's a bucket that can
- 2:22:53hold 10 tokens. A machine drops two new
- 2:22:57tokens into it every second until it's
- 2:23:00full. Whenever a request arrives, it
- 2:23:03must take one token from the bucket to
- 2:23:06be allowed through. If there's a token,
- 2:23:09the request goes ahead. If the bucket is
- 2:23:12empty, the request is turned away. It's
- 2:23:15like an arcade that hands out tokens at
- 2:23:18a slow, steady rate. You can save up a
- 2:23:21handful and spend them all at once on a
- 2:23:24favorite game. But once your handful is
- 2:23:27gone, you can only play as fast as new
- 2:23:30tokens arrive.
- 2:23:32Let's follow one client through it
- 2:23:34second by second. At the start, the
- 2:23:38bucket is full, 10 tokens. The client's
- 2:23:41app opens and it fires off a burst of 10
- 2:23:45requests at once to load everything on
- 2:23:48its first screen. Each takes a token.
- 2:23:52All 10 go through. The bucket is now
- 2:23:55empty. An 11th request arrives in the
- 2:23:58same instant. There's no token for it.
- 2:24:01It's refused. 1 second later, two new
- 2:24:05tokens have dripped in. The client sends
- 2:24:08two more requests and both go through.
- 2:24:12Empty again. If the client keeps
- 2:24:15hammering away as fast as it can, it
- 2:24:18gets exactly two requests through every
- 2:24:21second, no more. Over the first 10
- 2:24:24seconds, that's the 10 it saved up plus
- 2:24:27two for each of the 10 seconds that
- 2:24:30followed, 30 requests in all. Now say
- 2:24:33the client goes quiet for 5 seconds. Two
- 2:24:37tokens a second for 5 seconds is 10
- 2:24:41tokens. The bucket is full again and the
- 2:24:44client could send another burst of 10.
- 2:24:47So the bucket does two things at once.
- 2:24:50It allows short bursts up to the size of
- 2:24:54the bucket and over any longer stretch,
- 2:24:57it holds the client to the average rate,
- 2:25:00two a second. Two numbers, the bucket
- 2:25:04size and the drip rate, and you've
- 2:25:06described a whole policy. When a request
- 2:25:09is turned away, a well-designed back end
- 2:25:13says so clearly. The web has a status
- 2:25:16code just for this, 429,
- 2:25:20which means too many requests. It was
- 2:25:24added to the standards in 2012.
- 2:25:27Along with it, the back end can send a
- 2:25:30hint called retry after, saying how many
- 2:25:33seconds to wait. A polite client reads
- 2:25:36that hint and waits exactly as the
- 2:25:40backing off clients in the last chapter
- 2:25:42did. In practice, a back end usually
- 2:25:46keeps a separate bucket for each
- 2:25:48customer or each account or each key
- 2:25:51used to call its API. One heavy user
- 2:25:55empties only their own bucket. Everyone
- 2:25:58else's stays full. Many public APIs
- 2:26:01publish their limits openly so
- 2:26:04developers can plan around them. GitHub,
- 2:26:07for instance, has long allowed a
- 2:26:09signed-in user 5,000 requests an hour to
- 2:26:13its main interface and still did as of
- 2:26:172026.
- 2:26:18Every reply carries a small note saying
- 2:26:22how many requests remain in the current
- 2:26:24allowance and when it refills, rather
- 2:26:28like a fuel gauge on the dashboard. And
- 2:26:31the buckets have to be shared across all
- 2:26:33the servers. If there are 10 servers
- 2:26:36behind the load balancer, each keeping
- 2:26:39its own private buckets, a client could
- 2:26:43get 10 times its allowance by spreading
- 2:26:46requests across them. So the token
- 2:26:49counts often live in a fast shared
- 2:26:52store, such as the Redis we met earlier
- 2:26:55tonight, where every server can check
- 2:26:58and take tokens from the same bucket in
- 2:27:00a single quick step. Some simpler limits
- 2:27:04just count requests in fixed windows of
- 2:27:07time, 100 requests per minute, with the
- 2:27:10count starting fresh at the top of every
- 2:27:13minute. That's easy to build, but it has
- 2:27:16a funny weak spot. A client could send
- 2:27:19100 requests at 59 seconds past the
- 2:27:23minute and another 100 at the very start
- 2:27:26of the next minute. Each window sees
- 2:27:28only 100, but the server has just
- 2:27:31received 200 requests in about 2
- 2:27:34seconds. The token bucket doesn't have
- 2:27:38this weak spot because its tokens drip
- 2:27:41in smoothly with no edges to slip
- 2:27:44between. Rate limits are a kind of
- 2:27:47fairness. They make sure that one buggy
- 2:27:50program stuck in a loop or one person
- 2:27:53trying to copy an entire catalog can't
- 2:27:56crowd out everyone else. They're also a
- 2:27:59kind of protection for the back end
- 2:28:02itself, keeping it inside the load it
- 2:28:04was built to carry. But limits at the
- 2:28:07front door are only half the story.
- 2:28:10Sometimes the trouble starts deeper
- 2:28:12inside. Say a back end takes orders and
- 2:28:16passes them to a slower service that
- 2:28:18prints shipping labels. On an ordinary
- 2:28:21day, the label service keeps up. Then a
- 2:28:25big sale begins. Orders arrive faster
- 2:28:29than labels can be printed, and the
- 2:28:31queue between them starts to grow. If
- 2:28:34nothing pushes back, the queue keeps
- 2:28:37growing. Memory fills. Every order waits
- 2:28:41longer and longer. Eventually, something
- 2:28:45runs out of room and falls over, often
- 2:28:48taking other things with it. The answer
- 2:28:51is called back pressure. When a part of
- 2:28:54the system is overwhelmed, it signals
- 2:28:57the parts upstream to slow down rather
- 2:29:00than silently letting work pile up. A
- 2:29:03kitchen does this naturally. Above the
- 2:29:06pass where finished plates go out,
- 2:29:09there's a rail where order tickets are
- 2:29:11clipped. When the rail is full and the
- 2:29:14cooks are flat out, a good head chef
- 2:29:17tells the front of house to hold off
- 2:29:19seating new tables for a few minutes.
- 2:29:22That sounds unwelcoming, but the
- 2:29:25alternative is worse. If the host keeps
- 2:29:28seating people regardless, the tickets
- 2:29:31pile up. Every table waits an hour and
- 2:29:34the food that finally arrives is cold.
- 2:29:38In software, back pressure often begins
- 2:29:41with a limit on the length of a queue.
- 2:29:44Say the queue for printing labels can
- 2:29:46hold 10,000 tickets. When it's full, the
- 2:29:50order service is told, wait, there's no
- 2:29:54room. The order service, in turn, might
- 2:29:57slow down how fast it accepts new orders
- 2:30:01or tell customers that labels will be
- 2:30:04printed a little later. The pressure
- 2:30:06passes backward step by step until it
- 2:30:09reaches the edge of the system where
- 2:30:12something sensible can be done with it.
- 2:30:14A short wait, a polite message, a rate
- 2:30:18limit that tightens for a few minutes.
- 2:30:21And when there truly is more work than
- 2:30:23the system can handle, careful engineers
- 2:30:26decide in advance what to let go of
- 2:30:29first. Loading the recommendations on a
- 2:30:32product page can wait. Taking the
- 2:30:35payment for an order cannot. Engineers
- 2:30:38call this load shedding. Like a crew in
- 2:30:41a storm, you throw the deck chairs
- 2:30:43overboard and you keep the lifeboats.
- 2:30:47All of these ideas share one quiet
- 2:30:49principle, the one Air Lang's waiting
- 2:30:52lines taught us a few chapters ago. A
- 2:30:55system that's allowed to run at full
- 2:30:57stretch has no room left to absorb a
- 2:31:00surprise. Limits, buckets, and pushback
- 2:31:04keep a little room in reserve so that a
- 2:31:06busy evening stays a busy evening and
- 2:31:10doesn' become a collapse. But all of
- 2:31:12this assumes you know what's happening
- 2:31:14inside your system, that the queue is
- 2:31:17growing, that one client is using far
- 2:31:20more than its share. That one service
- 2:31:23has started answering slowly in a back
- 2:31:27end spread across hundreds of machines.
- 2:31:30That knowledge doesn't come for free. A
- 2:31:33request from your phone might pass
- 2:31:35through dozens of services before it
- 2:31:38comes back. And when it comes back
- 2:31:40slowly, which one was to blame is far
- 2:31:43from obvious. Around 2010,
- 2:31:47engineers at Google published a paper
- 2:31:50describing how they had learned to
- 2:31:52follow a single request on its whole
- 2:31:55journey, like dropping a little dye into
- 2:31:58a river and watching where the colored
- 2:32:00water flows.
- 2:32:04Chapter 13, Seeing Inside a Running
- 2:32:07System. It's the mid 2000s in Mountain
- 2:32:11View, and a young Google engineer named
- 2:32:14Ben Sigelman is working on a question
- 2:32:17that sounds simple, why one particular
- 2:32:20search was slow. When you type a few
- 2:32:23words into Google's search box and press
- 2:32:25enter, your request doesn't go to one
- 2:32:28computer. It fans out. One service
- 2:32:32checks your spelling. Others search
- 2:32:34different slices of the index of the web
- 2:32:37in parallel across many machines.
- 2:32:41Another picks advertisements. Another
- 2:32:44fetches the little snippets of text
- 2:32:46shown under each result. Each of those
- 2:32:49may call others in turn. A single search
- 2:32:53might touch thousands of machines and
- 2:32:56still come back in a fraction of a
- 2:32:58second most of the time. Now and then, a
- 2:33:02search takes noticeably longer. And when
- 2:33:05engineers tried to find out why, they
- 2:33:08ran into a wall. Each service kept its
- 2:33:11own records. Each could say how it was
- 2:33:15doing on average, but none of them could
- 2:33:17say what had happened to one particular
- 2:33:20request as it hopped from service to
- 2:33:23service. It was like trying to find out
- 2:33:26why one parcel arrived late by asking
- 2:33:30each post office how busy it had been
- 2:33:33that week. So Sigelman and his
- 2:33:35colleagues built a system to follow
- 2:33:38individual requests on their whole
- 2:33:40journey. They called it Dapper. In 2010
- 2:33:45they described it in a paper and it
- 2:33:47became one of the most influential
- 2:33:50descriptions of how to watch a large
- 2:33:52back end at work. To see what they
- 2:33:55built, it helps to know the three main
- 2:33:58ways engineers look inside a running
- 2:34:00system. They often call them metrics,
- 2:34:04logs, and traces. Metrics are numbers
- 2:34:08measured over time. How many requests
- 2:34:10arrived this second? How many failed?
- 2:34:14How long they took? How full the queue
- 2:34:16is? How much memory each server is
- 2:34:19using? Metrics are cheap to collect and
- 2:34:22easy to draw as graphs, so they're
- 2:34:25usually the first thing an engineer
- 2:34:27looks at. They're like the gauges on a
- 2:34:29car's dashboard. They tell you that
- 2:34:32something is wrong, but rarely why. A
- 2:34:35team might set an alarm to sound if,
- 2:34:39say, more than one request in a hundred
- 2:34:42starts failing for five minutes in a
- 2:34:44row, and then turn to the other two
- 2:34:47views to find out what happened. Logs
- 2:34:50are written records of individual
- 2:34:52events. Order 4018
- 2:34:56received, payment approved, email
- 2:34:59queued. Each line has a time and some
- 2:35:03details. Logs are like a diary kept by
- 2:35:07each program, and they're invaluable
- 2:35:10when you know roughly where to look. But
- 2:35:12a busy system writes millions of lines a
- 2:35:15minute, scattered across hundreds of
- 2:35:18machines, and finding the handful that
- 2:35:21matter can be like looking for one
- 2:35:24sentence in a library. Traces are the
- 2:35:27third view, and they're what Dapper made
- 2:35:29famous. A trace follows one single
- 2:35:33request from the moment it enters the
- 2:35:35system to the moment the answer goes
- 2:35:38back through every service it touches.
- 2:35:41The way it works is rather elegant. When
- 2:35:44a request first arrives, it's given a
- 2:35:46unique number, a trace number. Every
- 2:35:50time one service calls another on that
- 2:35:52request's behalf, it passes the trace
- 2:35:55number along like a tag tied to a
- 2:35:58parcel. Each piece of work done along
- 2:36:01the way, each call and its reply is
- 2:36:04recorded as a span. A span notes which
- 2:36:08service did the work, when it started,
- 2:36:11when it finished, and which span called
- 2:36:13it. Put all the spans with the same
- 2:36:16trace number together, and you get a
- 2:36:18tree. The top of the tree is the whole
- 2:36:21request. Below it are the calls it made.
- 2:36:25Below those are the calls they made. And
- 2:36:28because every span has a start and an
- 2:36:31end, you can draw them as bars on a
- 2:36:33timeline, one under another. Restaurant
- 2:36:37kitchens have something like this. In
- 2:36:40some, every order ticket gets a
- 2:36:42timestamp as it moves through the
- 2:36:45kitchen, received at the pass, started
- 2:36:48on the grill, off the grill, sauce
- 2:36:51added, plated, out the door. If one
- 2:36:54table's dinner took 40 minutes, the
- 2:36:57manager can read the stamps and see
- 2:37:00exactly where the time went. 12 minutes
- 2:37:03waiting for a free spot on the grill, as
- 2:37:05it turns out, and everything else was
- 2:37:08quick. That's what a trace does for a
- 2:37:11request. Let's read one. Your request to
- 2:37:15load a product page took 900,000
- 2:37:18of a second, almost a whole second, when
- 2:37:21it normally takes a tenth. The trace
- 2:37:24shows the top span, 900,000
- 2:37:28in total. Beneath it, the services it
- 2:37:31called one after another are laid out in
- 2:37:34a row. The product detail service took
- 2:37:3815, the price service took 12, the stock
- 2:37:42service took 10, and the reviews service
- 2:37:45took 840.
- 2:37:47There's your answer, sitting in plain
- 2:37:50sight, a single long bar among short
- 2:37:53ones. And because the reviews span has
- 2:37:56spans of its own beneath it, you can
- 2:37:59follow it down. The reviews service
- 2:38:02called the database, which was waiting
- 2:38:05on a lock held by a slow nightly job.
- 2:38:08The problem had nothing to do with
- 2:38:10product pages at all. Recording every
- 2:38:13span of every request at Google scale
- 2:38:17would have been far too expensive, so
- 2:38:19Dapper sampled. In its first version, it
- 2:38:23recorded about one request in every
- 2:38:251,024.
- 2:38:27That sounds like very few, but when a
- 2:38:30service handles many thousands of
- 2:38:32requests a second, one in a thousand
- 2:38:35still means plenty of examples of every
- 2:38:38kind of slowness. And because so little
- 2:38:41was recorded, the cost was tiny. The
- 2:38:44paper reported that in their tests,
- 2:38:47Dapper's overhead was a small fraction
- 2:38:50of a percent. The paper inspired a whole
- 2:38:53family of tools outside Google. Twitter
- 2:38:57released one called Zipkin in 2012. And
- 2:39:01in later years, the industry gathered
- 2:39:04around shared open standards for traces
- 2:39:07so that services written by different
- 2:39:10teams in different languages could all
- 2:39:13pass the trace number along in the same
- 2:39:16way. Now, there's one more idea in this
- 2:39:20chapter, and it's about how to read the
- 2:39:22numbers themselves. It may be the most
- 2:39:25useful thing tonight for anyone who ever
- 2:39:28has to judge whether a system is fast.
- 2:39:31Averages lie. Say a service handles a
- 2:39:34hundred requests. Ninety-eight of them
- 2:39:38take fifty thousandths of a second. Two
- 2:39:41of them take three whole seconds. Add
- 2:39:44them all up and divide by a hundred, and
- 2:39:47the average comes to a hundred and nine
- 2:39:50thousandths of a second. A tenth of a
- 2:39:53second, roughly. That sounds fine, but
- 2:39:56not a single request actually took a
- 2:39:59tenth of a second. Ninety-eight were
- 2:40:02quicker than that, and two were nearly
- 2:40:05thirty times slower than the average. So
- 2:40:08engineers use percentiles.
- 2:40:11Line up all one hundred requests from
- 2:40:14fastest to slowest. The one in the
- 2:40:16middle is the median, also called the
- 2:40:20fiftieth percentile. Here it's fifty
- 2:40:23thousandths of a second. That's what a
- 2:40:25typical request feels like. Now look
- 2:40:29near the slow end of the line. The
- 2:40:31ninety-ninth percentile is the time that
- 2:40:35ninety-nine percent of requests beat,
- 2:40:38and one percent don't. Here it's three
- 2:40:41seconds. That's what your unluckiest
- 2:40:44regular visitors feel. The median tells
- 2:40:47you about the typical day. The
- 2:40:49ninety-ninth percentile tells you about
- 2:40:52the bad moments. And for a busy back
- 2:40:55end, the bad moments matter far more
- 2:40:58than they seem to. In two thousand
- 2:41:00thirteen, two Google engineers, Jeffrey
- 2:41:03Dean and Luiz André Barroso, who was
- 2:41:07also one of the authors of the Dapper
- 2:41:09paper, published an article called The
- 2:41:11Tail at Scale. The tail is the slow end
- 2:41:15of that line of requests, and their
- 2:41:18point was that in a large system, the
- 2:41:21tail comes to dominate. Here's their
- 2:41:24example. Say a single server is fast
- 2:41:28almost all the time, but one request in
- 2:41:31a hundred takes a full second. Now say
- 2:41:35one user's request has to fan out to a
- 2:41:38hundred such servers and can't finish
- 2:41:42until every one of them has answered.
- 2:41:44The chance that any one server is fast
- 2:41:48is 99 in a hundred. The chance that all
- 2:41:51hundred are fast is 99 hundredths
- 2:41:55multiplied by itself a hundred times.
- 2:41:59That comes to about 37%.
- 2:42:02So about 63%
- 2:42:05of user requests will be slow. A problem
- 2:42:08that touches one request in a hundred on
- 2:42:11each server ends up touching nearly two
- 2:42:15in three requests for the people
- 2:42:17actually using the system. That's the
- 2:42:20same arithmetic we saw with
- 2:42:22microservices,
- 2:42:23where small chances of failure
- 2:42:25multiplied. Here, it's small chances of
- 2:42:29slowness multiplied. Dean and Barroso
- 2:42:33described ways to tame the tail. One was
- 2:42:36the hedged request. If an answer hasn't
- 2:42:39come back within the time that 95% of
- 2:42:43requests usually manage, send the same
- 2:42:46request to a second server and use
- 2:42:49whichever answers first. Only the
- 2:42:52slowest one in 20 requests ever gets a
- 2:42:55second copy, so the extra load stays
- 2:42:58small, but the long waits largely
- 2:43:01vanish. So the craft of watching a back
- 2:43:05end comes down to a few habits. Keep the
- 2:43:08gauges on the dashboard, keep a diary in
- 2:43:12every program, tag every request so its
- 2:43:15whole journey can be followed. And when
- 2:43:18you judge speed, look at the slow end of
- 2:43:21the line, not just the middle. All of
- 2:43:24this tells you very precisely when
- 2:43:27things are going wrong, but it raises a
- 2:43:30harder question, one that's as much
- 2:43:33about people as machines. The question
- 2:43:36is, how good counts as good enough? A
- 2:43:40system that's never allowed to fail can
- 2:43:43never change. A system that changes too
- 2:43:46freely will fail too often. Somewhere
- 2:43:50between them is a line, and someone has
- 2:43:53to decide where to draw it. At Google,
- 2:43:57around 2003,
- 2:43:59a software engineer was handed a small
- 2:44:02team of seven and asked to keep the
- 2:44:04company's production systems running.
- 2:44:07The answer he came up with turned the
- 2:44:10question into a number and gave it a
- 2:44:13name.
- 2:44:15Chapter 14, Breaking Things on Purpose.
- 2:44:19It's 2003
- 2:44:21in Mountain View, and a software
- 2:44:24engineer named Ben Trainer has just
- 2:44:27joined Google. He'll later be known as
- 2:44:30Ben Trainer Schloss. Google is already
- 2:44:34one of the busiest sites on the
- 2:44:36Internet, and someone has to keep its
- 2:44:38production systems running, the real
- 2:44:41machines serving real people. Trainer is
- 2:44:44given that job along with a small team,
- 2:44:48which he later described as a production
- 2:44:50team of seven engineers. Until then, by
- 2:44:54his own account, his working life had
- 2:44:57been spent writing software. He'd never
- 2:45:00run an operations team, and the
- 2:45:02traditional way of running one in most
- 2:45:05companies looked something like this.
- 2:45:08Developers wrote new features and threw
- 2:45:10them over a wall. On the other side, a
- 2:45:14separate operations team installed them,
- 2:45:17watched the servers, and got woken up
- 2:45:19when things broke. The developers wanted
- 2:45:22to ship changes quickly. The operations
- 2:45:25team wanted nothing to change because
- 2:45:27change is what breaks things. The two
- 2:45:30sides were, in a quiet way, permanently
- 2:45:34at odds. Treynor approached it as a
- 2:45:36software engineer would.
- 2:45:39of hiring more and more people to watch
- 2:45:42screens and restart servers by hand, his
- 2:45:45team would write software to do that
- 2:45:48work, automate the repetitive tasks,
- 2:45:51measure everything, treat reliability
- 2:45:54itself as an engineering problem with
- 2:45:57numbers and designs rather than a matter
- 2:46:00of heroics. He called the new discipline
- 2:46:03site reliability engineering, or SRE.
- 2:46:07When he later described it, he put it
- 2:46:09roughly this way. It's what happens when
- 2:46:12you ask a software engineer to design an
- 2:46:15operations team. The idea grew. Google's
- 2:46:19SRE teams spread across the company, and
- 2:46:23in 2016, several of its engineers
- 2:46:26published a book describing how they
- 2:46:28worked, which became a standard text far
- 2:46:32beyond Google. Two of Treynor's ideas
- 2:46:35are worth slowing down for. The first
- 2:46:38was a rule about time. An SRE team was
- 2:46:42expected to spend at least half of its
- 2:46:44time on engineering, building tools, and
- 2:46:47improving systems, and no more than
- 2:46:50about half on operational chores, the
- 2:46:53repetitive manual work that SRE teams
- 2:46:56came to call toil. If the chores crept
- 2:47:00above that, it was a sign that something
- 2:47:03needed to be automated or handed back to
- 2:47:06the developers. The second idea is the
- 2:47:09one that resolves the old argument
- 2:47:12between developers and operations. It's
- 2:47:15called the error budget. It starts from
- 2:47:18a slightly surprising claim. 100%
- 2:47:22reliability is the wrong goal for nearly
- 2:47:26everything. That sounds backward, so
- 2:47:28let's see why. Your phone's connection,
- 2:47:32your home Wi-Fi, the app itself all fail
- 2:47:36now and then. If a service is already
- 2:47:38more reliable than the phone and network
- 2:47:42in between, most users can't tell the
- 2:47:44difference when it gets even more
- 2:47:47reliable. Meanwhile, every extra step
- 2:47:50toward perfection costs more and moves
- 2:47:54more slowly. To be perfectly safe, you'd
- 2:47:57never release anything new at all. So
- 2:48:00instead of aiming for perfect, the team
- 2:48:03and the business agree on a target, say
- 2:48:0699.9%.
- 2:48:09Engineers call that target a service
- 2:48:12level objective. It's measured against
- 2:48:15something concrete, usually the share of
- 2:48:18real requests that succeed and succeed
- 2:48:21quickly enough. Then whatever is left
- 2:48:24over, the gap between the target and
- 2:48:27100% becomes a budget. It's an amount of
- 2:48:31failure the team is allowed to spend.
- 2:48:34Let's work out how big that budget is. A
- 2:48:37month of 30 days has 43,200
- 2:48:42minutes in it. 1/10 of 1% of that is
- 2:48:4643.2
- 2:48:48minutes. So a service with a target of
- 2:48:5199.9%
- 2:48:54is allowed about 43 minutes of trouble a
- 2:48:57month. At 99.99%
- 2:49:01the allowance shrinks to a tenth of
- 2:49:03that, just over 4 minutes a month. At
- 2:49:0799% it grows 10 times larger to about 7
- 2:49:12hours and 12 minutes. Each extra 9
- 2:49:15shrinks the budget tenfold and usually
- 2:49:19costs a great deal more to achieve.
- 2:49:22Engineers often describe these targets
- 2:49:25simply by counting the nines. Three
- 2:49:28nines, four nines, five nines, Five
- 2:49:32nines, 99.999%
- 2:49:36leaves room for only about 26 seconds of
- 2:49:40trouble in a whole month. Very few
- 2:49:43services truly need that, and the ones
- 2:49:46that do pay handsomely for it. Budgets
- 2:49:50can be counted in requests too. If a
- 2:49:53service expects 10 million requests in a
- 2:49:56month, a 99.9%
- 2:49:59target allows 10,000 of them to fail.
- 2:50:03Now, here's the clever part. The budget
- 2:50:06is something to spend. Say it's the
- 2:50:09middle of the month. A new release had a
- 2:50:11bug that caused 15 minutes of errors
- 2:50:14before it was rolled back. The team has
- 2:50:17spent 15 of its 43 minutes. About 28
- 2:50:21remain. They can keep releasing new
- 2:50:24features carefully. But say that a week
- 2:50:28later a second problem uses up the rest.
- 2:50:31The budget is gone. Under the agreement,
- 2:50:34new feature releases pause until the
- 2:50:37next month. Apart from urgent fixes, the
- 2:50:40team turns its attention to making
- 2:50:43things sturdier. The beauty of this is
- 2:50:46that nobody has to argue. Developers
- 2:50:49want to move quickly, and they can as
- 2:50:52long as there's budget left. When there
- 2:50:54isn't, everyone can see it in plain
- 2:50:57numbers, and the decision makes itself.
- 2:51:01The same number that sets the pace of
- 2:51:03change also protects the people who use
- 2:51:06the service. Five years after Trainer
- 2:51:09arrived at Google, a company in
- 2:51:12California learned about reliability the
- 2:51:15hard way. In August 2008, Netflix was
- 2:51:19still mostly a business that mailed DVDs
- 2:51:23to people's homes in red envelopes. That
- 2:51:26month, its main database suffered a
- 2:51:29serious corruption. For three days, the
- 2:51:32company couldn't ship DVDs to its
- 2:51:34members. Netflix's engineers drew a
- 2:51:37lesson from it. Their system had
- 2:51:39depended on big, single pieces of
- 2:51:42equipment in their own data center, and
- 2:51:45when one of those failed, everything
- 2:51:48stopped. They decided to move to the
- 2:51:50cloud, onto many smaller rented machines
- 2:51:55from Amazon Web Services, and to build
- 2:51:58their software so that no single machine
- 2:52:01mattered. The move took about seven
- 2:52:04years. The last pieces of the streaming
- 2:52:07service left Netflix's own data centers
- 2:52:10in early January 2016.
- 2:52:13But in the cloud, machines do fail
- 2:52:17routinely. A server can vanish at any
- 2:52:20moment. So Netflix's engineers asked
- 2:52:23themselves how they could be sure that
- 2:52:26their software really did survive losing
- 2:52:29a machine, not just in theory, but in
- 2:52:32practice on a busy evening. Their answer
- 2:52:36was to make it happen on purpose. They
- 2:52:38built a program called Chaos Monkey. Its
- 2:52:42job was simple. It wandered through
- 2:52:44Netflix's production systems and, at
- 2:52:47random, switched off servers, real
- 2:52:51servers, serving real customers. In July
- 2:52:552011, two Netflix engineering directors,
- 2:52:59Yury Izrailevsky and Ariel Tseitlin,
- 2:53:02described it publicly in a post about
- 2:53:05what they called the Simian Army. They
- 2:53:08asked readers to picture a wild monkey
- 2:53:11loose in a data center, unplugging
- 2:53:14machines and pulling out cables at
- 2:53:17random while the service carried on for
- 2:53:20customers as if nothing had happened.
- 2:53:23That sounds reckless, but there was a
- 2:53:25careful idea underneath. Chaos Monkey
- 2:53:28ran during the working day while
- 2:53:31engineers were at their desks, watching
- 2:53:34closely and ready to step in. If
- 2:53:36switching off a server caused a problem,
- 2:53:39the problem appeared at two in the
- 2:53:41afternoon with the right people in the
- 2:53:44room, rather than at three on a Sunday
- 2:53:47morning when nobody was looking. Each
- 2:53:50problem found that way could be fixed
- 2:53:53for good. Over time, the system learned
- 2:53:56to shrug off losing a server because it
- 2:53:59had practiced losing servers every day.
- 2:54:03It's the same reason schools hold fire
- 2:54:06drills on a calm afternoon. Nobody wants
- 2:54:09a fire. But if there is one, the moment
- 2:54:13to discover that a door sticks is during
- 2:54:16the drill, not the night of the real
- 2:54:18thing. A kitchen might do something
- 2:54:21similar. A head chef who quietly takes
- 2:54:24one cook off the line for an hour on a
- 2:54:27quiet Tuesday finds out whether the rest
- 2:54:30of the brigade can cover that station.
- 2:54:33Escoffier's system was built around
- 2:54:36exactly that kind of cover. The Simian
- 2:54:39Army had other members. Latency Monkey
- 2:54:42added artificial delays to calls between
- 2:54:45services to see how they coped with
- 2:54:48slowness rather than outright failure.
- 2:54:51Janitor Monkey cleaned up unused
- 2:54:53resources. And a larger cousin, Chaos
- 2:54:57Gorilla, simulated the loss of an entire
- 2:55:00zone of Amazon data centers to check
- 2:55:04that traffic would move over to the
- 2:55:06others on its own. Later came Chaos
- 2:55:09Kong, which rehearsed losing an entire
- 2:55:12region, a whole cluster of data centers
- 2:55:16in one part of the world. None of this
- 2:55:19started at full strength. Teams that run
- 2:55:21experiments like these usually begin
- 2:55:24small with one server in one corner of
- 2:55:28the system for a few minutes and widen
- 2:55:31the experiment only as their confidence
- 2:55:33grows. Engineers call the area an
- 2:55:36experiment could possibly affect its
- 2:55:39blast radius. And a first rule is to
- 2:55:42keep it small. In 2012,
- 2:55:46Netflix released Chaos Monkey as open
- 2:55:49source. Over the years, the practice
- 2:55:52grew into a field with its own name,
- 2:55:55Chaos Engineering. Its central idea is
- 2:55:58simple. Form a guess about how your
- 2:56:01system will behave when something
- 2:56:03breaks. Then break it gently, in a
- 2:56:06controlled way, and see if you were
- 2:56:09right. Site reliability engineering and
- 2:56:12chaos engineering fit together. One
- 2:56:15decides how much failure is acceptable.
- 2:56:18The other goes looking for failures
- 2:56:21before they find you. But some failures
- 2:56:24can't be found by unplugging machines.
- 2:56:27Some come from the outside world, from
- 2:56:30the strange and slippery thing that
- 2:56:33every backend has to keep track of in
- 2:56:35every single request. Time. On the last
- 2:56:39night of June 2012, the world's official
- 2:56:43clocks added one extra second to the
- 2:56:46day. And at midnight, by the world's
- 2:56:49reference clock, a surprising number of
- 2:56:51computers did not know what to do with
- 2:56:54it.
- 2:56:56Chapter 15. What time is it? Really,
- 2:57:00it's the last night of June 2012. On the
- 2:57:03world's reference clock, the time scale
- 2:57:06called Coordinated Universal Time or
- 2:57:09UTC, the seconds tick toward midnight.
- 2:57:1357, 58, 59. And then, instead of rolling
- 2:57:19over to midnight, the clock shows
- 2:57:21something it almost never shows. 23:59
- 2:57:26and 60 seconds. A leap second. The
- 2:57:29reason for it is the Earth itself. Our
- 2:57:32everyday time was once defined by the
- 2:57:35turning of the planet. But the Earth's
- 2:57:37spin isn't perfectly steady. It wobbles
- 2:57:41and slows very slightly with tides and
- 2:57:45with the shifting of the planets
- 2:57:46insides. Atomic clocks meanwhile keep
- 2:57:50time with almost perfect regularity.
- 2:57:54the years the two drift apart. So from
- 2:57:571972 onward every so often the official
- 2:58:01keepers of time added a single extra
- 2:58:04second to UTC to keep the clocks within
- 2:58:08a second of the Earth's rotation. Most
- 2:58:11people never noticed. But in June 2012
- 2:58:15some computers noticed very much. That
- 2:58:18weekend the link sharing site Reddit
- 2:58:21reported problems that it tied to the
- 2:58:23leap second. Engineers at Mozilla, the
- 2:58:27makers of the Firefox browser, found
- 2:58:30some of their Java based services
- 2:58:32struggling. LinkedIn had an outage the
- 2:58:35same day and in Australia the airline
- 2:58:39Qantas reported delays which some
- 2:58:42reports linked to trouble in a
- 2:58:43reservation system it relied on. Much of
- 2:58:47the trouble traced back to a bug in the
- 2:58:49way some versions of Linux, the
- 2:58:52operating system on a great many
- 2:58:54servers, handled the extra second. On
- 2:58:57affected machines certain programs
- 2:59:00started spinning using their processors
- 2:59:03flat out while doing nothing useful.
- 2:59:06Engineers around the world spent their
- 2:59:09weekend discovering one after another
- 2:59:12that the cure was often surprisingly
- 2:59:14simple. Setting the system clock again
- 2:59:17by hand usually calmed things down. One
- 2:59:21company had seen it coming. A year
- 2:59:24earlier in 2011
- 2:59:27Google had described a trick it called
- 2:59:30the leap smear. Instead of adding a
- 2:59:32whole second at once its time servers
- 2:59:36spread the extra second across many
- 2:59:38hours making each tick a tiny bit longer
- 2:59:42than normal. By the The the leap second
- 2:59:45arrived Google's clocks had already
- 2:59:48absorbed it in slices too small for any
- 2:59:51program to stumble over. A clock can't
- 2:59:55trip on a step it never sees. If you
- 2:59:58heard part three of our computer science
- 3:00:01series, you'll remember the trouble with
- 3:00:03keeping distant clocks in agreement.
- 3:00:06Tonight we'll stay on the back end side
- 3:00:09of that story with the everyday business
- 3:00:12of writing down the time because time
- 3:00:15for a back end is everywhere. Every
- 3:00:18order has a time, every log line, every
- 3:00:22payment, every message, every session
- 3:00:25that expires, and time is far stranger
- 3:00:29than it looks. To see why, we need to go
- 3:00:33back to a day in 1883
- 3:00:36that had two noons. Before then, most
- 3:00:39towns in the United States kept their
- 3:00:42own local time set by the sun. When the
- 3:00:45sun was highest in the sky, it was noon,
- 3:00:49and that moment comes a little later in
- 3:00:51each town as you travel west. So noon in
- 3:00:55one city might be 11 minutes after noon
- 3:00:58in another. There were more than 100
- 3:01:00different local times across the
- 3:01:02country. For the railroads, this was
- 3:01:05chaos. Timetables had to list departures
- 3:01:09in one town's time and arrivals in
- 3:01:12another's. So the railroads agreed to
- 3:01:14fix it themselves. A railway official
- 3:01:18named William F. Allen proposed a plan
- 3:01:21of four time zones across the country,
- 3:01:24each one hour apart. And on Sunday, the
- 3:01:2818th of November 1883,
- 3:01:31they switched over. At noon on that day,
- 3:01:35clocks in many towns were stopped or set
- 3:01:38back or moved forward to match the new
- 3:01:41zones. In places on the eastern edge of
- 3:01:44a zone, noon arrived twice, once by the
- 3:01:48old sun time and once by the new
- 3:01:51standard time zones spread around the
- 3:01:54world and with them came a tangle that
- 3:01:57back end engineers still deal with every
- 3:02:00day. Then came daylight saving time
- 3:02:04which shifts the clocks forward an hour
- 3:02:07in spring and back an hour in autumn in
- 3:02:11some places but not others on dates that
- 3:02:14vary by country and that governments
- 3:02:17change sometimes with only a few weeks
- 3:02:20notice. So the first rule of time in a
- 3:02:23back end is a simple one. Store every
- 3:02:27moment in UTC. UTC doesn't have time
- 3:02:31zones. It doesn't have daylight saving.
- 3:02:35Its clock just ticks forward the same
- 3:02:38everywhere on earth. Leap seconds aside
- 3:02:41it's as calm as time gets. Then convert
- 3:02:44to local time only at the very edges.
- 3:02:48When a person types in a time convert it
- 3:02:51to UTC before saving it. When you show a
- 3:02:54time to a person convert from UTC to
- 3:02:58their local time just before it appears
- 3:03:00on the screen. It keeps the logs honest
- 3:03:04too. Say a back end runs in data centers
- 3:03:07in Dublin and Tokyo and each server
- 3:03:10writes its log in its own local time.
- 3:03:13When something goes wrong an engineer
- 3:03:16trying to line up the two logs has to do
- 3:03:19mental arithmetic for every single line.
- 3:03:239 hours here an hour there. If both
- 3:03:26write in UTC the events simply fall into
- 3:03:30order one after another like a single
- 3:03:33story. Converting at the edges matters
- 3:03:37for people's plans too. Say someone in
- 3:03:39New York books a call for 9 in the
- 3:03:42morning. In January New York keeps
- 3:03:46Eastern Standard Time which is 5 hours
- 3:03:49behind UTC. So 9 in the morning in New
- 3:03:52York is 1400 in UTC. 2 in the afternoon.
- 3:03:58In July, New York keeps daylight time,
- 3:04:01which is only 4 hours behind UTC. So 9
- 3:04:05in the morning there is 1300 in UTC. 1
- 3:04:09in the afternoon. Same local time,
- 3:04:13different universal times depending on
- 3:04:16the date. A back end that simply stored
- 3:04:199:00 without saying where or when would
- 3:04:23get one of those wrong. And there's a
- 3:04:25subtler trap. Say the call is a weekly
- 3:04:28meeting at 9 every Monday morning, New
- 3:04:32York time. If the back end stores it as
- 3:04:351400 UTC, the meeting is right all
- 3:04:39winter. Then in March, the clocks spring
- 3:04:43forward and suddenly 1400 UTC is 10 in
- 3:04:48the morning in New York. The meeting has
- 3:04:51slid an hour later and nobody moved it.
- 3:04:54So for future events tied to people's
- 3:04:57lives, careful back ends store the local
- 3:05:01time together with the name of the
- 3:05:03place's time zone. 9 in the morning,
- 3:05:06America/New York. Then they work out the
- 3:05:10exact UTC moment for each meeting when
- 3:05:14the date comes using the rules as they
- 3:05:17stand. Those rules live in an unusual
- 3:05:21shared record called the Time Zone
- 3:05:24Database. It was started in the 1980s by
- 3:05:28a programmer named Arthur David Olson.
- 3:05:32And for many years now, it has been
- 3:05:34edited by Paul Eggert, a computer
- 3:05:37scientist at the University of
- 3:05:39California, Los Angeles. It records the
- 3:05:42time rules of every region of the world,
- 3:05:46past and present, using place names like
- 3:05:49America/New York or Europe/London.
- 3:05:53Volunteers update it several times a
- 3:05:56year whenever some government somewhere
- 3:05:59changes its clocks. Almost every phone,
- 3:06:02computer, and server on earth carries a
- 3:06:05copy. Daylight saving has two more traps
- 3:06:09and they catch back ends every year. In
- 3:06:12the United States, clocks spring forward
- 3:06:15on the second Sunday in March. At 2:00
- 3:06:18in the morning, the time jumps straight
- 3:06:21to 3:00. So on that night, 2:30 in the
- 3:06:24morning never happens. If a nightly job
- 3:06:28is scheduled for 2:30 local time, it may
- 3:06:32simply not run. In November, the clocks
- 3:06:35fall back. At 2:00 in the morning, the
- 3:06:38time returns to 1:00. So 1:30 happens
- 3:06:41twice. A job scheduled for 1:30 might
- 3:06:45run twice. A log might show events that
- 3:06:48seem to go backward in time. That's why
- 3:06:51many teams schedule their nightly jobs
- 3:06:54in UTC or at times like 4:00 in the
- 3:06:58morning that daylight saving never
- 3:07:01touches and countries don't change their
- 3:07:04clocks on the same day. Europe moves its
- 3:07:07clocks on the last Sunday in March, a
- 3:07:10couple of weeks after the United States.
- 3:07:13For those weeks, the gap between New
- 3:07:16York and London is 4 hours instead of
- 3:07:19the usual 5. Every year, somebody misses
- 3:07:23a meeting because of it. There's one
- 3:07:25last rule and it's about measuring how
- 3:07:28long something takes. If you want to
- 3:07:30time a request, don't subtract one wall
- 3:07:34clock reading from another. The wall
- 3:07:37clock can jump. It might be corrected by
- 3:07:40a time server or fall back an hour or
- 3:07:43meet a leap second. A request that
- 3:07:46really took a second could appear to
- 3:07:48have taken minus an hour. So computers
- 3:07:52also keep a second kind of clock called
- 3:07:55a monotonic clock, which only ever moves
- 3:07:59forward at a steady rate and doesn't
- 3:08:02care what time it is. It can't tell you
- 3:08:05the date, but it can tell you reliably
- 3:08:08how long something took. The leap second
- 3:08:11itself may soon fade into history. In
- 3:08:152022,
- 3:08:17the international body that oversees
- 3:08:20weights and measures voted to stop
- 3:08:23adding leap seconds by 2035,
- 3:08:26letting atomic time and the Earth's
- 3:08:28rotation drift further apart before
- 3:08:32anyone needs to reconcile them.
- 3:08:34Engineers everywhere quietly breathed
- 3:08:37out. So time for a back end comes down
- 3:08:42to a few humble habits. Store moments in
- 3:08:45UTC. Keep the places zone name for
- 3:08:49future human plans. Use a clock that
- 3:08:52only moves forward for measuring, and
- 3:08:55never assume that a day has 24 hours or
- 3:08:59that a minute has 60 seconds. Habits
- 3:09:02like these are what separate a back end
- 3:09:05that mostly works from one that keeps
- 3:09:08working year after year through every
- 3:09:12odd night on the calendar. And in 2011,
- 3:09:16an engineer at a cloud company in San
- 3:09:19Francisco tried to write down a whole
- 3:09:22set of such habits, 12 of them drawn
- 3:09:25from watching many thousands of
- 3:09:27applications live and die.
- 3:09:32Chapter 16, the 12 factors. It's 2011
- 3:09:37in San Francisco, and an engineer named
- 3:09:41Adam Wiggins is writing down what he has
- 3:09:43seen. A few years earlier, in 2007,
- 3:09:48Wiggins and two friends, James
- 3:09:50Lindenbaum and Orion Henry, had started
- 3:09:53a company called Heroku. Its promise was
- 3:09:57simple. Write your web application, send
- 3:10:00it to Heroku with one command, and
- 3:10:03Heroku would run it for you. No servers
- 3:10:06to set up, no machines to look after. At
- 3:10:09first, it ran only Ruby applications,
- 3:10:13many of them built with Rails, and later
- 3:10:16it took on many other languages. In
- 3:10:192010, the software company Salesforce
- 3:10:23bought it. From where they sat, the
- 3:10:25Heroku team had an unusual view. They
- 3:10:29could watch a vast number of
- 3:10:30applications being built, launched,
- 3:10:33grown, and sometimes slowly falling
- 3:10:37apart. Some ran smoothly for years,
- 3:10:41others broke every time anyone touched
- 3:10:43them, and the differences Wiggins
- 3:10:45noticed often came down to a handful of
- 3:10:48habits. So he wrote those habits down as
- 3:10:52a short document with 12 sections. He
- 3:10:56called it the 12 Factor App. It was
- 3:10:59published as a website, and it spread
- 3:11:02steadily from team to team until it had
- 3:11:05become a kind of shared checklist for
- 3:11:08how a modern backend ought to behave. In
- 3:11:11November 2024,
- 3:11:14Heroku released the document as an open
- 3:11:17project so the wider community could
- 3:11:19keep it up to date. It's a fitting place
- 3:11:22to end tonight because almost every
- 3:11:24factor is something we've already met.
- 3:11:27The first factor is one codebase, kept
- 3:11:31in version control, deployed to many
- 3:11:33places. One recipe book used in every
- 3:11:37branch of the restaurant. Second,
- 3:11:40declare all your dependencies,
- 3:11:43every outside library the program needs,
- 3:11:46explicitly so that a fresh machine can
- 3:11:49be set up from scratch with nothing
- 3:11:52assumed. The third factor is about
- 3:11:55configuration,
- 3:11:56and it's worth a worked example. Say a
- 3:11:59shop has two copies of its back end
- 3:12:02running. One is called staging, where
- 3:12:05the team tries out changes. The other is
- 3:12:09production, serving real customers. The
- 3:12:12code is identical. What differs is the
- 3:12:15settings. Staging talks to a test
- 3:12:18database and a test payment account.
- 3:12:22Production talks to the real ones. The
- 3:12:2412-factor rule is that those settings
- 3:12:28must never be written into the code
- 3:12:30itself. They live in the environment
- 3:12:32around the program, supplied when it
- 3:12:35starts. The same build, number 41, runs
- 3:12:39in staging with the test settings. When
- 3:12:42it's been tried out, the very same
- 3:12:45build, unchanged, runs in production
- 3:12:48with the real settings. That means
- 3:12:51nobody ever builds a special version for
- 3:12:54production that was never tested. And
- 3:12:57the secret keys for the real payment
- 3:12:59account never sit in the codebase, where
- 3:13:02anyone with access to the code could
- 3:13:05read them. It's like a chef who uses the
- 3:13:08same recipe in two kitchens, and only
- 3:13:11the address of the supplier changes. The
- 3:13:14fourth factor says to treat backing
- 3:13:17services, the database, the cache, the
- 3:13:21queue, the email service, as attached
- 3:13:24resources. Each one is reached through
- 3:13:28an address in the configuration.
- 3:13:30If the database moves to a new machine,
- 3:13:33you change one setting and the program
- 3:13:36connects to the new one. Redis, Rabbit
- 3:13:40MQ, the payment service, all plug in the
- 3:13:43same way. Factor five separates
- 3:13:47building, releasing, and running. A
- 3:13:50build turns code into something
- 3:13:52runnable. A release combines a build
- 3:13:55with a particular configuration and gets
- 3:13:58a number, release 41. If release 42
- 3:14:03turns out to have a problem, you go back
- 3:14:05to release 41, which still exists
- 3:14:09exactly as it was. That takes moments.
- 3:14:12The sixth factor is one we met in the
- 3:14:15chapter on the fail whale. Run the
- 3:14:17application as stateless processes. Keep
- 3:14:21nothing precious in a server's memory
- 3:14:23between requests. Put the remembering in
- 3:14:26a backing service. Then comes the
- 3:14:29seventh, which says the application
- 3:14:32should offer its services by listening
- 3:14:34on a port, a numbered door on the
- 3:14:37machine, rather than depending on some
- 3:14:39outside server being set up around it.
- 3:14:42Number eight is about concurrency, which
- 3:14:45here means scaling out by running more
- 3:14:49processes. Perhaps 10 processes handle
- 3:14:52web requests and four handle background
- 3:14:56jobs from the queue. On a busy day, make
- 3:14:59it 20 and eight. The ninth factor is
- 3:15:02called disposability,
- 3:15:05and it's the one that makes all the
- 3:15:07others work. Processes should start
- 3:15:09quickly and stop gracefully so they can
- 3:15:13be thrown away and replaced at any
- 3:15:15moment. Let's watch one stop. A web
- 3:15:19process is told to shut down, perhaps
- 3:15:22because a new release is arriving. At
- 3:15:25that moment, it's in the middle of 12
- 3:15:27requests. It stops accepting new ones
- 3:15:31and the load balancer sends fresh
- 3:15:33requests elsewhere. It finishes the 12,
- 3:15:37which takes about two seconds. Then it
- 3:15:40exits. A queue worker told to stop
- 3:15:43halfway through a job hands its
- 3:15:46unfinished ticket back to the broker so
- 3:15:50another worker can pick it up. No order
- 3:15:52lost, no email half sent. That's the
- 3:15:56acknowledgement from the deli counter
- 3:15:58chapter doing its quiet work. The 10th
- 3:16:02factor asks you to keep development,
- 3:16:05staging, and production as alike as
- 3:16:08possible, so that something that works
- 3:16:10on a developer's laptop on Tuesday works
- 3:16:14the same way in production on Wednesday.
- 3:16:1711th, treat logs as a stream of events.
- 3:16:21The program simply writes each event out
- 3:16:24one line after another and lets the
- 3:16:27surrounding platform gather the lines
- 3:16:30from every process into one place. It's
- 3:16:34the log book again and the diary that
- 3:16:37feeds the dashboards from our chapter on
- 3:16:40seeing inside a running system. And the
- 3:16:4312th says that one off administrative
- 3:16:46tasks like fixing a batch of records or
- 3:16:50changing the shape of a table should run
- 3:16:53in the same environment as the
- 3:16:55application using the same code and
- 3:16:58configuration rather than as a separate
- 3:17:01script someone keeps on their own
- 3:17:04laptop. 12 small rules. None of them is
- 3:17:07dramatic. Together they describe a back
- 3:17:11end that can be moved, copied, scaled,
- 3:17:15restarted, and rolled back without fear.
- 3:17:18Not every team follows all 12 to the
- 3:17:21letter and the document has been revised
- 3:17:24and argued over through the years, but a
- 3:17:26great deal of the cloud tooling that
- 3:17:29came afterward simply assumed that
- 3:17:32applications would behave this way.
- 3:17:35Forgetful processes, settings from the
- 3:17:38environment, logs as a stream, quick to
- 3:17:42start and gentle to stop. The habits
- 3:17:45Wiggins wrote down became for many teams
- 3:17:49the ordinary shape of a back end. And
- 3:17:52with that, our night in the back of
- 3:17:54house is almost done. We began in a
- 3:17:57basement in London in 1890
- 3:18:01with Escoffier dividing his kitchen into
- 3:18:04stations so that the plates went out hot
- 3:18:07and in order on the busiest nights. We
- 3:18:11followed a single coffee order through
- 3:18:14four steps, receive, validate, store,
- 3:18:18respond. From there, we met Roy Fielding
- 3:18:21at Irvine, writing down the quiet rules
- 3:18:25that held the web together, and learned
- 3:18:27to treat everything as a resource with
- 3:18:30an address and a handful of verbs. We
- 3:18:33watched Facebook's engineers ask for
- 3:18:36exactly what a phone needed, and Google
- 3:18:40send two bytes where 18 had been. We sat
- 3:18:44with DHH in Copenhagen, and with two
- 3:18:47young developers in a Kansas newsroom,
- 3:18:50pulling the plumbing out of their
- 3:18:52products and giving it away. We saw a
- 3:18:55whale lifted by eight orange birds, and
- 3:18:59learned why servers should remember
- 3:19:01nothing, so that any one of them can be
- 3:19:04replaced like a paper cup. In Sicily, we
- 3:19:08stood by Salvatore San Filippo's fast
- 3:19:11shelf. We took a number at the deli
- 3:19:14counter and found Agner Erlang's
- 3:19:17telephone mathematics hiding inside it.
- 3:19:20We read the ship's logbook that LinkedIn
- 3:19:22built and named after a writer. We heard
- 3:19:25the legend of a memo at Amazon and
- 3:19:28weighed one big kitchen against a food
- 3:19:31hall. We took a taxi instead of buying a
- 3:19:34car and waited for a barista to come in
- 3:19:37from home. We pressed an elevator button
- 3:19:40twice with Benjamin Pearce's word in our
- 3:19:43pocket and were charged exactly once.
- 3:19:47Then we let tokens drip into Jonathan
- 3:19:50Turner's bucket, followed a drop of dye
- 3:19:53through Google services, and learned
- 3:19:55that averages hide the slow end of the
- 3:19:58line. We spent an error budget, let a
- 3:20:01monkey loose in a data center on a calm
- 3:20:04afternoon, lived through a day with two
- 3:20:07noons and a minute with 61 seconds, and
- 3:20:11finished with 12 plain habits written
- 3:20:14down in San Francisco. Through all of
- 3:20:17it, the same few ideas kept returning.
- 3:20:20Check everything. Write it down before
- 3:20:24you promise it. Keep the servers
- 3:20:27forgetful and the records faithful. Make
- 3:20:30every action safe to repeat. Leave room
- 3:20:34for surprises and watch closely so you
- 3:20:37know when the surprise has come.
- 3:20:40Somewhere tonight, in a building you'll
- 3:20:42never see, a quiet machine is receiving
- 3:20:46an order. It checks it, writes it down,
- 3:20:50answers in a fraction of a second, and
- 3:20:52drops a ticket in a queue for the rest.
- 3:20:56A worker picks up the ticket. A log
- 3:20:58grows by one line. A dashboard ticks up
- 3:21:02by one. Nobody notices. That's how it's
- 3:21:06meant to be. Out front, the dining room
- 3:21:08is quiet and the chairs are up on the
- 3:21:11tables. But in the back, the brigade
- 3:21:14Escoffier imagined is still at its
- 3:21:17stations, one ticket at a time. Only now
- 3:21:21the stations are made of software and
- 3:21:24the tickets travel as light. The kitchen
- 3:21:27keeps cooking all night long so that in
- 3:21:30the morning, when you tap a button, the
- 3:21:33green tick is waiting. If you'd like to
- 3:21:35go deeper into the places we only
- 3:21:38glanced at tonight, our lecture on
- 3:21:41database engineering opens up the
- 3:21:44walk-in cooler itself. Our lecture on
- 3:21:47web development follows a request from
- 3:21:49the browser side, and our lecture on
- 3:21:52front-end engineering stays in the
- 3:21:55dining room with the screens and buttons
- 3:21:58you actually touch. But for now, there's
- 3:22:01nothing left to order. The tickets are
- 3:22:03cleared, the stations are wiped down,
- 3:22:06and the lights in the back of house are
- 3:22:09dimming one by one. Sleep well. Good
- 3:22:12night.
About this transcript
This page contains the full transcript of Literally Everything There Is To Know About Backend Engineering (For Sleep) by Pillow Academy, generated from the public captions YouTube serves with the video. The transcript has 26,812 words across 3,961 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.