SlideShare a Scribd company logo
1 of 45
Download to read offline
Consensus	and	
Raft	Algorithm
Huynh	Quang	Thao	
Distributed	System	-	Grokking
Giới	thiệu	về	hoạt	động	nhóm
• Tìm	hiểu	các	vấn	đề	cơ	bản	về	distributed	system	như	consensus	/	clock	synchronization	/	…	
• Follow	theo	course	MIT	distributed	system	6.824,	bao	gồm	làm	bài	tập	
• Trình	bày	các	vấn	đề	gặp	phải	trong	công	việc	(envoy	proxy	/	memcache	/	KaWka	/…)
Consensus
Tại	sao	đồng	thuận	trong	hệ	thống	phân	tán	quan	trọng
Nhu	cầu	các	node	đồng	thuận	trên	cùng	một	vấn	đề	trong	hệ	thống	phân	tán	rất	nhiều.	
Ví	dụ:	
Với	hệ	thống	KaWka,	các	brokers	cần	phải	thống	nhất	thông	tin	về:	
• Controller	(a.k.a:	leader	one	topic):	Chúng	ta	không	muốn	ở	một	thời	điểm	nào	đó,	có	2	controller	trên	cùng	một	
topic		
—>	leader	election	
• Access	Control	List:		Chúng	ta	không	muốn	có	2	consumer	thấy	2	phân	quyền	khác	nhau	trên	cùng	một	topic.	
—>	đồng	thuận	về	mặt	dữ	liệu		
ZooKeeper:	KaWka	/	Hadoop	/	Solr		
Etcd:	Kubernetes	/	CoreDNS	
Proof-of-work:	blockchain
Tại	sao	đồng	thuận	trong	hệ	thống	phân	tán	khó
Key Value
1.	Put	/	Get	K/V
ConEiguration	Server
Tại	sao	đồng	thuận	trong	hệ	thống	phân	tán	khó
1.	Put	/	Get	K/V
Key Value
Master
Key Value
Slave
Key Value
Slave
replicate
Tại	sao	đồng	thuận	trong	hệ	thống	phân	tán	khó
1.	Put	/	Get	K/V
Key Value
Master
Key Value
Slave
replicate
Key Value
Master
replicate ???
???
Split	Brain	Problem
Two	General’s	Problem
Vị	tướng	1:	gửi	message	“Tấn	công	vào	ngày	10/10”	tuy	nhiên	không	chắc	chắn	và	phải	đợi	conWirm	từ	vị	tướng	2.	
Vị	tướng	2:	gửi	message	“đồng	ý”.		
Tuy	nhiên,	lúc	này	vị	tướng	2	không	thể	chắc	chắn	message	này	đã	được	đến	được	vị	tướng	thứ	nhất.	
Vị	tướng	thứ	2	chờ	cho	vị	tướng	thứ	nhất	conWirm	lại	message	“đồng	ý”	
Cứ	thế,	hai	vị	tướng	không	thể	cùng	nhau	đồng	thuận	trên	vấn	đề	này	được.
Byzantine	General’s	Problem
FLP	Impossibility
The	FLP	theorem	states	that	in	an	asynchronous	network	where	messages	may	be	delayed	but	not	lost,	there	is	no	
consensus	algorithm	that	is	guaranteed	to	terminate	in	every	execution	for	all	starting	conditions,	if	at	least	one	
node	may	fail-stop.	
Source:	https://www.the-paper-trail.org/post/2012-03-25-Wlp-and-cap-arent-the-same-thing/
Raft	Algorithm
Architecture
Key Value
Raft
2.	call
Key Value
Raft
Key Value
Raft3.	replicate
3.	replicate
4.	return	
to	FSM
1.	Put	/	Get	K/V
5.	return
Leader
Follower	1
Follower	2
waiting	
quorum
Flow	diagram	khi	user	gửi	một	request
Key Value
Raft
2.	call
Key Value
Raft
Key Value
Raft3.	replicate
3.	replicate
4.	return	
to	FSM
1.	Put	/	Get	K/V
5.	return
Leader
Follower	1
Follower	2
waiting	
quorum
call
denied		
leader:	127.0.0.2
Flow	diagram	khi	user	gửi	một	request
Raft	Decomposition
Leader	election	
- Bầu	chọn	một	node	để	trở	thành	leader		
- Phát	hiện	việc	crash	để	bầu	chọn	leader	mới	
Log	replication	
• Leader	nhận	request	từ	client,	nối	thêm	vào	cuối	cùng	của	log.	
• Leader	replicate	log	qua	các	node	khác	(và	ghi	đè	những	log	inconsistent)	
Safety	
• Đảm	bảo	logs	consistent	giữa	các	node		
• Chỉ	có	server	có	log	up-to-date	mới	được	trở	thành	leader.	
Source:	https://raft.github.io/slides/uiuc2016.pdf
Raft	State
Follower:	chỉ	giữ	nhiệm	vụ	trả	lời	lại	các	lời	gọi	RPC,	và	không	trả	lời	command	từ	phía	user	(trừ	việc	redirect	qua	leader)	
Candidate:		
• Là	trạng	thái	trung	gian,	cố	gắng	để	trở	thành	leader.		
• Ở	trạng	thái	này,	node	sẽ	khởi	tạo	request	để	vote	đến	các	node	khác.	
Leader:		
• là	một	node	chủ	động	trong	cluster.		
• Node	này	sẽ	handle	các	request	từ	clients,	và	replicate	log	qua	các	node	con	khác.	
• Có	thể	có	nhiều	hơn	một	node	“nghĩ	rằng”	mình	là	leader.
Term
• Term	là	số	tự	nhiên,	đánh	số	liên	tục,	đóng	vai	trò	như	logical	timestamp.	
• Chỉ	có	tối	đa	một	leader	cho	mỗi	term.	
• Thông	tin	về	term	luôn	được	trao	đổi	giữa	các	server	mỗi	khi	gửi	nhận	message.	Nếu	một	server	thấy	message	từ	
server	khác	mà	có	term	lớn	hơn	term	hiện	tại:	
• Update	term	hiện	tại	thành	term	mới	nhận	
• Chuyển	về	trạng	thái	follower,	vì	chắc	chắn	có	một	node	khác	đang	làm	server	ở	term	mới	hơn	đấy.
Log
• Log	Entry:	data	+	index	+	term	
• Committed	entry:	khi	một	log	entry	được	gửi	thành	công	
trên	quá	bán	node	(bao	gồm	chính	nó),	log	entry	đấy	sẽ	được	
chuyển	sang	trạng	thái	commit.	
• Khi	log	chuyển	sang	trạng	thái	commit,	có	thể	an	toàn	update	
vào	FSM	và	trả	về	cho	client.
Thuật	toán	Raft	đảm	bảo	rằng:	mọi	committed	message	sẽ	được	replicate	đúng	thứ	tự	và	đầy	đủ	trên	mọi	node	đang	
chạy	đúng.
Request	Type
Raft	chỉ	có	2	dạng	request:	
RequestVote:	dùng	để	bầu	chọn	leader.	
AppendEntries:	
• dùng	cho	giao	thức	heartbeat.	Khi	đó,	mảng	các	log	cần	replicate	là	empty.	
• dùng	cho	việc	replicate	log:	initialize	các	Wield	khác	tương	tự	như	heartbeat,	tuy	nhiên	thêm	mảng	các	log	cần	
replicate	qua	các	follower	(theo	một	số	điều	kiện	cần	thiết).	
• Thông	qua	cơ	chế	replication,	leader	đồng	thời	gửi	luôn	thông	tin	latest	commit	ID.	Dựa	vào	đó,	các	follower	có	
thể	biết	và	commit	các	log	entries	vào	FSM	(Finite	State	Machine)
Request	Type:	AppendEntries	RPC
Giao	thức	hoạt	động	theo	từng	round,	a.k.a:	term		
Do	vậy,	những	message	vì	lí	do	latency	tới	sau	khi	node	
thấy	term	khác	cao	hơn	sẽ	không	được	accept	
Đồng	thời,	trả	về	term	giá	trị	mới	để	candidate	update
Request	Type:	AppendEntries	RPC
Follower	sẽ	dựa	vào	leaderCommit	ID	để	quyết	định	có	những	
log	entry	nào	mới	chưa	được	commit	—>	commit	vào	FSM
Request	Type:	AppendEntries	RPC
2	thông	số	quan	trọng	để	quyết	giải	quyết	log	inconsistency		
(Giải	thích	thêm	ở	phần	sau)
Request	Type:	RequestVote	RPC
Giao	thức	hoạt	động	theo	
từng	round,	a.k.a:	term		
Do	vậy,	những	message	vì	lí	
do	latency	tới	sau	khi	node	
thấy	term	khác	cao	hơn	sẽ	
không	được	accept	
Đồng	thời,	trả	về	term	giá	trị	
mới	để	candidate	update
Request	Type:	RequestVote	RPC
2	thông	số	quan	trọng	để	quyết	định	
candidate’s	log	có	up-to-date	hay	không		
(giải	thích	phần	sau)
Leader	Election
Leader	Election
• Khi	một	node	không	nhận	được	heartbeat	từ	leader	trong	thời	gian	cho	
phép.	Node	đó	sẽ	chuyển	từ	trạng	thái	Follower	-->	Candidate	
• Candidate	sẽ	tăng	term	hiện	tại	lên	1.	Sau	đó,	gửi	request	vote	cho	toàn	bộ	
các	node	khác	trong	hệ	thống.	
• Các	node	khác	trong	hệ	thống	khi	nhận	được	request	vote	mà	có	term	cao	
hơn	term	hiện	tại	(kèm	theo	các	điều	kiện	khác	liên	quan	về	log):	đồng	ý	và	
chuyển	về	trạng	thái	follower.
Khi	một	candidate	nhận	được	kết	quả:	
• Tồn	tại	một	term	lớn	hơn	nó:	chuyển	từ	Candidate	về	Follower		
• Timeout	nhưng	không	nhận	được	quá	bán	vote:	tăng	term	lên	1,	bắt	đầu	lại	quá	trình	leader	election	(vẫn	ở	Candidate)	
• Quá	bán	số	vote:	
• chuyển	từ	Candidate	qua	Leader.		
• Bắt	đầu	định	kì	gửi	heartbeat	request	tới	các	node	còn	lại	để	thông	báo	và	tiếp	tục	giữ	trạng	thái	leader.	
• Có	thể	bắt	đầu	phase	log	replication	cho	các	node	không	catchup	kịp	với	leader	ở	các	term	trước.
Leader	Election:	Voting	condition
Candidate	log	phải	"up-to-date"	với	receiver's	log,	được	deWine	ở	section	5.4.1:	Leader	restriction	
• Điểm	khác	biệt	với	Paxos:	điều	kiện	để	accept	của	Paxos	đơn	giản	hơn.	Do	vậy,	khi	một	node	trở	thành	leader,	Node	đấy	
phải	“tìm	cách”	để	đưa	log	về	trạng	thái	đồng	nhất	với	hệ	thống	(có	thể	thay	đổi	cả	log	entry	trên	leader)	—>	Theo	như	
paper,	điều	này	gây	khó	hiểu	cho	người	mới	bắt	đầu	tìm	hiểu	về	thuật	toán	consensus.	
• Khắc	phục:	thay	vào	đó,	follower	sẽ	bầu	chọn	cho	những	candidate	mà	chắc	chắn	log	đấy	"relevant",	khi	mà	candidate	
được	bầu	chọn	thành	leader,	follower	chỉ	cần	replicate	log	từ	leader	về	local	machine.	
Điều	kiện	“up-to-date”:	
candidate's	last	log	entry	term	>	lớn	hơn	follower's	last	log	entry	term	
OR	
candidate's	last	log	entry	term	==	follower's	last	log	entry	term	AND	candidate's	log	length	>	follower's	log	length
Question	&	Answer
Question:	Có	khi	nào	quá	trình	bầu	chọn	diễn	ra	vô	thời	hạn	?	
Answer:	Có.		
Khi	một	leader	bị	down,	có	thể	toàn	bộ	các	node	follower	nhận	ra	tại	cùng	một	thời	điểm	và	chuyển	thành	candidate.	
Việc	bầu	chọn	diễn	ra,	nhưng	không	ai	dành	được	đa	số.	Sau	khi	timeout,	các	candidate	tiếp	tục	reinit	leader	election	ở	
term	mới.	Quá	trình	này	diễn	ra	liên	tục.	
Solution	Raft	giải	quyết	bằng	cách	sử	dụng	random	timeout.		
Mỗi	một	candidate	khi	bắt	đầu	quá	trình	voting,	sẽ	ngẫu	nhiên	chọn	ra	một	giá	trị	timeout.	Do	việc	ngẫu	nhiên	chọn,	
timeout	mỗi	node	sẽ	khác,	sẽ	có	một	node	timeout	trước,	trở	thành	follower.	Node	này	cũng	sẽ	trở	thành	candidate	đầu	
tiên	ở	term	tiếp	theo.	Từ	đó,	việc	bầu	chọn	sẽ	kết	thúc.
#	initialise	state	
2020/06/06	14:34:23	id=0	[Follower]	[term=0]	enter	follower	loop.	Leader=-1	
2020/06/06	14:34:23	id=1	[Follower]	[term=0]	enter	follower	loop.	Leader=-1	
2020/06/06	14:34:23	id=2	[Follower]	[term=0]	enter	follower	loop.	Leader=-1	
#	all	followers	move	to	the	candidate	state	
2020/06/06	14:34:23	id=2	[Candidate]	[term=0]	enter	candidate	loop.	Leader=-1	
2020/06/06	14:34:23	id=0	[Candidate]	[term=0]	enter	candidate	loop.	Leader=-1	
2020/06/06	14:34:23	id=1	[Candidate]	[term=0]	enter	candidate	loop.	Leader=-1	
#	all	candidate	votes	for	itself	
2020/06/06	14:34:23	id=2	[Candidate]	[term=1]	[voteResult]	server=2	received	vote	from	
server=2	with	term=1.	Total:	1	
2020/06/06	14:34:23	id=1	[Candidate]	[term=1]	[voteResult]	server=1	received	vote	from	server=1	with	term=1.	Total:	1	
2020/06/06	14:34:23	id=0	[Candidate]	[term=1]	[voteResult]	server=0	received	vote	from	server=0	with	term=1.	Total:	1	
#	candidate	rejects	vote	from	other	(because	they're	same	term)	
2020/06/06	14:34:23	id=2	[Candidate]	[term=1]	[rpcRequestVote]	[server=2]	denied	vote:	candidate=0	!=	vote_for=	
2020/06/06	14:34:23	id=1	[Candidate]	[term=1]	[rpcRequestVote]	[server=1]	denied	vote:	candidate=2	!=	vote_for=1	
2020/06/06	14:34:23	id=0	[Candidate]	[term=1]	[rpcRequestVote]	[server=0]	denied	vote:	candidate=1	!=	vote_for=0	
2020/06/06	14:34:23	id=2	[Candidate]	[term=1]	[rpcRequestVote]	[server=2]	denied	vote:	candidate=1	!=	vote_for=2	
2020/06/06	14:34:23	id=1	[Candidate]	[term=1]	[rpcRequestVote]	[server=1]	denied	vote:	candidate=0	!=	vote_for=1	
2020/06/06	14:34:23	id=0	[Candidate]	[term=1]	[rpcRequestVote]	[server=0]	denied	vote:	candidate=2	!=	vote_for=0
#	NodeID=1	timeout	Wirst	
2020/06/06	14:34:23	id=1	[Candidate]	[term=1]	[voteResult]	server=1	election	timeout	
2020/06/06	14:34:23	id=1	[Candidate]	[term=1]	enter	candidate	loop.	Leader=1	
2020/06/06	14:34:23	id=1	[Candidate]	[term=2]	[voteResult]	server=1	received	vote	from	server=1	with	term=2.	Total:	1	
#	NodeID={2,3}	timeout	next	
2020/06/06	14:34:23	id=2	[Candidate]	[term=1]	[rpcRequestVote]	down	to	follower	because	larger	term:	old_term=1	->	(server=1)new_term=2	
2020/06/06	14:34:23	id=0	[Candidate]	[term=1]	[rpcRequestVote]	down	to	follower	because	larger	term:	old_term=1	->	(server=1)new_term=2	
#	NodeID={2,3}	receive	votes	with	term=2	from	NodeID=1	
2020/06/06	14:34:23	id=2	[Follower]	[term=2]	[rpcRequestVote]	accepted	vote	for	server=1	with	term=2	
2020/06/06	14:34:23	id=2	[Follower]	[term=2]	enter	follower	loop.	Leader=1	
2020/06/06	14:34:23	id=0	[Follower]	[term=2]	[rpcRequestVote]	accepted	vote	for	server=1	with	term=2	
2020/06/06	14:34:23	id=0	[Follower]	[term=2]	enter	follower	loop.	Leader=1	
#	NodeID=1	receives	major	vote.	become	leader	
2020/06/06	14:34:23	id=1	[Candidate]	[term=2]	[voteResult]	server=1	received	vote	from	server=2	with	term=2.	Total:	2	
2020/06/06	14:34:23	id=1	[Candidate]	[term=2]	[voteResult]	server=1	become	leader	at	term	2	
2020/06/06	14:34:23	id=1	[Leader]	[term=2]	enter	leader	loop.	Leader=1
Điều	kiện	“up-to-date”:	
candidate's	last	log	entry	term	>	lớn	hơn	follower's	last	log	entry	term	
OR	
candidate's	last	log	entry	term	==	follower's	last	log	entry	term	AND	candidate's	log	length	>	follower's	log	length	
Trả	lời:	Điều	đấy	là	không	chính	xác	với	scenario	sau	
S1:	5	6	7	
S2:	5	8	
S3:	5	8	
Giả	sử	ta	follow	Theo	rule	ở	trên,	S1	có	thể	trở	thành	leader	ở	term	thứ	9.	Khi	đó	trạng	thái	sẽ	là:	
S1:	5	6	7	9	
S2:	5	8	
S3:	5	8	
Sau	đấy,	S1	replicate	log	qua	toàn	bộ	các	node	khác	(leader	không	bao	giờ	xoá/sửa	log	trên	chính	nó)	
S1:	5	6	7	9	
S2:	5	6	7	9	
S3:	5	6	7	9	
—>	log	ở	term	8	sau	khi	replicate	trên	quá	bán	node	đã	bị	mất	—>	vi	phạm	
Câu	hỏi:	Một	suy	nghĩ	rất	tự	nhiên	là	tại	sao	ta	không	đơn	giản	đặt	điều	kiện	là	“node	nào	có	log	dài	hơn	thì	nó	sẽ	up-to-date	
hơn"	?
S1:	5	
S2:	5	
S3:	5
• S1	là	leader	của	term	5	
• Sau	đó,	S1	bị	crash	và	fast-restart	trước	khi	các	node	khác	nhận	được	time	out.		
• S1	start	quá	trình	bầu	chọn	ở	term	6.	
S1:	5	6	
S2:	5	
S3:	5
• S1	là	leader	của	term	6	
• Một	client	insert	data	vào	S1	tại	term	6.	Log	sẽ	được	append	vào	local	của	S1.	
• S1	chưa	kịp	replicate	log	qua	các	node	khác	thì	node	S1	bị	crash	và	fast-restart	trở	lại	(!!!)
S1:	5	6	7	
S2:	5	
S3:	5
• S1	tiếp	tục	là	leader	của	term	7	
• Tương	tự	như	trên,	một	client	insert	data	vào	S1	ở	term	7	
• Tương	tự,	S1	chưa	kịp	replicate	log	qua	các	node	khác	thì	node	S1	bị	crash
S1:	5	6	7	
S2:	5	8		
S3:	5	8
• Lần	này,	các	node	khác	nhận	ra	S1	bị	crash	do	timeout,	khởi	tạo	quá	trình	bầu	chọn.	
• S2	trở	thành	leader	tại	term	8.	
• Client	insert	data	vào	S2	tại	term	8.		
• S2	replicate	log	qua	S3	—>	majority.	Như	vậy,	chắc	chắn	data	tại	term	8	buộc	phải	available	trên	toàn	bộ	machine.
S1:	5	6	7	9	
S2:	5	8		
S3:	5	8
• Sau	khi	S1	phục	hồi,	bắt	đầu	lại	quá	trình	bầu	chọn	leader	với	term	9	
• Nếu	ta	chọn	giải	pháp	“candidate	nào	có	độ	dài	log	dài	hơn	là	leader”	—>	S1	sẽ	chiến	thắng	
• Khi	đó,	log	ở	S1	sẽ	replicate	qua	S2	và	S3	(log	ở	leader	không	bao	giờ	xoá)
S1:	5	6	7	9	
S2:	5	6	7	9	
S3:	5	6	7	9
• Khi	đó,	log	ở	term	8	bị	mất	—>	vi	phạm	tính	chất	thuật	toán:	một	log	khi	đã	replicate	qua	quá	bán	các	node,	log	
đấy	sẽ	không	bao	giờ	bị	thay	đổi.
Log	Replication
Leader
Follower
M
new	message
Leader
Follower
new	message
Follower	removes		
inconsistent	logs
Leader
Follower
M
new	message
Leader
Follower
new	message
Follower	removes		
inconsistent	logs
Leader
Follower
M
new	message
Leader
Follower
new	message
Follower	append	all	logs
Leader
Follower
M
new	message
Leader
Follower
new	message
Follower	append	all	logs
Raft	Properties
Election	Safety	
Tối	đa	một	leader	được	bầu	chọn	tại	một	term	bất	kì.	
Leader	Append-Only	
Leader	không	bao	giờ	sửa	hoặc	delete	log	entry	của	chính	nó.	
Log	Matching	
Nếu	hai	đoạn	log	của	hai	node	có	một	entry	có	cùng	index	và	cùng	term	tại	index	K—>	tất	cả	các	log	entry	
từ	đầu	đến	vị	trí	K	đều	tương	đồng.	
Leader	Completeness	
Nếu	một	log	entry	đã	committed	(replicated	trên	quá	bán	node)	ở	một	term	T,	log	entry	đó	sẽ	luôn	luôn	
tồn	tại	ở	mọi	leader	ở	mọi	term	>	T.	
State	Machine	Safety	Property	
Nếu	một	server	bất	kì	đã	apply	một	log	entry	bất	kì,	không	một	server
Node	2
Node	1
M
T
T
M
M
Log	Matching	Property
Phát biểu 1: Nếu 2 entry ở 2 đoạn log có chung index và term, 2 entry này có chung data.
Phát biểu 2: Nếu 2 entry ở 2 đoạn log có chung index và term tại vị trí T, toàn bộ log từ đầu đến T đều tương đồng nhau.
Leader	Completeness	Property
Đây	là	định	lí	quan	trọng	nhất,	và	khó	chứng	minh	nhất	trong	toàn	bộ	các	định	lí	khác.		
Nếu	một	log	entry	đã	được	commit	trong	một	term	bất	kì,	thì	log	entry	đó	sẽ	có	mặt	trong	mọi	log	của	mọi	leader	
khác	ở	các	term	khác	cao	hơn.	
Giải	thích:		
• Một	log	entry	bao	gồm	3	thuộc	tính:	data	+	term	+	index.	Một	log	entry	được	commit	sẽ	được	update	vào	FSM	
(Finate	State	Machine)	của	node	đó.		
• Định	lí	này	đảm	bảo	trong	mọi	trường	hợp,	khi	một	log	entry	đã	được	update	vào	FSM	ở	thứ	tự	cho	trước	là	K,	thì	
ở	bất	kì	leader	nào	khác	(trong	các	term	khác	cao	hơn),	log	entry	đấy	cũng	sẽ	được	update	ở	thứ	tự	là	K,	với	K	=	
1,2,3,	…	n	(chứng	minh	ở	định	lí	sau)	
—>	Nói	cách	khác,	định	lí	đảm	bảo	các	log	entry	sẽ	được	update	lên	FSM	ở	toàn	bộ	các	node	theo	đúng	một	
thứ	tự	duy	nhất.
State	Machine	Safety	Property
if	a	server	has	applied	a	log	entry	at	a	given	index	to	its	state	machine,	no	other	server	will	ever	apply	a	different	log	entry	for	
the	same	index.
Giải	thích		
Nếu	một	server	đã	apply	một	log	entry	vào	State	machine,	không	tồn	tại	server	nào	apply	một	log	entry	khác	ở	cùng	
một	index.	
Nói	cách	khác,	ở	một	node	bất	kì:	
• Nếu	node	đó	có	term	<	T,	node	đấy	sẽ	chưa	được	apply	log	entry	tại	vị	trí	T	
• Nếu	node	đó	có	term	>=	T,	tại	vị	trí	T,	node	đấy	sẽ	apply	cùng	log	entry	giống	với	mọi	server	khác.
Chứng	minh		
Ta	có	thể	chứng	minh	phản	chứng	như	sau:	
• Giả	sử	tồn	tại	một	follower	mà	có	index	tại	K	khác	với	bất	kì	một	node	nào	có	index	tại	K	những	đã	commit	lên	FSM	→	
leader	truyền	cho	node	mà	index	tại	K	cũng	phải	có	giá	trị	khác.	
• Dựa	vào	định	lí	Leader	completeness,	tất	cả	các	leader	của	các	term	sau	đó	đều	phải	lưu	trữ	các	log	entry	tương	tự	nhau	→	
vô	lí.	
• Do	vậy,	follower	có	index	tại	K	phải	chung	giá	trị	với	các	node	khác.	
• Do	việc	apply	lên	FSM	diễn	ra	tuần	tự	theo	index=0,1,2,…	→	dẫn	đến	định	lí	State	Machine	Safety	Property
Các	vấn	đề	khác	…
Log	Compaction	
• việc	để	log	grow	liên	tục	là	không	khả	thi	trên	thực	tế.	
• cần	có	cơ	chế,	để	sau	khi	update	log	vào	FSM,	có	thể	xoá	đi	các	log	cũ.	ví	dụ	[x	->	1;	x	->	2]	
• Khó	khăn:	nếu	ta	"vô	tình"	xoá	đi	những	log	entry	mà	chưa	được	replicate	qua	các	server	khác,	ta	sẽ	
không	thể	phục	hồi	nó	lại	được.	
ConEiguration	Changes	
• Raft	không	có	đặc	tả	về	việc	thêm	/	bớt	node.	
Byzantine	Problem	
Giả	sử	có	một	node	luôn	cố	gắng	làm	leader:	luôn	reply	“accept”	cho	mọi	RequestVote	request,	nhưng	
đồng	thời	lại	không	accept	request	đó	ở	local	và	gửi	RequestVote	cho	mọi	node	khác	ở	term	lớn	hơn.
Các	nguồn	tham	khảo
Paper:	In	Search	of	an	Understandable	Consensus	Algorithm		
Bài	báo	nguyên	bản	về	Raft:	chi	tiết	về	Raft	cùng	diagram	chi	tiết	về	cách	implement.	
Paper:		Raft	ReEloated:	Do	We	Have	Consensus?		
Dựa	trên	paper	ở	trên	và	giải	thích	rõ	hơn	về	Raft.	
Paper:		Paxos	vs	Raft:	Have	we	reached	consensus	on	distributed	consensus?		
Giải	thích	lại	Paxos	dựa	trên	cách	trình	bày	và	Wlow	của	Raft.	Qua	paper	này,	ta	thấy	rằng	Paxos	và	Raft	không	quá	nhiều	
điểm	khác	biệt	như	ta	tưởng.		
Điểm	lớn	nhất	khác	biệt	giữa	hai	thuật	toán	là	giai	đoạn	leader	election:	Raft	kiểm	soát	chặt	hơn	quá	trình	bầu	chọn,	buộc	
các	Candidate	phải	có	log	“up-to-date”.		
Ngược	lại,	Paxos	điều	kiện	lỏng	hơn,	do	vậy,	khi	một	node	trở	thành	leader,	node	đấy	sẽ	“vất	vả”	hơn	để	resolve	conWlict.	
MIT	Course	6.824:	Distributed	System	
Raft	user	study:	Raft	lecture	
https://www.youtube.com/watch?v=YbZ3zDzDnrw	
Raft	user	study:	Paxos	lecture	
https://www.youtube.com/watch?v=JEpsBg0AO6o	
https://github.com/hashicorp/raft	
Implementation	của	Raft	để	tham	khảo.
Grokking
Consensus	(Tu	Nguyen)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54395021/Consensus+-+ph+n+I	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54427776/Consensus+-+ph+n+II	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54427795/Consensus+-+ph+n+III	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54460534/Consensus+-+ph+n+IV	
Byzantine	Paxos	Algorithm	(Tu	Nguyen)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54394986/
State+Machine+Replication+Byzantine+Paxos+algorithm		
Leader	Election	(Anh	Nguyen)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54427749/Leader+Election+-+Raft+consensus	
Raft	Quiz	(Dinh	Nguyen)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/24480741/Raft+Quiz	
Raft	proof	(Thao	Huynh)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/8127023/Raft+Proof	
Introduction	to	Raft	(Thao	Huynh)	
https://grokking.atlassian.net/wiki/spaces/DS/pages/54427808/Gi+i+thi+u+v+Raft
Q&A	Session

