咖啡馆的订单危机 / The Coffee Shop Order Crisis: 为什么我们需要云架构
2018年秋末的上海临港大学城,风里已经飘起了糖炒栗子的香气。林晓蹲在自家“豆仓咖啡馆”的门口擦手冲壶,壶身的铜锈被擦得发亮,像他刚创业时那颗热得发烫的心。三年前他还是张江高科某互联网公司的后端开发,天天996改bug,攒了三年钱又跟家里借了点,在大学城的地下室租了个二十平的小店面,专门做云南保山的单品手冲,没想到刚开业半年就成了校园里的网红店,平时一天能卖七八十杯,周末能到一百杯,刚好是他当初找外包做点单小程序时说的“峰值承载量”。
The crisp autumn wind of late 2018 in Lingang University Town, Shanghai, already carried the sweet scent of roasted chestnuts. Lin Xiao squatted outside his "Bean Warehouse Coffee Shop" polishing a pour-over kettle, its brass surface shining bright as the burning passion he had when he first started his business. Three years prior, he was a backend developer at an internet company in Zhangjiang High-Tech Park, working 996 hours a week fixing bugs. After saving for three years and borrowing some money from his family, he rented a 20-square-meter basement space in the university town to specialize in single-origin pour-over coffee from Baoshan, Yunnan. Unexpectedly, the shop became a local hit within half a year of opening, selling 70 to 80 cups on weekdays and up to 100 on weekends — exactly the "peak capacity" he had quoted to the outsourced developer who built his ordering mini-program.
当初做小程序的时候,外包公司为了省成本,用的是最传统的单块架构(Monolithic Architecture):点单、支付、库存、会员所有功能都写在一个服务里,数据库是单节点的MySQL,部署在一台2核4G的阿里云ECS服务器上。林晓当时算了算,这台服务器一个月只要三百多块,一天一百单的峰值完全扛得住,性价比拉满,就没多想。他哪里知道,这个为了省成本做的决定,会在半个月后让他差点关店。
When he commissioned the mini-program, the outsourcing firm cut costs by using the most traditional monolithic architecture: all functions including ordering, payment, inventory management, and membership were written into a single service, with a single-node MySQL database deployed on an Alibaba Cloud ECS server with 2 cores and 4GB of RAM. Lin Xiao did the math back then: the server cost only about 300 yuan per month, and it could easily handle the peak of 100 orders a day, so he didn't think twice. He had no idea that this cost-cutting decision would almost force him to close his shop half a month later.
校园美食节是大学城每年一度的盛事,今年是第五届,主办方特意把美食节的摊位安排在了咖啡馆门口的路边,还跟林晓说,只要他愿意参与,主办方会帮他发优惠券,保证流量翻三倍。林晓高兴得差点跳起来,提前一个月就囤了价值两万的保山咖啡豆,还找了两名校门口兼职的学妹当服务员,特意跟外包公司提了一句:“今年美食节可能会订单暴增,能不能提前做个扩容准备?”外包满口答应,说“没问题,小意思”,转头就把这事忘了,直到美食节前三天才敷衍地给服务器升了个4核8G的配置,收了林晓五百块“升级费”。
The campus food festival was an annual grand event in the university town, and this year marked its fifth edition. The organizers specifically arranged the festival stalls right outside the coffee shop's entrance, and told Lin Xiao that as long as he participated, they would distribute coupons for him, guaranteeing triple the usual traffic. Lin Xiao was so thrilled he almost jumped for joy. A month in advance, he stocked up on 20,000 yuan worth of Baoshan coffee beans, hired two student girls from the campus as part-time servers, and specifically asked the outsourcing firm to "prepare for a surge in orders during the food festival." The outsourcing firm agreed readily, saying "no problem, it's a piece of cake," but completely forgot about it until three days before the festival, when they perfunctorily upgraded the server to 4 cores and 8GB of RAM, charging Lin Xiao 500 yuan for the "upgrade fee."
美食节当天早上八点半,林晓刚把门打开,还没把烘豆机开起来,门口就涌过来了一群人:动漫社的社长带着十几个社员,要订五十杯冰美式给下午的漫展当后勤;学生会宣传部的干事带着七八个人,要订三十杯手冲给评委当伴手礼;还有几个拍短视频的博主,要订二十杯特调当拍摄道具。林晓还没反应过来,点单小程序的提示音就响得没停过,两分钟就跳了二十单。
At 8:30 a.m. on the day of the food festival, Lin Xiao had just unlocked the shop door and hadn't even turned on the roasting machine when a crowd surged in: the president of the anime club came with a dozen members to order 50 iced Americanos for the afternoon comic exhibition; the publicity department of the student union came with seven or eight people to order 30 pour-overs as gifts for judges; and a few short video bloggers came to order 20 special brews as props for their shoots. Before Lin Xiao could react, the notification sound of the ordering mini-program kept ringing, with 20 orders popping up in two minutes.
一开始林晓还没当回事,直到九点十五分,他点开后台看订单量,居然已经突破了两百单每小时,点单小程序开始卡得转圈圈,付款成功的提示等了半分钟才跳出来。有个穿汉服的小姑娘站在柜台前急得快哭了,她给汉服社订了十二杯手冲,系统卡了半天付不了款,后面还有十几个人排队等着点单。林晓赶紧说“先记着,后面付也行”,手忙脚乱地开始做咖啡,结果刚做了五杯,服务器的提示音响了——运维发来的告警,说CPU使用率已经冲到100%,数据库连接池满了,系统随时可能崩。
At first Lin Xiao didn't take it seriously, until 9:15 a.m. when he checked the backend and saw that the order volume had already exceeded 200 orders per hour. The mini-program started to freeze and spin in circles, and the payment success notification took half a minute to pop up. A girl in hanfu stood at the counter almost crying: she had ordered 12 pour-overs for her hanfu club, but the system had been stuck for a long time and she couldn't pay, with more than ten people waiting in line to order behind her. Lin Xiao quickly said "just write it down, you can pay later," and started making coffee in a panic, but after making just five cups, the server alert sounded — the operations team sent a warning that CPU usage had hit 100%, the database connection pool was full, and the system could crash at any moment.
九点半,灾难还是来了。点单小程序直接跳成了白屏,怎么刷新都没用,后台的订单量已经突破四百单每小时,服务器直接死机了。排队的人群开始骚动,有人骂骂咧咧地走了,有人站在门口不肯走,非要林晓给个说法,那两个兼职学妹吓得手足无措,站在旁边不敢说话。林晓掏出手机给外包打电话,打了三遍才有人接,对方睡眼惺忪地说:“啊?峰值五百单?我们当初设计的时候只考虑了一百单啊,要扩容的话得改架构,至少得两周,加两万块开发费。”
At 9:30 a.m., the disaster struck. The mini-program screen went completely white and wouldn't refresh no matter what. The backend order volume had exceeded 400 orders per hour, and the server crashed directly. The queuing crowd started to get restless: some left cursing, some stood at the door refusing to leave and demanding an explanation from Lin Xiao. The two part-time student girls were so scared they didn't know what to do, standing to the side not daring to speak. Lin Xiao took out his phone and called the outsourcing firm, taking three tries to get someone to answer. The other party said sleepily, "Huh? Peak of 500 orders? We only designed it for 100 orders at most. To expand the capacity, we need to change the architecture, which will take at least two weeks and cost an extra 20,000 yuan in development fees."
林晓气得手都抖了,两周之后美食节都结束了,改架构有什么用?他咬咬牙,决定自己重启服务器试试,反正系统崩了也没办法,死马当活马医。重启花了二十分钟,刚把系统打开,订单量又涌了进来,结果系统撑了不到半小时,又直接白屏了。到中午十二点,峰值订单量居然突破了一千单每小时,林晓彻底没辙了,只能把点单系统关了,改成人工记账,用纸笔记订单,手写付款码,让顾客扫码付给他个人微信。一直到下午三点,人才少了点,林晓和两个学妹对着三百多张手写订单对账,对到凌晨三点,还漏了三十多单,算下来直接损失了一万两千块,还有两百多杯咖啡做了没人付钱,倒掉的时候林晓心疼得直掉眼泪。
Lin Xiao was so angry his hands were shaking. Two weeks later the food festival would be over, what was the use of changing the architecture then? He gritted his teeth and decided to try restarting the server himself — since the system was already broken, he had nothing to lose. The restart took 20 minutes, and as soon as he opened the system, orders surged in again. But the system couldn't hold on for even half an hour before going white again. By noon at 12 p.m., the peak order volume actually exceeded 1,000 orders per hour, and Lin Xiao had no choice but to shut down the ordering system and switch to manual record-keeping, writing down orders on paper and letting customers scan a personal WeChat code to pay. The crowd didn't thin out until 3 p.m., when Lin Xiao and the two student girls went through more than 300 handwritten orders to reconcile accounts until 3 a.m. the next day, still missing more than 30 orders. In total, he lost 12,000 yuan directly, plus more than 200 cups of coffee that were made but never paid for. When he poured them out, Lin Xiao's heart ached so much he almost cried.
那天晚上他躺在出租屋的床上,翻来覆去睡不着,脑子里全是白天顾客骂骂咧咧的脸,还有倒掉的咖啡。他当年在公司的时候,也做过类似的活动系统,知道这种单块架构根本扛不住峰值,当初外包要两万块改架构,他其实不是拿不出来,是刚开业三个月还没回本,舍不得花这笔钱。他翻出以前的技术论坛账号,发了个帖子问:“有没有成本低的方案,能扛住一千单每小时的订单峰值,最好是按使用量付费,不要一次性投入太多钱?”下面很快就有人回复,说他有个同学在做云原生,刚好能解决这个问题,让他第二天去市区的一家咖啡馆见面聊。
That night he lay in bed in his rented apartment, tossing and turning unable to sleep, his mind full of the cursing faces of customers from the day before, and the poured-out coffee. He had worked on similar event systems back when he was at the company, and knew this kind of monolithic architecture couldn't handle peak loads at all. The outsourcing firm had asked for 20,000 yuan to change the ar