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

Message Queues Explained with Practical Examples

Tech Forge 2026年08月14日 08:01 7 次阅读 来源:Dev.to

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

本文内容来源于互联网,版权归原作者所有
查看原文