More Related Content

What's hot

A Quick Introduction to Linux
A Quick Introduction to LinuxA Quick Introduction to Linux
A Quick Introduction to LinuxTusharadri Sarkar
 
All You Need to Know About HCL Notes 64-Bit Clients
All You Need to Know About HCL Notes 64-Bit ClientsAll You Need to Know About HCL Notes 64-Bit Clients
All You Need to Know About HCL Notes 64-Bit Clientspanagenda
 
What's new in apache hive
What's new in apache hive What's new in apache hive
What's new in apache hive DataWorks Summit
 
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...panagenda
 
Introducción a GIT
Introducción a GITIntroducción a GIT
Introducción a GITmaxfontana90
 
PostgreSQL: Performance Tuning
PostgreSQL: Performance TuningPostgreSQL: Performance Tuning
PostgreSQL: Performance TuningFernando Ike
 
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3Linaro
 
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~Naoto MATSUMOTO
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New HardwareRuggedBoardGroup
 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingMichelle Holley
 
ORC improvement in Apache Spark 2.3
ORC improvement in Apache Spark 2.3ORC improvement in Apache Spark 2.3
ORC improvement in Apache Spark 2.3DataWorks Summit
 
Scylla Summit 2022: Making Schema Changes Safe with Raft
Scylla Summit 2022: Making Schema Changes Safe with RaftScylla Summit 2022: Making Schema Changes Safe with Raft
Scylla Summit 2022: Making Schema Changes Safe with RaftScyllaDB
 
