分布式事务 - CAP不可能三角
分布式事务 之 CAP不可能三角
不可能三角,又称三元悖论,来自经济学,即只能拥有其中两项,而不能同时拥有三项(资本自由流动、固定汇率和货币政策独立性)。分布式系统的CAP定理,也是一个不可能三角。
CAP概念
Consistency 一致性:在分布式系统中的所有数据备份,在同一时刻是否同样的值。(等同于所有节点访问同一份最新的数据副本)
Available 可用性:在集群中一部分节点故障后,集群整体是否还能响应客户端的读写请求。(每次请求都能获取到非错的响应——但是不保证获取的数据为最新数据)
Partition Tolerance 分区容错性:分布式系统,出现任何单个节点服务、网络故障,仍然可以对外提供满足一致性或可用性的服务。——虽然是一个分布式系统,但对于用户而言,看上去好像是一个运转正常的整体。(以实际效果而言,分区相当于对通信的时限要求。系统如果不能在时限内达成数据一致性,就意味着发生了分区的情况,必须就当前操作在C和A之间做出选择。)
为什么CAP不可同时满足:
满足一致性(C),所有节点数据时刻保持一致,比如用户更新A节点数据后,系统同步数据到B节点,保证一致性, ...
分布式事务 - 2PC与3PC
分布式事务 之 2PC与3PC理论基础两阶段提交(2PC)两阶段提交又称2PC,2PC是一个非常经典的强一致、中心化的原子提交协议。
这里所说的中心化是指协议中有两类节点:一个是中心化协调者节点(coordinator)和N个参与者节点(partcipant)。
两个阶段:第一阶段:投票阶段 和第二阶段:提交/执行阶段。
第一阶段:投票阶段
第一阶段主要分为3步
事务询问
协调者 向所有的 参与者 发送事务预处理请求,称之为Prepare,并开始等待各 参与者 的响应。
执行本地事务
各个 参与者 节点执行本地事务操作,但在执行完成后并不会真正提交数据库本地事务,而是先向 协调者 报告说:“我这边可以处理了/我这边不能处理”。.
各参与者向协调者反馈事务询问的响应
如果 参与者 成功执行了事务操作,那么就反馈给协调者 Yes 响应,表示事务可以执行,如果没有 参与者 成功执行事务,那么就反馈给协调者 No 响应,表示事务不可以执行。
第一阶段执行完后,会有两种可能。1、所有都返回Yes. 2、有一个或者多个返回No。
第二阶段:提交/执行阶段(成功流程)
成功条件:所有参与 ...
Non-Relational Data Stores (NoSQL)
MotivationApplication GrowthAs our application experiences greater and greater popularity, the data layer proves difficult to scale horizontally. Without a scaling path for our data layer, our growing data set and/or our usage of the database will become a bottleneck limiting our application.
Relational Databases are great tools for our data layer. Unfortunately, there is no simple way to spread load across multiple RDBMSes. We’ve looked at a few techniques for scaling RDBMSes:
Distinguishing R ...
Load Testing With Tsung
What should we observe during load testing?
Response times
Error rates
Synthetic user success rate
We want to create load-test flows that resemble real traffic. There are a few things to consider:
Ensuring flows contain a mixture of reads and writes
Ensuring variance within the flows themselves (not all users have the same habits)
The test framework should be able to utilize data returned from prior requests (e.g., create a submission and make a comment on it)
Load Testing ToolsHigh Performan ...
Relational Database Management System (RDBMS) Scaling - II
Service Oriented Architecture (SOA) and MicroservicesService Oriented Architecture
Partitioning splits data of the same type into separate, unrelated groups.
Service Oriented Architectures (SOA) do something different. They partition both the data and the code based on type and function. Like with partitioning, no joins can automatically be performed across these partitions.
StackThe primary concept behind SOA is having many focused mini-applications (microservices). Each of these focused min ...
Relational Database Management System (RDBMS) Scaling - I
MotivationYou have your web application running on EC2. It is becoming increasingly popular and performance is degrading. What do you do?
Handling Concurrent RequestsYou have deployed application servers that can serve many concurrent requests. But as your site’s popularity continues to grow that is not sufficient.
Vertical Scaling
You have increased your instance sizes and handled more load. However, as the popularity continues to grow you are no longer able to continue scaling vertically.
Hor ...
Concurrency Control in Rails
Scenario
We have many application servers running our application. We are using a relational database to ensure that each request observes a consistent view of the database.
Locks in railsRails uses two types of concurrency control
Optimistic Locking: Assume that database modifications are going to succeed. Throw an exception when they do not.
Pessimistic Locking: Ensure that database modifications will succeed by explicitly avoiding conflict.
Optimistic LockDefinitionAny ActiveRecord model wi ...
Relational Databases - II
Relational Databases - IISchedule
T1
T2
T3
R(X)
W(X)
Commit
R(Y)
W(Y)
Commit
R(Z)
W(Z)
Commit
Describe the execution of transactionsthat run in a database
Schedule TypesA schedule is serial if
the transactions are executed non-interleaved
one transaction finishes before the next one starts
Two schedules are conflict equivalent if
they involve the same set of transactions
every pair of conflicting actions are ordered in the same way
A schedule ...
Relational Databases - I
Relational Databases - ISQL vs noSQLRelational (SQL)Examples:
MySQL (MariaDB, Percona)
PostgreSQL
Oracle
MS SQL
Features:
are a general-purpose persistence layer
offer more features
have a limited ability to scale horizontally
Non-relational (NoSQL)Examples:
Cassandra
MongoDB
Redis
Features:
often are more specialized
require more from the application layer
are better at scaling horizontally
If your needs fit within an RDBMS’s (relational database management system) ability to scale ...