รูปแบบการเลือกผู้นำ
สำรวจวิธีใช้ Redis เพื่ออำนวยความสะดวกในการเลือกผู้นำของบริการแบบกระจายและเพิ่มความพร้อมใช้งาน
รูปแบบการเลือกผู้นำ เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Redis Caching & Messaging (Pub/Sub, Streams) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Leader Election?
In a distributed system, multiple instances of your application run simultaneously. Sometimes, you need one instance to perform a specific task, like processing a queue or coordinating updates, to avoid conflicts or duplicate work.
This is where leader election comes in. It's a process where distributed nodes agree on a single node to be the "leader" at any given time.
Redis for Election Coordination
Redis is an excellent choice for implementing leader election due to its speed, atomic operations, and strong consistency guarantees for single-key operations.
- Atomic Operations: Commands like
SETNXorSET ... NX EXexecute entirely or not at all, preventing race conditions. - Persistence: If configured, Redis can persist data, making leader election state more durable.
- Centralized State: Provides a single, agreed-upon source of truth for who the current leader is.
Basic Election: SETNX
The simplest way to attempt leader election with Redis is using the SETNX command. SETNX key value ("Set if Not eXists") sets a key only if it doesn't already exist. If the key is set, it returns 1; otherwise, 0.
The first process to successfully set the leader key becomes the leader.
import redis
import time
# Connect to Redis
r = redis.Redis(decode_responses=True)
LEADER_KEY = "my_app:leader_v1"
MY_ID = "process_A" # Unique ID for this process
print(f"Process {MY_ID} attempting to become leader...")
# Try to acquire leadership
if r.setnx(LEADER_KEY, MY_ID):
print(f"Process {MY_ID} is now the leader!")
# Simulate leader work
time.sleep(3) # Work for 3 seconds
# In a real scenario, the leader would perform tasks
# and eventually release leadership or renew its lease.
r.delete(LEADER_KEY) # Release leadership
print(f"Process {MY_ID} released leadership.")
el:
current_leader = r.get(LEADER_KEY)
print(f"Process {MY_ID}: Another process ({current_leader}) is already the leader.")The Problem: Leader Failure
Consider the basic SETNX approach. What if the elected leader (process_A from the last example) crashes immediately after setting the LEADER_KEY, but before it has a chance to delete it?
The LEADER_KEY would remain in Redis indefinitely, preventing any other process from becoming leader. This creates a permanent deadlock, breaking your distributed system's high availability.
TTL for Fault Tolerance
To prevent deadlocks, we must add an expiration time (Time-To-Live, or TTL) to the leader key. This ensures that even if a leader crashes, its leadership key will eventually expire, allowing a new election.
We can use the EXPIRE key seconds command right after SETNX to set a TTL.
import redis
import time
r = redis.Redis(decode_responses=True)
LEADER_KEY = "my_app:leader_v2"
MY_ID = "process_B"
LOCK_TTL = 10 # seconds
print(f"Process {MY_ID} attempting to become leader...")
if r.setnx(LEADER_KEY, MY_ID):
r.expire(LEADER_KEY, LOCK_TTL) # Set expiration
print(f"Process {MY_ID} is now the leader with TTL {LOCK_TTL}s!")
# Simulate leader work for a short period
time.sleep(LOCK_TTL // 2)
print(f"Process {MY_ID} completed its short task.")
if r.get(LEADER_KEY) == MY_ID: # Check if still leader before deleting
r.delete(LEADER_KEY)
print(f"Process {MY_ID} released leadership.")
else:
print(f"Process {MY_ID}: Leadership lost or expired already.")
el:
current_leader = r.get(LEADER_KEY)
print(f"Process {MY_ID}: Another process ({current_leader}) is already the leader.")The SETNX + EXPIRE Race
While adding EXPIRE is better, a critical race condition still exists! Imagine this sequence:
- Process A calls
SETNX LEADER_KEY process_A. It succeeds (returns1). - Process A then crashes before it can call
EXPIRE LEADER_KEY 10.
The LEADER_KEY is set, but without a TTL, leading to the same deadlock situation as before. We need an atomic way to set the key and its expiration.
Atomic SET for Robustness
Redis provides a powerful, atomic SET command that combines setting a key's value and its expiration. The format is SET key value [EX seconds | PX milliseconds] [NX | XX].
NX: Only set the key if it does not already exist (likeSETNX).EX seconds: Set an expiration time in seconds.PX milliseconds: Set an expiration time in milliseconds.
Using SET LEADER_KEY MY_ID NX EX 10 ensures that both conditions (key not existing AND expiration) are applied atomically.
import redis
import time
r = redis.Redis(decode_responses=True)
LEADER_KEY = "my_app:leader_v3"
MY_ID = "process_C"
LOCK_TTL = 10 # seconds
print(f"Process {MY_ID} attempting to become leader atomically...")
# Try to acquire leadership using atomic SET with NX and EX
# This command returns True if the key was set, False otherwise.
if r.set(LEADER_KEY, MY_ID, nx=True, ex=LOCK_TTL):
print(f"Process {MY_ID} is now the leader with atomic TTL {LOCK_TTL}s!")
# Simulate leader work
time.sleep(LOCK_TTL // 2)
print(f"Process {MY_ID} still working as leader.")
if r.get(LEADER_KEY) == MY_ID: # Important: check value before deleting!
r.delete(LEADER_KEY)
print(f"Process {MY_ID} gracefully released leadership.")
else:
print(f"Process {MY_ID}: Leadership lost or expired already.")
el:
current_leader = r.get(LEADER_KEY)
print(f"Process {MY_ID}: Another process ({current_leader}) is already the leader.")Maintaining Leadership: Heartbeats
Once a leader is elected using the atomic SET command, it needs to periodically signal that it's still alive and capable of leading. This is done through "heartbeats."
A heartbeat involves the leader refreshing the expiration time of its leadership key before it expires (e.g., using EXPIRE LEADER_KEY NEW_TTL or PEXPIRE). If the leader fails to send heartbeats, its key will expire, triggering a new election among the remaining processes.
Check Your Understanding
Which of the following are benefits of using the atomic SET key value NX EX seconds command for leader election compared to separate SETNX and EXPIRE commands?
Recap: Leader Election
We've explored how Redis can facilitate leader election in distributed systems. We started with basic SETNX, identified its pitfalls, and learned to improve it with EXPIRE.
Crucially, we discovered the robust and atomic SET key value NX EX seconds command to prevent race conditions and ensure keys always have an expiration. Finally, we touched upon the importance of leader heartbeats to maintain active leadership.
คำถามที่พบบ่อย
บทเรียน “รูปแบบการเลือกผู้นำ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบการเลือกผู้นำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Redis Caching & Messaging (Pub/Sub, Streams) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบการเลือกผู้นำ”
สำรวจวิธีใช้ Redis เพื่ออำนวยความสะดวกในการเลือกผู้นำของบริการแบบกระจายและเพิ่มความพร้อมใช้งาน คุณปฏิบัติ Redis Caching & Messaging (Pub/Sub, Streams) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Redis Caching & Messaging (Pub/Sub, Streams) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Redis Caching & Messaging (Pub/Sub, Streams) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบการเลือกผู้นำ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม
ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ล็อกแบบกระจายด้วย Redis
- รูปแบบการเลือกผู้นำ
- Redis ในฐานะบริการประสานงาน
- การจำกัดอัตราแบบกระจาย