Fluentd v0.14 Plugin API Details
Fluentd v0.14 Plugin API DetailsFluentd v0.14 Plugin API Details
Fluentd v0.14 Plugin API DetailsSATOSHI TAGOMORI
 
Lcu14 107- op-tee on ar mv8
Lcu14 107- op-tee on ar mv8Lcu14 107- op-tee on ar mv8
Lcu14 107- op-tee on ar mv8Linaro
 
Trusted firmware deep_dive_v1.0_
Trusted firmware deep_dive_v1.0_Trusted firmware deep_dive_v1.0_
Trusted firmware deep_dive_v1.0_Linaro
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsDivye Kapoor
 
Ozone- Object store for Apache Hadoop
Ozone- Object store for Apache HadoopOzone- Object store for Apache Hadoop
Ozone- Object store for Apache HadoopHortonworks
 
Hadoop Operations - Best Practices from the Field
Hadoop Operations - Best Practices from the FieldHadoop Operations - Best Practices from the Field
Hadoop Operations - Best Practices from the FieldDataWorks Summit
 

What's hot (20)

A Quick Introduction to Linux
A Quick Introduction to LinuxA Quick Introduction to Linux
A Quick Introduction to Linux
 
All You Need to Know About HCL Notes 64-Bit Clients
All You Need to Know About HCL Notes 64-Bit ClientsAll You Need to Know About HCL Notes 64-Bit Clients
All You Need to Know About HCL Notes 64-Bit Clients
 
What's new in apache hive
What's new in apache hive What's new in apache hive
What's new in apache hive
 
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...
From A to Z-itrix: Setting up the most stable and fastest HCL Notes client on...
 
Introducción a GIT
Introducción a GITIntroducción a GIT
Introducción a GIT
 
PostgreSQL: Performance Tuning
PostgreSQL: Performance TuningPostgreSQL: Performance Tuning
PostgreSQL: Performance Tuning
 
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3
LAS16-111: Easing Access to ARM TrustZone – OP-TEE and Raspberry Pi 3
 
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~
ビジネス向けIBネットワークを考える ~HPCクラスタでの経験を踏まえて~
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
What's New in Apache Hive
What's New in Apache HiveWhat's New in Apache Hive
What's New in Apache Hive
 
DPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet ProcessingDPDK & Layer 4 Packet Processing
DPDK & Layer 4 Packet Processing
 
ORC improvement in Apache Spark 2.3
ORC improvement in Apache Spark 2.3ORC improvement in Apache Spark 2.3
ORC improvement in Apache Spark 2.3
 
Os
OsOs
Os
 
Scylla Summit 2022: Making Schema Changes Safe with Raft
Scylla Summit 2022: Making Schema Changes Safe with RaftScylla Summit 2022: Making Schema Changes Safe with Raft
Scylla Summit 2022: Making Schema Changes Safe with Raft
 
