注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
Interview Questions
- Why use message queues (MQ)?
- What are the advantages and disadvantages of using message queues
- Question Breakdown
Why Use Message Queues?
This question is essentially asking about MQ use cases and how you applied them in your specific project. A strong answer describes a business scenario and the technical challenges it faced, explaining how MQ provided significant benefits over alternatives.
The three core use cases for MQ are: Decoupling, Asynchrony, and Traffic Shaving (Buffering).
Decoupling
Imagine a scenario where System A sends data to Systems B, C, and D via direct API calls. What happens if System E also needs that data? Or if System C no longer needs it? The maintainer of System A would be overwhelmed.
In this scenario, System A is tightly coupled with multiple downstream systems. System A must worry about whether Systems B, C, D, or E are down, whether to retry, and whether to store the messages.
By introducing an MQ, System A simply publishes a message to the queue. Any system that needs the data becomes a subscriber. New systems can subscribe, and existing ones can unsubscribe without any changes to System A's code. System A no longer needs to worry about successful calls, failures, or timeouts.
Key Takeaway: Using a Pub/Sub model through an MQ completely decouples System A from its consumers.
Interview Tip: Reflect on your projects for similar patterns where synchronous API calls made maintenance difficult. Consider if asynchronous decoupling could have improved the architecture. Mentioning this on your resume shows architectural maturity.
Asynchrony
Consider System A receiving a request that requires local database writes and subsequent writes to Systems B, C, and D. If local write takes 3ms and downstream writes take 300ms, 450ms, and 200ms respectively, the total latency is nearly 1 second. This is unacceptable for a modern web application.
Typical internet-scale applications aim for response times under 200ms to ensure a seamless user experience.
With an MQ, System A can send three messages to the queue (taking ~5ms) and immediately respond to the user. The total latency drops to 8ms. The downstream systems then process the messages asynchronously.
Traffic Shaving (削峰 - Peak Shaving)
Imagine System A usually handles about 50 requests per second. However, during a peak period (e.g., 12:00 PM), traffic spikes to 5,000+ requests per second. If the system is directly backed by MySQL, which might only handle ~2,000 requests per second, the surge could crash the database and the entire system.
With an MQ, these 5,000 requests are written to the queue. System A then pulls and processes requests from the MQ at a sustainable rate (e.g., 2,000/sec). This prevents the system from crashing during surges. Requests might temporarily backlog in the MQ during peak hours, but they will be processed quickly once the surge subsides.
What are the Advantages and Disadvantages of MQ?
Advantages: As discussed - Decoupling, Asynchrony, and Traffic Shaving.
Disadvantages:
- Reduced System Availability: MQ adds an external dependency. If the MQ goes down, the entire system might collapse. You must ensure high availability (HA) for your MQ.
- Increased System Complexity: Introducing an MQ brings questions like: How to ensure idempotency (prevent duplicate consumption)? How to handle message loss? How to ensure message ordering?
- Consistency Issues: System A might respond successfully to the user, but what if System C fails to process the message while B and D succeed? This leads to data inconsistency across systems.
MQ is a sophisticated architectural choice. While it offers great benefits, it requires robust technical solutions to mitigate its downsides, often increasing system complexity by an order of magnitude.
|