When we use a server on the production line to provide data services, I will encounter two problems as follows:
1 The performance of a single server is insufficient to provide sufficient capacity to service all network requests.
2 We are always afraid of our server downtime, resulting in service unavailable or data loss.
So we have to expand our servers, add more machines to share performance problems, and solve the problem of single point of failure. In general, we will expand our data services in two ways:
1 Data partition: It is to put the data blocks on different servers (such as: uid% 16, consistent hash, etc.).
2 Data mirroring: Let all the servers have the same data, provide a considerable service.
For the first case, we can not solve the problem of data loss, a single server problems, there will be some data loss. Therefore, the high availability of data services can only be done in a second way--redundant storage of data (the number of backups that the general industry considers to be more secure should be 3 copies, such as Hadoop and Dynamo). However, adding more machines can complicate our data services, especially cross server transactions, that is, data consistency across servers . This is a difficult question. Let us use the most classic case: "A account to the B account remittance" to explain, familiar with the RDBMS transactions are aware from account A to account B requires 6 operations:
Read the balance from account a.
Do subtraction on a account number.
Write the results back to the a account number.
Read the balance from the B account.
Add operations to B account number.
Write the results back to the B account number.
For the sake of data consistency, these 6 things, either successfully completed, or are unsuccessful, and the process of the operation of A, B account other access must be locked, the so-called lock is to exclude other reading and writing operations, or there will be dirty data problems, this is the business. So, after we've added more machines, this thing is going to get complicated:
1 in the data partition scheme : If a account number and B account data is not on the same server how to do? We need a transaction processing across machines. That is to say, if A's deduction succeeds, but B's money is not successful, we also need to return the operation of a to roll back. This becomes more complicated in the case of a cross machine.
2 in the data mirroring scheme : A account number and B of the remittance between the account can be done on a machine, but do not forget that we have multiple machines exist a account number and B account copy. What if you have two concurrent operations for a account number (to be remitted to B and C), and what happens to the two different servers? That is to say, in the data mirror, how does the write operation to the same data on different server guarantee its consistency, guarantee data not conflict?
At the same time, we have to consider the performance factor, if not consider the performance, the transaction is not difficult to ensure that the system slow down. In addition to considering performance, we have to consider usability, that is, a machine is gone, the data is not lost, the service can continue to be provided by other machines. So we need to focus on the following:
1 Disaster Tolerance : Data is not lost, node failover
2 consistency of data : Transaction processing
3 Performance: Throughput, response time
As mentioned earlier, to solve the data is not lost, only through the data redundancy method, even if the data partition, each area also need to do data redundancy processing. This is a copy of the data: When a node's data is lost, it can be read from a copy, which is the only way the distributed system resolves data loss anomalies. So, in this article, for simplicity's sake, we'll only discuss the problem of considering data consistency and performance in the case of data redundancy. In short,
1 If you want the data to be highly available, you have to write multiple copies of the data.
2 Write multiple copies of the problem will result in data consistency problems.
3 The problem of data consistency will cause performance problems
This is software development, pressed the gourd up the scoop.
Consistency model
Speaking of data consistency, there are three types of simple (of course, if subdivided, there are many consistency models, such as: sequential consistency, FIFO consistency, session consistency, single read consistency, single write consistency, but for the simple readability of this article, I only said the following three kinds):
1Weak Weak consistency : When you write a new value, the read operation may or may not be read on the copy of the data. For example: Some cache system, network game other players data and you do not have any relationship, such as VoIP system, or Baidu search engine (hehe).
2Eventually final consistency : When you write a new value, you may not be able to read it, but after a certain time window to ensure that the final read out. For example: DNS, email, Amazon S3,google search engine such a system.
3Strong Strong consistency : Once new data is written, new values can be read at any time in any copy. For example: File system, Rdbms,azure table are strongly consistent.
From these three consistent models, we can see that weak and eventually are generally asynchronous redundancy, while strong is generally synchronous and redundant, and asynchronous usually means better performance, but it also means more complex state control. Synchronization means simplicity, but it also means a drop in performance. Well, let's take a step-by-step look at what technologies are available: