YouTube2Text

Literally Everything There Is To Know About Backend Engineering (For Sleep) — Transcript

by Pillow Academy · 26,812 words · 3,961 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Welcome to this exploration of back-end
  2. 0:03engineering, designed for learning at
  3. 0:05your own pace. Whether you're actively
  4. 0:08listening or letting the information
  5. 0:10wash over you as you fall asleep, at
  6. 0:13some point today, you tapped a button
  7. 0:16that said order or send or pay. And
  8. 0:20before you'd even put your phone down, a
  9. 0:23quiet machine somewhere had read your
  10. 0:26request, checked it, saved it, and told
  11. 0:30half a dozen other machines what you'd
  12. 0:32done. But what really happens behind the
  13. 0:35screen when you press that button? How
  14. 0:38does one request find its way through
  15. 0:41all those machines, get handled exactly
  16. 0:44once, and come back in less than a
  17. 0:47second? Even on a night when millions of
  18. 0:50other people are pressing buttons too.
  19. 0:53The answer is more surprising than you
  20. 0:56might expect. The back end is a kitchen
  21. 0:59you never see, but you taste its cooking
  22. 1:02every day. If you find this helpful,
  23. 1:05consider subscribing for more content on
  24. 1:08science and technology. And we'd love to
  25. 1:10know, where are you listening from
  26. 1:13tonight? Drop your city or country in
  27. 1:16the comments below. Now, find a
  28. 1:18comfortable position. Let your body
  29. 1:21settle and let's discover how the hidden
  30. 1:24side of every app actually works.
  31. 1:29Chapter one, the kitchen behind the
  32. 1:32restaurant. It's 1890 in London, and a
  33. 1:36new hotel on the Strand has a problem in
  34. 1:39its basement. The hotel is the Savoy. It
  35. 1:43has electric lights and lifts and a
  36. 1:46dining room where the fashionable people
  37. 1:49of the city come to be seen. Upstairs,
  38. 1:53everything is calm. Waiters glide
  39. 1:55between tables. Silver gleams. Nobody
  40. 1:59raises voice. Downstairs is the kitchen,
  41. 2:03and the kitchen is where the evening is
  42. 2:05won or lost. The owner has hired a chef
  43. 2:09named Auguste Escoffier to run it.
  44. 2:12Escoffier is in his forties, a small,
  45. 2:15neat man from the south of France, and
  46. 2:18he has spent years thinking about a
  47. 2:20question that sounds simple. How to get
  48. 2:23dozens of cooks to produce hundreds of
  49. 2:26different dishes, all at the right
  50. 2:29moment without chaos. His answer, worked
  51. 2:32out over years in different kitchens and
  52. 2:35brought with him to London, came partly
  53. 2:38from his time as an army cook. He
  54. 2:40organized the kitchen like a brigade.
  55. 2:43Every cook had one station and one job.
  56. 2:46One cook made the sauces, one handled
  57. 2:49the roasts, one did the vegetables and
  58. 2:52soups, one kept the cold larder, the
  59. 2:56salads and the patés, one made the
  60. 2:59pastry. Over all of them stood the head
  61. 3:02chef, and between the kitchen and the
  62. 3:04dining room stood a person whose job was
  63. 3:07to receive each order from the waiters
  64. 3:10and call it out to the right stations so
  65. 3:13that every plate for a table would be
  66. 3:16ready together. That system, the brigade
  67. 3:19de cuisine, spread to restaurant
  68. 3:22kitchens around the world. If you've
  69. 3:25ever peeked through a kitchen door and
  70. 3:27heard someone calling out an order and
  71. 3:30someone else answering, yes, chef,
  72. 3:33you've heard its echo. What matters for
  73. 3:36us tonight is the shape of the thing. A
  74. 3:39calm room where guests sit and choose,
  75. 3:42and a busy room out of sight where the
  76. 3:45real work happens. Restaurant people
  77. 3:48have names for those two rooms, the
  78. 3:51front of house and the back of house.
  79. 3:54Software engineers ended up with almost
  80. 3:56exactly the same words. The part of an
  81. 3:59app you see and touch, the buttons and
  82. 4:03screens and scrolling lists is called
  83. 4:06the front end. The part you never see
  84. 4:09running on computers in a data center
  85. 4:12somewhere is called the back end.
  86. 4:14Tonight is about the back of house. If
  87. 4:17you heard our lecture on web
  88. 4:19development, you may remember the
  89. 4:21chapter about the other side of the wire
  90. 4:24where web servers first learned to cook
  91. 4:27a fresh page for every visitor. Tonight
  92. 4:30we'll walk all the way into that kitchen
  93. 4:33and stay a while. So let's look at what
  94. 4:36a back end actually does all day long.
  95. 4:40Strip away the details and almost every
  96. 4:43request it handles goes through four
  97. 4:46steps. It receives, it validates, it
  98. 4:50stores, it responds. Let's follow one
  99. 4:54order through those steps start to
  100. 4:57finish. It's a quiet evening and you're
  101. 5:00ordering from a small coffee shop that
  102. 5:02lets you order ahead on your phone. You
  103. 5:05choose two flat whites and a blueberry
  104. 5:08muffin and you press the button that
  105. 5:10says place order. At that moment your
  106. 5:14phone writes a short message. It's a
  107. 5:16request and it says in effect this
  108. 5:20customer wants two of item number seven
  109. 5:23and one of item number 12 for pickup at
  110. 5:27the shop on Market Street. Your phone
  111. 5:30sends that message across the internet
  112. 5:32to an address belonging to the coffee
  113. 5:35shop's back end. Step one, the back end
  114. 5:38receives it. Somewhere in a data center
  115. 5:42a program has been waiting listening for
  116. 5:45exactly this kind of message. It accepts
  117. 5:48the request and reads it. Before it does
  118. 5:51anything else it wants to know who's
  119. 5:54asking. Your phone included a small
  120. 5:57token that proves you signed in earlier
  121. 6:00and the back end checks that token. Yes,
  122. 6:04this is a real customer with a real
  123. 6:06account. Now the request is in the
  124. 6:09kitchen. The ticket has arrived. Step
  125. 6:12two, the back end validates it. This is
  126. 6:16the step people outside the kitchen tend
  127. 6:19to forget, and it's the one good back
  128. 6:21ends are most careful about. The back
  129. 6:24end never simply believes what it's
  130. 6:27told. It checks. It checks that item
  131. 6:30number seven exists. It does, and it's a
  132. 6:34flat white. Item 12 exists too, a
  133. 6:38blueberry muffin. It checks that the
  134. 6:40quantity is sensible. Two is fine. If
  135. 6:44the request had asked for minus three
  136. 6:46coffees or 9,000, the back end would
  137. 6:50stop right here and say no. It checks
  138. 6:53that the Market Street shop is open and
  139. 6:56that it still has muffins. And then it
  140. 6:59turns to the price. Here's a subtle
  141. 7:02point. Your phone showed you a price,
  142. 7:05but the back end doesn't use the price
  143. 7:07your phone sent. Even if it sent one, it
  144. 7:11looks the prices up for itself in its
  145. 7:14own records. A flat white is $4.50.
  146. 7:19A muffin is $3.25.
  147. 7:23Two flat whites come to $9. Add the
  148. 7:26muffin and the total is $12.25.
  149. 7:31The back end works that out on its own
  150. 7:34because anything that arrives from
  151. 7:36outside could have been changed along
  152. 7:39the way. A phone can have a bug. An old
  153. 7:42version of the app might be showing
  154. 7:45yesterday's prices, and occasionally
  155. 7:47someone will try to send a request by
  156. 7:50hand that says a coffee costs 1 cent. A
  157. 7:55good kitchen doesn't let the customer
  158. 7:57write the bill. Step three, the back end
  159. 8:01stores it. The order now gets written
  160. 8:03into the shop's database, the back end's
  161. 8:07long-term
  162. 8:08A new row appears, recording who you
  163. 8:11are, what you ordered, which shop, the
  164. 8:15total of $12.25
  165. 8:18and the time. The order is given a
  166. 8:20number. Let's say it's order 4,018.
  167. 8:24Until that row is safely written, the
  168. 8:27order hasn't really happened. If the
  169. 8:30power went out a moment before, there
  170. 8:33would be no order at all, and the honest
  171. 8:36thing would be to tell you so. Once it's
  172. 8:38written, the order exists, and it will
  173. 8:42still exist tomorrow and next year. Step
  174. 8:45four, the back end responds. It sends a
  175. 8:49message back to your phone. Your order
  176. 8:52is confirmed. Number 4,018,
  177. 8:56$12.25,
  178. 8:59ready in about 10 minutes. Your screen
  179. 9:02shows a small green tick. That whole
  180. 9:05round trip might take a quarter of a
  181. 9:07second. Most of that is the request
  182. 9:10crossing the internet and coming back.
  183. 9:13The work in the kitchen itself, the
  184. 9:15checking and the saving might take only
  185. 9:18a few thousandths of a second. But
  186. 9:21notice that the back end isn't finished.
  187. 9:24There's still the barista's tablet
  188. 9:26behind the counter, which needs to show
  189. 9:29the new order. There's a receipt to
  190. 9:31email you. There's a loyalty program
  191. 9:34that wants to add points to your
  192. 9:35account. There's the payment to arrange.
  193. 9:39There's a dashboard somewhere where the
  194. 9:41owner likes to watch sales come in. A
  195. 9:44sensible back end doesn't make you wait
  196. 9:47for all of that. It answers you as soon
  197. 9:50as the order is safely stored, and it
  198. 9:52hands the other jobs off to be done in
  199. 9:55the background a moment later. That
  200. 9:58trick, answering quickly and doing the
  201. 10:00rest afterward, gets a whole chapter of
  202. 10:03its own later tonight. Receive,
  203. 10:06validate, store, respond. Most of what
  204. 10:10backends do from the smallest coffee
  205. 10:13shop to the largest bank is some version
  206. 10:17of those four steps repeated many
  207. 10:19thousands of times a second. And there's
  208. 10:22a reason those steps live in the back of
  209. 10:25house rather than on your phone. The
  210. 10:27first reason is trust. Your phone is in
  211. 10:31your pocket. It belongs to you and you
  212. 10:34can change what it does. The backend
  213. 10:37belongs to the business and it's the
  214. 10:40only place where the rules can be
  215. 10:42enforced for everyone. The price of a
  216. 10:44coffee, the number of muffins left,
  217. 10:47whether you've really paid. Those
  218. 10:50decisions have to be made somewhere the
  219. 10:52customer can't reach in and adjust. The
  220. 10:56second reason is sharing. The muffins on
  221. 10:59the shelf are the same muffins for
  222. 11:01everyone. If 10 people order at once,
  223. 11:05something has to keep a single true
  224. 11:08count. Your phone only knows about you.
  225. 11:11The backend sees everyone. And third,
  226. 11:15there's memory. Your phone could be lost
  227. 11:18tomorrow or dropped in a river. Your
  228. 11:21order history, your account, your
  229. 11:24loyalty points all need to live
  230. 11:26somewhere more permanent. So the backend
  231. 11:30is where the rules live, where the
  232. 11:32shared things live, and where the memory
  233. 11:35lives. It's also where the hard problems
  234. 11:38live and they're the subject of the rest
  235. 11:41of tonight. What happens when 10,000
  236. 11:44people order at the same moment instead
  237. 11:47of one? What happens when the payment
  238. 11:50service takes too long to reply and your
  239. 11:53phone gives up and tries again and you
  240. 11:56might be charged twice? How does the
  241. 11:59kitchen keep cooking when one of its
  242. 12:02ovens breaks in the middle of the dinner
  243. 12:04rush? And how do the engineers who run
  244. 12:07it even know that an oven has broken
  245. 12:10when the whole kitchen is
  246. 12:13spread across thousands of machines in
  247. 12:16buildings they may never visit. Back end
  248. 12:19engineering is the craft of answering
  249. 12:22those questions. It's less about any one
  250. 12:25clever trick and more about a set of
  251. 12:28habits. Check everything, write things
  252. 12:31down before you promise them. Expect
  253. 12:34parts to fail. Never let one slow job
  254. 12:38hold up the whole line. Escoffier would
  255. 12:41have recognized every one of those
  256. 12:43habits. His kitchens were famous, not
  257. 12:46because any single cook was a genius,
  258. 12:49though some were, but because the system
  259. 12:52kept working on the busiest nights. A
  260. 12:55cook could fall ill, a delivery could
  261. 12:58arrive late, and the plates still went
  262. 13:01out hot and in order. Back ends are
  263. 13:04judged the same way. Nobody praises a
  264. 13:07back end for a good day. A good back end
  265. 13:11is one you never think about at all. You
  266. 13:13press the button, the green tick
  267. 13:15appears, and you go on with your
  268. 13:18evening. There's one more thing to
  269. 13:20notice about our coffee order before we
  270. 13:23leave it. Your phone and the back end
  271. 13:26had to agree on a great deal before any
  272. 13:29of this could work. Your phone had to
  273. 13:32know where to send the order and how to
  274. 13:35write it and what to expect back. It
  275. 13:38said item number seven, and the back end
  276. 13:41understood that to mean a flat white.
  277. 13:45The back end said order confirmed, and
  278. 13:48your phone knew to show a green tick.
  279. 13:50None of that was an accident. Someone
  280. 13:53wrote down the rules of that
  281. 13:55conversation carefully in advance, and
  282. 13:59both sides agreed to follow them.
  283. 14:01Engineers call that written agreement an
  284. 14:04interface. And at the turn of this
  285. 14:07century, a young computer scientist in
  286. 14:11California wrote a dissertation that
  287. 14:13changed how much of the world's software
  288. 14:16would design those agreements. He wasn't
  289. 14:19trying to invent anything new. He was
  290. 14:21trying to describe as precisely as he
  291. 14:25could something that already existed and
  292. 14:28already worked better than almost anyone
  293. 14:31had expected.
  294. 14:34Chapter two, a contract between
  295. 14:37programs. It's the late 1990s
  296. 14:41at the University of California Irvine
  297. 14:44and a graduate student named Roy
  298. 14:47Fielding has a slightly unusual problem
  299. 14:50for a student. He has already helped
  300. 14:52build the thing he's meant to be
  301. 14:54studying. Through the middle of the
  302. 14:56decade, Fielding had been one of the
  303. 14:59principal authors of the specifications
  304. 15:02for HTTP,
  305. 15:04the language that web browsers and web
  306. 15:07servers use to talk to each other. He
  307. 15:09was also one of the people who started
  308. 15:12the Apache web server project, which
  309. 15:15went on to run a large share of the
  310. 15:17world's websites. So he knew the web
  311. 15:20from the inside. He'd sat in the
  312. 15:22discussions where its rules were argued
  313. 15:25over line by line and he'd noticed
  314. 15:28something. The web was growing at a rate
  315. 15:31no one had planned for. Millions of
  316. 15:34machines, all built by different people,
  317. 15:38most of whom would never meet. And yet
  318. 15:41it mostly worked. A browser written in
  319. 15:44one country could talk to a server
  320. 15:47written in another and neither needed to
  321. 15:50know anything about the others' insides.
  322. 15:53Fielding wanted to explain why. He
  323. 15:57wanted to pin down the rules that made
  324. 15:59it hold together and to see whether
  325. 16:02other software could be designed to
  326. 16:04follow them on purpose. In 2000 he
  327. 16:08finished his doctoral dissertation. Its
  328. 16:11title is long and dry, Architectural
  329. 16:14Styles and the Design of Network-Based
  330. 16:18architectures, but its fifth chapter
  331. 16:21gave a name to the style he had found in
  332. 16:24the web's design. He called it
  333. 16:26representational state transfer, REST
  334. 16:30for short. To see why that idea mattered
  335. 16:33so much to back-end engineers, we need
  336. 16:36to go back to the coffee order from the
  337. 16:39last chapter. Your phone and the back
  338. 16:42end had to agree on how to talk. That
  339. 16:45agreement is called an API, short for
  340. 16:49application programming interface. It's
  341. 16:52the menu and the ordering rules written
  342. 16:55down for programs instead of people.
  343. 16:58Before REST caught on, many of these
  344. 17:01agreements were built around a simple
  345. 17:03idea. One program would call a function
  346. 17:06inside another as if it were sitting
  347. 17:09right next door. Get customer balance,
  348. 17:13update shipping address, cancel order
  349. 17:16with reason. Every system invented its
  350. 17:19own list of commands with its own names
  351. 17:22and its own rules. That worked in a way,
  352. 17:26but every API was a different language.
  353. 17:29To use a new one, you had to learn all
  354. 17:32of its commands from scratch. It was a
  355. 17:35bit like walking into a library where
  356. 17:37every librarian had their own private
  357. 17:40set of phrases and you had to memorize
  358. 17:43them before you could borrow anything.
  359. 17:46Fielding's description of the web
  360. 17:48pointed to a different way of organizing
  361. 17:51things and it rests on a few plain
  362. 17:54ideas. The first idea is that everything
  363. 17:57worth talking about is a resource and
  364. 18:00every resource has a name. On the web,
  365. 18:04the name is an address. A page about
  366. 18:07soup has one address. A photo of a cat
  367. 18:10has another. In a REST API, the same is
  368. 18:13true of the things a business cares
  369. 18:16about. A customer is a resource. An
  370. 18:19order is a resource. A book in a library
  371. 18:23is a resource. Each one gets its own
  372. 18:26address, and the address stays the same.
  373. 18:29The second idea is that you use the same
  374. 18:32small set of verbs for every resource.
  375. 18:36HTTP
  376. 18:37already had these verbs built in. Get
  377. 18:41means please show me this. Post means
  378. 18:45here's something new. Please add it. Put
  379. 18:49means please replace this with what I'm
  380. 18:52sending. And delete means please remove
  381. 18:55this. That's nearly the whole
  382. 18:58vocabulary. Four verbs applied to any
  383. 19:01resource at all. The third idea is that
  384. 19:04what travels back and forth isn't the
  385. 19:07thing itself, but a representation of
  386. 19:10it. A book on a shelf can't travel down
  387. 19:13a wire. What travels is a description of
  388. 19:16the book, its title, its author, whether
  389. 19:20it's on loan, written out in an agreed
  390. 19:23format. These days that format is very
  391. 19:26often a simple text layout called JSON,
  392. 19:31which a programmer named Douglas
  393. 19:33Crockford described and popularized in
  394. 19:36the early 2000s. It looks much like a
  395. 19:39tidy list of labels and values. And the
  396. 19:43fourth idea is that each request should
  397. 19:46carry everything needed to understand
  398. 19:48it. The server shouldn't have to
  399. 19:51remember what you asked a moment ago.
  400. 19:54That one turns out to be so important
  401. 19:56for running large systems that we'll
  402. 19:59give it a whole chapter of its own a
  403. 20:01little later tonight. Let's put these
  404. 20:04ideas to work by building an API for a
  405. 20:08small town library. The library has
  406. 20:11about 1,200 books, and it wants members
  407. 20:14to be able to search the catalog and
  408. 20:17borrow books from an app on their
  409. 20:19phones. The collection of all the books
  410. 20:22gets one address. We'll call it books.
  411. 20:25Each individual book gets its own
  412. 20:27address made from that one plus a
  413. 20:30number. Book number 42 lives at
  414. 20:34books/42.
  415. 20:36Now let's use the verbs. A member opens
  416. 20:40the app and wants to see book 42. The
  417. 20:44app sends a get request to books/42.
  418. 20:49The back end looks it up and sends back
  419. 20:52a representation. Title, Moby Dick.
  420. 20:55Author, Herman Melville. Published,
  421. 20:581851.
  422. 21:00Status, available. Then the librarian
  423. 21:04receives a new donation, a copy of a
  424. 21:07cookbook, and adds it through her own
  425. 21:09screen. Her app sends a post request to
  426. 21:13books, the address for the whole
  427. 21:15collection, carrying the cookbook's
  428. 21:17title and author. Post means add
  429. 21:21something new here. The back end creates
  430. 21:24the record, gives it the next free
  431. 21:26number, which happens to be 1,201,
  432. 21:31and replies, created. And you'll find it
  433. 21:34at books/1,201.
  434. 21:38A week later, she notices she spelled
  435. 21:41the author's name wrong. Her app sends a
  436. 21:44put request to books/1,201,
  437. 21:50carrying the corrected record in full.
  438. 21:53Put means replace what lives at this
  439. 21:56address with this. And a few years on,
  440. 22:00when the cookbook falls apart from use,
  441. 22:03a delete request to that same address
  442. 22:06removes it from the catalog. Notice that
  443. 22:09nobody had to invent a command called
  444. 22:12add new book or fix book author or
  445. 22:15remove damaged book. The library's API
  446. 22:19is just addresses and four verbs, the
  447. 22:22same four verbs that every other rest
  448. 22:25API uses. Borrowing is where it gets
  449. 22:28clever. You might expect a special
  450. 22:31command called borrow, but in rest you
  451. 22:34usually turn the action into a resource
  452. 22:37of its own. A loan is a thing. It has a
  453. 22:40start date, a due date, a book, and a
  454. 22:44member. So the library adds one more
  455. 22:46collection called loans. On the 5th of
  456. 22:50November, member number seven wants to
  457. 22:53borrow Moby Dick. Her app sends a post
  458. 22:56request to loans saying member seven
  459. 23:00book 42. The back end checks that the
  460. 23:03book is available and that member seven
  461. 23:06doesn't have overdue fines. Then it
  462. 23:08creates loan number 503
  463. 23:12due back 14 days later on the 19th of
  464. 23:15November. Now if anyone sends a get
  465. 23:19request to books/42,
  466. 23:22the status says on loan. And when member
  467. 23:25seven brings it back, a request to
  468. 23:28update loan 503
  469. 23:31marks it returned. Every reply also
  470. 23:34comes with a short number that says how
  471. 23:37things went and those numbers are part
  472. 23:39of the same agreement. 200 means fine.
  473. 23:43201 means created. 404 means there's
  474. 23:48nothing at that address. And 500 means
  475. 23:52the kitchen itself went wrong. This
  476. 23:55style brought some real gifts. One was
  477. 23:59predictability.
  478. 24:00Once a developer had used one rest API,
  479. 24:04the next one felt familiar. Collections
  480. 24:07and items for verbs, a handful of status
  481. 24:11numbers. You could often guess how an
  482. 24:14API worked before reading its
  483. 24:16documentation.
  484. 24:18Another gift came from the verbs
  485. 24:20themselves. A get request is only
  486. 24:23allowed to look, never to change
  487. 24:26anything. That sounds like a small
  488. 24:29but it means a get can be repeated
  489. 24:32safely, and its answer can be saved and
  490. 24:35reused by anything along the way. Put
  491. 24:38and delete have a special quality of
  492. 24:41their own too. Doing them twice leaves
  493. 24:44things exactly as doing them once would.
  494. 24:48That quality has a name, and it turns
  495. 24:51out to be the key to never charging
  496. 24:54anyone twice, which is a story for later
  497. 24:57tonight. Through the 2000s, as companies
  498. 25:01began opening their systems to outside
  499. 25:04developers, rest became the usual way to
  500. 25:07do it. Photo sites, maps, payment
  501. 25:11companies, and social networks all
  502. 25:13published rest APIs, and a whole
  503. 25:17generation of programmers learned the
  504. 25:19pattern of addresses and verbs. Fielding
  505. 25:22himself has said more than once that
  506. 25:25many of the APIs calling themselves
  507. 25:27restful don't follow all of his
  508. 25:30constraints. By his strict definition,
  509. 25:33that's fair. In everyday use, the word
  510. 25:37drifted toward meaning something looser.
  511. 25:40Resources with addresses, the standard
  512. 25:43verbs, JSON going back and forth. But
  513. 25:46even that looser version did a great
  514. 25:49deal of good. There's one more thing
  515. 25:51worth knowing about any API, rest or
  516. 25:55otherwise. It's a contract, and
  517. 25:57contracts are hard to change once people
  518. 26:00rely on them. Say the library's app has
  519. 26:04been in use for two years. Thousands of
  520. 26:07members have it on their phones, and
  521. 26:09many of them never update anything. If
  522. 26:12the backend team decides to rename the
  523. 26:16field called title to name, every one of
  524. 26:19those old apps will suddenly find no
  525. 26:22titles at all. Their screens will show
  526. 26:25blank lines where the books should be.
  527. 26:28So backend engineers learn a careful
  528. 26:31habit. You can add new fields to a
  529. 26:33contract because old apps will simply
  530. 26:37ignore what they don't recognize, but
  531. 26:39you almost never remove or rename one.
  532. 26:43When a big change is truly needed, you
  533. 26:46publish a second version of the API
  534. 26:49alongside the first, often with the
  535. 26:52version number right in the address, and
  536. 26:54you keep the old one running until the
  537. 26:57last of the old apps has faded away. An
  538. 27:00API is a promise made to people you'll
  539. 27:03never meet. Good back-ends keep their
  540. 27:06promises for years. For a long time,
  541. 27:10rest seemed like the end of the story.
  542. 27:13Clean addresses, simple verbs, a
  543. 27:16contract everyone could read. But around
  544. 27:192012, at a company with hundreds of
  545. 27:22millions of people looking at it through
  546. 27:24small phones on slow connections,
  547. 27:27engineers started to feel that rest was
  548. 27:30making their app do too much work. The
  549. 27:33app kept asking for whole resources when
  550. 27:36it only needed a few small pieces of
  551. 27:39each. It was like ordering the entire
  552. 27:42menu just to get one dish.
  553. 27:47Chapter three. Ask for exactly what you
  554. 27:50need. It's 2012 in Menlo Park,
  555. 27:54California, and Facebook has decided to
  556. 27:57rebuild its iPhone app. The first
  557. 28:00versions of the app had leaned heavily
  558. 28:02on web pages shown inside a frame on the
  559. 28:05phone. That had made it quick to update,
  560. 28:08but it felt slow and sticky in the hand.
  561. 28:12Later that year, Mark Zuckerberg would
  562. 28:15say publicly that betting so heavily on
  563. 28:18that approach had been one of the
  564. 28:20company's biggest mistakes. So a team
  565. 28:23set out to write the app again,
  566. 28:26natively, as a proper phone program. The
  567. 28:29hardest screen was news feed, the
  568. 28:32endless scroll of posts from friends.
  569. 28:36And the trouble with newsfeed wasn't
  570. 28:38really on the phone, it was in how the
  571. 28:41phone got its data. One of the engineers
  572. 28:44in the middle of this was Lee Byron, who
  573. 28:47was managing the iPhone team. As he
  574. 28:50later told the story, the old app had
  575. 28:53been pulling newsfeed through an aging
  576. 28:56interface that was never designed for
  577. 28:59it, and posts were going missing. The
  578. 29:02team concluded that they needed a new
  579. 29:04way for the app to ask for data, built
  580. 29:07from scratch. Byron worked with two
  581. 29:10other engineers, Nick Schrock and Dan
  582. 29:13Schäfer. Schrock built a prototype he
  583. 29:16called Supergraph. Over a month or two
  584. 29:19of steady improvement, it grew into
  585. 29:22something new. The team called it
  586. 29:24GraphQL. To understand what problem they
  587. 29:28were solving, let's look at what a REST
  588. 29:31interface makes a phone do. Say the app
  589. 29:34needs to draw a small screen showing 10
  590. 29:37of your friends, each with a name and a
  591. 29:40small photo. In a typical REST design,
  592. 29:44the app first asks for your list of
  593. 29:46friends. Back comes a list of 10 friend
  594. 29:49numbers. That's one request. Then, for
  595. 29:53each friend, it asks for that person's
  596. 29:56record. Get user 104,
  597. 30:00get user 2009,
  598. 30:03and so on, 10 more times. That's 11
  599. 30:06requests in all, and each of those user
  600. 30:10records is the full representation
  601. 30:13because REST hands you the whole
  602. 30:15resource. Name and photo, yes, but also
  603. 30:19birthday, hometown, workplace, schools,
  604. 30:23relationship status, the date the
  605. 30:26account was made, a dozen links and
  606. 30:29settings. Say each record comes to about
  607. 30:326 kilobytes. 10 of them is 60 kilobytes
  608. 30:36plus a little for the friend list. Call
  609. 30:39it 61 kilobytes to draw a screen that
  610. 30:43needs only names and photos. There are
  611. 30:45two problems here and they have names.
  612. 30:49The first is overfetching. The app
  613. 30:52ordered whole records when it wanted two
  614. 30:54fields from each. It's like walking into
  615. 30:57a restaurant wanting only the soup and
  616. 31:00being told the kitchen only serves the
  617. 31:03entire menu, every course, every time
  618. 31:07you eat your soup and leave the rest.
  619. 31:10The second is underfetching. No single
  620. 31:14request gave the app everything it
  621. 31:16needed, so it had to keep going back.
  622. 31:19Each trip across a mobile network in
  623. 31:222012 might easily take 3/10 of a second.
  624. 31:27If the app made its 11 requests one
  625. 31:30after another, that's about 3 and 1/3
  626. 31:33seconds of waiting with a spinning wheel
  627. 31:36on the screen. A desktop browser on a
  628. 31:40fast office connection could shrug that
  629. 31:43off. A phone on a train couldn't.
  630. 31:46GraphQL changed who decides the shape of
  631. 31:49the answer. Instead of the server
  632. 31:51deciding what each resource contains,
  633. 31:54the app writes a small description of
  634. 31:57exactly the data it wants and sends that
  635. 32:00description in one request. In plain
  636. 32:04words, the app's request says, starting
  637. 32:07from me, give me my friends, the first
  638. 32:1010 of them, and for each one, only their
  639. 32:13name and their photo. The server reads
  640. 32:17that description, gathers exactly those
  641. 32:20pieces, and sends back one answer shaped
  642. 32:24exactly like the question, 10 names, 10
  643. 32:28photo addresses, about 2 kilobytes, one
  644. 32:32trip, 61 kilobytes in 11 trips or 2
  645. 32:36kilobytes in one. It's like ordering à
  646. 32:39la carte with a very precise waiter.
  647. 32:42Soup, no croutons, and a half portion of
  648. 32:46bread, and it all comes out together on
  649. 32:49one tray. Behind the scenes, a GraphQL
  650. 32:53server starts with a written description
  651. 32:56of everything it can offer, called a
  652. 32:58schema. The schema says that a user has
  653. 33:01a name, which is text, and a photo,
  654. 33:05which is an address, and friends, which
  655. 33:08is a list of other users. Every field
  656. 33:11has a type, so the app can't ask for
  657. 33:14something that doesn't exist, and tools
  658. 33:17can check a request for mistakes before
  659. 33:19it's ever sent. For each field in the
  660. 33:22schema, the backend has a small piece of
  661. 33:25logic that knows how to fetch it. When a
  662. 33:27request arrives, the server walks
  663. 33:30through the description, calls the right
  664. 33:33piece for each field, and assembles the
  665. 33:36result. The phone never needs to know
  666. 33:39where each piece came from. A friend's
  667. 33:42name might live in one database and
  668. 33:45their photo address in another. The
  669. 33:48answer arrives as one neat bundle.
  670. 33:51Facebook used GraphQL internally for a
  671. 33:55few years. In 2015, it published the
  672. 33:59idea and a reference version as open
  673. 34:02source. Other companies picked it up,
  674. 34:05and in 2018, Facebook moved the project
  675. 34:09into a new independent foundation so
  676. 34:12that no single company would control it.
  677. 34:15GraphQL isn't free, though. It moves a
  678. 34:17lot of power to the person writing the
  679. 34:20request, and the backend has to be ready
  680. 34:23for that. A request can ask for friends
  681. 34:26of friends of friends, nested 10 levels
  682. 34:29deep and turn into millions of lookups.
  683. 34:33So GraphQL servers usually measure how
  684. 34:36expensive a request will be before
  685. 34:39running it and refuse ones that go too
  686. 34:42far. Saving answers for reuse also gets
  687. 34:46harder. With rest, a get request for
  688. 34:49books/42
  689. 34:51always means the same thing, so a copy
  690. 34:55of the answer can be kept and handed out
  691. 34:58again. With GraphQL, almost every
  692. 35:01request is a slightly different
  693. 35:03question, often sent to the same single
  694. 35:07address, so those simple saved copies
  695. 35:10don't work as well. Many teams use both,
  696. 35:14a public rest interface for outside
  697. 35:16developers and GraphQL behind their own
  698. 35:20apps, where the screens change every
  699. 35:23month and the phone needs exactly what
  700. 35:26it needs. Now, let's leave the phone
  701. 35:29behind and go deeper into the kitchen.
  702. 35:32Inside a large company, most requests
  703. 35:36aren't coming from phones at all.
  704. 35:38They're coming from other parts of the
  705. 35:40back end. One program asks another to
  706. 35:44check a price. That one asks a third to
  707. 35:47look up a stock level. A single tap on
  708. 35:50your screen might set off dozens of
  709. 35:53these internal conversations.
  710. 35:56For those conversations,
  711. 35:58speed and strictness matter far more
  712. 36:01than readability. Google had faced this
  713. 36:04early. For more than a decade, its
  714. 36:07internal programs had spoken to each
  715. 36:10other through a system of its own, known
  716. 36:13inside the company as Stubby. In
  717. 36:16February 2015,
  718. 36:18Google released a new open version of
  719. 36:21the same ideas built for the outside
  720. 36:24world. It was called gRPC.
  721. 36:28The RPC part stands for remote procedure
  722. 36:32call. That's the old idea from the last
  723. 36:35chapter, calling a function inside
  724. 36:38another program as if it were next door.
  725. 36:41Rest had moved away from it for public
  726. 36:44interfaces, but for programs inside the
  727. 36:47same building owned by the same company,
  728. 36:50calling functions directly turned out to
  729. 36:53be exactly right as long as both sides
  730. 36:57agreed precisely on what those functions
  731. 36:59were. That precise agreement is written
  732. 37:03in a small contract file. It lists each
  733. 37:06function, what it takes in and what it
  734. 37:09gives back, and the exact shape of every
  735. 37:12message. From that one file, tools
  736. 37:16generate matching code for both sides in
  737. 37:19many different programming languages. A
  738. 37:22service written in one language can call
  739. 37:25a service written in another, and if
  740. 37:28either side breaks the contract, the
  741. 37:30mistake shows up while the code is being
  742. 37:33built, not at 3:00 in the morning. The
  743. 37:35messages themselves travel in a compact
  744. 37:39binary format called protocol buffers,
  745. 37:42which Google had used internally for
  746. 37:44years. Here's where the savings come
  747. 37:47from. Say one program sends another a
  748. 37:51temperature reading of 21 degrees. In
  749. 37:54JSON, the message is written out in
  750. 37:57text, an opening brace, the word
  751. 38:00temperature in quotation marks, a colon,
  752. 38:04the digits two and one, and a closing
  753. 38:07brace. That's 18 characters, so 18
  754. 38:13bytes. In protocol buffers, the word
  755. 38:16temperature never travels at all. Both
  756. 38:19sides already have the contract, which
  757. 38:22says that field number one is the
  758. 38:25temperature. So the message is one byte
  759. 38:29that means field number one followed by
  760. 38:32one byte holding the number 21, two
  761. 38:35bytes instead of 18. It's the same trick
  762. 38:39a busy kitchen uses on its tickets.
  763. 38:42Nobody writes two flat whites with oat
  764. 38:45milk for the customer at table seven.
  765. 38:48They write T7 to FW oat. Everyone at the
  766. 38:53pass knows the shorthand, so the
  767. 38:56shorthand is all that needs to travel.
  768. 38:59On a single message, 16 bytes saved is
  769. 39:03nothing. But by Google's own account in
  770. 39:052015, its data centers were handling
  771. 39:09tens of billions of these calls every
  772. 39:12second. At that scale, small savings in
  773. 39:15every message add up to a great many
  774. 39:18machines. gRPC also rides on HTTP
  775. 39:23version two, which can carry many
  776. 39:26conversations at once over a single
  777. 39:29connection and lets either side stream a
  778. 39:32long run of messages without starting a
  779. 39:35new request each time. So by the middle
  780. 39:38of the 2010s, backend engineers had
  781. 39:42three main ways for programs to talk.
  782. 39:45REST with its clean addresses and
  783. 39:48familiar verbs for the wide outside
  784. 39:51world. GraphQL for apps that want to ask
  785. 39:55for exactly the shape of data they need.
  786. 39:58And gRPC,
  787. 40:00fast and strict for the busy internal
  788. 40:03corridors of the backend. None of them
  789. 40:06replaced the others. Each fits a
  790. 40:08different kind of conversation, but an
  791. 40:11interface is only the doorway. Someone
  792. 40:15still has to write everything behind it.
  793. 40:18The logic that checks orders and saves
  794. 40:20rows and sends replies over and over for
  795. 40:25every new product. In 2004,
  796. 40:29a young programmer in Copenhagen,
  797. 40:31working remotely for a small company in
  798. 40:34Chicago, got tired of writing the same
  799. 40:37pieces over and over. So he pulled them
  800. 40:40out of the product he was building, gave
  801. 40:43them a name, and gave them away.
  802. 40:47Chapter four, frameworks that shipped.
  803. 40:51It's 2003
  804. 40:53in Copenhagen, and a young Danish
  805. 40:55programmer named David Heinemeier
  806. 40:58Hansson is building a web application
  807. 41:01for a small design company in Chicago.
  808. 41:04The company is called 37signals.
  809. 41:07One of its founders, Jason Fried, wants
  810. 41:10a simple tool for managing projects with
  811. 41:13clients, a place to post messages, keep
  812. 41:17to-do lists, and track milestones. It
  813. 41:20will be called Basecamp. Heinemeier
  814. 41:23Hansson, who would become known to
  815. 41:25almost everyone in the field by his
  816. 41:27initials, DHH,
  817. 41:30was working remotely, part-time, across
  818. 41:34an ocean and seven time zones. He'd
  819. 41:37chosen to write Basecamp in Ruby, a
  820. 41:40programming language created in Japan by
  821. 41:43Yukihiro Matsumoto in the mid-1990s
  822. 41:47and still little known in the West at
  823. 41:49the time. DHH liked it because it read
  824. 41:53almost like English and because it let a
  825. 41:56programmer bend the language to suit the
  826. 41:59problem. As he built Basecamp, he
  827. 42:02noticed something every back-end
  828. 42:04programmer notices sooner or later. Most
  829. 42:08of the work wasn't about project
  830. 42:10management at all. It was plumbing,
  831. 42:13taking a request that arrives at an
  832. 42:15address and deciding which piece of code
  833. 42:18should handle it, reading the fields
  834. 42:21from a form, checking that an email
  835. 42:24address looks like an email address,
  836. 42:26turning a row from the database into an
  837. 42:29object the program can use and turning
  838. 42:33it back again, filling in a page
  839. 42:35template with the results. Every web
  840. 42:38application needed all of this, and
  841. 42:41every team was writing it again by hand
  842. 42:45from scratch. So DHH wrote his plumbing
  843. 42:49carefully in a way that wasn't tied to
  844. 42:52Basecamp. Basecamp launched in early
  845. 42:552004
  846. 42:56and that July, he pulled the plumbing
  847. 42:59out, gave it a name, and released it for
  848. 43:02anyone to use free. He called it Ruby on
  849. 43:06Rails. It's a little like a chef who has
  850. 43:09spent years setting up kitchens and
  851. 43:12finally writes down the layout that
  852. 43:14works, where the stoves go, where the
  853. 43:17knives hang, which station is next to
  854. 43:20which. Any new restaurant can start with
  855. 43:23that layout on day one and get straight
  856. 43:27to cooking. Rails was built around two
  857. 43:30short phrases that its fans repeated
  858. 43:33often. The first was don't repeat
  859. 43:36yourself. Every piece of knowledge in a
  860. 43:38program should live in exactly one
  861. 43:41place. If the rule that a price can't be
  862. 43:44negative is written in one place, then
  863. 43:47fixing it means changing one place. The
  864. 43:51second phrase was convention over
  865. 43:53configuration,
  866. 43:55and that's where Rails felt different
  867. 43:57from almost everything before it. Many
  868. 44:00frameworks of the time asked you to
  869. 44:03describe everything in long
  870. 44:06configuration files. This class of
  871. 44:09object is stored in that table. This
  872. 44:12column holds that field. This address
  873. 44:15goes to that piece of code. Rails took a
  874. 44:18different approach. It made sensible
  875. 44:21guesses and only asked you to speak up
  876. 44:24when you wanted something different.
  877. 44:27Let's watch those guesses at work. Say
  878. 44:30you're building a small shop and you
  879. 44:32tell Rails you have a kind of thing
  880. 44:34called a product. Without being told,
  881. 44:37Rails assumes the products are stored in
  882. 44:40a database table called products, the
  883. 44:43plural. Each column in that table
  884. 44:46becomes a property of a product
  885. 44:49automatically. If the table has a column
  886. 44:51called price, then every product simply
  887. 44:54has a price. Rails knows English
  888. 44:58plurals, even the awkward ones. Tell it
  889. 45:01about a person and it looks for a table
  890. 45:04called people. Now say each order
  891. 45:07belongs to a customer. By convention,
  892. 45:11the orders table has a column named
  893. 45:14customer _ id. Rails sees that name and
  894. 45:20understands the link on its own. Ask an
  895. 45:23order for its customer and it goes and
  896. 45:26fetches the right one. Then there are
  897. 45:29addresses. One short line in Rails
  898. 45:33declares that products are a resource,
  899. 45:36the very idea from Roy Fielding's
  900. 45:39dissertation two chapters ago. From that
  901. 45:42one line, Rails sets up seven standard
  902. 45:45actions. List all the products, show one
  903. 45:49product, show a form for a new product,
  904. 45:53create it, show a form to edit a
  905. 45:56product, update it, and delete it. One
  906. 46:00line, seven actions, each wired to the
  907. 46:03right address and the right verb.
  908. 46:05Underneath, Rails organized every
  909. 46:08application into three kinds of pieces,
  910. 46:12an arrangement called model, view,
  911. 46:15controller, which traces back to work by
  912. 46:18Trygve Reenskaug at Xerox Park in the
  913. 46:22late 1970s.
  914. 46:24Let's follow one request through them. A
  915. 46:27customer's browser asks for products /
  916. 46:3042. First, the router reads the address
  917. 46:34and sees the pattern, products, then a
  918. 46:37number. That means the show action of
  919. 46:40the products controller with the number
  920. 46:4242. Next, the controller is the
  921. 46:46expediter of this little kitchen. It
  922. 46:49doesn't cook. It asks the model for
  923. 46:51product 42. The model is the part that
  924. 46:55knows about data and rules. It fetches
  925. 46:59row 42 from the products table, a
  926. 47:02teapot, $24.
  927. 47:04Last, the controller hands that teapot
  928. 47:08to the view, a page template with gaps
  929. 47:11in it, and the view fills in the gaps.
  930. 47:14Teapot, $24.
  931. 47:17Add to cart. Router, controller, model,
  932. 47:21view. Every request in every Rails
  933. 47:24application takes the same path. A
  934. 47:28programmer joining a new Rails project
  935. 47:31already knows where everything lives
  936. 47:34because it lives where it always lives.
  937. 47:37In 2005,
  938. 47:39DHH recorded a short screencast, a video
  939. 47:43of his own screen as he worked, in which
  940. 47:46he built a simple blog in about 15
  941. 47:49minutes. Posts, comments, a page to
  942. 47:52write new ones. At several points, as
  943. 47:56the story is usually told, he paused to
  944. 47:59point out all the things he wasn't
  945. 48:01having to do. For programmers who had
  946. 48:04spent weeks wiring up their last
  947. 48:06project, it was startling to watch. The
  948. 48:10video spread quickly, and many people
  949. 48:13later said it was the reason they tried
  950. 48:16Rails at all. Rails reached version 1.0
  951. 48:20in December 2005.
  952. 48:23The following year, Apple announced that
  953. 48:25it would include Rails with the next
  954. 48:28version of its desktop operating system,
  955. 48:31Mac OS X Leopard, which shipped in 2007.
  956. 48:36In the years after, a long line of
  957. 48:38companies started life on Rails. Among
  958. 48:42them, GitHub and Shopify and a small
  959. 48:45messaging service called Twitter, which
  960. 48:47we'll meet again in the next chapter. At
  961. 48:50almost exactly the same time, in a very
  962. 48:53different setting, the same idea was
  963. 48:56being discovered again. It's autumn 2003
  964. 49:00in Lawrence, Kansas, a university town
  965. 49:04on the edge of the prairie. The local
  966. 49:06newspaper is the Lawrence Journal-World,
  967. 49:09and it's unusually ambitious about the
  968. 49:12web. It has a small team of web
  969. 49:14developers working right in the
  970. 49:16newsroom. Two of them are Adrian
  971. 49:19Holovaty, a young journalist who also
  972. 49:22programmed, and Simon Willison, an
  973. 49:25intern who had come over from England.
  974. 49:28They were building sites for the paper
  975. 49:30in the Python language, and they were
  976. 49:33building them on newspaper time. A
  977. 49:35newsroom runs on deadlines. A big local
  978. 49:39story breaks in the afternoon, and the
  979. 49:42editors want a page with a searchable
  980. 49:44database of results or a map or a
  981. 49:48collection of readers' photos live by
  982. 49:51the evening edition. There's no room for
  983. 49:54weeks of setup. So, like DHH, Holovaty
  984. 49:58and Willison found themselves writing
  985. 50:01the same plumbing again and again and
  986. 50:04pulled it out into a framework of their
  987. 50:06own. Another developer, Jacob
  988. 50:09Kaplan-Moss, joined the team soon after.
  989. 50:12In July 2005,
  990. 50:15the newspaper released it to the world
  991. 50:17free. Holovaty plays jazz guitar in the
  992. 50:21swinging gypsy jazz style, and he named
  993. 50:25the framework after the most famous
  994. 50:27player of that style, the Belgian-born
  995. 50:30Romani guitarist Django Reinhardt. So,
  996. 50:34the framework was called Django. Its
  997. 50:36tagline described it as the web
  998. 50:39framework for perfectionists with
  999. 50:42deadlines. Django shared many of Rails's
  1000. 50:45ideas, models and templates and tidy
  1001. 50:49addresses, but it had one feature that
  1002. 50:52came straight from the newsroom, and it
  1003. 50:54amazed people when they first saw it,
  1004. 50:57the admin. In a newspaper, the people
  1005. 51:00who enter the
  1006. 51:02aren't programmers. They're reporters,
  1007. 51:05editors, and clerks. So Django could
  1008. 51:08look at the models you described and
  1009. 51:10build a complete editing screen for them
  1010. 51:13automatically with no extra work. Say
  1011. 51:16the paper wants a page listing local
  1012. 51:19restaurant inspections. A developer
  1013. 51:22describes one model, an inspection with
  1014. 51:25five fields, the restaurant's name, its
  1015. 51:29address, the date, the score, and the
  1016. 51:32inspector's notes. That's all. From that
  1017. 51:35single description, Django's admin
  1018. 51:38produces a list of every inspection with
  1019. 51:41search and filters, a form to add a new
  1020. 51:44one that checks each field is filled in
  1021. 51:47properly, a form to edit, and a way to
  1022. 51:50delete. A reporter could start typing in
  1023. 51:53inspection results that same afternoon
  1024. 51:56while the developer got on with the
  1025. 51:58public page. Django spread from
  1026. 52:01newsrooms into everywhere else. By 2008,
  1027. 52:05it had its own independent foundation to
  1028. 52:08look after it. And over the years, it
  1029. 52:11carried some very large sites, including
  1030. 52:14Instagram in its early days. Rails and
  1031. 52:18Django had the same deeper effect. They
  1032. 52:21turned the back end from a craft that
  1033. 52:24every team reinvented into a set of good
  1034. 52:27habits that came in the box. Validate
  1035. 52:30the input, keep the rules in the model,
  1036. 52:33make database changes in small recorded
  1037. 52:37steps, so every copy of the application
  1038. 52:40can be brought up to date the same way.
  1039. 52:43Follow a predictable shape so the next
  1040. 52:46person can find their way around. They
  1041. 52:49didn't make back end engineering easy,
  1042. 52:52but they made the first version of an
  1043. 52:54idea fast. A small team could go from a
  1044. 52:57sketch on a napkin to a working site in
  1045. 53:01days. And sometimes that worked a little
  1046. 53:04too well. A small site built in days
  1047. 53:08could find itself a year or two later
  1048. 53:12with millions of people pressing its
  1049. 53:14buttons at once. The framework would
  1050. 53:16still route the requests and fill in the
  1051. 53:19templates exactly as designed, but one
  1052. 53:23server or even a handful could no longer
  1053. 53:27keep up. In 2008, one of the most talked
  1054. 53:31about sites on the internet kept falling
  1055. 53:34over. And each time it did, its visitors
  1056. 53:37were shown the same small, cheerful
  1057. 53:40drawing.
  1058. 53:43Chapter Five, Remembering Nothing. It's
  1059. 53:46the spring of 2008 and a drawing of a
  1060. 53:50whale is about to become famous. A young
  1061. 53:53designer named Yi Ying Lu had made the
  1062. 53:57picture as a greeting for friends who
  1063. 54:00lived far away. It shows a large,
  1064. 54:03smiling whale being lifted out of the
  1065. 54:06sea by a flock of small orange birds,
  1066. 54:10eight of them. She called it Lifting a
  1067. 54:12Dreamer, and she posted it on a website
  1068. 54:16where artists sell their images for
  1069. 54:18other people to use. Somewhere on the
  1070. 54:21other side of that website, one of the
  1071. 54:24founders of Twitter, Biz Stone, was
  1072. 54:27looking for something to show people
  1073. 54:29when the service was overloaded. He
  1074. 54:31found the whale. Around May of 2008,
  1075. 54:35Twitter began to use it. And that year,
  1076. 54:39people saw it a great deal. Twitter was
  1077. 54:41only two years old, a service for
  1078. 54:44posting short messages to anyone who
  1079. 54:47chose to follow you. It had grown far
  1080. 54:50faster than anyone had planned. Every
  1081. 54:53time a big event happened, a keynote, a
  1082. 54:56sports final, a piece of breaking news,
  1083. 54:59huge numbers of people rushed to post
  1084. 55:02about it at the same moment. And again
  1085. 55:05and again, the site couldn't keep up.
  1086. 55:09Instead of your messages, you'd see the
  1087. 55:11whale held aloft by its little birds and
  1088. 55:15a short note saying Twitter was over
  1089. 55:19capacity. Users gave it a nickname. They
  1090. 55:22called it the fail whale. It turned up
  1091. 55:25on t-shirts and in jokes, and it
  1092. 55:28gathered a fan following of its own.
  1093. 55:31It's one of the very few error messages
  1094. 55:34people have ever felt fond of. Behind
  1095. 55:37that gentle drawing was a hard
  1096. 55:39engineering problem, and it's one almost
  1097. 55:42every successful back end meets sooner
  1098. 55:45or later. The problem is how to handle
  1099. 55:4810 times as many people as last month,
  1100. 55:52and then 10 times that. There are two
  1101. 55:55basic answers. The first is to buy a
  1102. 55:58bigger machine, more processors, more
  1103. 56:02memory, faster disks. Engineers call
  1104. 56:05this scaling up or vertical scaling.
  1105. 56:08It's simple, and for a while it works
  1106. 56:11beautifully, but there's always a
  1107. 56:13biggest machine you can buy, and the
  1108. 56:16price climbs steeply as you approach it.
  1109. 56:19Worse, if that one machine fails,
  1110. 56:23everything fails with it. The second
  1111. 56:25answer is to use more machines side by
  1112. 56:29side. Engineers call this scaling out or
  1113. 56:34horizontal scaling. In a kitchen, the
  1114. 56:37first answer is like hiring one
  1115. 56:39superhuman cook who chops twice as fast.
  1116. 56:43The second is hiring more cooks.
  1117. 56:46Superhuman cooks are rare and expensive,
  1118. 56:50and now and then they fall ill. Ordinary
  1119. 56:53cooks are easy to find, and if one goes
  1120. 56:56home sick, the others keep cooking.
  1121. 56:59Almost every large back end in the world
  1122. 57:02today scales out, but scaling out only
  1123. 57:05works if the cooks are interchangeable.
  1124. 57:08Any order must be able to go to any
  1125. 57:11cook. And that leads to one of the most
  1126. 57:14important habits in all of back end
  1127. 57:17engineering. The servers should remember
  1128. 57:19nothing. Let's see why. Say you're
  1129. 57:22shopping on a site that runs on two
  1130. 57:25servers, A and B. You add a kettle to
  1131. 57:28your cart, and that request happens to
  1132. 57:31go to server A, which writes kettle in a
  1133. 57:34note in its own memory. Your next click
  1134. 57:38to add a rug happens to go to server B.
  1135. 57:41Server B has never heard of you. It has
  1136. 57:44no note. As far as it knows, your cart
  1137. 57:48is empty. You could try to fix this by
  1138. 57:50always sending the same customer to the
  1139. 57:53same server. Engineers call that sticky
  1140. 57:56sessions, and it's sometimes used, but
  1141. 58:00it's fragile. If server A needs a
  1142. 58:03restart, every cart it was holding is
  1143. 58:06lost. And if one server ends up with a
  1144. 58:09crowd of very busy customers, it can't
  1145. 58:12hand any of them off to its quieter
  1146. 58:14neighbor. The cleaner answer is to make
  1147. 58:17every server stateless. The server keeps
  1148. 58:21nothing about you between requests.
  1149. 58:24Anything that needs to be remembered,
  1150. 58:26your cart, your login, your half
  1151. 58:29finished order goes into a shared store
  1152. 58:33that every server can reach, usually a
  1153. 58:36database or a fast shared memory of the
  1154. 58:39kind we'll meet in the next chapter. If
  1155. 58:42you heard our lecture on web
  1156. 58:44development, you'll remember the
  1157. 58:46session, the number on a slip of paper
  1158. 58:49matched to a notebook behind the
  1159. 58:51counter. A stateless back end simply
  1160. 58:55moves that notebook out from behind any
  1161. 58:58one counter and puts it in a back office
  1162. 59:01where every counter can read it. Now
  1163. 59:04each request carries what it needs, its
  1164. 59:07session number, its token, the details
  1165. 59:10of what it wants. Any server can pick it
  1166. 59:13up, look up what it needs in the shared
  1167. 59:16store, do the work, and forget all about
  1168. 59:19it. It's the fourth of Roy Fielding's
  1169. 59:22ideas from earlier tonight put to work.
  1170. 59:25Each request should carry everything
  1171. 59:27needed to understand it. The servers
  1172. 59:30become like cooks working from tickets.
  1173. 59:33Any cook can pick up any ticket because
  1174. 59:36everything about the order is written on
  1175. 59:39it or in the shared order book on the
  1176. 59:41wall. No cook needs to remember who
  1177. 59:44ordered what an hour ago. Once the
  1178. 59:47servers are interchangeable, you need
  1179. 59:50someone at the door to share out the
  1180. 59:52work. That's the load balancer. A load
  1181. 59:55balancer is a program or sometimes a
  1182. 59:59dedicated device that sits in front of
  1183. 1:00:02all the servers. Every request from the
  1184. 1:00:05outside world arrives at the load
  1185. 1:00:07balancer first. It chooses a server,
  1186. 1:00:11passes the request along, and passes the
  1187. 1:00:14answer back. To the customer, it all
  1188. 1:00:17looks like one single, very capable
  1189. 1:00:21machine. It's like the host at the front
  1190. 1:00:23of a busy restaurant who greets each
  1191. 1:00:26party and seats them in a different
  1192. 1:00:29waiter's section so that no single
  1193. 1:00:31waiter is buried while another stands
  1194. 1:00:34idle. The simplest way to choose is to
  1195. 1:00:38take turns. First, request to server
  1196. 1:00:41one, next to server two, next to server
  1197. 1:00:44three, and round again. That's called
  1198. 1:00:47round robin. A slightly smarter way is
  1199. 1:00:51to send each new request to whichever
  1200. 1:00:54server is currently handling the fewest.
  1201. 1:00:57Whatever the method, the load balancer
  1202. 1:00:59also keeps checking on its server's
  1203. 1:01:02health and stops sending work to any
  1204. 1:01:04that aren't answering. And because the
  1205. 1:01:07load balancer is now the one door that
  1206. 1:01:10every request passes through, it's
  1207. 1:01:12usually set up as a pair. If one of the
  1208. 1:01:15pair fails, its twin takes over within
  1209. 1:01:19moments, and the customers waiting
  1210. 1:01:21outside never know there was a change of
  1211. 1:01:24host. Let's put some numbers on this.
  1212. 1:01:28Say a shop's back end receives 3,000
  1213. 1:01:31requests every second at its busiest
  1214. 1:01:34time of day. And say one server can
  1215. 1:01:37comfortably handle 500 requests a second
  1216. 1:01:41before it starts to slow down. 3,000
  1217. 1:01:44divided by 500 is 6. So six servers in
  1218. 1:01:49theory. But running every server flat
  1219. 1:01:52out leaves no room for surprises, a
  1220. 1:01:55sudden rush, a slow database, a server
  1221. 1:01:59that falls over. So engineers usually
  1222. 1:02:02aim to run each server at a comfortable
  1223. 1:02:05fraction of its limit, say 60%. That's
  1224. 1:02:09300 requests a second per server. 3,000
  1225. 1:02:14divided by 300 is 10. So the shop runs
  1226. 1:02:1810 servers. Now one server fails. Its
  1227. 1:02:22power supply gives out or its software
  1228. 1:02:25freezes. The load balancer has been
  1229. 1:02:27checking each server every 5 seconds
  1230. 1:02:31with a small request that amounts to,
  1231. 1:02:34are you alive? After three checks in a
  1232. 1:02:36row with no answer, it decides the
  1233. 1:02:39server is gone. That takes about 15
  1234. 1:02:42seconds. During those seconds, some
  1235. 1:02:46requests sent to the broken server fail
  1236. 1:02:49and are tried again. Then the load
  1237. 1:02:52balancer simply stops sending it
  1238. 1:02:54anything. The 3,000 requests a second
  1239. 1:02:58are now shared among nine servers
  1240. 1:03:01instead of 10. That's about 333
  1241. 1:03:05each, two-thirds of what a server can
  1242. 1:03:08handle. Everything keeps working.
  1243. 1:03:11Customers might notice nothing at all.
  1244. 1:03:14Meanwhile, an automatic system notices
  1245. 1:03:17that the remaining servers are busier
  1246. 1:03:19than usual and starts a fresh one to
  1247. 1:03:22replace the lost one. A couple of
  1248. 1:03:25minutes later, there are ten again. No
  1249. 1:03:28one had to wake up. That's the quiet
  1250. 1:03:30gift of stateless servers because none
  1251. 1:03:34of them holds anything precious, any one
  1252. 1:03:36of them can be thrown away and replaced
  1253. 1:03:39at any moment like a paper cup. This
  1254. 1:03:42also makes growth almost mechanical. If
  1255. 1:03:46the shop's busiest moment doubles next
  1256. 1:03:49year to 6,000 requests a second, the
  1257. 1:03:53answer is 20 servers instead of 10. Many
  1258. 1:03:56systems now do this automatically
  1259. 1:03:59through the day, adding servers as the
  1260. 1:04:02morning rush arrives and removing them
  1261. 1:04:04again at night. Statelessness doesn't
  1262. 1:04:07make the hard problems disappear though.
  1263. 1:04:10It moves them. All that remembering now
  1264. 1:04:13happens in the shared stores, the
  1265. 1:04:16databases and caches, and those have to
  1266. 1:04:19be fast and reliable in their own right.
  1267. 1:04:22Much of the rest of back-end engineering
  1268. 1:04:25is about looking after them. For
  1269. 1:04:27Twitter, the road out of the fail whale
  1270. 1:04:30years was long. Over the following
  1271. 1:04:33years, its engineers rebuilt large parts
  1272. 1:04:36of the system, spreading the work across
  1273. 1:04:39many more machines and separating out
  1274. 1:04:42the pieces that were under the most
  1275. 1:04:44strain. Bit by bit, the whale appeared
  1276. 1:04:47less often and eventually it was retired
  1277. 1:04:51altogether. Yi Ying Lu's drawing
  1278. 1:04:54outlived its job. It became a small
  1279. 1:04:57piece of internet history, a reminder
  1280. 1:05:00that even the busiest services began as
  1281. 1:05:04a few servers struggling to keep up and
  1282. 1:05:07that a little kindness on an error page
  1283. 1:05:10can turn frustration into affection. But
  1284. 1:05:13there was one lesson in all of this that
  1285. 1:05:16every growing site learned, Twitter
  1286. 1:05:18included. When you look closely at what
  1287. 1:05:21those overloaded servers were actually
  1288. 1:05:23doing, a surprising amount of their work
  1289. 1:05:27was the same work repeated. The same
  1290. 1:05:30popular profile fetched from the
  1291. 1:05:32database 10,000 times in a minute. The
  1292. 1:05:36same front page rebuilt from scratch for
  1293. 1:05:40every single visitor. When the answer
  1294. 1:05:43hasn't changed, there's no need to work
  1295. 1:05:46it out again. You can keep it close at
  1296. 1:05:48hand instead, on a shelf you can reach
  1297. 1:05:52without moving your feet. In 2009
  1298. 1:05:55in Sicily, a programmer who was
  1299. 1:05:58struggling to keep up with a flood of
  1300. 1:06:00small writes decided to build exactly
  1301. 1:06:03that kind of shelf.
  1302. 1:06:07Chapter six, the fast shelf. It's early
  1303. 1:06:112009
  1304. 1:06:12in Catania, on the eastern coast of
  1305. 1:06:15Sicily, where the city sits at the foot
  1306. 1:06:18of Mount Etna and the volcano's long
  1307. 1:06:21slope fills the horizon. A programmer
  1308. 1:06:25named Salvatore Sanfilippo, known online
  1309. 1:06:29by the nickname Antirez, is working on a
  1310. 1:06:32small startup. One of its products is a
  1311. 1:06:35tool called Loog. Website owners put a
  1312. 1:06:39tiny snippet on their pages and Loog
  1313. 1:06:42shows them live who is visiting right
  1314. 1:06:46now, which page each visitor is on,
  1315. 1:06:49where they came from, what they click
  1316. 1:06:51next, scrolling past on the screen as it
  1317. 1:06:55happens. It's a charming idea and it has
  1318. 1:06:58an awkward property. Every single page
  1319. 1:07:01view on every site that uses it has to
  1320. 1:07:05be written down immediately. Thousands
  1321. 1:07:08of tiny writes all day long and readers
  1322. 1:07:11who want to see the latest ones at once.
  1323. 1:07:14Loog kept its data in a conventional
  1324. 1:07:17database, MySQL, which stores things on
  1325. 1:07:21disk, and disks, even good ones, are
  1326. 1:07:25slow compared with a computer's memory.
  1327. 1:07:28Each small write had to wait its turn on
  1328. 1:07:31the disk. As more sites signed up, the
  1329. 1:07:34database fell further and further
  1330. 1:07:37behind. San Filippo tried the usual
  1331. 1:07:40remedies, tuning and indexing and adding
  1332. 1:07:43hardware, and none of them really
  1333. 1:07:45changed the picture. The problem was the
  1334. 1:07:48shape of the tool. He was asking a
  1335. 1:07:51careful, disk-based filing cabinet to
  1336. 1:07:54behave like a whiteboard, so he built a
  1337. 1:07:57whiteboard. His first prototype was only
  1338. 1:08:00a few hundred lines long, written in a
  1339. 1:08:03small scripting language called TCL. It
  1340. 1:08:07kept all of its data in memory, where
  1341. 1:08:10reading and writing take a tiny fraction
  1342. 1:08:13of the time a disk needs. And it didn't
  1343. 1:08:16just store simple values. It understood
  1344. 1:08:19lists, so Logue could push each new page
  1345. 1:08:23view onto the front of a list and trim
  1346. 1:08:25the old ones off the back. It worked. He
  1347. 1:08:29rewrote it in the faster language C, and
  1348. 1:08:32in February 2009
  1349. 1:08:35he shared it with the world as a free,
  1350. 1:08:38open project. He called it Redis, short
  1351. 1:08:42for Remote Dictionary Server. A
  1352. 1:08:45dictionary in programming is just a
  1353. 1:08:48collection of labels and values. Look up
  1354. 1:08:51a label, get back its value. The label
  1355. 1:08:55might be user 404
  1356. 1:08:58profile, and the value might be that
  1357. 1:09:01user's name, photo address, and bio.
  1358. 1:09:04Remote means the dictionary lives on its
  1359. 1:09:08own server, where many other programs
  1360. 1:09:10can reach it over the network. So Redis
  1361. 1:09:14is a big shared dictionary kept in
  1362. 1:09:16memory and very fast. At first it was a
  1363. 1:09:20side project from Sicily. About a A
  1364. 1:09:23later, a large software company, VMware,
  1365. 1:09:27hired San Filippo to work on it
  1366. 1:09:30full-time, and Redis went on to become
  1367. 1:09:33one of the most widely used pieces of
  1368. 1:09:36back-end software in the world. He
  1369. 1:09:38stepped back from leading it in 2020 and
  1370. 1:09:42returned to work on it again at the end
  1371. 1:09:45of 2024.
  1372. 1:09:47It wasn't the first tool of its kind. If
  1373. 1:09:50you heard our lecture on database
  1374. 1:09:52engineering, you'll remember Memcached
  1375. 1:09:55D, written in 2003
  1376. 1:09:58to keep a busy blogging site from
  1377. 1:10:00drowning. Memcached was a plain shared
  1378. 1:10:04memory for simple values. Redis took the
  1379. 1:10:07same idea and gave it richer shapes and
  1380. 1:10:11the ability to save a copy of itself to
  1381. 1:10:14disk so it could come back after a
  1382. 1:10:17restart. To see why a shared memory like
  1383. 1:10:20this matters so much, let's go back into
  1384. 1:10:23the kitchen. In a busy restaurant
  1385. 1:10:25kitchen, the main stores are in a
  1386. 1:10:28walk-in cooler down the hall. Everything
  1387. 1:10:31is there, carefully labeled, kept at the
  1388. 1:10:34right temperature, but walking to the
  1389. 1:10:36cooler takes time. If a cook went to the
  1390. 1:10:39cooler for every pinch of chopped
  1391. 1:10:42parsley, the dinner service would grind
  1392. 1:10:45to a halt. So before service, each cook
  1393. 1:10:48fills a small shelf right beside their
  1394. 1:10:51station. Chopped herbs, sauces, butter,
  1395. 1:10:56the things they reach for again and
  1396. 1:10:58again. Most of the time, what they need
  1397. 1:11:02is right there, within arms reach. Only
  1398. 1:11:05now and then do they walk down the hall.
  1399. 1:11:09The walk-in cooler is the database. The
  1400. 1:11:12little shelf is the cache. A cache is a
  1401. 1:11:16place to keep copies of answers you're
  1402. 1:11:18likely to need again, somewhere much
  1403. 1:11:21faster to reach than the original.
  1404. 1:11:24Backends use Redis as a cache more than
  1405. 1:11:27for anything else. The pattern goes like
  1406. 1:11:30this. When a request needs a piece of
  1407. 1:11:33data, the back end checks the cache
  1408. 1:11:36first. If the answer is there, that's
  1409. 1:11:39called a hit, and the back end uses it
  1410. 1:11:42straight away. If it isn't, that's
  1411. 1:11:44called a miss. The back end goes to the
  1412. 1:11:47database, gets the answer, puts a copy
  1413. 1:11:51on the shelf for next time, and carries
  1414. 1:11:53on. Let's put some numbers on that. Say
  1415. 1:11:57a shop's back end needs to look up
  1416. 1:12:00product details 10,000 times a second,
  1417. 1:12:03and say a trip to the database takes
  1418. 1:12:06about 10,000ths of a second, while a
  1419. 1:12:09trip to the cache takes about half of
  1420. 1:12:121/1000th. Without a cache, all 10,000
  1421. 1:12:17lookups go to the database every second,
  1422. 1:12:20each taking 10,000ths of a second. Now
  1423. 1:12:24add a cache and say 95% of lookups are
  1424. 1:12:28hits. Then 9,500
  1425. 1:12:31lookups a second are answered from the
  1426. 1:12:34shelf. Only 500 a second go to the
  1427. 1:12:37database. The database's load has
  1428. 1:12:40dropped from 10,000 to 500, a 20th of
  1429. 1:12:44what it was. The average time per lookup
  1430. 1:12:47drops too. 95% of lookups take half a
  1431. 1:12:51thousandth of a second. The other 5%
  1432. 1:12:55first check the cache, find nothing, and
  1433. 1:12:59then go to the database. So they take 10
  1434. 1:13:02and 1/2 thousandths. Blend those
  1435. 1:13:04together, and the average comes to
  1436. 1:13:07almost exactly 1/1000th of a second,
  1437. 1:13:101/1000th instead of 10, 10 times faster
  1438. 1:13:14on average. With a database that's
  1439. 1:13:17barely breaking a sweat, the hit rate
  1440. 1:13:20matters more than you might expect. If
  1441. 1:13:23it slips from 95% to 80, the database's
  1442. 1:13:27load goes from 500 lookups a second to
  1443. 1:13:312000, four times as much, from a drop of
  1444. 1:13:3515 percentage points. That's why back
  1445. 1:13:38end engineers watch their hit rates
  1446. 1:13:41closely. a catch, of course, and it's
  1447. 1:13:44the one every cache brings with it. A
  1448. 1:13:47copy can go out of date. Say the shop
  1449. 1:13:50drops the price of its teapot from $24
  1450. 1:13:54to 20. The database now says 20, but the
  1451. 1:13:58cache still holds a copy that says 24.
  1452. 1:14:03Until that copy is replaced, some
  1453. 1:14:05customers will be shown the old price.
  1454. 1:14:09Removing or refreshing stale copies is
  1455. 1:14:12called cache invalidation, and back ends
  1456. 1:14:15use two main approaches, usually
  1457. 1:14:18together. The first is to give every
  1458. 1:14:21copy an expiry time. When the back end
  1459. 1:14:25puts the teapot on the shelf, it says,
  1460. 1:14:28keep this for 60 seconds. After that,
  1461. 1:14:31Redis quietly throws it away, and the
  1462. 1:14:34next lookup goes to the database and
  1463. 1:14:37fetches the fresh price. So a stale copy
  1464. 1:14:40can never live longer than a minute. The
  1465. 1:14:43second is to clear the shelf
  1466. 1:14:45deliberately. When the price changes in
  1467. 1:14:48the database, the code that changed it
  1468. 1:14:51also deletes the teapot from the cache.
  1469. 1:14:55The very next lookup misses and fetches
  1470. 1:14:58$20.
  1471. 1:15:00Even with both, there can be brief
  1472. 1:15:02moments when the shelf and the cooler
  1473. 1:15:05disagree. So careful back ends follow a
  1474. 1:15:08simple rule. Use the cache for showing
  1475. 1:15:12and the database for deciding. The
  1476. 1:15:15product page can show a price from the
  1477. 1:15:18cache, but at checkout, when real money
  1478. 1:15:21changes hands, the back end looks the
  1479. 1:15:24price up in the database itself. That's
  1480. 1:15:27the validation step from our coffee
  1481. 1:15:30order in the first chapter. A good
  1482. 1:15:32kitchen doesn't let a sticky note on the
  1483. 1:15:35shelf write the bill. Redis does more
  1484. 1:15:39than keep copies. Because it understands
  1485. 1:15:42shapes of data, it gets used for all
  1486. 1:15:45sorts of jobs where speed matters. It
  1487. 1:15:48can be the shared notebook from the last
  1488. 1:15:51chapter, holding every customer's
  1489. 1:15:53session so that any server can find it.
  1490. 1:15:57It can count things. Add one to the
  1491. 1:15:59number of likes on this photo. Add one
  1492. 1:16:03to the number of requests from this
  1493. 1:16:05customer in the last minute, which, as
  1494. 1:16:08we'll see later tonight, is how many
  1495. 1:16:11back ends decide when someone is asking
  1496. 1:16:14too often. And it keeps something called
  1497. 1:16:16a sorted set, a collection that stays in
  1498. 1:16:20order by score all the time, however
  1499. 1:16:23often it changes. Say an online game has
  1500. 1:16:27a million players, each with a score. A
  1501. 1:16:30player finishes a round and their score
  1502. 1:16:33rises from 820
  1503. 1:16:35to 905.
  1504. 1:16:37Redis slides them into their new place
  1505. 1:16:40in the ranking straight away. Asking for
  1506. 1:16:43the top 10 players or for one player's
  1507. 1:16:46exact rank out of a million takes a
  1508. 1:16:49moment because the order is already kept
  1509. 1:16:52rather than worked out from scratch each
  1510. 1:16:55time. That's the general gift of Redis.
  1511. 1:16:58It takes the structures a programmer
  1512. 1:17:01already thinks in, lists and counters
  1513. 1:17:04and rankings, and makes them shared and
  1514. 1:17:07fast for every server in the kitchen.
  1515. 1:17:10And there's something fitting about
  1516. 1:17:12where it began. Its very first job was
  1517. 1:17:15to hold a stream of events as they
  1518. 1:17:18happened. Page views arriving one after
  1519. 1:17:22another, too fast for a filing cabinet
  1520. 1:17:25to keep up. Not every job in a back end
  1521. 1:17:28needs to be fast, though. Some only need
  1522. 1:17:31to happen eventually and reliably
  1523. 1:17:34without making anyone wait for them.
  1524. 1:17:37When you sign up for a new account,
  1525. 1:17:40someone has to send you a welcome email.
  1526. 1:17:43When you upload a photo, someone has to
  1527. 1:17:45make smaller copies of it for different
  1528. 1:17:48screens. When a shop takes 10,000 orders
  1529. 1:17:52before lunch, someone has to print
  1530. 1:17:5410,000 labels. None of that should
  1531. 1:17:57happen while you stand at the counter.
  1532. 1:18:00It should go on a list to be done
  1533. 1:18:03properly in order by someone in the
  1534. 1:18:06back. Anyone who has waited at a busy
  1535. 1:18:09deli counter already knows how that list
  1536. 1:18:12works. You pull a paper ticket from a
  1537. 1:18:15little red machine and you wait for your
  1538. 1:18:18number to be called.
  1539. 1:18:22Chapter seven, take a number. It's 1909
  1540. 1:18:26in Copenhagen and a quiet mathematician
  1541. 1:18:30named Agner Krarup Erlang is watching
  1542. 1:18:33telephone calls. He works for the
  1543. 1:18:36Copenhagen Telephone Company. The city's
  1544. 1:18:39telephone exchanges are rooms full of
  1545. 1:18:42switchboards with operators who plug
  1546. 1:18:45cables into sockets to connect one
  1547. 1:18:47caller to another. The company has a
  1548. 1:18:50practical question and it's an expensive
  1549. 1:18:53one. It needs to know how many lines and
  1550. 1:18:56how many operators an exchange really
  1551. 1:18:59requires. Too few and callers hear
  1552. 1:19:02nothing but a busy signal or wait and
  1553. 1:19:05wait for an operator to answer. Too many
  1554. 1:19:09and the company is paying for equipment
  1555. 1:19:11and people who sit idle most of the day.
  1556. 1:19:14Calls don't arrive on a timetable. They
  1557. 1:19:17come in random clumps, 10 in one minute,
  1558. 1:19:21two in the next, 15 in the one after.
  1559. 1:19:25Erlang set out to describe that
  1560. 1:19:27randomness with mathematics and in 1909
  1561. 1:19:31he published a paper on the theory of
  1562. 1:19:34probabilities and telephone
  1563. 1:19:36conversations. Over the following years,
  1564. 1:19:39his work became the foundation of a
  1565. 1:19:42whole field, the study of waiting lines,
  1566. 1:19:45which mathematicians call queuing
  1567. 1:19:48theory. Telephone engineers honored him
  1568. 1:19:51by naming their basic unit of traffic,
  1569. 1:19:54the Erlang, and much later, a
  1570. 1:19:57programming language built at the
  1571. 1:19:59Swedish telephone company Ericsson for
  1572. 1:20:02systems that must juggle huge numbers of
  1573. 1:20:05simultaneous calls was named after him
  1574. 1:20:08too. Keep that language in mind. It will
  1575. 1:20:12turn up again in a moment. A queue is
  1576. 1:20:15about the most ordinary thing in the
  1577. 1:20:17world. You've stood in hundreds of them.
  1578. 1:20:20At a busy deli counter, there's often a
  1579. 1:20:23little red machine by the door that
  1580. 1:20:26dispenses paper tickets with numbers on
  1581. 1:20:29them. You pull a ticket. You're number
  1582. 1:20:3147. Above the counter, a display says
  1583. 1:20:3539. And then something nice happens. You
  1584. 1:20:40don't have to stand in a line. You can
  1585. 1:20:42wander off and look at the cheeses. When
  1586. 1:20:46your number is called, you step up and
  1587. 1:20:49someone serves you. That ticket machine
  1588. 1:20:52separates two things that would
  1589. 1:20:54otherwise be tangled together. The
  1590. 1:20:57moment you ask for something and the
  1591. 1:20:59moment someone does it, back ends use
  1592. 1:21:02exactly the same trick, and it's one of
  1593. 1:21:05the most useful ideas in the whole
  1594. 1:21:07craft. Remember in the first chapter
  1595. 1:21:11that our coffee order wasn't finished
  1596. 1:21:14when the green tick appeared. There was
  1597. 1:21:16a receipt to email, loyalty points to
  1598. 1:21:19add, the barista's screen to update. A
  1599. 1:21:23good back end doesn't do those jobs
  1600. 1:21:25while you wait. It writes each one on a
  1601. 1:21:28ticket and puts it in a queue. Separate
  1602. 1:21:31programs, called workers, take tickets
  1603. 1:21:34from the queue one at a time and do the
  1604. 1:21:37work. If the queue gets long, you add
  1605. 1:21:40more workers, like opening another till
  1606. 1:21:43at the counter. The program that puts
  1607. 1:21:46tickets in the queue is called the
  1608. 1:21:48producer. The programs that take them
  1609. 1:21:50out are called consumers, and in between
  1610. 1:21:54sits a piece of software whose only job
  1611. 1:21:57is to hold the tickets safely and hand
  1612. 1:22:00them out in order. It's called a message
  1613. 1:22:03broker. For a long time, message brokers
  1614. 1:22:07were expensive commercial products and
  1615. 1:22:09everyone spoke its own private language.
  1616. 1:22:13That bothered a man named John O'Hara,
  1617. 1:22:16who worked on technology at the bank JP
  1618. 1:22:19Morgan Chase in London. Banks are full
  1619. 1:22:22of messages, trades, payments, prices,
  1620. 1:22:26confirmations, all flowing between
  1621. 1:22:29systems that must never lose a single
  1622. 1:22:32one. In 2003,
  1623. 1:22:34O'Hara began work on an open standard
  1624. 1:22:38for message brokers so that any program
  1625. 1:22:41could talk to any broker in the same way
  1626. 1:22:44and no bank would be tied to one
  1627. 1:22:46supplier forever. It became the Advanced
  1628. 1:22:50Message Queuing Protocol or AMQP, a
  1629. 1:22:54standard needs software that speaks it.
  1630. 1:22:57In 2007,
  1631. 1:22:59a small joint venture between two
  1632. 1:23:01companies, the London software firm L
  1633. 1:23:05Shift and a firm called Cohesive FT,
  1634. 1:23:09released a free message broker that
  1635. 1:23:12spoke AMQP. They called it RabbitMQ
  1636. 1:23:16and they wrote it in Erlang, the
  1637. 1:23:18telephone language named for the
  1638. 1:23:20mathematician from Copenhagen. A
  1639. 1:23:23language designed for juggling vast
  1640. 1:23:25numbers of calls without dropping any
  1641. 1:23:28turned out to be a natural fit for
  1642. 1:23:30juggling messages. Let's watch a queue
  1643. 1:23:33earn its keep. Say a new app runs a
  1644. 1:23:36television advertisement and in the next
  1645. 1:23:39few minutes, 10,000 people sign up.
  1646. 1:23:43Every one of them should get a welcome
  1647. 1:23:45email. Sending an email means talking to
  1648. 1:23:48an email service across the internet,
  1649. 1:23:51and say that takes about a fifth of a
  1650. 1:23:53second. Without a queue, the sign up
  1651. 1:23:56request itself would send the email
  1652. 1:23:59before answering. Each new user would
  1653. 1:24:02wait an extra fifth of a second, which
  1654. 1:24:05isn't terrible, but if the email service
  1655. 1:24:07has a bad moment and takes 30 seconds to
  1656. 1:24:11reply, every sign up takes 30 seconds.
  1657. 1:24:14If the email service goes down
  1658. 1:24:16altogether, nobody can sign up at all. A
  1659. 1:24:20problem in a small side job has broken
  1660. 1:24:23the front door. With a queue, the sign
  1661. 1:24:25up request does only the essential work.
  1662. 1:24:28It checks the details, stores the new
  1663. 1:24:31account, and drops a small ticket in the
  1664. 1:24:34queue that says send a welcome email to
  1665. 1:24:37this person. Then it answers in perhaps
  1666. 1:24:40a 20th of a second. The new user is in.
  1667. 1:24:44Meanwhile, the workers chew through the
  1668. 1:24:47tickets. 10,000 emails at a fifth of a
  1669. 1:24:50second each is 2,000 seconds of sending.
  1670. 1:24:54If a single worker did them all, one
  1671. 1:24:57after another, the last person would get
  1672. 1:25:00their welcome email about 33 minutes
  1673. 1:25:03after signing up. With 20 workers
  1674. 1:25:06sharing the queue, each takes its own
  1675. 1:25:09share, and the whole pile is cleared in
  1676. 1:25:12about 100 seconds, a minute and 40
  1677. 1:25:15seconds. And if the email service goes
  1678. 1:25:18down for 10 minutes in the middle of it
  1679. 1:25:21all, nothing is lost. The tickets simply
  1680. 1:25:24wait in the queue. When the service
  1681. 1:25:27comes back, the workers pick up where
  1682. 1:25:29they left off. In this way, a queue
  1683. 1:25:33works like a reservoir behind a dam.
  1684. 1:25:36Water may rush in after a storm, but it
  1685. 1:25:39flows out through the turbines at a
  1686. 1:25:42steady rate. The workers never feel the
  1687. 1:25:45storm. They only see a pile of tickets
  1688. 1:25:48and they work through it at their own
  1689. 1:25:50pace. Erlang's mathematics has something
  1690. 1:25:53important to say here too. Say the
  1691. 1:25:56signups settle down to a steady 100
  1692. 1:26:00every second all day. Each worker can
  1693. 1:26:04send five emails a second. To keep up,
  1694. 1:26:07you need at least 20 workers because 20
  1695. 1:26:10* 5 is 100. If you run only 15 workers,
  1696. 1:26:15they can handle 75 a second. 25 more
  1697. 1:26:19arrive than leave every second and the
  1698. 1:26:23queue grows without stopping. After 10
  1699. 1:26:26minutes, 15,000 tickets are waiting. But
  1700. 1:26:30here's the subtler lesson and it's the
  1701. 1:26:32one Erlang's descendants spelled out.
  1702. 1:26:36Even when the workers can just about
  1703. 1:26:38keep up, waiting times don't grow
  1704. 1:26:41gently. They climb steeply as the
  1705. 1:26:44workers get close to fully busy. In the
  1706. 1:26:47simplest model of a single worker with
  1707. 1:26:51random arrivals, the average time a
  1708. 1:26:54ticket spends waiting in line is the
  1709. 1:26:57time a job takes multiplied by a ratio.
  1710. 1:27:01The ratio is how busy the worker is
  1711. 1:27:05divided by how idle it is. If the worker
  1712. 1:27:08is busy half the time, the ratio is a
  1713. 1:27:11half divided by a half, which is one. A
  1714. 1:27:15ticket waits on average about as long as
  1715. 1:27:19one job takes. At 80% busy, the ratio is
  1716. 1:27:2380 divided by 20, which is four. At 90%,
  1717. 1:27:28it's nine. At 99%,
  1718. 1:27:31it's 99. So a worker that's nearly
  1719. 1:27:35always busy, which might look efficient
  1720. 1:27:38on paper, keeps people waiting a very
  1721. 1:27:41long time. That's why the deli always
  1722. 1:27:44seems to have one more person behind the
  1723. 1:27:47counter than strictly
  1724. 1:27:50on a Saturday morning. And it's why back
  1725. 1:27:53end engineers rarely run anything close
  1726. 1:27:56to flat out. A good message broker adds
  1727. 1:27:59one more careful step and it's the
  1728. 1:28:01reason queues are trusted with important
  1729. 1:28:04work. When a worker takes a ticket, the
  1730. 1:28:07broker doesn't throw the ticket away. It
  1731. 1:28:10sets it aside and waits. Only when the
  1732. 1:28:13worker reports back done does the broker
  1733. 1:28:17delete it. That report is called an
  1734. 1:28:19acknowledgement. If the worker crashes
  1735. 1:28:22halfway through, the acknowledgement
  1736. 1:28:25never comes. After a while, the broker
  1737. 1:28:28puts the ticket back in the queue and
  1738. 1:28:31another worker picks it up. No job is
  1739. 1:28:34silently lost because one worker fell
  1740. 1:28:37over. And if a ticket fails again and
  1741. 1:28:41again, say five times in a row because
  1742. 1:28:44the email address is broken, the broker
  1743. 1:28:47moves it to a separate holding area,
  1744. 1:28:50often called a dead letter queue, where
  1745. 1:28:53a person can look at it later. One bad
  1746. 1:28:56ticket shouldn't block the counter
  1747. 1:28:58forever. But notice what that safety net
  1748. 1:29:01implies. Say a worker sends the email
  1749. 1:29:05and then crashes a split second before
  1750. 1:29:08it can say done. The broker never heard
  1751. 1:29:11the acknowledgement, so it hands the
  1752. 1:29:13ticket to another worker who sends the
  1753. 1:29:16email again. The customer gets two
  1754. 1:29:19welcome emails. Engineers call this at
  1755. 1:29:22least once delivery. Every job will be
  1756. 1:29:26done, but some may be done twice. For a
  1757. 1:29:29welcome email, that's a small
  1758. 1:29:32embarrassment. For a payment, it would
  1759. 1:29:34be a serious problem. Doing things twice
  1760. 1:29:38safely is such an important puzzle that
  1761. 1:29:41it gets its own chapter later tonight.
  1762. 1:29:44So by the late 2000s, back ends had a
  1763. 1:29:47sturdy way to hand off work. Producers
  1764. 1:29:51drop tickets
  1765. 1:29:52brokers keep them safe, workers take
  1766. 1:29:55them out, do the job, and say done. And
  1767. 1:29:59then the ticket is gone. That last part,
  1768. 1:30:03the ticket being gone, turned out to
  1769. 1:30:05matter. At LinkedIn, around 2010,
  1770. 1:30:09engineers were drowning in a different
  1771. 1:30:12kind of problem. They didn't just need
  1772. 1:30:14to hand jobs to workers. They had dozens
  1773. 1:30:18of systems that all wanted to know about
  1774. 1:30:21the same events. Every profile view,
  1775. 1:30:24every click, every new connection. And
  1776. 1:30:27they wanted to be able to go back and
  1777. 1:30:30read those events again, hours or days
  1778. 1:30:33later. A queue that forgets each ticket
  1779. 1:30:37once it's served couldn't do that. They
  1780. 1:30:40needed something more like a book where
  1781. 1:30:42every event is written down in order and
  1782. 1:30:45stays written down for anyone to read.
  1783. 1:30:51Chapter eight, the log that remembers
  1784. 1:30:54everything. It's around 2010 in Mountain
  1785. 1:30:58View, California, and a software
  1786. 1:31:00engineer at LinkedIn named Jay Kreps is
  1787. 1:31:04looking at a diagram that has gotten out
  1788. 1:31:07of hand. LinkedIn, the social network
  1789. 1:31:10for working life, had started with one
  1790. 1:31:13main database, like most young websites.
  1791. 1:31:17Everything lived in it. But as the site
  1792. 1:31:20grew, the team added more and more
  1793. 1:31:23specialized systems around it. A search
  1794. 1:31:26engine so you could look people up. A
  1795. 1:31:29system for suggesting people you might
  1796. 1:31:32know. A store for the activity feed. A
  1797. 1:31:36big warehouse where analysts studied how
  1798. 1:31:39the site was used. Monitoring systems
  1799. 1:31:42that watched the servers. Each of these
  1800. 1:31:45needed data from the others. The search
  1801. 1:31:48engine needed to know when someone
  1802. 1:31:50changed their job title. The suggestion
  1803. 1:31:53system needed to know who viewed whose
  1804. 1:31:56profile. The warehouse wanted to know
  1805. 1:31:59about everything. So, engineers had
  1806. 1:32:02built pipelines between them. A custom
  1807. 1:32:05pipe from the main database to search,
  1808. 1:32:08another from the activity store to the
  1809. 1:32:11warehouse, another from the warehouse
  1810. 1:32:14back to the suggestion system. Each pipe
  1811. 1:32:17was written separately. Each one
  1812. 1:32:20formatted the data its own way, and each
  1813. 1:32:23one could break in its own particular
  1814. 1:32:25way. When you draw that on a whiteboard,
  1815. 1:32:28it looks like a plate of spaghetti.
  1816. 1:32:30Let's count the pipes. Say there are 10
  1817. 1:32:33systems, and in the worst case, every
  1818. 1:32:36one of them needs to send data to every
  1819. 1:32:39other. Each system sends to nine others.
  1820. 1:32:4210 * 9 is 91 way pipes. Add an 11th
  1821. 1:32:47system and you need 20 more. Krebs,
  1822. 1:32:50working with two colleagues, Neha
  1823. 1:32:53Narkhede and Jun Rao, came at the
  1824. 1:32:55problem from a different angle. Instead
  1825. 1:32:58of connecting every system to every
  1826. 1:33:01other, they would connect every system
  1827. 1:33:03to one shared thing, a single central
  1828. 1:33:07record of everything that happened. Each
  1829. 1:33:10system would write its events into that
  1830. 1:33:12record once. Each system that wanted to
  1831. 1:33:15know about events would read from it. 10
  1832. 1:33:18systems, each with one connection for
  1833. 1:33:21writing and one for reading. 20
  1834. 1:33:24connections instead of 90. The central
  1835. 1:33:27record would be a log. That word has a
  1836. 1:33:30particular meaning for engineers, and
  1837. 1:33:33Krebs later wrote a long, much-shared
  1838. 1:33:36essay about it in 2013.
  1839. 1:33:39A log, in this sense, is a sequence of
  1840. 1:33:43records that can only be added to at the
  1841. 1:33:46end. Nothing in the middle is ever
  1842. 1:33:48changed. Each new record goes after the
  1843. 1:33:51last one, and each gets the next number
  1844. 1:33:54in line. It's one of the oldest kinds of
  1845. 1:33:57record there is. Consider the log book
  1846. 1:34:00on a ship. Every few hours, the officer
  1847. 1:34:03of the watch writes an entry, the time,
  1848. 1:34:06the position, the speed, the weather,
  1849. 1:34:10anything worth noting. Entries go in
  1850. 1:34:12order, one after another down the page.
  1851. 1:34:16Nobody tears out an old page or rewrites
  1852. 1:34:19yesterday's weather. If a mistake was
  1853. 1:34:22made, a new entry corrects it. The log
  1854. 1:34:25book is a faithful history of the voyage
  1855. 1:34:28in the order things happened, and many
  1856. 1:34:31people can read it. The captain checks
  1857. 1:34:33the last few entries each morning. The
  1858. 1:34:36navigator reads back through a week of
  1859. 1:34:39positions to plot the course. Back on
  1860. 1:34:42shore, months later, someone might read
  1861. 1:34:45the whole thing from the first page to
  1862. 1:34:47the last. Each reader keeps their own
  1863. 1:34:50place. The captain's bookmark is near
  1864. 1:34:53the end. The shore clerk's is somewhere
  1865. 1:34:55near the start. Reading doesn't use up
  1866. 1:34:58an entry, and one reader's bookmark has
  1867. 1:35:01nothing to do with another's. That's the
  1868. 1:35:04heart of what the LinkedIn team built.
  1869. 1:35:07They called it Kafka. The name came from
  1870. 1:35:10Krebs. As he later explained, the system
  1871. 1:35:13was optimized for writing, so it seemed
  1872. 1:35:16fitting to name it after a writer, and
  1873. 1:35:19he liked the work of Franz Kafka.
  1874. 1:35:22LinkedIn released Kafka as open source
  1875. 1:35:25in early 2011. It moved to the Apache
  1876. 1:35:29Software Foundation, a home for shared
  1877. 1:35:32open projects, and became a full Apache
  1878. 1:35:35project in 2012. By the time of Krebs's
  1879. 1:35:39essay in 2013,
  1880. 1:35:41LinkedIn was writing more than 60
  1881. 1:35:44billion messages a day into its Kafka
  1882. 1:35:47logs. Part of what made Kafka so fast
  1883. 1:35:50was that very simplicity. Adding to the
  1884. 1:35:53end of a file and reading straight
  1885. 1:35:56through it from front to back are the
  1886. 1:35:58things disks, and operating systems do
  1887. 1:36:01best. There's no searching and no
  1888. 1:36:04rearranging, just a steady stream like
  1889. 1:36:07reeling out a ribbon. Let's look at how
  1890. 1:36:10it works with numbers. In Kafka, a log
  1891. 1:36:15is called a topic. Say LinkedIn has a
  1892. 1:36:18topic called profile views. Every time
  1893. 1:36:22anyone looks at anyone's profile, a
  1894. 1:36:24small record is added to the end of it.
  1895. 1:36:27Who looked, whose profile, and when?
  1896. 1:36:31Each record gets a number called its
  1897. 1:36:34offset. The very first record ever
  1898. 1:36:36written is offset zero. The next is
  1899. 1:36:40offset one. After a busy few days, the
  1900. 1:36:43newest record might be at offset 1
  1901. 1:36:46million and 200. Now three different
  1902. 1:36:49systems are reading this topic. The
  1903. 1:36:51first is the notification system, which
  1904. 1:36:54tells people someone viewed your
  1905. 1:36:57profile. It wants to be quick, so it
  1906. 1:37:00reads right at the end. Its bookmark is
  1907. 1:37:03at offset 1 million and 200. When a new
  1908. 1:37:07view arrives, it reads it within a
  1909. 1:37:10moment. The second is the analytics
  1910. 1:37:12warehouse. It doesn't need to be quick.
  1911. 1:37:16Once an hour it wakes up and reads
  1912. 1:37:18everything since its last visit. Right
  1913. 1:37:20now its bookmark is back at offset
  1914. 1:37:23980,000.
  1915. 1:37:26It has about 20,000 records to catch up
  1916. 1:37:28on, and it will get through them in a
  1917. 1:37:30few seconds when it wakes. And the third
  1918. 1:37:34is brand new. A team has just built a
  1919. 1:37:37system to spot fake accounts, which tend
  1920. 1:37:40to view large numbers of profiles very
  1921. 1:37:43quickly. They want to train it on
  1922. 1:37:46history, so it starts its bookmark at
  1923. 1:37:49the oldest record still kept and reads
  1924. 1:37:52forward through days of views at its own
  1925. 1:37:55pace until it catches up with the
  1926. 1:37:58present. Three readers, three bookmarks,
  1927. 1:38:01one log. None of them gets in the others
  1928. 1:38:04way, and the system writing the views
  1929. 1:38:07doesn't even know they exist. Compare
  1930. 1:38:10this with the queue from the last
  1931. 1:38:12chapter. In a queue, once a worker has
  1932. 1:38:15taken a ticket and said done, the ticket
  1933. 1:38:18is gone. That's right for jobs that
  1934. 1:38:21should happen once. A log keeps every
  1935. 1:38:24record for a set period, often a week,
  1936. 1:38:28whether anyone has read it or not. Any
  1937. 1:38:31number of readers can come along and
  1938. 1:38:33read the same records, and a new reader
  1939. 1:38:36can go back and replay the past. Of
  1940. 1:38:39course, one log on one machine couldn't
  1941. 1:38:42possibly hold 60 billion records a day,
  1942. 1:38:45so Kafka splits each topic into pieces
  1943. 1:38:49called partitions and spreads them
  1944. 1:38:52across many machines. Say profile views
  1945. 1:38:56are split into six partitions. When a
  1946. 1:38:58new view comes in, Kafka needs to decide
  1947. 1:39:02which partition it goes into. A common
  1948. 1:39:05rule is to choose by the person being
  1949. 1:39:08viewed. All views of the same person
  1950. 1:39:11always land in the same partition, so
  1951. 1:39:15they stay in order relative to one
  1952. 1:39:17another. Different people's views are
  1953. 1:39:20spread across the six, so six machines
  1954. 1:39:23can share the work, and six readers can
  1955. 1:39:26read in parallel, and each partition is
  1956. 1:39:29copied to several machines, often three.
  1957. 1:39:33If one machine fails, another already
  1958. 1:39:36holds an identical copy and carries on.
  1959. 1:39:40The log idea has one more consequence,
  1960. 1:39:44and it's quietly beautiful. If you keep
  1961. 1:39:47every event in order, then you can
  1962. 1:39:50rebuild the current state of anything
  1963. 1:39:53from scratch just by reading the events
  1964. 1:39:55again. Say a simple bank account begins
  1965. 1:39:59at zero. The log for that account has
  1966. 1:40:02three entries, deposit $100,
  1967. 1:40:06withdraw 30, deposit 50. Read them in
  1968. 1:40:10order. 0 + 1 00 is 100 - 30 is 70 + 50
  1969. 1:40:18is 120.
  1970. 1:40:19The balance is $120,
  1971. 1:40:23but the log holds more than the balance.
  1972. 1:40:26It holds the whole story of how the
  1973. 1:40:28balance got there. If a bug in the
  1974. 1:40:31system once calculated a balance
  1975. 1:40:33wrongly, you can fix the bug, replay the
  1976. 1:40:37log, and arrive at the right answer. If
  1977. 1:40:40a new team wants to know how often
  1978. 1:40:42people withdraw money on Fridays, the
  1979. 1:40:45history is all there, waiting. Engineers
  1980. 1:40:49call this way of building systems event
  1981. 1:40:52sourcing. The events are the truth.
  1982. 1:40:55Everything else, every table and screen
  1983. 1:40:57and summary is just a view built by
  1984. 1:41:00reading them. If you heard our lecture
  1985. 1:41:03on database engineering, you may
  1986. 1:41:05remember that databases had used this
  1987. 1:41:08trick inside themselves for years,
  1988. 1:41:11writing every change to a log before
  1989. 1:41:14touching anything else so they could
  1990. 1:41:16recover from a crash. Kafka took that
  1991. 1:41:19private diary out of the database and
  1992. 1:41:22made it a shared log book for the whole
  1993. 1:41:24company. In 2014, Krebs, Narkhede, and
  1994. 1:41:29Rao left LinkedIn to start a company
  1995. 1:41:32built around Kafka, and the project went
  1996. 1:41:35on to be used at a great many large
  1997. 1:41:37organizations, carrying everything from
  1998. 1:41:41ride requests to bank payments to the
  1999. 1:41:44readings of sensors on factory floors.
  2000. 1:41:47The deeper change was in how engineers
  2001. 1:41:49drew their diagrams. Instead of a plate
  2002. 1:41:52of spaghetti, there was a spine, one
  2003. 1:41:56shared log running down the middle and
  2004. 1:41:59every system hanging off it, writing
  2005. 1:42:01what happened and reading what it
  2006. 1:42:04needed. A system could be added or
  2007. 1:42:06removed without rewiring anything else,
  2008. 1:42:10which raised an obvious next question.
  2009. 1:42:13If systems could be that independent of
  2010. 1:42:16each other, connected only by logs and
  2011. 1:42:19interfaces, then someone had to decide
  2012. 1:42:22how big each one should be, one large
  2013. 1:42:25program or many small ones. At Amazon,
  2014. 1:42:30nearly a decade before Kafka, that
  2015. 1:42:32question had been answered by decree in
  2016. 1:42:35a memo that is passed into legend.
  2017. 1:42:40Chapter nine, one big program or many
  2018. 1:42:44small ones. It's sometime around 2002
  2019. 1:42:49in Seattle, and according to a story
  2020. 1:42:51that engineers still retell, Jeff Bezos
  2021. 1:42:55has sent an instruction to everyone
  2022. 1:42:57building software at Amazon. We should
  2023. 1:43:00be careful with this one because nobody
  2024. 1:43:03outside Amazon has ever published the
  2025. 1:43:05original. Almost everything we know
  2026. 1:43:08about it comes from one source, written
  2027. 1:43:11nearly a decade later. In October 2011,
  2028. 1:43:15an engineer named Steve Yegge, who had
  2029. 1:43:19spent about six and a half years at
  2030. 1:43:21Amazon before moving to Google, wrote a
  2031. 1:43:24very long post about the differences
  2032. 1:43:27between the two companies. He meant it
  2033. 1:43:30to be read only by his colleagues inside
  2034. 1:43:33Google. By accident, he shared it with
  2035. 1:43:36the whole world. He took it down
  2036. 1:43:38quickly, but by then it had been copied
  2037. 1:43:42everywhere. In that post, Yegge
  2038. 1:43:45described the mandate as he remembered
  2039. 1:43:47it, placing it around 2002,
  2040. 1:43:50give or take a year. In his telling, it
  2041. 1:43:53went roughly like this. From now on,
  2042. 1:43:57every team will expose its data and its
  2043. 1:44:00functions only through service
  2044. 1:44:03interfaces. Teams will talk to one
  2045. 1:44:06another only through those interfaces,
  2046. 1:44:09no reaching directly into another team's
  2047. 1:44:12database. No shared memory, no
  2048. 1:44:15backdoors, no exceptions. It doesn't
  2049. 1:44:18matter what technology each team uses.
  2050. 1:44:22Every interface must be designed from
  2051. 1:44:25the start as if outside developers might
  2052. 1:44:28one day use it. And anyone who doesn't
  2053. 1:44:31do this will be fired. Yegge added a
  2054. 1:44:34final line, which he presented as a
  2055. 1:44:37joke. Thank you and have a nice day.
  2056. 1:44:40Whether every word is accurate, we can't
  2057. 1:44:43say, but there's little doubt about the
  2058. 1:44:45direction Amazon took. Over the
  2059. 1:44:48following years, its giant online store
  2060. 1:44:51was rebuilt from one large program into
  2061. 1:44:55many separate services, each owned by a
  2062. 1:44:58small team, each talking to the others
  2063. 1:45:01only through its published interface. To
  2064. 1:45:04see why that was such a big decision,
  2065. 1:45:07let's first look at what Amazon was
  2066. 1:45:09moving away from. The simplest way to
  2067. 1:45:12build a backend is as one program. All
  2068. 1:45:16the code for the shop, the product
  2069. 1:45:18pages, the cart, the checkout, the stock
  2070. 1:45:22counts, the recommendations
  2071. 1:45:24lives together in one codebase, is built
  2072. 1:45:28together and runs together on each
  2073. 1:45:31server. Engineers call this a monolith,
  2074. 1:45:35from the Greek for a single stone. A
  2075. 1:45:38monolith has real virtues, and it's
  2076. 1:45:41worth being fair to them. It's simple to
  2077. 1:45:44understand. There's one program to
  2078. 1:45:47build, one to test, one to deploy. When
  2079. 1:45:51the checkout needs a stock count, it
  2080. 1:45:54just calls a function right there in the
  2081. 1:45:56same program. That call takes a few
  2082. 1:46:00billionths of a second, and it never
  2083. 1:46:02gets lost on a network. And when an
  2084. 1:46:05order and its stock change need to be
  2085. 1:46:08saved together, both can go into the
  2086. 1:46:11same database in a single transaction.
  2087. 1:46:15Either both happen or neither does. Most
  2088. 1:46:18backends start as monoliths and many
  2089. 1:46:22very successful ones stay that way for
  2090. 1:46:25years. The Rails and Django applications
  2091. 1:46:28from earlier tonight were almost always
  2092. 1:46:32monoliths. The trouble comes with size
  2093. 1:46:35and especially with the number of
  2094. 1:46:37people. Picture a single large kitchen
  2095. 1:46:41with one long counter where every cook
  2096. 1:46:44works on every dish. With five cooks,
  2097. 1:46:47it's friendly and efficient. With 50,
  2098. 1:46:51they're reaching across each other for
  2099. 1:46:53the same pans. Nobody can rearrange
  2100. 1:46:56their own station without disturbing
  2101. 1:46:58three neighbors. And if one cook drops a
  2102. 1:47:02pan of oil on the floor, the whole
  2103. 1:47:04kitchen stops. A monolith with hundreds
  2104. 1:47:08of engineers works the same way.
  2105. 1:47:11Everyone's changes go into the same
  2106. 1:47:13program, so everyone's changes have to
  2107. 1:47:16be tested together and released
  2108. 1:47:19together. A mistake in the
  2109. 1:47:21recommendations code can crash the
  2110. 1:47:23checkout. And if one part needs far more
  2111. 1:47:27computing power than the rest, you still
  2112. 1:47:29have to run more copies of the whole
  2113. 1:47:32thing. The alternative is to split the
  2114. 1:47:35kitchen up. Instead of one long counter,
  2115. 1:47:38there's a food hall, separate stalls,
  2116. 1:47:41each with its own small team, its own
  2117. 1:47:44menu, and its own equipment. The noodle
  2118. 1:47:47stall can change its recipes, buy a new
  2119. 1:47:50stove, or close for an afternoon without
  2120. 1:47:53asking the dumpling stall for
  2121. 1:47:55permission. If one stall has a problem,
  2122. 1:47:59the others keep serving. In software,
  2123. 1:48:02each stall is a service. A service owns
  2124. 1:48:05one area of the business and its own
  2125. 1:48:08data. The cart service owns carts. The
  2126. 1:48:12stock service owns stock counts, and no
  2127. 1:48:15one else is allowed to read its database
  2128. 1:48:18directly. Other services ask it through
  2129. 1:48:21its interface. That's exactly what the
  2130. 1:48:24mandate demanded. Splitting things up
  2131. 1:48:27also lets each part grow on its own. Say
  2132. 1:48:31a big holiday sale is coming. The
  2133. 1:48:34checkout is about to get 10 times
  2134. 1:48:36busier, but hardly anyone will be
  2135. 1:48:39writing reviews. In a monolith, you'd
  2136. 1:48:42have to run 40 copies of the whole
  2137. 1:48:44program to give the checkout 40 copies
  2138. 1:48:48worth of power. With services, you run
  2139. 1:48:5140 copies of the checkout service and
  2140. 1:48:54leave the reviews service at four. It's
  2141. 1:48:57the food hall putting extra cooks on the
  2142. 1:48:59busiest stall and leaving the quiet ones
  2143. 1:49:03alone. In March 2014,
  2144. 1:49:06two consultants, James Lewis and Martin
  2145. 1:49:09Fowler, published a widely read article
  2146. 1:49:13that gave this style the name that
  2147. 1:49:15stuck. They called it microservices.
  2148. 1:49:19Their description was of a single
  2149. 1:49:21application built as a suite of small
  2150. 1:49:24services, each running on its own and
  2151. 1:49:27each deployable independently. They also
  2152. 1:49:30pointed back to an observation made in
  2153. 1:49:331968
  2154. 1:49:35by a programmer named Melvin Conway.
  2155. 1:49:38Organizations, he noticed, tend to
  2156. 1:49:41design systems that mirror their own
  2157. 1:49:43communication structure. In other words,
  2158. 1:49:47if four teams build a program together,
  2159. 1:49:50it tends to come out in four parts.
  2160. 1:49:53Microservices turned that around and
  2161. 1:49:56used it on purpose. Decide how you want
  2162. 1:49:59your teams to work, small and
  2163. 1:50:02independent, and shape the software to
  2164. 1:50:04match. Amazon has often been described
  2165. 1:50:08as organizing around small teams,
  2166. 1:50:11sometimes said to be no bigger than two
  2167. 1:50:13pizzas could feed. Each one could own a
  2168. 1:50:16service, change it, and release it
  2169. 1:50:20whenever it was ready, without waiting
  2170. 1:50:22for anyone else. But splitting things up
  2171. 1:50:25isn't free, and here's where we should
  2172. 1:50:28put some numbers down. Say a product
  2173. 1:50:31page needs information from five
  2174. 1:50:34services. Product details, price, stock,
  2175. 1:50:38reviews, and recommendations.
  2176. 1:50:41In a monolith, those would be five
  2177. 1:50:44function calls, taking almost no time at
  2178. 1:50:47all. With microservices,
  2179. 1:50:50they're five trips across the network.
  2180. 1:50:52Say each trip takes 20,000ths of a
  2181. 1:50:55second. If the page asks the five
  2182. 1:50:58services one after another, that's
  2183. 1:51:01100,000ths of a second, a tenth of a
  2184. 1:51:04second just in waiting. Reliability gets
  2185. 1:51:08more subtle, too. Say each service is
  2186. 1:51:11working correctly 99.9%
  2187. 1:51:15of the time. That sounds excellent, but
  2188. 1:51:18if the page needs all five, it only
  2189. 1:51:22works when all five work at once. You
  2190. 1:51:25find that by multiplying 0.999
  2191. 1:51:30multiplied by itself five times comes to
  2192. 1:51:34about 0.995,
  2193. 1:51:3799 and 1/2 percent. So, instead of
  2194. 1:51:41failing about one time in a thousand,
  2195. 1:51:44the page now fails about one time in
  2196. 1:51:47200, five times as often, just from
  2197. 1:51:50adding up small chances. The food hall
  2198. 1:51:54has the same problem. If your set meal
  2199. 1:51:57needs a dish from each of five stalls,
  2200. 1:52:00then any one stall running late holds up
  2201. 1:52:03your whole tray. Engineers have built up
  2202. 1:52:06a toolkit of habits for this. Ask the
  2203. 1:52:09five services at the same time, rather
  2204. 1:52:12than one after another. So the page
  2205. 1:52:14waits for the slowest single answer
  2206. 1:52:17instead of the sum. Set a firm time
  2207. 1:52:20limit on every call, so one slow stall
  2208. 1:52:24can't hold up the tray forever. And
  2209. 1:52:26decide in advance which parts are
  2210. 1:52:29essential. If the recommendation service
  2211. 1:52:32doesn't answer in time, show the page
  2212. 1:52:35without recommendations.
  2213. 1:52:37Second customers will never notice. If
  2214. 1:52:40the price service doesn't answer though,
  2215. 1:52:43the page really can't be shown. There
  2216. 1:52:46are other costs. Data that used to sit
  2217. 1:52:50in one database is now scattered, so
  2218. 1:52:53keeping an order and its stock count in
  2219. 1:52:55step needs more care. A single
  2220. 1:52:58customer's request might pass through
  2221. 1:53:01dozens of services, so finding out what
  2222. 1:53:04went wrong becomes detective work, which
  2223. 1:53:07is a subject for later tonight. And
  2224. 1:53:10someone has to run all those services,
  2225. 1:53:13each with its own servers and its own
  2226. 1:53:16releases. So the honest answer to the
  2227. 1:53:19chapter's question is it depends, and
  2228. 1:53:22mostly it depends on people. A small
  2229. 1:53:26team with a young product usually does
  2230. 1:53:29best with a well-organized monolith.
  2231. 1:53:32Many experienced engineers advise
  2232. 1:53:35starting there and splitting off
  2233. 1:53:37services only when a part of the system
  2234. 1:53:40clearly needs to grow or change on its
  2235. 1:53:43own. Some large and successful companies
  2236. 1:53:47have deliberately kept big, carefully
  2237. 1:53:50organized monoliths for years, and DHH
  2238. 1:53:54himself has written in their defense. A
  2239. 1:53:57large organization with hundreds of
  2240. 1:54:00teams that need to move independently
  2241. 1:54:03often finds that services are the only
  2242. 1:54:06way to stop everyone tripping over each
  2243. 1:54:09other. Neither is a mistake. They're
  2244. 1:54:11different shapes of kitchen suited to
  2245. 1:54:14different numbers of cooks. The mandate
  2246. 1:54:17had one more consequence that Jager's
  2247. 1:54:19post pointed out, and it may have been
  2248. 1:54:22the most important of all. One of its
  2249. 1:54:25rules said every interface must be built
  2250. 1:54:29as if outsiders might use it. And as
  2251. 1:54:32Jäger and many others have told the
  2252. 1:54:34story, once Amazon had turned its own
  2253. 1:54:38infrastructure into services, its
  2254. 1:54:40storage, its computing, its queues, each
  2255. 1:54:44behind a clean interface, a thought
  2256. 1:54:47followed naturally. If its own teams
  2257. 1:54:50could rent computing through an
  2258. 1:54:52interface, so could anyone. That thought
  2259. 1:54:56became Amazon Web Services. And in 2014,
  2260. 1:55:01it led to an offer that would have
  2261. 1:55:02sounded strange only a few years
  2262. 1:55:05earlier. Don't rent a server at all,
  2263. 1:55:08just hand over a function and let
  2264. 1:55:11someone else worry about where it runs.
  2265. 1:55:16Chapter 10, Servers You Never See. It's
  2266. 1:55:20Thursday, the 13th of November, 2014,
  2267. 1:55:25in Las Vegas, and a large hall is full
  2268. 1:55:29of people who build software for a
  2269. 1:55:31living. They've come to reinvent Amazon
  2270. 1:55:35Web Services yearly conference. On stage
  2271. 1:55:38is Werner Vogels, Amazon's Chief
  2272. 1:55:41Technology Officer, a Dutchman who had
  2273. 1:55:45been a computer science researcher at
  2274. 1:55:47Cornell University before he came to
  2275. 1:55:50Amazon. Over the course of his keynote,
  2276. 1:55:53he introduces a new service with a
  2277. 1:55:55short, slightly mysterious name, AWS
  2278. 1:56:00Lambda. The idea was easy to state and
  2279. 1:56:03strange to hear. You wouldn't rent a
  2280. 1:56:06server anymore. You wouldn't choose a
  2281. 1:56:08machine or install an operating system
  2282. 1:56:12or decide how many copies to run. You
  2283. 1:56:15would simply hand Amazon a function, a
  2284. 1:56:18small piece of code that does one job
  2285. 1:56:21and tell it when that function should
  2286. 1:56:23run. When a photo is uploaded, run this.
  2287. 1:56:27When a new row appears in this table,
  2288. 1:56:30run that. Amazon would find a machine,
  2289. 1:56:33run your code, and charge you for the
  2290. 1:56:36time it actually spent running, measured
  2291. 1:56:39in slices of a tenth of a second. When
  2292. 1:56:42nothing was happening, you'd pay nothing
  2293. 1:56:44at all. At launch, Lambda was a preview
  2294. 1:56:48with some firm limits. Functions could
  2295. 1:56:51only be written in one language,
  2296. 1:56:54JavaScript running on Node, and each one
  2297. 1:56:57could run for at most a minute. It
  2298. 1:57:00opened to everyone in April 2015,
  2299. 1:57:04and over the following years, it grew
  2300. 1:57:07more languages and much longer time
  2301. 1:57:10limits. It wasn't the very first service
  2302. 1:57:13of its kind. Google, for instance, had
  2303. 1:57:16offered a platform for running web
  2304. 1:57:19applications without managing servers
  2305. 1:57:22since 2008.
  2306. 1:57:24But Lambda's pure form, a single
  2307. 1:57:27function woken by an event, billed by
  2308. 1:57:31the slice, caught the imagination of the
  2309. 1:57:34industry. People started calling the
  2310. 1:57:36approach serverless. The name is a
  2311. 1:57:40little cheeky. There are servers, of
  2312. 1:57:42course, racks and racks of them humming
  2313. 1:57:45in Amazon's data centers. They're just
  2314. 1:57:48not yours to think about. To feel the
  2315. 1:57:51difference, let's compare three ways to
  2316. 1:57:54get somewhere. You can own a car. It's
  2317. 1:57:57always in the driveway, ready to go at a
  2318. 1:58:00moment's notice. But you pay for it
  2319. 1:58:03whether you drive it or not. Insurance,
  2320. 1:58:06parking, the loan all month long. That's
  2321. 1:58:10like owning or renting a server. It's
  2322. 1:58:13there all the time, and the bill arrives
  2323. 1:58:16all the time. You can hire a car for a
  2324. 1:58:19week. That's like renting cloud servers
  2325. 1:58:22by the hour. More flexible, but still,
  2326. 1:58:26if you hire it and leave it parked,
  2327. 1:58:29you're paying for a parked car. Or you
  2328. 1:58:31can take a taxi. You pay for each trip
  2329. 1:58:35and only for the trip. When you're not
  2330. 1:58:37going anywhere, it costs nothing. That's
  2331. 1:58:41serverless. As with real taxis, the
  2332. 1:58:45question of which is cheaper depends
  2333. 1:58:47entirely on how much you travel. Let's
  2334. 1:58:51put numbers on it. These are made-up
  2335. 1:58:54round prices chosen to make the
  2336. 1:58:57arithmetic easy. Real prices vary and
  2337. 1:59:00change over time. Say a small server
  2338. 1:59:04costs 10 cents an hour. There are 720
  2339. 1:59:08hours in a 30-day month, so the server
  2340. 1:59:11costs $72 a month, whether it's busy or
  2341. 1:59:15idle. And say a serverless function for
  2342. 1:59:19a typical small request works out to $2
  2343. 1:59:23for every million requests. Now consider
  2344. 1:59:26three different apps. The first is a
  2345. 1:59:29little app for a local sports club. It
  2346. 1:59:32gets about a million requests a month,
  2347. 1:59:35mostly on match days. On the server,
  2348. 1:59:38that's $72.
  2349. 1:59:40As functions, it's a million requests at
  2350. 1:59:44$2 per million. $2 $2 instead of 72. For
  2351. 1:59:50a quiet app, serverless is a gift, and
  2352. 1:59:53there's a hidden saving on top of that.
  2353. 1:59:56Nobody at the club has to install
  2354. 1:59:58updates on a server, renew it, or worry
  2355. 2:00:02about it overnight. For a club run by
  2356. 2:00:05volunteers, that might matter even more
  2357. 2:00:08than the $70.
  2358. 2:00:10The second app gets 36 million requests
  2359. 2:00:14a month. 36 * 2 is $72,
  2360. 2:00:19exactly the same as the server. That's
  2361. 2:00:22the break-even point. The third app is
  2362. 2:00:25busy with 100 million requests a month.
  2363. 2:00:29As functions, that's $200
  2364. 2:00:32and 100 million requests a month is only
  2365. 2:00:35about 39 requests a second on average,
  2366. 2:00:39which a single modest server could
  2367. 2:00:41likely handle. On the server, it's still
  2368. 2:00:44$72.
  2369. 2:00:46So the taxi is a fine choice for
  2370. 2:00:48occasional trips and expensive as a
  2371. 2:00:51daily commute. Many companies end up
  2372. 2:00:54with both functions for spiky,
  2373. 2:00:57occasional or unpredictable work and
  2374. 2:01:00ordinary servers for the steady, heavy
  2375. 2:01:03traffic. The price isn't the only
  2376. 2:01:05difference. There's also a matter of
  2377. 2:01:08waking up. When a function hasn't run
  2378. 2:01:10for a while, the service doesn't keep it
  2379. 2:01:13loaded on a machine waiting. So when a
  2380. 2:01:16request arrives, the service has to find
  2381. 2:01:19a machine, set up a fresh, isolated
  2382. 2:01:23space for your code, load the code, and
  2383. 2:01:26start it running. Only then can it
  2384. 2:01:29handle the request. That extra delay is
  2385. 2:01:32called a cold start. Say a warm
  2386. 2:01:35function, one that's already loaded
  2387. 2:01:38because it ran a moment ago, answers in
  2388. 2:01:4120,000ths of a second. A cold start
  2389. 2:01:44might add most of a second on top,
  2390. 2:01:47depending on the language and how much
  2391. 2:01:49the code has to load. The first visitor
  2392. 2:01:52of the morning waits nearly a second.
  2393. 2:01:55The next visitor, a moment later, gets
  2394. 2:01:58the warm function and waits a 50th of a
  2395. 2:02:02second. It's like a small cafe where the
  2396. 2:02:05barista goes home between customers.
  2397. 2:02:09When you arrive, someone has to call her
  2398. 2:02:11in. She has to put on her apron and warm
  2399. 2:02:14up the machine, and then she makes your
  2400. 2:02:17coffee. The next customer who arrives
  2401. 2:02:21while she's still there gets served
  2402. 2:02:24right away. After a quiet spell, she
  2403. 2:02:27goes home again. For a background job,
  2404. 2:02:30like making thumbnail copies of an
  2405. 2:02:33uploaded photo, a cold start doesn'
  2406. 2:02:35matter at all. Nobody is watching the
  2407. 2:02:38clock. For a page a person is waiting
  2408. 2:02:41on, it can matter a lot, and engineers
  2409. 2:02:44have developed tricks to soften it,
  2410. 2:02:47keeping code small so it loads quickly,
  2411. 2:02:50paying a little extra to keep a few
  2412. 2:02:52copies always warm, or simply choosing
  2413. 2:02:55ordinary servers for the parts where
  2414. 2:02:58every moment counts. There's a deeper
  2415. 2:03:00lesson in serverless, and it's one
  2416. 2:03:03you've heard before tonight. A function
  2417. 2:03:06can't remember anything between one call
  2418. 2:03:08and the next. The machine it ran on
  2419. 2:03:11might be a different machine next time,
  2420. 2:03:14or might not exist at all. So everything
  2421. 2:03:17that matters has to be kept somewhere
  2422. 2:03:20outside, in a database, a cache, or a
  2423. 2:03:24queue. That's exactly the stateless
  2424. 2:03:27habit from the chapter on the fail
  2425. 2:03:29whale. Serverless simply takes it to its
  2426. 2:03:32natural end. The cooks don't just forget
  2427. 2:03:36the last customer. They go home between
  2428. 2:03:39orders, and serverless fits beautifully
  2429. 2:03:42with the queues and logs from the last
  2430. 2:03:45few chapters. A message lands in a
  2431. 2:03:48queue, and a function wakes up to handle
  2432. 2:03:51it. A new event is written to a log, and
  2433. 2:03:54a function wakes up to react. A file
  2434. 2:03:58appears in storage, and a function wakes
  2435. 2:04:01up to process it. Events in, small jobs
  2436. 2:04:05out, no machine to look after. It also
  2437. 2:04:08scales in both directions without anyone
  2438. 2:04:11deciding anything. If one photo arrives,
  2439. 2:04:14one copy of the function runs. If 10,000
  2440. 2:04:18arrive in the same minute, the platform
  2441. 2:04:21runs many copies side by side, up to a
  2442. 2:04:24limit set on the account, which, as of
  2443. 2:04:272026,
  2444. 2:04:29is usually 1,000 at once by default, and
  2445. 2:04:33lower for some new accounts. When the
  2446. 2:04:35rush is over, the copies simply vanish.
  2447. 2:04:39Nobody had to add servers in the morning
  2448. 2:04:42or remove them at night. The kitchen
  2449. 2:04:44grows and shrinks with the orders.
  2450. 2:04:47Behind the scenes, keeping strangers
  2451. 2:04:50code apart on shared machines is serious
  2452. 2:04:53work. Each function runs inside its own
  2453. 2:04:56sealed compartment, so one customer's
  2454. 2:04:59code can never peek at another's. In
  2455. 2:05:022018, Amazon released the technology it
  2456. 2:05:06had built for this, a way of starting
  2457. 2:05:09tiny, stripped-down virtual machines in
  2458. 2:05:12a fraction of a second so that every
  2459. 2:05:15function could have walls of its own
  2460. 2:05:18without a long wait. Let's follow one
  2461. 2:05:21through. You're on holiday and you
  2462. 2:05:23upload a photo from your phone to a
  2463. 2:05:26photo sharing app. The photo arrives in
  2464. 2:05:29the app storage. That arrival is an
  2465. 2:05:32event. It wakes a function which reads
  2466. 2:05:35the full-size photo and makes three
  2467. 2:05:38smaller copies, one for thumbnails, one
  2468. 2:05:42for phones, and one for tablets. It
  2469. 2:05:45saves them and goes back to sleep. The
  2470. 2:05:48whole thing takes a couple of seconds.
  2471. 2:05:51You never knew it happened. The app's
  2472. 2:05:53owners paid for those couple of seconds
  2473. 2:05:56and nothing else. Serverless has its
  2474. 2:05:59critics and they make fair points. Every
  2475. 2:06:03provider's version works a little
  2476. 2:06:05differently, so code written for one can
  2477. 2:06:08be hard to move to another. Running a
  2478. 2:06:11function far from its database can be
  2479. 2:06:14slow and opening a fresh database
  2480. 2:06:17connection on every cold start can
  2481. 2:06:20strain the database itself. And when a
  2482. 2:06:23system is built from hundreds of small
  2483. 2:06:25functions, each triggered by others, it
  2484. 2:06:29can be hard to see the whole picture at
  2485. 2:06:31once. But the core idea has lasted. Pay
  2486. 2:06:35for what runs, not for what waits. Let
  2487. 2:06:38someone else keep the machines alive.
  2488. 2:06:42There's one thing all these systems have
  2489. 2:06:44in common though, and it causes trouble
  2490. 2:06:47no matter how they're built. When a
  2491. 2:06:49function fails, the platform will often
  2492. 2:06:52run it again automatically. When a queue
  2493. 2:06:55isn't sure a job was done, it hands it
  2494. 2:06:58out again. When a phone doesn't hear
  2495. 2:07:01back from a server, it tries again.
  2496. 2:07:04Retrying is how distributed systems
  2497. 2:07:07survive. But retrying has a dark side.
  2498. 2:07:11If the thing being retried was a
  2499. 2:07:13payment, the customer might be charged
  2500. 2:07:16twice. Fortunately, there's a
  2501. 2:07:18mathematical idea named in 1870 by a
  2502. 2:07:23professor at Harvard that turns out to
  2503. 2:07:26be exactly the right tool. You've used
  2504. 2:07:28it yourself many times in an elevator.
  2505. 2:07:34Chapter 11, Charging Only Once. It's
  2506. 2:07:381870
  2507. 2:07:40in Washington, and a Harvard
  2508. 2:07:42mathematician named Benjamin Peirce is
  2509. 2:07:45presenting a long, demanding piece of
  2510. 2:07:48work to the National Academy of
  2511. 2:07:51Sciences. Peirce was one of the most
  2512. 2:07:54respected American mathematicians of his
  2513. 2:07:57century. He taught at Harvard for
  2514. 2:07:59decades, worked on astronomy and on the
  2515. 2:08:03survey of the American coast, and was
  2516. 2:08:06known for lectures so dense that only
  2517. 2:08:09his best students could follow them. His
  2518. 2:08:12son, Charles Sanders Peirce, would
  2519. 2:08:15become a famous philosopher and logician
  2520. 2:08:18in his own right. The work he presented
  2521. 2:08:21that year was called Linear Associative
  2522. 2:08:24Algebra. It was a survey of strange new
  2523. 2:08:27number systems, algebras in which the
  2524. 2:08:30familiar rules of arithmetic didn't
  2525. 2:08:33always hold. It was printed that same
  2526. 2:08:36year in a small run of lithographed
  2527. 2:08:38copies and later reprinted in a
  2528. 2:08:41mathematics journal by his son. To
  2529. 2:08:43describe the creatures he found in those
  2530. 2:08:46algebras, Pierce needed new words. One
  2531. 2:08:50of them was nilpotent for a quantity
  2532. 2:08:53that becomes zero when you multiply it
  2533. 2:08:56by itself enough times. Another
  2534. 2:08:59described a quantity with a different
  2535. 2:09:01and rather calm property. Multiply it by
  2536. 2:09:04itself and you get exactly itself back.
  2537. 2:09:08A times A equals A. He called that
  2538. 2:09:12quality idempotent. It comes from the
  2539. 2:09:15Latin roots for same and power. Raise it
  2540. 2:09:19to any power and it stays the same. For
  2541. 2:09:23a long time, that word lived quietly in
  2542. 2:09:27algebra textbooks. Then computer
  2543. 2:09:29scientists borrowed it and gave it a
  2544. 2:09:32slightly broader meaning. An action is
  2545. 2:09:35idempotent if doing it twice has exactly
  2546. 2:09:39the same effect as doing it once. You
  2547. 2:09:42know this from everyday life better than
  2548. 2:09:45you might think. When you call an
  2549. 2:09:47elevator, you press the button and it
  2550. 2:09:50lights up. If you're in a hurry, you
  2551. 2:09:52might press it again and again. Five
  2552. 2:09:56presses, ten presses, only one elevator
  2553. 2:10:00comes. Pressing the button doesn't mean
  2554. 2:10:03send another elevator. It means make
  2555. 2:10:06sure an elevator is coming. Once that's
  2556. 2:10:09true, pressing it again changes nothing.
  2557. 2:10:12Compare a light switch that toggles.
  2558. 2:10:15Each press flips the light from on to
  2559. 2:10:18off or off to on. Press it twice and
  2560. 2:10:22you're back where you started. That's
  2561. 2:10:24not idempotent at all. The number of
  2562. 2:10:27presses matters. Or think about a
  2563. 2:10:29thermostat. Set the temperature to 20
  2564. 2:10:32degrees is idempotent. Say it once or
  2565. 2:10:36say it five times, the room ends up at
  2566. 2:10:3920. Raise the temperature by 1 degree is
  2567. 2:10:43not. Say it five times and the room ends
  2568. 2:10:47up five degrees warmer. That's why
  2569. 2:10:50careful designers of interfaces,
  2570. 2:10:53wherever they can, prefer requests that
  2571. 2:10:56state the result they want over requests
  2572. 2:10:59that describe a change. Set the quantity
  2573. 2:11:03in the basket to two survives being
  2574. 2:11:06repeated. Add one more to the basket
  2575. 2:11:08does not. You may remember from the
  2576. 2:11:11chapter on rest that get, put, and
  2577. 2:11:14delete were designed to be idempotent.
  2578. 2:11:17Fetching book 42 twice changes nothing.
  2579. 2:11:21Replacing it with the same record twice
  2580. 2:11:24leaves the same record. Deleting it
  2581. 2:11:27twice leaves it deleted. But post, which
  2582. 2:11:31means add something new, isn't. Post an
  2583. 2:11:35order twice and you might have two
  2584. 2:11:37orders. And that's where payments come
  2585. 2:11:40in. Here's the situation that keeps
  2586. 2:11:43payment engineers up at night. Your
  2587. 2:11:46phone sends a request to charge your
  2588. 2:11:48card $25.
  2589. 2:11:51It waits for a reply. 10 seconds pass
  2590. 2:11:55and nothing comes back. The phone gives
  2591. 2:11:58up and shows an error. Consider what
  2592. 2:12:01might actually have happened. There are
  2593. 2:12:04three possibilities. Maybe the request
  2594. 2:12:07never reached the server at all, lost
  2595. 2:12:10somewhere on a weak connection. Maybe it
  2596. 2:12:13reached the server which charged your
  2597. 2:12:15card perfectly and then the reply was
  2598. 2:12:18lost on its way back. Or maybe the
  2599. 2:12:21server is still working on it, held up
  2600. 2:12:24by a slow bank. From the phone's side,
  2601. 2:12:27all three look exactly the same.
  2602. 2:12:30Silence. If the phone doesn't retry and
  2603. 2:12:34the first possibility was the true one,
  2604. 2:12:37you never paid and your order never
  2605. 2:12:39happens. If the phone does retry and the
  2606. 2:12:43second possibility was true, you've just
  2607. 2:12:46been charged twice, $50 instead of 25.
  2608. 2:12:51There's no way for the phone to know
  2609. 2:12:53which. This isn't a matter of better
  2610. 2:12:56programming. It's built into the nature
  2611. 2:12:59of sending messages across an unreliable
  2612. 2:13:02network. When you don't hear back, you
  2613. 2:13:05can't tell whether your message was lost
  2614. 2:13:08or the answer was. So, instead of trying
  2615. 2:13:11to know, engineers make the retry safe.
  2616. 2:13:15They turn a non-idempotent action,
  2617. 2:13:18charge this card, into an idempotent
  2618. 2:13:21one. The tool for this is called an
  2619. 2:13:24idempotency key, and the payments
  2620. 2:13:26company Stripe, founded in 2010
  2621. 2:13:30by two brothers from Ireland, Patrick
  2622. 2:13:33and John Collison, helped make it
  2623. 2:13:35familiar to a whole generation of
  2624. 2:13:38developers. Here's how it works. Before
  2625. 2:13:41the phone sends the payment for the
  2626. 2:13:43first time, it creates a long, random
  2627. 2:13:47string of letters and numbers, so long
  2628. 2:13:50that no two phones will ever create the
  2629. 2:13:52same one by chance. That string is the
  2630. 2:13:56idempotency key. It means in effect this
  2631. 2:14:00particular attempt to pay for this
  2632. 2:14:02particular order. The phone sends the
  2633. 2:14:05key along with the request. If it ever
  2634. 2:14:08needs to retry, it sends the same key
  2635. 2:14:12again. On the server's side, the first
  2636. 2:14:15time a key arrives, the server does the
  2637. 2:14:18work and writes down the key along with
  2638. 2:14:21the result. When a request arrives with
  2639. 2:14:24a key it has seen before, the server
  2640. 2:14:27doesn't do the work again. It simply
  2641. 2:14:30looks up the saved result and sends that
  2642. 2:14:33back. Let's walk through it with times.
  2643. 2:14:36At 9:00 and no seconds, your phone sends
  2644. 2:14:40the payment request. $25
  2645. 2:14:44with a key we'll call key K. 1 second
  2646. 2:14:48later, the server receives it. Key K is
  2647. 2:14:52new. It charges your card $25
  2648. 2:14:56and writes down key K, payment number
  2649. 2:14:59881
  2650. 2:15:01succeeded. At two seconds past nine, the
  2651. 2:15:05server sends its reply. Somewhere in a
  2652. 2:15:08tunnel between two phone masts, the
  2653. 2:15:11reply is lost. By 10 seconds past nine,
  2654. 2:15:15your phone has given up waiting. It
  2655. 2:15:18waits a second, then tries again with
  2656. 2:15:21the same key K. At 11 seconds past nine,
  2657. 2:15:26the server receives the retry. It looks
  2658. 2:15:29up key K and finds it. This payment has
  2659. 2:15:33already been made, so it doesn't charge
  2660. 2:15:36your card again. It sends back the saved
  2661. 2:15:39answer. Payment 881
  2662. 2:15:43succeeded. Your phone shows a green
  2663. 2:15:46tick. Your card has been charged $25
  2664. 2:15:51exactly once. Stripe's own documentation
  2665. 2:15:55describes the details. The key travels
  2666. 2:15:58in a header on the request called
  2667. 2:16:01idempotency key. The server saves the
  2668. 2:16:04result of the first request, even if it
  2669. 2:16:07was an error, and returns that same
  2670. 2:16:10result to any retry with the same key.
  2671. 2:16:14Stripe also checks that a retry asks for
  2672. 2:16:17the same thing as the original and
  2673. 2:16:20refuses it if someone tries to reuse a
  2674. 2:16:23key for a different payment. And as of
  2675. 2:16:262026,
  2676. 2:16:28it keeps keys for at least a day, long
  2677. 2:16:31enough to cover any sensible run of
  2678. 2:16:33retries. On the server's side, there's a
  2679. 2:16:36detail that matters more than it looks.
  2680. 2:16:39The note that says key K was used and
  2681. 2:16:43the record of the payment itself should
  2682. 2:16:46be saved together in one step so that
  2683. 2:16:49one can never exist without the other.
  2684. 2:16:52If the server charged the card and then
  2685. 2:16:55crashed before writing down the key, the
  2686. 2:16:58retry would find no key and charge
  2687. 2:17:01again.
  2688. 2:17:03both at once, the way the database
  2689. 2:17:05lecture described transactions, closes
  2690. 2:17:09that gap. There's one more subtle case.
  2691. 2:17:12Two copies of the same request might
  2692. 2:17:15arrive at almost exactly the same moment
  2693. 2:17:19before either has finished. A careful
  2694. 2:17:21server notices that the key is already
  2695. 2:17:24being worked on and tells the second
  2696. 2:17:27copy to wait and try again rather than
  2697. 2:17:30doing the work twice in parallel. The
  2698. 2:17:33same idea rescues the welcome emails
  2699. 2:17:36from our queue chapter. Recall the
  2700. 2:17:39worker that sent an email and then
  2701. 2:17:41crashed before it could say done, so the
  2702. 2:17:44email went out twice. If each ticket
  2703. 2:17:47carries a key and the worker writes down
  2704. 2:17:51which keys it has finished, then a
  2705. 2:17:53ticket that comes back a second time can
  2706. 2:17:56be recognized and set aside. At least
  2707. 2:17:59once delivery, combined with idempotent
  2708. 2:18:02handling, adds up to an effect that
  2709. 2:18:05happens just once. It's one of the
  2710. 2:18:07quietest and most useful tricks in the
  2711. 2:18:10whole craft. Retrying well has one more
  2712. 2:18:14ingredient and it's about patience. If a
  2713. 2:18:18server is struggling, the worst thing
  2714. 2:18:21every client can do is retry instantly,
  2715. 2:18:25all together. Thousands of phones, all
  2716. 2:18:29failing at the same moment and all
  2717. 2:18:31retrying at the same moment, hit the
  2718. 2:18:34struggling server with a second wave
  2719. 2:18:37just when it can least cope. So
  2720. 2:18:39well-behaved clients back off. After the
  2721. 2:18:42first failure, wait 1 second. After the
  2722. 2:18:46second, 2 seconds. Then 4, then 8. Each
  2723. 2:18:50wait is double the last. Four retries
  2724. 2:18:54spread across 15 seconds of waiting,
  2725. 2:18:57which gives a troubled server room to
  2726. 2:18:59recover. And they add a little
  2727. 2:19:01randomness to each wait, a touch of
  2728. 2:19:04jitter, so that thousands of phones
  2729. 2:19:07don't all come back at precisely the
  2730. 2:19:09same instant. Instead of a crowd
  2731. 2:19:12charging the door together, they drift
  2732. 2:19:14back in ones and twos. So the elevator
  2733. 2:19:18button teaches the whole lesson. Make
  2734. 2:19:21your actions safe to repeat, then repeat
  2735. 2:19:24them gently until you're sure. But even
  2736. 2:19:28gentle retries can add up. One phone
  2737. 2:19:31politely backing off is fine. A million
  2738. 2:19:34phones doing it at once, along with all
  2739. 2:19:37the ordinary traffic, can still be more
  2740. 2:19:41than a backend can handle. And not every
  2741. 2:19:43client is polite. Some programs, through
  2742. 2:19:47bugs or bad intentions, send requests as
  2743. 2:19:51fast as their machines allow. A backend
  2744. 2:19:54needs a way to say kindly, but firmly,
  2745. 2:19:58that's enough for now. And one of the
  2746. 2:20:01neatest ways to do that involves an
  2747. 2:20:04imaginary bucket slowly filling with
  2748. 2:20:07tokens.
  2749. 2:20:10Chapter 12, not too many at once. It's
  2750. 2:20:141986
  2751. 2:20:16and an engineer named Jonathan Turner, a
  2752. 2:20:19professor at Washington University in
  2753. 2:20:21St. Louis, is thinking about the
  2754. 2:20:23networks of the future. He's been
  2755. 2:20:26working on designs for fast packet
  2756. 2:20:28networks, the kind that might one day
  2757. 2:20:31carry voice, video, and data all
  2758. 2:20:35together over the same lines. And he can
  2759. 2:20:38see a problem coming. In a shared
  2760. 2:20:40network, one greedy user sending far
  2761. 2:20:44more than their fair share could swamp
  2762. 2:20:47everyone else. The network needs a way
  2763. 2:20:50to hold each user to a promise, an
  2764. 2:20:53agreed average rate, while still
  2765. 2:20:55allowing a little burst now and then.
  2766. 2:20:58That year, in an article in a magazine
  2767. 2:21:01for communications engineers, Turner
  2768. 2:21:04described a simple mechanism for doing
  2769. 2:21:07exactly that. He called it a leaky
  2770. 2:21:10bucket. Picture an ordinary bucket with
  2771. 2:21:13a small hole in the bottom. Water drips
  2772. 2:21:16out of the hole at a steady rate, no
  2773. 2:21:19matter what. You can pour water in as
  2774. 2:21:21fast as you like, in splashes or in a
  2775. 2:21:25trickle. If you pour slowly, the bucket
  2776. 2:21:28never fills. If you pour a quick splash,
  2777. 2:21:31the bucket holds it for a moment and
  2778. 2:21:34lets it drip away. But if you keep
  2779. 2:21:36pouring faster than the hole can drain,
  2780. 2:21:39the bucket fills to the brim, and
  2781. 2:21:42anything more spills over the side and
  2782. 2:21:45is lost. In Turner's version, the water
  2783. 2:21:49was packets. The size of the hole set
  2784. 2:21:52the average rate a user was allowed. The
  2785. 2:21:55size of the bucket set how big a burst
  2786. 2:21:58could be absorbed.
  2787. 2:22:00And the water that spilled over was
  2788. 2:22:02traffic the network would refuse. In the
  2789. 2:22:05machine itself, of course, there was no
  2790. 2:22:08water at all. Turner's bucket was just a
  2791. 2:22:11counter, nudged up by one with every
  2792. 2:22:14packet that arrived and down by one at a
  2793. 2:22:18steady tick, with a line drawn near the
  2794. 2:22:21top. The idea spread through
  2795. 2:22:23telecommunications.
  2796. 2:22:25A version of it became part of the
  2797. 2:22:28standards for a type of high-speed
  2798. 2:22:30network in the 1990s.
  2799. 2:22:33And a close cousin with the same
  2800. 2:22:35behavior seen from the other side became
  2801. 2:22:38the favorite tool of backend engineers.
  2802. 2:22:42It's called the token bucket. The token
  2803. 2:22:45bucket turns the picture upside down.
  2804. 2:22:48Instead of water leaking out, tokens
  2805. 2:22:51drip in. Say there's a bucket that can
  2806. 2:22:53hold 10 tokens. A machine drops two new
  2807. 2:22:57tokens into it every second until it's
  2808. 2:23:00full. Whenever a request arrives, it
  2809. 2:23:03must take one token from the bucket to
  2810. 2:23:06be allowed through. If there's a token,
  2811. 2:23:09the request goes ahead. If the bucket is
  2812. 2:23:12empty, the request is turned away. It's
  2813. 2:23:15like an arcade that hands out tokens at
  2814. 2:23:18a slow, steady rate. You can save up a
  2815. 2:23:21handful and spend them all at once on a
  2816. 2:23:24favorite game. But once your handful is
  2817. 2:23:27gone, you can only play as fast as new
  2818. 2:23:30tokens arrive.
  2819. 2:23:32Let's follow one client through it
  2820. 2:23:34second by second. At the start, the
  2821. 2:23:38bucket is full, 10 tokens. The client's
  2822. 2:23:41app opens and it fires off a burst of 10
  2823. 2:23:45requests at once to load everything on
  2824. 2:23:48its first screen. Each takes a token.
  2825. 2:23:52All 10 go through. The bucket is now
  2826. 2:23:55empty. An 11th request arrives in the
  2827. 2:23:58same instant. There's no token for it.
  2828. 2:24:01It's refused. 1 second later, two new
  2829. 2:24:05tokens have dripped in. The client sends
  2830. 2:24:08two more requests and both go through.
  2831. 2:24:12Empty again. If the client keeps
  2832. 2:24:15hammering away as fast as it can, it
  2833. 2:24:18gets exactly two requests through every
  2834. 2:24:21second, no more. Over the first 10
  2835. 2:24:24seconds, that's the 10 it saved up plus
  2836. 2:24:27two for each of the 10 seconds that
  2837. 2:24:30followed, 30 requests in all. Now say
  2838. 2:24:33the client goes quiet for 5 seconds. Two
  2839. 2:24:37tokens a second for 5 seconds is 10
  2840. 2:24:41tokens. The bucket is full again and the
  2841. 2:24:44client could send another burst of 10.
  2842. 2:24:47So the bucket does two things at once.
  2843. 2:24:50It allows short bursts up to the size of
  2844. 2:24:54the bucket and over any longer stretch,
  2845. 2:24:57it holds the client to the average rate,
  2846. 2:25:00two a second. Two numbers, the bucket
  2847. 2:25:04size and the drip rate, and you've
  2848. 2:25:06described a whole policy. When a request
  2849. 2:25:09is turned away, a well-designed back end
  2850. 2:25:13says so clearly. The web has a status
  2851. 2:25:16code just for this, 429,
  2852. 2:25:20which means too many requests. It was
  2853. 2:25:24added to the standards in 2012.
  2854. 2:25:27Along with it, the back end can send a
  2855. 2:25:30hint called retry after, saying how many
  2856. 2:25:33seconds to wait. A polite client reads
  2857. 2:25:36that hint and waits exactly as the
  2858. 2:25:40backing off clients in the last chapter
  2859. 2:25:42did. In practice, a back end usually
  2860. 2:25:46keeps a separate bucket for each
  2861. 2:25:48customer or each account or each key
  2862. 2:25:51used to call its API. One heavy user
  2863. 2:25:55empties only their own bucket. Everyone
  2864. 2:25:58else's stays full. Many public APIs
  2865. 2:26:01publish their limits openly so
  2866. 2:26:04developers can plan around them. GitHub,
  2867. 2:26:07for instance, has long allowed a
  2868. 2:26:09signed-in user 5,000 requests an hour to
  2869. 2:26:13its main interface and still did as of
  2870. 2:26:172026.
  2871. 2:26:18Every reply carries a small note saying
  2872. 2:26:22how many requests remain in the current
  2873. 2:26:24allowance and when it refills, rather
  2874. 2:26:28like a fuel gauge on the dashboard. And
  2875. 2:26:31the buckets have to be shared across all
  2876. 2:26:33the servers. If there are 10 servers
  2877. 2:26:36behind the load balancer, each keeping
  2878. 2:26:39its own private buckets, a client could
  2879. 2:26:43get 10 times its allowance by spreading
  2880. 2:26:46requests across them. So the token
  2881. 2:26:49counts often live in a fast shared
  2882. 2:26:52store, such as the Redis we met earlier
  2883. 2:26:55tonight, where every server can check
  2884. 2:26:58and take tokens from the same bucket in
  2885. 2:27:00a single quick step. Some simpler limits
  2886. 2:27:04just count requests in fixed windows of
  2887. 2:27:07time, 100 requests per minute, with the
  2888. 2:27:10count starting fresh at the top of every
  2889. 2:27:13minute. That's easy to build, but it has
  2890. 2:27:16a funny weak spot. A client could send
  2891. 2:27:19100 requests at 59 seconds past the
  2892. 2:27:23minute and another 100 at the very start
  2893. 2:27:26of the next minute. Each window sees
  2894. 2:27:28only 100, but the server has just
  2895. 2:27:31received 200 requests in about 2
  2896. 2:27:34seconds. The token bucket doesn't have
  2897. 2:27:38this weak spot because its tokens drip
  2898. 2:27:41in smoothly with no edges to slip
  2899. 2:27:44between. Rate limits are a kind of
  2900. 2:27:47fairness. They make sure that one buggy
  2901. 2:27:50program stuck in a loop or one person
  2902. 2:27:53trying to copy an entire catalog can't
  2903. 2:27:56crowd out everyone else. They're also a
  2904. 2:27:59kind of protection for the back end
  2905. 2:28:02itself, keeping it inside the load it
  2906. 2:28:04was built to carry. But limits at the
  2907. 2:28:07front door are only half the story.
  2908. 2:28:10Sometimes the trouble starts deeper
  2909. 2:28:12inside. Say a back end takes orders and
  2910. 2:28:16passes them to a slower service that
  2911. 2:28:18prints shipping labels. On an ordinary
  2912. 2:28:21day, the label service keeps up. Then a
  2913. 2:28:25big sale begins. Orders arrive faster
  2914. 2:28:29than labels can be printed, and the
  2915. 2:28:31queue between them starts to grow. If
  2916. 2:28:34nothing pushes back, the queue keeps
  2917. 2:28:37growing. Memory fills. Every order waits
  2918. 2:28:41longer and longer. Eventually, something
  2919. 2:28:45runs out of room and falls over, often
  2920. 2:28:48taking other things with it. The answer
  2921. 2:28:51is called back pressure. When a part of
  2922. 2:28:54the system is overwhelmed, it signals
  2923. 2:28:57the parts upstream to slow down rather
  2924. 2:29:00than silently letting work pile up. A
  2925. 2:29:03kitchen does this naturally. Above the
  2926. 2:29:06pass where finished plates go out,
  2927. 2:29:09there's a rail where order tickets are
  2928. 2:29:11clipped. When the rail is full and the
  2929. 2:29:14cooks are flat out, a good head chef
  2930. 2:29:17tells the front of house to hold off
  2931. 2:29:19seating new tables for a few minutes.
  2932. 2:29:22That sounds unwelcoming, but the
  2933. 2:29:25alternative is worse. If the host keeps
  2934. 2:29:28seating people regardless, the tickets
  2935. 2:29:31pile up. Every table waits an hour and
  2936. 2:29:34the food that finally arrives is cold.
  2937. 2:29:38In software, back pressure often begins
  2938. 2:29:41with a limit on the length of a queue.
  2939. 2:29:44Say the queue for printing labels can
  2940. 2:29:46hold 10,000 tickets. When it's full, the
  2941. 2:29:50order service is told, wait, there's no
  2942. 2:29:54room. The order service, in turn, might
  2943. 2:29:57slow down how fast it accepts new orders
  2944. 2:30:01or tell customers that labels will be
  2945. 2:30:04printed a little later. The pressure
  2946. 2:30:06passes backward step by step until it
  2947. 2:30:09reaches the edge of the system where
  2948. 2:30:12something sensible can be done with it.
  2949. 2:30:14A short wait, a polite message, a rate
  2950. 2:30:18limit that tightens for a few minutes.
  2951. 2:30:21And when there truly is more work than
  2952. 2:30:23the system can handle, careful engineers
  2953. 2:30:26decide in advance what to let go of
  2954. 2:30:29first. Loading the recommendations on a
  2955. 2:30:32product page can wait. Taking the
  2956. 2:30:35payment for an order cannot. Engineers
  2957. 2:30:38call this load shedding. Like a crew in
  2958. 2:30:41a storm, you throw the deck chairs
  2959. 2:30:43overboard and you keep the lifeboats.
  2960. 2:30:47All of these ideas share one quiet
  2961. 2:30:49principle, the one Air Lang's waiting
  2962. 2:30:52lines taught us a few chapters ago. A
  2963. 2:30:55system that's allowed to run at full
  2964. 2:30:57stretch has no room left to absorb a
  2965. 2:31:00surprise. Limits, buckets, and pushback
  2966. 2:31:04keep a little room in reserve so that a
  2967. 2:31:06busy evening stays a busy evening and
  2968. 2:31:10doesn' become a collapse. But all of
  2969. 2:31:12this assumes you know what's happening
  2970. 2:31:14inside your system, that the queue is
  2971. 2:31:17growing, that one client is using far
  2972. 2:31:20more than its share. That one service
  2973. 2:31:23has started answering slowly in a back
  2974. 2:31:27end spread across hundreds of machines.
  2975. 2:31:30That knowledge doesn't come for free. A
  2976. 2:31:33request from your phone might pass
  2977. 2:31:35through dozens of services before it
  2978. 2:31:38comes back. And when it comes back
  2979. 2:31:40slowly, which one was to blame is far
  2980. 2:31:43from obvious. Around 2010,
  2981. 2:31:47engineers at Google published a paper
  2982. 2:31:50describing how they had learned to
  2983. 2:31:52follow a single request on its whole
  2984. 2:31:55journey, like dropping a little dye into
  2985. 2:31:58a river and watching where the colored
  2986. 2:32:00water flows.
  2987. 2:32:04Chapter 13, Seeing Inside a Running
  2988. 2:32:07System. It's the mid 2000s in Mountain
  2989. 2:32:11View, and a young Google engineer named
  2990. 2:32:14Ben Sigelman is working on a question
  2991. 2:32:17that sounds simple, why one particular
  2992. 2:32:20search was slow. When you type a few
  2993. 2:32:23words into Google's search box and press
  2994. 2:32:25enter, your request doesn't go to one
  2995. 2:32:28computer. It fans out. One service
  2996. 2:32:32checks your spelling. Others search
  2997. 2:32:34different slices of the index of the web
  2998. 2:32:37in parallel across many machines.
  2999. 2:32:41Another picks advertisements. Another
  3000. 2:32:44fetches the little snippets of text
  3001. 2:32:46shown under each result. Each of those
  3002. 2:32:49may call others in turn. A single search
  3003. 2:32:53might touch thousands of machines and
  3004. 2:32:56still come back in a fraction of a
  3005. 2:32:58second most of the time. Now and then, a
  3006. 2:33:02search takes noticeably longer. And when
  3007. 2:33:05engineers tried to find out why, they
  3008. 2:33:08ran into a wall. Each service kept its
  3009. 2:33:11own records. Each could say how it was
  3010. 2:33:15doing on average, but none of them could
  3011. 2:33:17say what had happened to one particular
  3012. 2:33:20request as it hopped from service to
  3013. 2:33:23service. It was like trying to find out
  3014. 2:33:26why one parcel arrived late by asking
  3015. 2:33:30each post office how busy it had been
  3016. 2:33:33that week. So Sigelman and his
  3017. 2:33:35colleagues built a system to follow
  3018. 2:33:38individual requests on their whole
  3019. 2:33:40journey. They called it Dapper. In 2010
  3020. 2:33:45they described it in a paper and it
  3021. 2:33:47became one of the most influential
  3022. 2:33:50descriptions of how to watch a large
  3023. 2:33:52back end at work. To see what they
  3024. 2:33:55built, it helps to know the three main
  3025. 2:33:58ways engineers look inside a running
  3026. 2:34:00system. They often call them metrics,
  3027. 2:34:04logs, and traces. Metrics are numbers
  3028. 2:34:08measured over time. How many requests
  3029. 2:34:10arrived this second? How many failed?
  3030. 2:34:14How long they took? How full the queue
  3031. 2:34:16is? How much memory each server is
  3032. 2:34:19using? Metrics are cheap to collect and
  3033. 2:34:22easy to draw as graphs, so they're
  3034. 2:34:25usually the first thing an engineer
  3035. 2:34:27looks at. They're like the gauges on a
  3036. 2:34:29car's dashboard. They tell you that
  3037. 2:34:32something is wrong, but rarely why. A
  3038. 2:34:35team might set an alarm to sound if,
  3039. 2:34:39say, more than one request in a hundred
  3040. 2:34:42starts failing for five minutes in a
  3041. 2:34:44row, and then turn to the other two
  3042. 2:34:47views to find out what happened. Logs
  3043. 2:34:50are written records of individual
  3044. 2:34:52events. Order 4018
  3045. 2:34:56received, payment approved, email
  3046. 2:34:59queued. Each line has a time and some
  3047. 2:35:03details. Logs are like a diary kept by
  3048. 2:35:07each program, and they're invaluable
  3049. 2:35:10when you know roughly where to look. But
  3050. 2:35:12a busy system writes millions of lines a
  3051. 2:35:15minute, scattered across hundreds of
  3052. 2:35:18machines, and finding the handful that
  3053. 2:35:21matter can be like looking for one
  3054. 2:35:24sentence in a library. Traces are the
  3055. 2:35:27third view, and they're what Dapper made
  3056. 2:35:29famous. A trace follows one single
  3057. 2:35:33request from the moment it enters the
  3058. 2:35:35system to the moment the answer goes
  3059. 2:35:38back through every service it touches.
  3060. 2:35:41The way it works is rather elegant. When
  3061. 2:35:44a request first arrives, it's given a
  3062. 2:35:46unique number, a trace number. Every
  3063. 2:35:50time one service calls another on that
  3064. 2:35:52request's behalf, it passes the trace
  3065. 2:35:55number along like a tag tied to a
  3066. 2:35:58parcel. Each piece of work done along
  3067. 2:36:01the way, each call and its reply is
  3068. 2:36:04recorded as a span. A span notes which
  3069. 2:36:08service did the work, when it started,
  3070. 2:36:11when it finished, and which span called
  3071. 2:36:13it. Put all the spans with the same
  3072. 2:36:16trace number together, and you get a
  3073. 2:36:18tree. The top of the tree is the whole
  3074. 2:36:21request. Below it are the calls it made.
  3075. 2:36:25Below those are the calls they made. And
  3076. 2:36:28because every span has a start and an
  3077. 2:36:31end, you can draw them as bars on a
  3078. 2:36:33timeline, one under another. Restaurant
  3079. 2:36:37kitchens have something like this. In
  3080. 2:36:40some, every order ticket gets a
  3081. 2:36:42timestamp as it moves through the
  3082. 2:36:45kitchen, received at the pass, started
  3083. 2:36:48on the grill, off the grill, sauce
  3084. 2:36:51added, plated, out the door. If one
  3085. 2:36:54table's dinner took 40 minutes, the
  3086. 2:36:57manager can read the stamps and see
  3087. 2:37:00exactly where the time went. 12 minutes
  3088. 2:37:03waiting for a free spot on the grill, as
  3089. 2:37:05it turns out, and everything else was
  3090. 2:37:08quick. That's what a trace does for a
  3091. 2:37:11request. Let's read one. Your request to
  3092. 2:37:15load a product page took 900,000
  3093. 2:37:18of a second, almost a whole second, when
  3094. 2:37:21it normally takes a tenth. The trace
  3095. 2:37:24shows the top span, 900,000
  3096. 2:37:28in total. Beneath it, the services it
  3097. 2:37:31called one after another are laid out in
  3098. 2:37:34a row. The product detail service took
  3099. 2:37:3815, the price service took 12, the stock
  3100. 2:37:42service took 10, and the reviews service
  3101. 2:37:45took 840.
  3102. 2:37:47There's your answer, sitting in plain
  3103. 2:37:50sight, a single long bar among short
  3104. 2:37:53ones. And because the reviews span has
  3105. 2:37:56spans of its own beneath it, you can
  3106. 2:37:59follow it down. The reviews service
  3107. 2:38:02called the database, which was waiting
  3108. 2:38:05on a lock held by a slow nightly job.
  3109. 2:38:08The problem had nothing to do with
  3110. 2:38:10product pages at all. Recording every
  3111. 2:38:13span of every request at Google scale
  3112. 2:38:17would have been far too expensive, so
  3113. 2:38:19Dapper sampled. In its first version, it
  3114. 2:38:23recorded about one request in every
  3115. 2:38:251,024.
  3116. 2:38:27That sounds like very few, but when a
  3117. 2:38:30service handles many thousands of
  3118. 2:38:32requests a second, one in a thousand
  3119. 2:38:35still means plenty of examples of every
  3120. 2:38:38kind of slowness. And because so little
  3121. 2:38:41was recorded, the cost was tiny. The
  3122. 2:38:44paper reported that in their tests,
  3123. 2:38:47Dapper's overhead was a small fraction
  3124. 2:38:50of a percent. The paper inspired a whole
  3125. 2:38:53family of tools outside Google. Twitter
  3126. 2:38:57released one called Zipkin in 2012. And
  3127. 2:39:01in later years, the industry gathered
  3128. 2:39:04around shared open standards for traces
  3129. 2:39:07so that services written by different
  3130. 2:39:10teams in different languages could all
  3131. 2:39:13pass the trace number along in the same
  3132. 2:39:16way. Now, there's one more idea in this
  3133. 2:39:20chapter, and it's about how to read the
  3134. 2:39:22numbers themselves. It may be the most
  3135. 2:39:25useful thing tonight for anyone who ever
  3136. 2:39:28has to judge whether a system is fast.
  3137. 2:39:31Averages lie. Say a service handles a
  3138. 2:39:34hundred requests. Ninety-eight of them
  3139. 2:39:38take fifty thousandths of a second. Two
  3140. 2:39:41of them take three whole seconds. Add
  3141. 2:39:44them all up and divide by a hundred, and
  3142. 2:39:47the average comes to a hundred and nine
  3143. 2:39:50thousandths of a second. A tenth of a
  3144. 2:39:53second, roughly. That sounds fine, but
  3145. 2:39:56not a single request actually took a
  3146. 2:39:59tenth of a second. Ninety-eight were
  3147. 2:40:02quicker than that, and two were nearly
  3148. 2:40:05thirty times slower than the average. So
  3149. 2:40:08engineers use percentiles.
  3150. 2:40:11Line up all one hundred requests from
  3151. 2:40:14fastest to slowest. The one in the
  3152. 2:40:16middle is the median, also called the
  3153. 2:40:20fiftieth percentile. Here it's fifty
  3154. 2:40:23thousandths of a second. That's what a
  3155. 2:40:25typical request feels like. Now look
  3156. 2:40:29near the slow end of the line. The
  3157. 2:40:31ninety-ninth percentile is the time that
  3158. 2:40:35ninety-nine percent of requests beat,
  3159. 2:40:38and one percent don't. Here it's three
  3160. 2:40:41seconds. That's what your unluckiest
  3161. 2:40:44regular visitors feel. The median tells
  3162. 2:40:47you about the typical day. The
  3163. 2:40:49ninety-ninth percentile tells you about
  3164. 2:40:52the bad moments. And for a busy back
  3165. 2:40:55end, the bad moments matter far more
  3166. 2:40:58than they seem to. In two thousand
  3167. 2:41:00thirteen, two Google engineers, Jeffrey
  3168. 2:41:03Dean and Luiz André Barroso, who was
  3169. 2:41:07also one of the authors of the Dapper
  3170. 2:41:09paper, published an article called The
  3171. 2:41:11Tail at Scale. The tail is the slow end
  3172. 2:41:15of that line of requests, and their
  3173. 2:41:18point was that in a large system, the
  3174. 2:41:21tail comes to dominate. Here's their
  3175. 2:41:24example. Say a single server is fast
  3176. 2:41:28almost all the time, but one request in
  3177. 2:41:31a hundred takes a full second. Now say
  3178. 2:41:35one user's request has to fan out to a
  3179. 2:41:38hundred such servers and can't finish
  3180. 2:41:42until every one of them has answered.
  3181. 2:41:44The chance that any one server is fast
  3182. 2:41:48is 99 in a hundred. The chance that all
  3183. 2:41:51hundred are fast is 99 hundredths
  3184. 2:41:55multiplied by itself a hundred times.
  3185. 2:41:59That comes to about 37%.
  3186. 2:42:02So about 63%
  3187. 2:42:05of user requests will be slow. A problem
  3188. 2:42:08that touches one request in a hundred on
  3189. 2:42:11each server ends up touching nearly two
  3190. 2:42:15in three requests for the people
  3191. 2:42:17actually using the system. That's the
  3192. 2:42:20same arithmetic we saw with
  3193. 2:42:22microservices,
  3194. 2:42:23where small chances of failure
  3195. 2:42:25multiplied. Here, it's small chances of
  3196. 2:42:29slowness multiplied. Dean and Barroso
  3197. 2:42:33described ways to tame the tail. One was
  3198. 2:42:36the hedged request. If an answer hasn't
  3199. 2:42:39come back within the time that 95% of
  3200. 2:42:43requests usually manage, send the same
  3201. 2:42:46request to a second server and use
  3202. 2:42:49whichever answers first. Only the
  3203. 2:42:52slowest one in 20 requests ever gets a
  3204. 2:42:55second copy, so the extra load stays
  3205. 2:42:58small, but the long waits largely
  3206. 2:43:01vanish. So the craft of watching a back
  3207. 2:43:05end comes down to a few habits. Keep the
  3208. 2:43:08gauges on the dashboard, keep a diary in
  3209. 2:43:12every program, tag every request so its
  3210. 2:43:15whole journey can be followed. And when
  3211. 2:43:18you judge speed, look at the slow end of
  3212. 2:43:21the line, not just the middle. All of
  3213. 2:43:24this tells you very precisely when
  3214. 2:43:27things are going wrong, but it raises a
  3215. 2:43:30harder question, one that's as much
  3216. 2:43:33about people as machines. The question
  3217. 2:43:36is, how good counts as good enough? A
  3218. 2:43:40system that's never allowed to fail can
  3219. 2:43:43never change. A system that changes too
  3220. 2:43:46freely will fail too often. Somewhere
  3221. 2:43:50between them is a line, and someone has
  3222. 2:43:53to decide where to draw it. At Google,
  3223. 2:43:57around 2003,
  3224. 2:43:59a software engineer was handed a small
  3225. 2:44:02team of seven and asked to keep the
  3226. 2:44:04company's production systems running.
  3227. 2:44:07The answer he came up with turned the
  3228. 2:44:10question into a number and gave it a
  3229. 2:44:13name.
  3230. 2:44:15Chapter 14, Breaking Things on Purpose.
  3231. 2:44:19It's 2003
  3232. 2:44:21in Mountain View, and a software
  3233. 2:44:24engineer named Ben Trainer has just
  3234. 2:44:27joined Google. He'll later be known as
  3235. 2:44:30Ben Trainer Schloss. Google is already
  3236. 2:44:34one of the busiest sites on the
  3237. 2:44:36Internet, and someone has to keep its
  3238. 2:44:38production systems running, the real
  3239. 2:44:41machines serving real people. Trainer is
  3240. 2:44:44given that job along with a small team,
  3241. 2:44:48which he later described as a production
  3242. 2:44:50team of seven engineers. Until then, by
  3243. 2:44:54his own account, his working life had
  3244. 2:44:57been spent writing software. He'd never
  3245. 2:45:00run an operations team, and the
  3246. 2:45:02traditional way of running one in most
  3247. 2:45:05companies looked something like this.
  3248. 2:45:08Developers wrote new features and threw
  3249. 2:45:10them over a wall. On the other side, a
  3250. 2:45:14separate operations team installed them,
  3251. 2:45:17watched the servers, and got woken up
  3252. 2:45:19when things broke. The developers wanted
  3253. 2:45:22to ship changes quickly. The operations
  3254. 2:45:25team wanted nothing to change because
  3255. 2:45:27change is what breaks things. The two
  3256. 2:45:30sides were, in a quiet way, permanently
  3257. 2:45:34at odds. Treynor approached it as a
  3258. 2:45:36software engineer would.
  3259. 2:45:39of hiring more and more people to watch
  3260. 2:45:42screens and restart servers by hand, his
  3261. 2:45:45team would write software to do that
  3262. 2:45:48work, automate the repetitive tasks,
  3263. 2:45:51measure everything, treat reliability
  3264. 2:45:54itself as an engineering problem with
  3265. 2:45:57numbers and designs rather than a matter
  3266. 2:46:00of heroics. He called the new discipline
  3267. 2:46:03site reliability engineering, or SRE.
  3268. 2:46:07When he later described it, he put it
  3269. 2:46:09roughly this way. It's what happens when
  3270. 2:46:12you ask a software engineer to design an
  3271. 2:46:15operations team. The idea grew. Google's
  3272. 2:46:19SRE teams spread across the company, and
  3273. 2:46:23in 2016, several of its engineers
  3274. 2:46:26published a book describing how they
  3275. 2:46:28worked, which became a standard text far
  3276. 2:46:32beyond Google. Two of Treynor's ideas
  3277. 2:46:35are worth slowing down for. The first
  3278. 2:46:38was a rule about time. An SRE team was
  3279. 2:46:42expected to spend at least half of its
  3280. 2:46:44time on engineering, building tools, and
  3281. 2:46:47improving systems, and no more than
  3282. 2:46:50about half on operational chores, the
  3283. 2:46:53repetitive manual work that SRE teams
  3284. 2:46:56came to call toil. If the chores crept
  3285. 2:47:00above that, it was a sign that something
  3286. 2:47:03needed to be automated or handed back to
  3287. 2:47:06the developers. The second idea is the
  3288. 2:47:09one that resolves the old argument
  3289. 2:47:12between developers and operations. It's
  3290. 2:47:15called the error budget. It starts from
  3291. 2:47:18a slightly surprising claim. 100%
  3292. 2:47:22reliability is the wrong goal for nearly
  3293. 2:47:26everything. That sounds backward, so
  3294. 2:47:28let's see why. Your phone's connection,
  3295. 2:47:32your home Wi-Fi, the app itself all fail
  3296. 2:47:36now and then. If a service is already
  3297. 2:47:38more reliable than the phone and network
  3298. 2:47:42in between, most users can't tell the
  3299. 2:47:44difference when it gets even more
  3300. 2:47:47reliable. Meanwhile, every extra step
  3301. 2:47:50toward perfection costs more and moves
  3302. 2:47:54more slowly. To be perfectly safe, you'd
  3303. 2:47:57never release anything new at all. So
  3304. 2:48:00instead of aiming for perfect, the team
  3305. 2:48:03and the business agree on a target, say
  3306. 2:48:0699.9%.
  3307. 2:48:09Engineers call that target a service
  3308. 2:48:12level objective. It's measured against
  3309. 2:48:15something concrete, usually the share of
  3310. 2:48:18real requests that succeed and succeed
  3311. 2:48:21quickly enough. Then whatever is left
  3312. 2:48:24over, the gap between the target and
  3313. 2:48:27100% becomes a budget. It's an amount of
  3314. 2:48:31failure the team is allowed to spend.
  3315. 2:48:34Let's work out how big that budget is. A
  3316. 2:48:37month of 30 days has 43,200
  3317. 2:48:42minutes in it. 1/10 of 1% of that is
  3318. 2:48:4643.2
  3319. 2:48:48minutes. So a service with a target of
  3320. 2:48:5199.9%
  3321. 2:48:54is allowed about 43 minutes of trouble a
  3322. 2:48:57month. At 99.99%
  3323. 2:49:01the allowance shrinks to a tenth of
  3324. 2:49:03that, just over 4 minutes a month. At
  3325. 2:49:0799% it grows 10 times larger to about 7
  3326. 2:49:12hours and 12 minutes. Each extra 9
  3327. 2:49:15shrinks the budget tenfold and usually
  3328. 2:49:19costs a great deal more to achieve.
  3329. 2:49:22Engineers often describe these targets
  3330. 2:49:25simply by counting the nines. Three
  3331. 2:49:28nines, four nines, five nines, Five
  3332. 2:49:32nines, 99.999%
  3333. 2:49:36leaves room for only about 26 seconds of
  3334. 2:49:40trouble in a whole month. Very few
  3335. 2:49:43services truly need that, and the ones
  3336. 2:49:46that do pay handsomely for it. Budgets
  3337. 2:49:50can be counted in requests too. If a
  3338. 2:49:53service expects 10 million requests in a
  3339. 2:49:56month, a 99.9%
  3340. 2:49:59target allows 10,000 of them to fail.
  3341. 2:50:03Now, here's the clever part. The budget
  3342. 2:50:06is something to spend. Say it's the
  3343. 2:50:09middle of the month. A new release had a
  3344. 2:50:11bug that caused 15 minutes of errors
  3345. 2:50:14before it was rolled back. The team has
  3346. 2:50:17spent 15 of its 43 minutes. About 28
  3347. 2:50:21remain. They can keep releasing new
  3348. 2:50:24features carefully. But say that a week
  3349. 2:50:28later a second problem uses up the rest.
  3350. 2:50:31The budget is gone. Under the agreement,
  3351. 2:50:34new feature releases pause until the
  3352. 2:50:37next month. Apart from urgent fixes, the
  3353. 2:50:40team turns its attention to making
  3354. 2:50:43things sturdier. The beauty of this is
  3355. 2:50:46that nobody has to argue. Developers
  3356. 2:50:49want to move quickly, and they can as
  3357. 2:50:52long as there's budget left. When there
  3358. 2:50:54isn't, everyone can see it in plain
  3359. 2:50:57numbers, and the decision makes itself.
  3360. 2:51:01The same number that sets the pace of
  3361. 2:51:03change also protects the people who use
  3362. 2:51:06the service. Five years after Trainer
  3363. 2:51:09arrived at Google, a company in
  3364. 2:51:12California learned about reliability the
  3365. 2:51:15hard way. In August 2008, Netflix was
  3366. 2:51:19still mostly a business that mailed DVDs
  3367. 2:51:23to people's homes in red envelopes. That
  3368. 2:51:26month, its main database suffered a
  3369. 2:51:29serious corruption. For three days, the
  3370. 2:51:32company couldn't ship DVDs to its
  3371. 2:51:34members. Netflix's engineers drew a
  3372. 2:51:37lesson from it. Their system had
  3373. 2:51:39depended on big, single pieces of
  3374. 2:51:42equipment in their own data center, and
  3375. 2:51:45when one of those failed, everything
  3376. 2:51:48stopped. They decided to move to the
  3377. 2:51:50cloud, onto many smaller rented machines
  3378. 2:51:55from Amazon Web Services, and to build
  3379. 2:51:58their software so that no single machine
  3380. 2:52:01mattered. The move took about seven
  3381. 2:52:04years. The last pieces of the streaming
  3382. 2:52:07service left Netflix's own data centers
  3383. 2:52:10in early January 2016.
  3384. 2:52:13But in the cloud, machines do fail
  3385. 2:52:17routinely. A server can vanish at any
  3386. 2:52:20moment. So Netflix's engineers asked
  3387. 2:52:23themselves how they could be sure that
  3388. 2:52:26their software really did survive losing
  3389. 2:52:29a machine, not just in theory, but in
  3390. 2:52:32practice on a busy evening. Their answer
  3391. 2:52:36was to make it happen on purpose. They
  3392. 2:52:38built a program called Chaos Monkey. Its
  3393. 2:52:42job was simple. It wandered through
  3394. 2:52:44Netflix's production systems and, at
  3395. 2:52:47random, switched off servers, real
  3396. 2:52:51servers, serving real customers. In July
  3397. 2:52:552011, two Netflix engineering directors,
  3398. 2:52:59Yury Izrailevsky and Ariel Tseitlin,
  3399. 2:53:02described it publicly in a post about
  3400. 2:53:05what they called the Simian Army. They
  3401. 2:53:08asked readers to picture a wild monkey
  3402. 2:53:11loose in a data center, unplugging
  3403. 2:53:14machines and pulling out cables at
  3404. 2:53:17random while the service carried on for
  3405. 2:53:20customers as if nothing had happened.
  3406. 2:53:23That sounds reckless, but there was a
  3407. 2:53:25careful idea underneath. Chaos Monkey
  3408. 2:53:28ran during the working day while
  3409. 2:53:31engineers were at their desks, watching
  3410. 2:53:34closely and ready to step in. If
  3411. 2:53:36switching off a server caused a problem,
  3412. 2:53:39the problem appeared at two in the
  3413. 2:53:41afternoon with the right people in the
  3414. 2:53:44room, rather than at three on a Sunday
  3415. 2:53:47morning when nobody was looking. Each
  3416. 2:53:50problem found that way could be fixed
  3417. 2:53:53for good. Over time, the system learned
  3418. 2:53:56to shrug off losing a server because it
  3419. 2:53:59had practiced losing servers every day.
  3420. 2:54:03It's the same reason schools hold fire
  3421. 2:54:06drills on a calm afternoon. Nobody wants
  3422. 2:54:09a fire. But if there is one, the moment
  3423. 2:54:13to discover that a door sticks is during
  3424. 2:54:16the drill, not the night of the real
  3425. 2:54:18thing. A kitchen might do something
  3426. 2:54:21similar. A head chef who quietly takes
  3427. 2:54:24one cook off the line for an hour on a
  3428. 2:54:27quiet Tuesday finds out whether the rest
  3429. 2:54:30of the brigade can cover that station.
  3430. 2:54:33Escoffier's system was built around
  3431. 2:54:36exactly that kind of cover. The Simian
  3432. 2:54:39Army had other members. Latency Monkey
  3433. 2:54:42added artificial delays to calls between
  3434. 2:54:45services to see how they coped with
  3435. 2:54:48slowness rather than outright failure.
  3436. 2:54:51Janitor Monkey cleaned up unused
  3437. 2:54:53resources. And a larger cousin, Chaos
  3438. 2:54:57Gorilla, simulated the loss of an entire
  3439. 2:55:00zone of Amazon data centers to check
  3440. 2:55:04that traffic would move over to the
  3441. 2:55:06others on its own. Later came Chaos
  3442. 2:55:09Kong, which rehearsed losing an entire
  3443. 2:55:12region, a whole cluster of data centers
  3444. 2:55:16in one part of the world. None of this
  3445. 2:55:19started at full strength. Teams that run
  3446. 2:55:21experiments like these usually begin
  3447. 2:55:24small with one server in one corner of
  3448. 2:55:28the system for a few minutes and widen
  3449. 2:55:31the experiment only as their confidence
  3450. 2:55:33grows. Engineers call the area an
  3451. 2:55:36experiment could possibly affect its
  3452. 2:55:39blast radius. And a first rule is to
  3453. 2:55:42keep it small. In 2012,
  3454. 2:55:46Netflix released Chaos Monkey as open
  3455. 2:55:49source. Over the years, the practice
  3456. 2:55:52grew into a field with its own name,
  3457. 2:55:55Chaos Engineering. Its central idea is
  3458. 2:55:58simple. Form a guess about how your
  3459. 2:56:01system will behave when something
  3460. 2:56:03breaks. Then break it gently, in a
  3461. 2:56:06controlled way, and see if you were
  3462. 2:56:09right. Site reliability engineering and
  3463. 2:56:12chaos engineering fit together. One
  3464. 2:56:15decides how much failure is acceptable.
  3465. 2:56:18The other goes looking for failures
  3466. 2:56:21before they find you. But some failures
  3467. 2:56:24can't be found by unplugging machines.
  3468. 2:56:27Some come from the outside world, from
  3469. 2:56:30the strange and slippery thing that
  3470. 2:56:33every backend has to keep track of in
  3471. 2:56:35every single request. Time. On the last
  3472. 2:56:39night of June 2012, the world's official
  3473. 2:56:43clocks added one extra second to the
  3474. 2:56:46day. And at midnight, by the world's
  3475. 2:56:49reference clock, a surprising number of
  3476. 2:56:51computers did not know what to do with
  3477. 2:56:54it.
  3478. 2:56:56Chapter 15. What time is it? Really,
  3479. 2:57:00it's the last night of June 2012. On the
  3480. 2:57:03world's reference clock, the time scale
  3481. 2:57:06called Coordinated Universal Time or
  3482. 2:57:09UTC, the seconds tick toward midnight.
  3483. 2:57:1357, 58, 59. And then, instead of rolling
  3484. 2:57:19over to midnight, the clock shows
  3485. 2:57:21something it almost never shows. 23:59
  3486. 2:57:26and 60 seconds. A leap second. The
  3487. 2:57:29reason for it is the Earth itself. Our
  3488. 2:57:32everyday time was once defined by the
  3489. 2:57:35turning of the planet. But the Earth's
  3490. 2:57:37spin isn't perfectly steady. It wobbles
  3491. 2:57:41and slows very slightly with tides and
  3492. 2:57:45with the shifting of the planets
  3493. 2:57:46insides. Atomic clocks meanwhile keep
  3494. 2:57:50time with almost perfect regularity.
  3495. 2:57:54the years the two drift apart. So from
  3496. 2:57:571972 onward every so often the official
  3497. 2:58:01keepers of time added a single extra
  3498. 2:58:04second to UTC to keep the clocks within
  3499. 2:58:08a second of the Earth's rotation. Most
  3500. 2:58:11people never noticed. But in June 2012
  3501. 2:58:15some computers noticed very much. That
  3502. 2:58:18weekend the link sharing site Reddit
  3503. 2:58:21reported problems that it tied to the
  3504. 2:58:23leap second. Engineers at Mozilla, the
  3505. 2:58:27makers of the Firefox browser, found
  3506. 2:58:30some of their Java based services
  3507. 2:58:32struggling. LinkedIn had an outage the
  3508. 2:58:35same day and in Australia the airline
  3509. 2:58:39Qantas reported delays which some
  3510. 2:58:42reports linked to trouble in a
  3511. 2:58:43reservation system it relied on. Much of
  3512. 2:58:47the trouble traced back to a bug in the
  3513. 2:58:49way some versions of Linux, the
  3514. 2:58:52operating system on a great many
  3515. 2:58:54servers, handled the extra second. On
  3516. 2:58:57affected machines certain programs
  3517. 2:59:00started spinning using their processors
  3518. 2:59:03flat out while doing nothing useful.
  3519. 2:59:06Engineers around the world spent their
  3520. 2:59:09weekend discovering one after another
  3521. 2:59:12that the cure was often surprisingly
  3522. 2:59:14simple. Setting the system clock again
  3523. 2:59:17by hand usually calmed things down. One
  3524. 2:59:21company had seen it coming. A year
  3525. 2:59:24earlier in 2011
  3526. 2:59:27Google had described a trick it called
  3527. 2:59:30the leap smear. Instead of adding a
  3528. 2:59:32whole second at once its time servers
  3529. 2:59:36spread the extra second across many
  3530. 2:59:38hours making each tick a tiny bit longer
  3531. 2:59:42than normal. By the The the leap second
  3532. 2:59:45arrived Google's clocks had already
  3533. 2:59:48absorbed it in slices too small for any
  3534. 2:59:51program to stumble over. A clock can't
  3535. 2:59:55trip on a step it never sees. If you
  3536. 2:59:58heard part three of our computer science
  3537. 3:00:01series, you'll remember the trouble with
  3538. 3:00:03keeping distant clocks in agreement.
  3539. 3:00:06Tonight we'll stay on the back end side
  3540. 3:00:09of that story with the everyday business
  3541. 3:00:12of writing down the time because time
  3542. 3:00:15for a back end is everywhere. Every
  3543. 3:00:18order has a time, every log line, every
  3544. 3:00:22payment, every message, every session
  3545. 3:00:25that expires, and time is far stranger
  3546. 3:00:29than it looks. To see why, we need to go
  3547. 3:00:33back to a day in 1883
  3548. 3:00:36that had two noons. Before then, most
  3549. 3:00:39towns in the United States kept their
  3550. 3:00:42own local time set by the sun. When the
  3551. 3:00:45sun was highest in the sky, it was noon,
  3552. 3:00:49and that moment comes a little later in
  3553. 3:00:51each town as you travel west. So noon in
  3554. 3:00:55one city might be 11 minutes after noon
  3555. 3:00:58in another. There were more than 100
  3556. 3:01:00different local times across the
  3557. 3:01:02country. For the railroads, this was
  3558. 3:01:05chaos. Timetables had to list departures
  3559. 3:01:09in one town's time and arrivals in
  3560. 3:01:12another's. So the railroads agreed to
  3561. 3:01:14fix it themselves. A railway official
  3562. 3:01:18named William F. Allen proposed a plan
  3563. 3:01:21of four time zones across the country,
  3564. 3:01:24each one hour apart. And on Sunday, the
  3565. 3:01:2818th of November 1883,
  3566. 3:01:31they switched over. At noon on that day,
  3567. 3:01:35clocks in many towns were stopped or set
  3568. 3:01:38back or moved forward to match the new
  3569. 3:01:41zones. In places on the eastern edge of
  3570. 3:01:44a zone, noon arrived twice, once by the
  3571. 3:01:48old sun time and once by the new
  3572. 3:01:51standard time zones spread around the
  3573. 3:01:54world and with them came a tangle that
  3574. 3:01:57back end engineers still deal with every
  3575. 3:02:00day. Then came daylight saving time
  3576. 3:02:04which shifts the clocks forward an hour
  3577. 3:02:07in spring and back an hour in autumn in
  3578. 3:02:11some places but not others on dates that
  3579. 3:02:14vary by country and that governments
  3580. 3:02:17change sometimes with only a few weeks
  3581. 3:02:20notice. So the first rule of time in a
  3582. 3:02:23back end is a simple one. Store every
  3583. 3:02:27moment in UTC. UTC doesn't have time
  3584. 3:02:31zones. It doesn't have daylight saving.
  3585. 3:02:35Its clock just ticks forward the same
  3586. 3:02:38everywhere on earth. Leap seconds aside
  3587. 3:02:41it's as calm as time gets. Then convert
  3588. 3:02:44to local time only at the very edges.
  3589. 3:02:48When a person types in a time convert it
  3590. 3:02:51to UTC before saving it. When you show a
  3591. 3:02:54time to a person convert from UTC to
  3592. 3:02:58their local time just before it appears
  3593. 3:03:00on the screen. It keeps the logs honest
  3594. 3:03:04too. Say a back end runs in data centers
  3595. 3:03:07in Dublin and Tokyo and each server
  3596. 3:03:10writes its log in its own local time.
  3597. 3:03:13When something goes wrong an engineer
  3598. 3:03:16trying to line up the two logs has to do
  3599. 3:03:19mental arithmetic for every single line.
  3600. 3:03:239 hours here an hour there. If both
  3601. 3:03:26write in UTC the events simply fall into
  3602. 3:03:30order one after another like a single
  3603. 3:03:33story. Converting at the edges matters
  3604. 3:03:37for people's plans too. Say someone in
  3605. 3:03:39New York books a call for 9 in the
  3606. 3:03:42morning. In January New York keeps
  3607. 3:03:46Eastern Standard Time which is 5 hours
  3608. 3:03:49behind UTC. So 9 in the morning in New
  3609. 3:03:52York is 1400 in UTC. 2 in the afternoon.
  3610. 3:03:58In July, New York keeps daylight time,
  3611. 3:04:01which is only 4 hours behind UTC. So 9
  3612. 3:04:05in the morning there is 1300 in UTC. 1
  3613. 3:04:09in the afternoon. Same local time,
  3614. 3:04:13different universal times depending on
  3615. 3:04:16the date. A back end that simply stored
  3616. 3:04:199:00 without saying where or when would
  3617. 3:04:23get one of those wrong. And there's a
  3618. 3:04:25subtler trap. Say the call is a weekly
  3619. 3:04:28meeting at 9 every Monday morning, New
  3620. 3:04:32York time. If the back end stores it as
  3621. 3:04:351400 UTC, the meeting is right all
  3622. 3:04:39winter. Then in March, the clocks spring
  3623. 3:04:43forward and suddenly 1400 UTC is 10 in
  3624. 3:04:48the morning in New York. The meeting has
  3625. 3:04:51slid an hour later and nobody moved it.
  3626. 3:04:54So for future events tied to people's
  3627. 3:04:57lives, careful back ends store the local
  3628. 3:05:01time together with the name of the
  3629. 3:05:03place's time zone. 9 in the morning,
  3630. 3:05:06America/New York. Then they work out the
  3631. 3:05:10exact UTC moment for each meeting when
  3632. 3:05:14the date comes using the rules as they
  3633. 3:05:17stand. Those rules live in an unusual
  3634. 3:05:21shared record called the Time Zone
  3635. 3:05:24Database. It was started in the 1980s by
  3636. 3:05:28a programmer named Arthur David Olson.
  3637. 3:05:32And for many years now, it has been
  3638. 3:05:34edited by Paul Eggert, a computer
  3639. 3:05:37scientist at the University of
  3640. 3:05:39California, Los Angeles. It records the
  3641. 3:05:42time rules of every region of the world,
  3642. 3:05:46past and present, using place names like
  3643. 3:05:49America/New York or Europe/London.
  3644. 3:05:53Volunteers update it several times a
  3645. 3:05:56year whenever some government somewhere
  3646. 3:05:59changes its clocks. Almost every phone,
  3647. 3:06:02computer, and server on earth carries a
  3648. 3:06:05copy. Daylight saving has two more traps
  3649. 3:06:09and they catch back ends every year. In
  3650. 3:06:12the United States, clocks spring forward
  3651. 3:06:15on the second Sunday in March. At 2:00
  3652. 3:06:18in the morning, the time jumps straight
  3653. 3:06:21to 3:00. So on that night, 2:30 in the
  3654. 3:06:24morning never happens. If a nightly job
  3655. 3:06:28is scheduled for 2:30 local time, it may
  3656. 3:06:32simply not run. In November, the clocks
  3657. 3:06:35fall back. At 2:00 in the morning, the
  3658. 3:06:38time returns to 1:00. So 1:30 happens
  3659. 3:06:41twice. A job scheduled for 1:30 might
  3660. 3:06:45run twice. A log might show events that
  3661. 3:06:48seem to go backward in time. That's why
  3662. 3:06:51many teams schedule their nightly jobs
  3663. 3:06:54in UTC or at times like 4:00 in the
  3664. 3:06:58morning that daylight saving never
  3665. 3:07:01touches and countries don't change their
  3666. 3:07:04clocks on the same day. Europe moves its
  3667. 3:07:07clocks on the last Sunday in March, a
  3668. 3:07:10couple of weeks after the United States.
  3669. 3:07:13For those weeks, the gap between New
  3670. 3:07:16York and London is 4 hours instead of
  3671. 3:07:19the usual 5. Every year, somebody misses
  3672. 3:07:23a meeting because of it. There's one
  3673. 3:07:25last rule and it's about measuring how
  3674. 3:07:28long something takes. If you want to
  3675. 3:07:30time a request, don't subtract one wall
  3676. 3:07:34clock reading from another. The wall
  3677. 3:07:37clock can jump. It might be corrected by
  3678. 3:07:40a time server or fall back an hour or
  3679. 3:07:43meet a leap second. A request that
  3680. 3:07:46really took a second could appear to
  3681. 3:07:48have taken minus an hour. So computers
  3682. 3:07:52also keep a second kind of clock called
  3683. 3:07:55a monotonic clock, which only ever moves
  3684. 3:07:59forward at a steady rate and doesn't
  3685. 3:08:02care what time it is. It can't tell you
  3686. 3:08:05the date, but it can tell you reliably
  3687. 3:08:08how long something took. The leap second
  3688. 3:08:11itself may soon fade into history. In
  3689. 3:08:152022,
  3690. 3:08:17the international body that oversees
  3691. 3:08:20weights and measures voted to stop
  3692. 3:08:23adding leap seconds by 2035,
  3693. 3:08:26letting atomic time and the Earth's
  3694. 3:08:28rotation drift further apart before
  3695. 3:08:32anyone needs to reconcile them.
  3696. 3:08:34Engineers everywhere quietly breathed
  3697. 3:08:37out. So time for a back end comes down
  3698. 3:08:42to a few humble habits. Store moments in
  3699. 3:08:45UTC. Keep the places zone name for
  3700. 3:08:49future human plans. Use a clock that
  3701. 3:08:52only moves forward for measuring, and
  3702. 3:08:55never assume that a day has 24 hours or
  3703. 3:08:59that a minute has 60 seconds. Habits
  3704. 3:09:02like these are what separate a back end
  3705. 3:09:05that mostly works from one that keeps
  3706. 3:09:08working year after year through every
  3707. 3:09:12odd night on the calendar. And in 2011,
  3708. 3:09:16an engineer at a cloud company in San
  3709. 3:09:19Francisco tried to write down a whole
  3710. 3:09:22set of such habits, 12 of them drawn
  3711. 3:09:25from watching many thousands of
  3712. 3:09:27applications live and die.
  3713. 3:09:32Chapter 16, the 12 factors. It's 2011
  3714. 3:09:37in San Francisco, and an engineer named
  3715. 3:09:41Adam Wiggins is writing down what he has
  3716. 3:09:43seen. A few years earlier, in 2007,
  3717. 3:09:48Wiggins and two friends, James
  3718. 3:09:50Lindenbaum and Orion Henry, had started
  3719. 3:09:53a company called Heroku. Its promise was
  3720. 3:09:57simple. Write your web application, send
  3721. 3:10:00it to Heroku with one command, and
  3722. 3:10:03Heroku would run it for you. No servers
  3723. 3:10:06to set up, no machines to look after. At
  3724. 3:10:09first, it ran only Ruby applications,
  3725. 3:10:13many of them built with Rails, and later
  3726. 3:10:16it took on many other languages. In
  3727. 3:10:192010, the software company Salesforce
  3728. 3:10:23bought it. From where they sat, the
  3729. 3:10:25Heroku team had an unusual view. They
  3730. 3:10:29could watch a vast number of
  3731. 3:10:30applications being built, launched,
  3732. 3:10:33grown, and sometimes slowly falling
  3733. 3:10:37apart. Some ran smoothly for years,
  3734. 3:10:41others broke every time anyone touched
  3735. 3:10:43them, and the differences Wiggins
  3736. 3:10:45noticed often came down to a handful of
  3737. 3:10:48habits. So he wrote those habits down as
  3738. 3:10:52a short document with 12 sections. He
  3739. 3:10:56called it the 12 Factor App. It was
  3740. 3:10:59published as a website, and it spread
  3741. 3:11:02steadily from team to team until it had
  3742. 3:11:05become a kind of shared checklist for
  3743. 3:11:08how a modern backend ought to behave. In
  3744. 3:11:11November 2024,
  3745. 3:11:14Heroku released the document as an open
  3746. 3:11:17project so the wider community could
  3747. 3:11:19keep it up to date. It's a fitting place
  3748. 3:11:22to end tonight because almost every
  3749. 3:11:24factor is something we've already met.
  3750. 3:11:27The first factor is one codebase, kept
  3751. 3:11:31in version control, deployed to many
  3752. 3:11:33places. One recipe book used in every
  3753. 3:11:37branch of the restaurant. Second,
  3754. 3:11:40declare all your dependencies,
  3755. 3:11:43every outside library the program needs,
  3756. 3:11:46explicitly so that a fresh machine can
  3757. 3:11:49be set up from scratch with nothing
  3758. 3:11:52assumed. The third factor is about
  3759. 3:11:55configuration,
  3760. 3:11:56and it's worth a worked example. Say a
  3761. 3:11:59shop has two copies of its back end
  3762. 3:12:02running. One is called staging, where
  3763. 3:12:05the team tries out changes. The other is
  3764. 3:12:09production, serving real customers. The
  3765. 3:12:12code is identical. What differs is the
  3766. 3:12:15settings. Staging talks to a test
  3767. 3:12:18database and a test payment account.
  3768. 3:12:22Production talks to the real ones. The
  3769. 3:12:2412-factor rule is that those settings
  3770. 3:12:28must never be written into the code
  3771. 3:12:30itself. They live in the environment
  3772. 3:12:32around the program, supplied when it
  3773. 3:12:35starts. The same build, number 41, runs
  3774. 3:12:39in staging with the test settings. When
  3775. 3:12:42it's been tried out, the very same
  3776. 3:12:45build, unchanged, runs in production
  3777. 3:12:48with the real settings. That means
  3778. 3:12:51nobody ever builds a special version for
  3779. 3:12:54production that was never tested. And
  3780. 3:12:57the secret keys for the real payment
  3781. 3:12:59account never sit in the codebase, where
  3782. 3:13:02anyone with access to the code could
  3783. 3:13:05read them. It's like a chef who uses the
  3784. 3:13:08same recipe in two kitchens, and only
  3785. 3:13:11the address of the supplier changes. The
  3786. 3:13:14fourth factor says to treat backing
  3787. 3:13:17services, the database, the cache, the
  3788. 3:13:21queue, the email service, as attached
  3789. 3:13:24resources. Each one is reached through
  3790. 3:13:28an address in the configuration.
  3791. 3:13:30If the database moves to a new machine,
  3792. 3:13:33you change one setting and the program
  3793. 3:13:36connects to the new one. Redis, Rabbit
  3794. 3:13:40MQ, the payment service, all plug in the
  3795. 3:13:43same way. Factor five separates
  3796. 3:13:47building, releasing, and running. A
  3797. 3:13:50build turns code into something
  3798. 3:13:52runnable. A release combines a build
  3799. 3:13:55with a particular configuration and gets
  3800. 3:13:58a number, release 41. If release 42
  3801. 3:14:03turns out to have a problem, you go back
  3802. 3:14:05to release 41, which still exists
  3803. 3:14:09exactly as it was. That takes moments.
  3804. 3:14:12The sixth factor is one we met in the
  3805. 3:14:15chapter on the fail whale. Run the
  3806. 3:14:17application as stateless processes. Keep
  3807. 3:14:21nothing precious in a server's memory
  3808. 3:14:23between requests. Put the remembering in
  3809. 3:14:26a backing service. Then comes the
  3810. 3:14:29seventh, which says the application
  3811. 3:14:32should offer its services by listening
  3812. 3:14:34on a port, a numbered door on the
  3813. 3:14:37machine, rather than depending on some
  3814. 3:14:39outside server being set up around it.
  3815. 3:14:42Number eight is about concurrency, which
  3816. 3:14:45here means scaling out by running more
  3817. 3:14:49processes. Perhaps 10 processes handle
  3818. 3:14:52web requests and four handle background
  3819. 3:14:56jobs from the queue. On a busy day, make
  3820. 3:14:59it 20 and eight. The ninth factor is
  3821. 3:15:02called disposability,
  3822. 3:15:05and it's the one that makes all the
  3823. 3:15:07others work. Processes should start
  3824. 3:15:09quickly and stop gracefully so they can
  3825. 3:15:13be thrown away and replaced at any
  3826. 3:15:15moment. Let's watch one stop. A web
  3827. 3:15:19process is told to shut down, perhaps
  3828. 3:15:22because a new release is arriving. At
  3829. 3:15:25that moment, it's in the middle of 12
  3830. 3:15:27requests. It stops accepting new ones
  3831. 3:15:31and the load balancer sends fresh
  3832. 3:15:33requests elsewhere. It finishes the 12,
  3833. 3:15:37which takes about two seconds. Then it
  3834. 3:15:40exits. A queue worker told to stop
  3835. 3:15:43halfway through a job hands its
  3836. 3:15:46unfinished ticket back to the broker so
  3837. 3:15:50another worker can pick it up. No order
  3838. 3:15:52lost, no email half sent. That's the
  3839. 3:15:56acknowledgement from the deli counter
  3840. 3:15:58chapter doing its quiet work. The 10th
  3841. 3:16:02factor asks you to keep development,
  3842. 3:16:05staging, and production as alike as
  3843. 3:16:08possible, so that something that works
  3844. 3:16:10on a developer's laptop on Tuesday works
  3845. 3:16:14the same way in production on Wednesday.
  3846. 3:16:1711th, treat logs as a stream of events.
  3847. 3:16:21The program simply writes each event out
  3848. 3:16:24one line after another and lets the
  3849. 3:16:27surrounding platform gather the lines
  3850. 3:16:30from every process into one place. It's
  3851. 3:16:34the log book again and the diary that
  3852. 3:16:37feeds the dashboards from our chapter on
  3853. 3:16:40seeing inside a running system. And the
  3854. 3:16:4312th says that one off administrative
  3855. 3:16:46tasks like fixing a batch of records or
  3856. 3:16:50changing the shape of a table should run
  3857. 3:16:53in the same environment as the
  3858. 3:16:55application using the same code and
  3859. 3:16:58configuration rather than as a separate
  3860. 3:17:01script someone keeps on their own
  3861. 3:17:04laptop. 12 small rules. None of them is
  3862. 3:17:07dramatic. Together they describe a back
  3863. 3:17:11end that can be moved, copied, scaled,
  3864. 3:17:15restarted, and rolled back without fear.
  3865. 3:17:18Not every team follows all 12 to the
  3866. 3:17:21letter and the document has been revised
  3867. 3:17:24and argued over through the years, but a
  3868. 3:17:26great deal of the cloud tooling that
  3869. 3:17:29came afterward simply assumed that
  3870. 3:17:32applications would behave this way.
  3871. 3:17:35Forgetful processes, settings from the
  3872. 3:17:38environment, logs as a stream, quick to
  3873. 3:17:42start and gentle to stop. The habits
  3874. 3:17:45Wiggins wrote down became for many teams
  3875. 3:17:49the ordinary shape of a back end. And
  3876. 3:17:52with that, our night in the back of
  3877. 3:17:54house is almost done. We began in a
  3878. 3:17:57basement in London in 1890
  3879. 3:18:01with Escoffier dividing his kitchen into
  3880. 3:18:04stations so that the plates went out hot
  3881. 3:18:07and in order on the busiest nights. We
  3882. 3:18:11followed a single coffee order through
  3883. 3:18:14four steps, receive, validate, store,
  3884. 3:18:18respond. From there, we met Roy Fielding
  3885. 3:18:21at Irvine, writing down the quiet rules
  3886. 3:18:25that held the web together, and learned
  3887. 3:18:27to treat everything as a resource with
  3888. 3:18:30an address and a handful of verbs. We
  3889. 3:18:33watched Facebook's engineers ask for
  3890. 3:18:36exactly what a phone needed, and Google
  3891. 3:18:40send two bytes where 18 had been. We sat
  3892. 3:18:44with DHH in Copenhagen, and with two
  3893. 3:18:47young developers in a Kansas newsroom,
  3894. 3:18:50pulling the plumbing out of their
  3895. 3:18:52products and giving it away. We saw a
  3896. 3:18:55whale lifted by eight orange birds, and
  3897. 3:18:59learned why servers should remember
  3898. 3:19:01nothing, so that any one of them can be
  3899. 3:19:04replaced like a paper cup. In Sicily, we
  3900. 3:19:08stood by Salvatore San Filippo's fast
  3901. 3:19:11shelf. We took a number at the deli
  3902. 3:19:14counter and found Agner Erlang's
  3903. 3:19:17telephone mathematics hiding inside it.
  3904. 3:19:20We read the ship's logbook that LinkedIn
  3905. 3:19:22built and named after a writer. We heard
  3906. 3:19:25the legend of a memo at Amazon and
  3907. 3:19:28weighed one big kitchen against a food
  3908. 3:19:31hall. We took a taxi instead of buying a
  3909. 3:19:34car and waited for a barista to come in
  3910. 3:19:37from home. We pressed an elevator button
  3911. 3:19:40twice with Benjamin Pearce's word in our
  3912. 3:19:43pocket and were charged exactly once.
  3913. 3:19:47Then we let tokens drip into Jonathan
  3914. 3:19:50Turner's bucket, followed a drop of dye
  3915. 3:19:53through Google services, and learned
  3916. 3:19:55that averages hide the slow end of the
  3917. 3:19:58line. We spent an error budget, let a
  3918. 3:20:01monkey loose in a data center on a calm
  3919. 3:20:04afternoon, lived through a day with two
  3920. 3:20:07noons and a minute with 61 seconds, and
  3921. 3:20:11finished with 12 plain habits written
  3922. 3:20:14down in San Francisco. Through all of
  3923. 3:20:17it, the same few ideas kept returning.
  3924. 3:20:20Check everything. Write it down before
  3925. 3:20:24you promise it. Keep the servers
  3926. 3:20:27forgetful and the records faithful. Make
  3927. 3:20:30every action safe to repeat. Leave room
  3928. 3:20:34for surprises and watch closely so you
  3929. 3:20:37know when the surprise has come.
  3930. 3:20:40Somewhere tonight, in a building you'll
  3931. 3:20:42never see, a quiet machine is receiving
  3932. 3:20:46an order. It checks it, writes it down,
  3933. 3:20:50answers in a fraction of a second, and
  3934. 3:20:52drops a ticket in a queue for the rest.
  3935. 3:20:56A worker picks up the ticket. A log
  3936. 3:20:58grows by one line. A dashboard ticks up
  3937. 3:21:02by one. Nobody notices. That's how it's
  3938. 3:21:06meant to be. Out front, the dining room
  3939. 3:21:08is quiet and the chairs are up on the
  3940. 3:21:11tables. But in the back, the brigade
  3941. 3:21:14Escoffier imagined is still at its
  3942. 3:21:17stations, one ticket at a time. Only now
  3943. 3:21:21the stations are made of software and
  3944. 3:21:24the tickets travel as light. The kitchen
  3945. 3:21:27keeps cooking all night long so that in
  3946. 3:21:30the morning, when you tap a button, the
  3947. 3:21:33green tick is waiting. If you'd like to
  3948. 3:21:35go deeper into the places we only
  3949. 3:21:38glanced at tonight, our lecture on
  3950. 3:21:41database engineering opens up the
  3951. 3:21:44walk-in cooler itself. Our lecture on
  3952. 3:21:47web development follows a request from
  3953. 3:21:49the browser side, and our lecture on
  3954. 3:21:52front-end engineering stays in the
  3955. 3:21:55dining room with the screens and buttons
  3956. 3:21:58you actually touch. But for now, there's
  3957. 3:22:01nothing left to order. The tickets are
  3958. 3:22:03cleared, the stations are wiped down,
  3959. 3:22:06and the lights in the back of house are
  3960. 3:22:09dimming one by one. Sleep well. Good
  3961. 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.