Fluentd v0.14 Plugin API Details
Fluentd v0.14 Plugin API DetailsFluentd v0.14 Plugin API Details
Fluentd v0.14 Plugin API Details
 
Lcu14 107- op-tee on ar mv8
Lcu14 107- op-tee on ar mv8Lcu14 107- op-tee on ar mv8
Lcu14 107- op-tee on ar mv8
 
Trusted firmware deep_dive_v1.0_
Trusted firmware deep_dive_v1.0_Trusted firmware deep_dive_v1.0_
Trusted firmware deep_dive_v1.0_
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOs
 
Ozone- Object store for Apache Hadoop
Ozone- Object store for Apache HadoopOzone- Object store for Apache Hadoop
Ozone- Object store for Apache Hadoop
 
Hadoop Operations - Best Practices from the Field
Hadoop Operations - Best Practices from the FieldHadoop Operations - Best Practices from the Field
Hadoop Operations - Best Practices from the Field
 

Similar to Consensus and Raft algorithm (Vietnamese version)

Domain Driven Design và Event Driven Architecture
Domain Driven Design và Event Driven Architecture Domain Driven Design và Event Driven Architecture
Domain Driven Design và Event Driven Architecture IT Expert Club
 
Bao cao-cloud-linux-hosting
Bao cao-cloud-linux-hostingBao cao-cloud-linux-hosting
Bao cao-cloud-linux-hostinglaonap166
 
