今日已更新 35 条资讯 | 累计 37284 条内容
关于我们

标签:#messagequeue

找到 2 篇相关文章

AI 资讯

Message Queues Explained with Practical Examples

What Is a Message Queue? A message queue is a buffer that stores messages between producers and consumers. Producers send data to the queue, and consumers read from it. The queue decouples the two sides so they don't need to know about each other. This is a core pattern in distributed systems. Think of it like a restaurant ordering system. You (the producer) write your order on a ticket and put it on a spindle. The kitchen (the consumer) picks tickets off the spindle when they're ready. You don't shout at the chef, and the chef doesn't wait for you. The spindle is the queue. Why Use a Message Queue? Three big reasons: Decoupling : Producers and consumers evolve independently. You can change one without touching the other. Buffering : Producers can run faster than consumers. The queue absorbs spikes and prevents overload. Scaling : You can add more consumers to handle more load, or more producers to generate more work. Core Concepts Producer : Sends messages. Consumer : Receives messages. Queue : Stores messages until consumed. Broker : The server that hosts the queue (e.g., RabbitMQ, Kafka, Redis). Acknowledgment : When a consumer tells the broker it successfully processed a message. Dead Letter Queue : Where messages go if they can't be processed after retries. Simple Example with Redis Redis has a simple list-based queue using LPUSH and BRPOP . Here's a minimal Python example using redis-py . import redis import time r = redis . Redis ( host = ' localhost ' , port = 6379 ) # Producer r . lpush ( ' tasks ' , ' send_email ' ) r . lpush ( ' tasks ' , ' generate_report ' ) # Consumer (blocking pop) while True : task = r . brpop ( ' tasks ' , timeout = 5 ) if task : print ( f " Processing: { task [ 1 ]. decode () } " ) time . sleep ( 1 ) # simulate work else : break This is a simple FIFO queue. It works for basic cases but lacks features like acknowledgments, retries, and routing. Real-World Example with RabbitMQ RabbitMQ is a full-featured broker. Here's a producer an

2026-08-14 原文 →
AI 资讯

Message Queue — Async Processing

Async processing qua message queue: vì sao đẩy việc nặng ra khỏi request path, và cái giá phải trả bằng eventual consistency Async processing là mô hình tách một request thành hai giai đoạn: request handler nhận việc, xác nhận với client, rồi giao phần xử lý thật cho một worker chạy ngoài request path — thường qua một message queue (RabbitMQ, AWS SQS, Kafka, Redis Streams, hoặc queue trên nền Redis như BullMQ/Sidekiq). Lý do dev gặp nó trong việc thật rất cụ thể: một endpoint gọi payment provider mất 3s, gửi email confirm mất 1s, resize ảnh mất 5s — nếu làm tuần tự trong request, p99 latency của endpoint là tổng các con số đó, và một downstream chậm hoặc chết đủ để làm timeout hết thread pool của app server. Đẩy vào queue thì request trả về trong vài chục ms; nhưng đổi lại, cái "xong" mà client thấy không còn nghĩa là việc đã thực sự hoàn thành. Cơ chế hoạt động Ba thành phần: producer (thường là API server) đóng gói việc thành message rồi publish vào broker; broker (RabbitMQ/SQS/Kafka…) giữ message trong queue có persistence tuỳ cấu hình; consumer/worker poll hoặc được push message, xử lý, rồi ack để broker biết xoá. Nếu worker chết trước khi ack, broker redeliver — đây là gốc của semantic at-least-once : mỗi message được giao ít nhất một lần, có thể nhiều lần. Exactly-once trong hệ phân tán chỉ đạt được ở lớp application bằng cách consumer viết idempotent, không phải bằng cấu hình broker. Ví dụ với RabbitMQ + Node ( amqplib ): // producer — trong HTTP handler const ch = await conn . createConfirmChannel () await ch . assertQueue ( ' image.resize ' , { durable : true }) app . post ( ' /upload ' , async ( req , res ) => { const jobId = crypto . randomUUID () const payload = Buffer . from ( JSON . stringify ({ jobId , s3Key : req . body . key })) await ch . sendToQueue ( ' image.resize ' , payload , { persistent : true , // ghi xuống disk, sống sót broker restart messageId : jobId , // để consumer dedupe contentType : ' application/json ' , }) // đợi broker confirm đ

2026-07-08 原文 →