ความจำเป็นของตัวกลางข้อความภายนอก
ทำความเข้าใจข้อจำกัดของตัวกลางข้อความในหน่วยความจำเมื่อขยายระบบ และข้อดีของโซลูชันภายนอก
ความจำเป็นของตัวกลางข้อความภายนอก เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Scaling Real-Time Apps
Welcome! Modern web applications often need to communicate in real-time. Think about chat apps, live dashboards, or online games.
As your app grows, more users will connect simultaneously. This lesson explores a key challenge: how do you keep your real-time system fast and reliable when handling thousands or millions of users?
Understanding In-Memory Brokers
When you first set up a Spring WebSocket application, you typically use an in-memory message broker (like Spring's SimpleBrokerMessageHandler).
An in-memory broker lives directly within your application's process. It handles message routing and subscriptions for all clients connected to that specific application instance.
Single Server Operation
On a single server, an in-memory broker works perfectly. All WebSocket clients connect to the same application instance, and therefore, to the same in-memory broker.
- Client A sends a message.
- The in-memory broker receives it.
- The broker routes it to Client B and Client C, which are also connected to this same server.
Everything runs smoothly in a single-instance setup!
Introducing Load Balancers
To handle more users than a single server can manage, applications are often deployed in a cluster. This means running multiple instances of your application.
A load balancer sits in front of these instances, distributing incoming client connections across them. For example, Client A might connect to Server 1, and Client B to Server 2.
The In-Memory Broker's Flaw
Here's where the problem arises: with multiple server instances, each instance has its own, separate in-memory broker.
These brokers are completely isolated. They don't know about each other, and they don't share any information.
Message Silos in Action
Imagine this scenario:
- Client A connects to Server 1.
- Client B connects to Server 2.
- Client A sends a message to a public chat topic.
Only the in-memory broker on Server 1 receives this message. It will correctly deliver it to any other clients connected to Server 1, but Client B on Server 2 will never receive it!
Why We Need to Share State
For a truly scalable real-time application, messages must be delivered to all relevant clients, regardless of which server instance they are connected to.
The in-memory broker prevents this by creating isolated 'silos' of messages. We need a way for all server instances to communicate and share messages.
Introducing External Message Brokers
This is where external message brokers come in. These are standalone services (like RabbitMQ, Apache Kafka, or Redis Pub/Sub) that operate independently of your application instances.
Instead of each server having its own broker, all your server instances connect to a single, centralized external broker.
How External Brokers Solve Scaling
With an external broker:
- A client sends a message to its connected server instance.
- That server instance immediately forwards the message to the external broker.
- The external broker then distributes the message to all other connected server instances.
- Each server instance then delivers the message to its own connected clients.
This ensures all clients receive messages, regardless of which server they are connected to.
Key Advantages of External Brokers
Using an external message broker offers significant benefits for scalable real-time applications:
- Horizontal Scalability: Easily add or remove server instances without affecting message delivery.
- Reliability: Many external brokers offer message persistence, ensuring messages aren't lost even if a server instance goes down.
- Decoupling: Your WebSocket server instances become stateless, making them easier to manage and scale independently.
Quick Check: Broker Limitations
You're building a chat application and expect to have thousands of users. You've deployed multiple instances of your Spring WebSocket server behind a load balancer. What is a key limitation of using Spring's default in-memory simple broker in this scaled environment?
Recap: The Need for External Brokers
In this lesson, we explored the crucial need for external message brokers when scaling Spring WebSocket applications.
- In-memory brokers work well for single instances but fail in distributed environments.
- Load balancers distribute connections, but in-memory brokers create message silos.
- External brokers (like RabbitMQ, Kafka) provide a centralized hub for message exchange, enabling true horizontal scalability and reliability for your real-time systems.
Next, we'll dive into integrating these external solutions!
คำถามที่พบบ่อย
บทเรียน “ความจำเป็นของตัวกลางข้อความภายนอก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความจำเป็นของตัวกลางข้อความภายนอก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความจำเป็นของตัวกลางข้อความภายนอก”
ทำความเข้าใจข้อจำกัดของตัวกลางข้อความในหน่วยความจำเมื่อขยายระบบ และข้อดีของโซลูชันภายนอก คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ความจำเป็นของตัวกลางข้อความภายนอก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม
ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความจำเป็นของตัวกลางข้อความภายนอก
- การผสาน RabbitMQ/Kafka
- สถาปัตยกรรม WebSocket แบบกระจาย
- การกำหนดค่ารีเลย์นายหน้าของ STOMP