สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
สำรวจวิธีที่การเผยแพร่และรับสมาชิกของ Redis ช่วยอำนวยความสะดวกด้านการสื่อสารและการแจ้งเตือนระหว่างไมโครเซอร์วิสที่ขับเคลื่อนด้วยเหตุการณ์
สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์ เป็นบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Redis Caching & Messaging (Pub/Sub, Streams) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Redis Caching & Messaging (Pub/Sub, Streams) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Event-Driven Architecture?
Welcome! In this lesson, we'll explore Event-Driven Architecture (EDA) and how Redis Pub/Sub is perfect for it.
EDA is a software design pattern where components communicate by reacting to changes in state, known as events.
- Think of it like a news channel: producers publish news (events), and anyone interested (consumers) can tune in.
Key Concepts of EDA
EDA revolves around three main components:
- Events: A record of something that happened (e.g., 'user registered', 'order placed'). They are immutable facts.
- Event Producers: Services that detect an event and publish it to an event channel.
- Event Consumers: Services that subscribe to event channels and react to specific events.
These components communicate indirectly.
Why Use EDA?
EDA offers significant benefits, especially for complex systems:
- Loose Coupling: Producers don't need to know about consumers, making services independent.
- Scalability: You can easily add new consumers without changing producers.
- Real-time Responsiveness: Events are processed as they occur, enabling immediate reactions.
- Flexibility: New features can be added by simply creating new consumers.
Redis Pub/Sub as the Event Bus
Redis Pub/Sub acts as an ideal event bus for EDA.
- An event bus is the central communication channel for events.
- In Redis, channels serve as these event buses.
Producers publish events to a Redis channel, and all interested consumers subscribed to that channel receive the event.
Decoupling Microservices
One of the biggest advantages of EDA with Redis Pub/Sub is decoupling microservices.
Imagine a user registration service. Without EDA, it might directly call an email service, a logging service, etc.
With EDA, the registration service just publishes a 'user registered' event. Email and logging services subscribe and react independently. This makes services easier to build and maintain!
Example: User Registration Event
Let's consider a practical scenario: a user registers on your application.
The User Service (producer) registers the user and then publishes a user:registered event to a Redis channel.
Other services, like a Notification Service or a Analytics Service (consumers), can then subscribe to this channel and perform their tasks.
Producer: Publishing an Event
Here's how a Python service might publish a user:registered event after a new user signs up. Make sure Redis is running!
import redis
import json
import time
r = redis.Redis(decode_responses=True)
def register_user(u_id, name, mail):
print(f"User {name} registering...")
event_data = {
"user_id": u_id,
"username": name,
"email": mail,
"timestamp": time.time()
}
r.publish("user_events", json.dumps(event_data))
print(f"Published 'user:registered' for {name}")
if __name__ == "__main__":
register_user("101", "alice", "alice@example.com")
time.sleep(0.5)
register_user("102", "bob", "bob@example.com")Consumer: Reacting to an Event
Now, let's see how a Notification Service (consumer) could subscribe to the user_events channel and send a welcome email.
Run this code in a separate terminal after starting the producer.
import redis
import json
import time
r = redis.Redis(decode_responses=True)
p = r.pubsub()
def send_welcome_email(user_data):
print(f"Sending email to {user_data['email']}")
time.sleep(0.3) # Simulate sending
print(f"Email sent to {user_data['username']}.")
if __name__ == "__main__":
print("Notification Service started.")
print("Subscribing to 'user_events'...")
p.subscribe('user_events')
for msg in p.listen():
if msg['type'] == 'message':
try:
data = json.loads(msg['data'])
print(f"Received: {data['username']}")
send_welcome_email(data)
except json.JSONDecodeError:
print(f"Error decoding: {msg['data']}")
time.sleep(0.01)Notifications & Real-time Updates
Beyond microservice communication, Redis Pub/Sub in an EDA is excellent for real-time notifications.
- Chat applications: Send new messages instantly to all participants.
- Live dashboards: Update metrics or status for users in real time.
- Alerts: Notify administrators of critical system events.
The immediate nature of Pub/Sub makes these scenarios simple to implement.
Quick Check: EDA with Redis
Which of the following best describes the role of Redis Pub/Sub in an Event-Driven Architecture?
Recap: EDA & Redis Pub/Sub
You've learned how Redis Pub/Sub is a powerful tool for building Event-Driven Architectures!
- EDA relies on events, producers, and consumers for communication.
- Redis Pub/Sub acts as an efficient event bus.
- It enables loose coupling and scalability for microservices.
- It's perfect for real-time notifications and inter-service communication.
This pattern makes your applications more resilient and flexible. Well done!
คำถามที่พบบ่อย
บทเรียน “สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Redis Caching & Messaging (Pub/Sub, Streams) นี้ได้ไหม
ได้ บทเรียน Redis Caching & Messaging (Pub/Sub, Streams) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสมัครรับตามรูปแบบ
- การออกแบบแชตแบบเรียลไทม์
- สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
- การติดตามการมีอยู่และสถานะออนไลน์