查看: 1995| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[学习资料] 【系统设计】为何使用Message Queue

 
💯 2
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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.






评分

参与人数 7大米 +20 收起 理由
Cabbage1123 + 2 楼主/层主请继续!
instant_dev + 10 给你点个赞!
bugaosuni + 1 给你点个赞!
xliu34 + 1 欢迎来一亩三分地论坛!
Grace1226 + 2 给你点个赞!

查看全部评分


上一篇:老程序员再学习System Design
下一篇:System Design 你只需要知道这些 - 系统设计完全经验分享
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表