未登录,请登录后再发表信息
最新评论 (0)
播放视频

两个将军的故事

The Two Generals’ Problem

September 2018. I was tired, I was exhausted.
2018年9月 我很累并且疲惫
I’ d just got back home from something,
忙完后 我回到家
and I didn’t have the energy to cook.
也没精力去做饭
So I went to my phone,
所以我拿出手机
opened up a certain food-delivery app that’s popular in the UK,
打开了一个英国热门的外卖APP
and I ordered pizza.
点了份披萨
Now I know: that
我知道 现在
food-delivery company’s employment practices are questionable
外卖公司的员工技能培训受人质疑
there are more ethical ways to get dinner delivered.
也有其他更规范的送餐选择
But I was tired, and I was hungry.
不过那时我身心俱疲 饥肠辘辘
As are a lot of their drivers.
就像很多送餐骑手一样
But that was the Night of the Multiple Orders,
但那天有很多人下单订餐
when a bug in that app meant
App里的错误导致
that some people around Britain ended up with indentical food orders being delivered two or three times,
住在Britain附近的人收到重复两三次的订单
and others got nothing at all.
其他人的什么都没收到
And I nearly got caught up in the chaos.
我差点儿就陷入了这场混乱中
To explain what happened,
在解释事情发生之前
I need to tell you a story about two generals.
我要给你讲一个有关两个将军的故事
The Two Generals’ Problem is a classic of computer science,
“两个将军的问题”是计算机科学的经典问题
and it goes like this: picture a valley.
它是这样的:想象有一个山谷
In the middle of the valley is a heavily fortified castle.
在山谷中有一座固若金汤的城堡
On the edges of the valley are two armies.
山谷的边缘有两支军队
The generals of those armies know
两方的将军都知道
that the only way they can win a battle and overwhelm the castle
他们赢得战役并占领城堡的唯一方法
is if they both attack at the same time.
是他们能够同时进攻
A single army isn’t going to make it.
单独作战不能取得胜利
They need the combined strengt from both sides of the valley to win.
他们需要联合两军的力量来获胜
And the only way they can communicate
而他们交流的唯一途径
is by sending messengers on a risky path through the valley.
是派信使穿过峡谷的那条险恶的小路
And General A won’t know
直到所有人都就位
what the right time to attack is until everyone’s already in position.
将军A才能知道进攻的最佳时机
How can those two generals coordinate
这两个将军如何协调才能
to make sure they attack at the same time?
确保他们同时进攻呢?
This is a magical computer-science-land problem by the way,
顺便一提 这是计算机科学领域的一个奇妙的问题
so reasonable suggestions like “semaphore” or “telescopes” don’ t apply
像“旗语”或“望远镜”的现实选择是不行的
On the surface the problem seems trivial.
从表面看 这个问题很简单
General A could just send a message to General B with a proposed time.
将军A可以通知将军B一个建议时间
say 8 o’clock.
例如8点
But the messenger has to pass through the valley,
但是信使必须要穿过山谷
and if they’re spotted,
如果他们暴露了
they’re,um, not going to make it to the other side to deliver the message.
就不能把消息成功地传给另一个军队
So how does General A know that General B received the message?
所以将军A怎么能知道将军B收到信了
The messenger might not have made it if that happens
假如信使暴露 任务就失败了
A will attack,B won’t…
A准时进攻 而B没有
and they’ll lose.
那么他们就会失败
So maybe they arrange it so
也许他们安排
General B has to send an acknowledgment back
将军B要送一份确认信回来
and General A will only attack if that acknowledgement arrives.
在收到后 将军A再开始进攻
But that now runs into the same problem:
但这会遇到同样的问题
how does B know that A has received the acknowledgement?
将军B怎么知道将军A收到了确认信呢?
If it doesn’t get through,
如果确认信没有送达
A won’t attack, B will,
B展开进攻 而A没有
and a lose.
他们也会失败
General A could send another
将军A收到确认信后
acknowledgement for the acknowledgement.
再发给将军B一封确认信
But how do they know that message has gotten through ball?
但他们怎么知道信是否送达呢?
General B could send an acknowledgment for the acknowledgement
将军B可以给回信的确认信
for the acknowledgement
再发一封确认信
and so on, and so on, and so on.
然后循环下去
This problem is unsolvable.
这个问题是无解的
I know, it feels like there should be some hacky workaround
我知道 这问题似乎有个变通的办法
like sending 200 messengers,
比如派送200名信使
and sure, that would probably work in the real world.
当然 在现实中可能实现
But this is magical information-theory computer-science land.
但这是在奇妙的信息论计算机科学的领域里
Under these strict rules,
在这些严格的规则下
there is never a guarantee,
永远都没有确定的事
there is no certainty,
没有绝对
there is no arrangement that can be made,
这些安排都不能实现
there is no way that the two generals,
两个将军不可能
the two computers sending data
用两台电脑传递数据
can agree that the message has definitely been received and acknowledged.
保证信息一定能传达到和被确认过
now with computers you’re not usually dealing with such high stakes.
而在计算机领域 你就不必冒这么大的风险了
If you are in computer science
如果你在计算机科学领域
and working on a problem that involves potential loss of life,
并且正研究一个可能涉及丧失生命的问题
I really hope you aren’t watching a series called”The Basics”.
我真心希望你没有看过“The Basics”系列
anyway, I was ordering food.
无论如何 那时我正在点餐
And I put my order together,I tapped ‘ pay ’,
我把订单放一起 然后点击’支付’
I put fingerprint on my phone’s reader.
把指尖放在手机的指纹识别处
I got the little Apple Pay progress bar, and the little tick.
我看着苹果支付的进度条 收款滴的响了
And then I got a message from the app saying that there had been a problem,
接着APP弹出一条订单有误的消息
and my order had failed to go through.
我下单失败
Would I like to try again?
我会尝试再下单吗?
And I was about to I was about to hit ‘pay’ again.
然后我正要再次点击“支付”
And then something just in the back of my head,said: hang on.
突然我想起什么:等等
There was a little tick saying payment had worked.
那会儿页面显示我已经支付成功
And I’m enough of a computer nerd to go
我是个十足的电脑迷
“I’m not sure I believe that failed”.
“我可不相信支付失败了”
So I checked the ‘order history’ page.
所以我打开了“历史订单”
it took a few tries to load,
页面加载了很多次
but when it finnally did,
但最后加载完时
there was my order, processing.
显示出我的订单处理中
It had gone through, but the acknowledgement hadn’t come back.
订单被接受了 但是反馈没有发过来
or rather, something had gone wrong on the app’s servers,
也许 这个APP的服务器出错了
and the logic they’d written thought it had failed when it hadn’t.
当反馈没有发送时 他们写的编程出错了
So I sat tight,
所以我焦急地坐着
I hoped that my food would arrive
期望我的外卖能到
and I figured that the engineers were probably having a very bad day.
我想程序员今天可能过得不怎么愉快
They really were,
他们确实是这样
Because I wasn’t the only one.
因为我不是唯一的受害者
People all over the UK ordering via the app again,
英国各地的人都会再次用APP下单
going to the payment screen,
跳转到支付页
hitting the button and getting”try again”.
点击支付 被告知“请重试”
And a lot of them did.
很多人重试了
Again, and again, and again.
还多次重试
They were General A,
他们好比将军A
and the app’s server was General B,
APP服务器就是将军B
and they were part of a real-life, complicated version of the Two Generals’ problem.
他们就是现实中复杂版的两个将军的问题
Imagine all the customers as General A
设想用户们是将军A
sending message after message to General B.
把信息一遍又一遍地发给将军B
B received the messeage dutifully
B尽职地接受信息
took the money from the credit card
从信用卡中提钱
they attacked,
他们行动了
but something had happened that stopped the confirmation message getting through.
但发生了一些情况阻止了正确信息的传达
According to the flood of angry reports on Twitter,
从Twitter上洪水般的愤怒评论来看
sometimes the restaurant would realise the problem and just send one order.
有时候餐馆会察觉到这种问题 然后只送一份餐
Sometimes the restaurant wouldn’t realise
但有时餐馆没有意识到
and three different drivers would arrive with three identical orders.
三个不同的骑手会送来同一份订单
Sometimes no food would arrive at all.
而有时一份外卖也没送到
The app’s customer service line was swamped.
这个APP的客服电话都被打爆了
To be clear:
进一步说:
this was not the sort of thing that is one engineer’s fault.
这不是那种一个程序员失误的问题
When something goes this drastically wrong,
当出现这种重大的失误时
there have been many poor decisions made over a long period of time.
一定是长期中许多错误的决策引起的
A single human error is never a root cause.
一个人的错误从不是最根本的原因
So what else could the app team have done?
所以这个APP团队该怎么做呢?
How can you solve the Two Generals’ problem in the real world?
你应该怎样解决现实版两个将军问题呢?
well,first , maybe no-one should have been able to place two identical orders
好吧 首先 没人会在几分钟内
on the same credit card for the same restaurant,
用一张信用卡在一家饭店下
within a few minutes of each other.
两份完全相同的订单
That seems like the sort of thing there should have been a check for?
似乎这儿该有种核实什么的?
But the real solution is
但真正的解决方案是
an “idempotency token” or an “idempotency key “.
“幂等令牌”或者称为“幂等键”
That’s a unique vally generated on the app or on the website
这是在APP或网络上出现的独特的标志
and it’s a shopping card ID, basically,
根本上说 它是一种购物卡
and it’s sent along with the order.
并且同指令一起被传输
it’s not just for shopping cards though
尽管这不仅用在购物卡上
the idempotency token could be attached to
幂等代码还可能设置在
instructions to delete the oldest log file or send a text message
删除的旧文件或已发送短信的指令中
or anything that you only want to happen once.
以及任何你不希望再次发生的事情上
The server stores the idempotency key to keep track of the request.
服务器保存幂等键来跟踪请求
And if another request arrives with the same key attached,
如果出现了另一个带有相同密钥的请求
then the server knows it’s already dealt with that request.
服务器就会知道这个请求已处理过
So it doesn’t fulfill it again;
所以它就不会再次处理
instead it knows that the reply didn’t get through,
反而它知道回执没有发出去
so it just sends back a copy of that first acknowledgement again.
最终它会再发送最初信息的回执
now that still won’t help before none of the messengers get through
如果联络彻底中断 信息传递不出去
if the connection completely fails,
这仍然没有用
but for real-world problems, humans will notice that.
但在现实生活中 人们会注意到这个问题
Idempotence means that you can request the same thing multiple times
幂等意味你可以多次发出同一事情的请求
and it’ll only ever happen once.
并且它最终只接收一次
That’s the way to fix the Two Generals’ Problem.
这就是两个将军问题的解决办法
I was lucky. I placed one order.
我很幸运 只下一份订单
I was charged for one order.
付了一次款
and one order of food arrived half an hour later.
半小时后 我点的外卖就到了
Next time, I’ll just cook for myself.
下一次 我要自己做饭
This series of The Basics is sponsored by Dashlane, the password manager.
The Basics系列由Dashlane 密码管理器赞助
I mentioned in the previous sponsored section
我在之前的赞助中提到过
that they sync all your passwords
在不知道密码的情况下
and payment details between all your devices without ever knowing those passwords.
它们会同步你所有设备的密码及支付记录
Which sounds a bit like magic.
这听起来有些魔幻
When you sign up to Dashlane,
当你注册Dashlane
you choose a master password.
选择一个主密码
And incidentally, you can do that by
顺便一提 你也可以通过访问
going to dashlane.com/tomscott for a free 30-day trial.
“dashlane.com/tomscott”获取30天免费试用权
anyway, when you sign up,
总之 当你注册的时候
you pick a single Master Password,
你要选一个主密码
and that is never transmitted over the internet.
而且这绝不会在网络上泄露
Not even to Dashlane, or to their servers, nowhere.
也不会泄露给Dashlane及其服务器等
If you don’t know that password,
在不知道密码的情况下
all that private data just looks like random noise.
所有的私密数据 就像是随机噪声一样
when you sign up to a new website
当你在某个网站注册时
and Dashlane generates a long complicated password for you,
Dashlane会为你生成一串复杂密码
it is bundled up and encrypted using your master password,
它会用你自己知道的主密码进行绑定
that only you know.
并将它整合加密
That encryption takes just long enough, a few fractions of a second,
加密过程仅需数秒便可完成
that there’s no way to brute-force it back open.
绝不可能被强行破解
That encrypted bundle gets sent to Dashlane,
经加密后被传送至Dashlane
they just see random noise with a label saying “please synchronise this”.
他们只能接收附着“请同步”的随机噪声
So they pass the bundle on to your other devices,
之后他们将加密密码传输给你的其他设备
and those devices,
是这些设备
and only those devices can decrypt it.
并且只有这些设备才能解密
because at some point,
因为有些观点认为
you’re gonna open up the app and type in your Master Password.
你需要打开APP并输入主密码
In truth, it’s actually a little more secure than that,
实际上它比只用主密码更安全
because behind-the-scenes
因为它们在后台
they also generate a different key for each device you log in to,
为你登录的每个设备生成不同的密码
but that is a whole other level of security
但这是另一个层面的安全性了
that I have actually found it impossible to explain in a script.
我觉得一期课程很难解释清楚
but in the massively unlikely event that
但对于这个不太可能发生的事件来说
someone did compromise Dashlane’s servers
要是真有人破坏了Dashlane的服务器
or bribe some employee, it wouldn’t work.
或贿赂一些程序员 那也没用
All they could do is watch those packets of random noise get shuffled around.
他们只能做到看着一堆随机噪音到处飞
All your data stays on your own devices.
你所有的数据只保存在你自己的设备上
Which means, if you lose your Master Password,
这意味着如果忘了你的主密码
Dashlane can’t help you.
Dashlane也帮不了你
But that’s fine, because now you’ve just got a single password to remember.
但没关系 现在你只需记住一个密码
That is massively more secure than reusing the same password
这从很大程度上来说比重复用同一密码或
or a lot of variations everywhere online.
在网上用很多不同的密码安全多了
Like I said last time: you should use a password manager.
就像我刚才说 你需要一个密码管理器
so dashlane.com/tomscott for
搜索dashlane com/tomscott
a free 30-day trial of Dashlane Premium
获取30的天会员试用
and if you like it
如果你喜欢
you can use the code ” tomscott “for 10 % off at purchase.
你可以凭’tomscott’兑换码享受九折优惠

发表评论

译制信息
视频概述

你经历过重复付款的窘境吗?来了解一下计算机领域“两个将军的问题”,以及这两个问题所蕴涵的奥秘吧!

听录译者

收集自网络

翻译译者

夜暮

审核员

审核员FW

视频来源

https://www.youtube.com/watch?v=IP-rGJKSZ3s

相关推荐