Quan ly cau hinh pm
Quan ly cau hinh pmQuan ly cau hinh pm
Quan ly cau hinh pmNguyen Tran
 
Cluster bao cao 1
Cluster   bao cao 1Cluster   bao cao 1
Cluster bao cao 1phanleson
 
đề Cương chi tiết
đề Cương chi tiếtđề Cương chi tiết
đề Cương chi tiếtNguyễn Long
 
Hdth02 ltudql02-su dungsubversion-1
Hdth02 ltudql02-su dungsubversion-1Hdth02 ltudql02-su dungsubversion-1
Hdth02 ltudql02-su dungsubversion-1Dũng Đinh
 
1.+tai+lieu+thiet+ke
1.+tai+lieu+thiet+ke1.+tai+lieu+thiet+ke
1.+tai+lieu+thiet+keLinh Hoang
 
02 phan-thai-trung-drupal
02 phan-thai-trung-drupal02 phan-thai-trung-drupal
02 phan-thai-trung-drupalhazzaz
 
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsi
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsiRoom 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsi
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsiVietnam Open Infrastructure User Group
 

Similar to Consensus and Raft algorithm (Vietnamese version) (13)

Domain Driven Design và Event Driven Architecture
Domain Driven Design và Event Driven Architecture Domain Driven Design và Event Driven Architecture
Domain Driven Design và Event Driven Architecture
 
