在分布式系统的学习过程中,无论是读论文还是做项目,总能看到好多名词:Consistency, failure detector, order, timer;好多问题:Consensus, broadcast; 好多结论:FLP, CAP。了解单一名词的意义比较容易,但这些名词背后和分布式系统的联系,和商业分布式系统是如何利用这些理论的,一直不是很直观。啃了好多论文,却是只见树木,不见森林。偶然间翻阅到一个大神写的博客,刚好串起了这个领域里的一些知识点。
需要说明的是,因为大神写的也不是正式的论文,里面难免有不严谨的地方。而我这篇博客也只是总结性质,又加上了自己的理解,难免有不严谨甚至错误的地方,发现之后再补充完善。附上原文链接,感兴趣的同学可以之间看原文教程:
1. Distributed systems 介绍
一般来说,我们利用分布式系统是为了:
-
Storage:扩展存储能力
-
Computation:扩展计算能力
当一个网站的数据量很小的时候,没有必要使用分布式系统,单点系统更加简单可靠。但当数据量增大到一定数目时,单点系统可能会不足,而更换更好的单点系统又过于昂贵。这时候,我们可以采用分布式系统。之所以要使用分布式系统,根源在于数据量和业务量的扩张。
1.1 分布式系统想要达成的目标
Scalability:
“Is the ability of a system,network, or process, to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth。”
一个系统处理 “增长” 的能力(即增长之后性能不会受太大影响)。“增长”可以划分为三个维度:
-
Size scalability:增加nodes,可以使系统线性增长,增加dataset不会增加latency
-
Geographic scalability:
-
Administrative scalability:增加nodes不应该增加administrative costs
Performance(and latency):
Is characterized by the amount of useful work accomplished by a computer system compared to the time and resources used.
Performance就是看反应时间和消耗的资源。具体来说:
-
Low latency: 延迟
-
High throughout:吞吐量
-
Low utilization of computing resource: 资源利用率
Availability:
The proportion of time a system is in a functioning condition. If a user cannot access the system, it is said to be unavailable.
一个系统可用的时间所占的比例。
从技术角度来说,availability 与 fault tolerant 相关。一个系统容错能力强,它的availability也就可能越高。
Fault tolerance:
Ability of a system to behave in a well-defined manner once faults occur.
一个系统在出现错误时依然可以”behave in a well-defined manner”.
这里所说的容错,是容纳你能考虑到的错误。定义所有你能想到的faults,之后用系统或算法来“容忍”它们。注意,你不能“容忍”没有定义的faults。
1.2 分布式系统中的模型
Abstractions and Models:
-
System model (asynchronous / synchronous)
-
Failure model: (crash-fail, partitions, Byzantine)
-
Consistency model:(strong, weak, eventual)
理想状态下,我们想让分布式系统“表现得好像一个单系统”。但事与愿违,不同种类的failures让这个目标实现起来很困难。比如当出现partition(分区)的时候,你是要为了availability接受用户请求呢,还是为了safety拒绝用户。关于权衡分布式系统各个方面的表现,最著名的就是CAP理论。
1.3 分布式系统常用技术
Design techniques: partition and replicate
在节点之间如何分发数据是很重要的,它决定了如何定位数据以及我们如何处理它们。分布式系统中关于数据有两种technique:
-
Partition:把数据且分开,便于并行处理
-
Replication:把数据拷贝多份便于fault tolerance
在设计分布式系统时,要想着这两种方式。这是解决分布式问题的两大法宝,针对对两个技术,有若干算法。
Partition的好处是:
-
Performance:限制数据块大小,提高处理效率
-
Availability: 一个数据分块,提高availability(一部分的损坏不会影响其余部分)
Replication允许我们达到availability, performance,fault-telerance。
-
Afraid of loss of availability? 备份!
-
Slow computation? 在多个系统中进行计算
-
Slow I/O? 在本地缓存中备份数据。
但replication的引入也引起了consistency的问题。所以定义了不同级别的consistency model:
-
Strong model可以让程序员感觉不到replication的存在。
-
weak model会让程序员看到一些内部状态,但是又提供了更短的latency和更高的 availability
2. Up and down the level of abstraction
为了更好地描述分布式系统,需要定义一系列的抽象来描述系统的特征
-
System model
-
Nodes in the system model