Bao cao-cloud-linux-hosting
Bao cao-cloud-linux-hostingBao cao-cloud-linux-hosting
Bao cao-cloud-linux-hosting
 
Quan ly cau hinh pm
Quan ly cau hinh pmQuan ly cau hinh pm
Quan ly cau hinh pm
 
Ứng Dụng Thuật Toán Lomet Trong Cung Cấp Tài Nguyên Phân Tán Cho Hệ Thống Máy...
Ứng Dụng Thuật Toán Lomet Trong Cung Cấp Tài Nguyên Phân Tán Cho Hệ Thống Máy...Ứng Dụng Thuật Toán Lomet Trong Cung Cấp Tài Nguyên Phân Tán Cho Hệ Thống Máy...
Ứng Dụng Thuật Toán Lomet Trong Cung Cấp Tài Nguyên Phân Tán Cho Hệ Thống Máy...
 
Process and thread
Process and threadProcess and thread
Process and thread
 
Cluster bao cao 1
Cluster   bao cao 1Cluster   bao cao 1
Cluster bao cao 1
 
Git it
Git itGit it
Git it
 
đề Cương chi tiết
đề Cương chi tiếtđề Cương chi tiết
đề Cương chi tiết
 
Hdth02 ltudql02-su dungsubversion-1
Hdth02 ltudql02-su dungsubversion-1Hdth02 ltudql02-su dungsubversion-1
Hdth02 ltudql02-su dungsubversion-1
 
1.+tai+lieu+thiet+ke
1.+tai+lieu+thiet+ke1.+tai+lieu+thiet+ke
1.+tai+lieu+thiet+ke
 
Dsd02 sta
Dsd02 staDsd02 sta
Dsd02 sta
 
02 phan-thai-trung-drupal
02 phan-thai-trung-drupal02 phan-thai-trung-drupal
02 phan-thai-trung-drupal
 
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsi
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsiRoom 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsi
Room 1 - 2 - Nguyễn Văn Thắng & Dzung Nguyen - Proxmox VE và ZFS over iscsi
 

More from Thao Huynh Quang

2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdfThao Huynh Quang
 
Consensus and Raft Algorithm in Distributed System
Consensus and  Raft Algorithm in Distributed SystemConsensus and  Raft Algorithm in Distributed System
Consensus and Raft Algorithm in Distributed SystemThao Huynh Quang
 
Kotlin Introduction with Android applications
Kotlin Introduction with Android applicationsKotlin Introduction with Android applications
Kotlin Introduction with Android applicationsThao Huynh Quang
 
Git Introduction with illustrations
Git Introduction with illustrationsGit Introduction with illustrations
Git Introduction with illustrationsThao Huynh Quang
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence libraryThao Huynh Quang
 
Kubernetes and service mesh application
Kubernetes  and service mesh applicationKubernetes  and service mesh application
Kubernetes and service mesh applicationThao Huynh Quang
 
Kafka: All an engineer needs to know
Kafka: All an engineer needs to knowKafka: All an engineer needs to know
Kafka: All an engineer needs to knowThao Huynh Quang
 
Concurrency pattern in Kotlin
Concurrency pattern in KotlinConcurrency pattern in Kotlin
Concurrency pattern in KotlinThao Huynh Quang
 
Observability and its application
Observability and its applicationObservability and its application
Observability and its applicationThao Huynh Quang
 
Android Reverse Engineering
Android Reverse EngineeringAndroid Reverse Engineering
Android Reverse EngineeringThao Huynh Quang
 

More from Thao Huynh Quang (16)

2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf2021-03-08-telegram-vs-signal.pdf
2021-03-08-telegram-vs-signal.pdf
 
Consensus and Raft Algorithm in Distributed System
Consensus and  Raft Algorithm in Distributed SystemConsensus and  Raft Algorithm in Distributed System
Consensus and Raft Algorithm in Distributed System
 
Kotlin Introduction with Android applications
Kotlin Introduction with Android applicationsKotlin Introduction with Android applications
Kotlin Introduction with Android applications
 
Git Introduction with illustrations
Git Introduction with illustrationsGit Introduction with illustrations
Git Introduction with illustrations
 
Android Jetpack: Room persistence library
Android Jetpack: Room persistence libraryAndroid Jetpack: Room persistence library
Android Jetpack: Room persistence library
 
Android Performance Tips
Android Performance TipsAndroid Performance Tips
Android Performance Tips
 
Kubernetes and service mesh application
Kubernetes  and service mesh applicationKubernetes  and service mesh application
Kubernetes and service mesh application
 
Kafka: All an engineer needs to know
Kafka: All an engineer needs to knowKafka: All an engineer needs to know
Kafka: All an engineer needs to know
 
Blockchain introduction
Blockchain introductionBlockchain introduction
Blockchain introduction
 
Concurrency pattern in Kotlin
Concurrency pattern in KotlinConcurrency pattern in Kotlin
Concurrency pattern in Kotlin
 
Observability and its application
Observability and its applicationObservability and its application
Observability and its application
 
GraphQL in Android
GraphQL in AndroidGraphQL in Android
GraphQL in Android
 
Android GRPC
Android GRPCAndroid GRPC
Android GRPC
 
Android Reverse Engineering
Android Reverse EngineeringAndroid Reverse Engineering
Android Reverse Engineering
 
nosql
nosqlnosql
nosql
 
android deep linking
android deep linkingandroid deep linking
android deep linking
 

Consensus and Raft algorithm (Vietnamese version)