One Pager Cheat Sheet
- A
message queueis an asynchronous channel whereproducerssend messages to abrokerfor later processing byconsumers, decoupling services, absorbing traffic spikes, and improving resilience so systems can hand off work without waiting and users see “it just works.” - Queues provide
decoupling,buffering,load leveling,retry, andfan-out—patterns that underpin microservices and serverless systems—and implementbackpressureby growing when consumers lag, signaling the need to scale workers or throttle producers. - Queues let producers proceed without waiting for consumers because the intermediary
message queuedecouples producers and consumers and buffers work so a producer can return once anenqueueis acknowledged (enabling asynchronous processing, failure isolation, and higher throughput), though this depends on queue capacity and policies (leading to backpressure) and requires handlingFIFO/ordering,at-least-oncedelivery,durability,idempotency, and eventual consistency. - Messaging systems have components —
Producer(creates messages),Broker(persists, routes, delivers), andConsumer(receives; can join aconsumer group) — rely on acknowledgment (Ack) to signal processing and avoid redelivery, and support delivery modes likeat-least-once(may duplicate),at-most-once(may drop), andexactly-once(hard, platform-constrained). - Delivery guarantees span At-least-once (duplicates possible—build consumer idempotency; e.g.,
SQS Standardmay deliver duplicates and reorder), FIFO/Exactly-once-ish (per-queue deduplication such asSQS FIFO's deduplication windows), and Exactly-once processing (achieved withtransactions+idempotent producersinKafka Streams/apps, with important trade-offs). - Exactly-once guarantees are neither free nor universal: achieving
exactly-oncerequires broker-specific primitives (e.g.,idempotent producers,transactions,sequence numbers) plus extra coordination and persistent state, which introduces performance and operational costs, often only applies within a bounded scope (not end-to-end withouttwo-phase commitor application-level idempotency), and still places a correctness burden on client design to avoid duplicates. - A minimal
producer/consumerdemo using Python's standard library (no external deps) that demonstratesbuffering,retries, andack-like behavior. - Because a
brokercan't know whether aconsumerfinished processing until it receives anack, it treatsunacknowledged messages(or those past avisibility timeout) as potentially unprocessed and will redeliver them to prevent message loss, which yields at-least-once delivery—so consumers must handle duplicate delivery via idempotent processing or deduplication. FIFOenforces ordering andpartitionslet you scale by splitting a stream while preserving order within each partition; popularized byKafka, consumers in the same group share partitions soconsumer groupsprovide horizontal scaling with automatic rebalancing, letting you add nodes to increase throughput at the cost of per-partition order only.- Protocols like
AMQP(withexchanges,queues, andbindings— e.g.AMQP 0-9-1in RabbitMQ),MQTT(a lightweight pub/sub for IoT) and cloudHTTPAPIs determine message-routing semantics (e.g.fanout,topic,direct), QoS levels, and interoperability across languages. - Because
AMQPmodels messaging as a broker-mediated messaging model with first-classexchanges,queues, andbindings—so the broker applies routing logic (e.g.,direct,fanout,topic,headers) viaexchange/bindingrules, providing flexible, server-side routing that decouples producers from consumers. - RabbitMQ is a mature broker widely deployed on-premises and in the cloud that supports
AMQPandMQTT, where youdeclare exchanges,bind queues,publishandack, offers both classic queues and stream-like features, and follows the core mental modelpublishers → exchange (routing) → queue → consumerswithdead-letter exchangesused for retries. - Producers always publish to an
exchangeviabasic.publish, and thedefault exchange("") only makes it look like publishing to a queue while exchanges fundamentally decouple producers and queues to enable routing, pub/sub, dead-lettering and other features, with UI/client conveniences hiding but not removing the exchange layer. - Apache Kafka is a distributed commit log built around
partitionsandretentionthat delivers high throughput, durable history and replay for both streaming pipelines and classic messaging, whileKafka Streamsprovides stateful processing withexactly-oncesemantics when configured correctly—operators must still managereplication,partitions, andcompactionpolicies. - Kafka is fundamentally an append-only, durable log where each
topicis split intopartitions(ordered sequences of messages with monotonically increasingoffsets stored insegmentfiles), achieving durability via local persistence and replication (e.g.,acks=all,min.insync.replicas,ISR), enabling replayable history, high throughput, and fault tolerance, with data kept according tocleanup.policy(retention.ms/retention.bytesorcompact). Redis Streamsprovide append-only logs via commands likeXADD/XREADand support consumer groups withXREADGROUPfor ordered entries with IDs, pending lists and claiming of stuck deliveries—offering more control than simple pub/sub and are great for lightweight pipelines if you run Redis, but watch memory usage and persistence settings (AOF/RDB).- AWS SQS offers
Standardqueues with high throughput, at‑least‑once delivery, and best‑effort ordering (duplicates possible), andFIFOqueues for ordered, exactly‑once processing via deduplication windows (no duplicate inserts within thededupe interval); all useHTTP APIsand are ideal to decouple serverless functions, run background jobs, and handle retries withvisibility timeouts. - Amazon SQS
Standardqueues offer high throughput andat-least-oncedelivery but only best-effort ordering (due to distribution, duplicates, concurrentReceiveMessageconsumers, retries/visibility timeouts and batching), whileFIFOqueues give guaranteed ordering within amessage group IDplus deduplication/exactly-once processing(with lower throughput); if you must useStandard, include sequence numbers and make consumers idempotent. - Google Cloud Pub/Sub uses
topicsandsubscriptionswhere subscribers mustackto prevent redelivery; it avoids simultaneous delivery of the same message to multiple subscribers of a single subscription, deletes acknowledged messages asynchronously, and supports global fan-out, event ingestion, and managedpush/pullsubscriptions. - Apache Pulsar separates
serving(brokers) fromstorage(Apache BookKeeper), enablingsegment-tiered storageand independent scaling, and making it a strong fit for multi-tenancy, geo-replication, and very long retention with offloading, unlikeKafka's tighter storage–compute coupling. Pulsarachieves a separation of serving and storage by havingbrokersperform client-facing serving whileBookKeeperbookiespersist messages in replicatedledgersfor durable, replicated storage andoffloadingto object stores, enabling independent scaling, lightweight brokers, long retention, and simpler operations compared with systems that couple compute and storage.NATSusessubjectsfor ultra-fast messaging, whileJetStreamadds persistence, replay, and configurable QoS, giving you both low-latency fire-and-forget and durable streams — ideal for control-plane and edge messaging withJetStreamproviding the durability.Point-to-pointuses one queue with many workers (competing consumers) so each message goes to one worker, whereasPub/Subpublishes to atopicwith fan-out so many subscriptions each receive a copy and typically usesack+redelivery.- Do Receive message, Process message, Commit / Save side effects, then Ack (
ack) — because brokers often useat-least-once delivery, so you must ensure side effects are durably persisted before sending theackto avoid lost side effects, and handle possible redelivery withidempotency, theoutbox pattern/transactions,visibility timeoutor lease-renewal, and adead-letter queuefor poison messages. - In
AMQP-ish Routing, since we can’t importRabbitMQlibs here, we’ll model routing decisions with standardcollections. - A
topicexchange implements pattern-based (wildcard) routing by comparing a message'srouting keyto queuebinding keypatterns and delivering to any queue whose binding pattern matches the routing key;binding keys use token-based wildcards where*matches exactly one word and#matches zero or more words (with#as a catch‑all and support for placements likea.#.b), this is not regular-expression matching, and it therefore differs fromdirect,fanout, andheadersexchanges. - Because
at-least-oncecan duplicate deliveries, make consumersidempotent(e.g., use keys, dedupe tables, or atransactional outbox); even withKafka'sexactly-oncefeatures, end-to-end guarantees still depend on the whole pipeline. - Idempotent handlers ensure the same result on repeated processing by making operations deterministic or guarded by deduplication/transactional mechanisms—using
idempotency keys with adedupe tableorunique constraint,upsertsemantics, atransactional outbox, or trackinglast_processed_id—so the system’s observable state and external side effects remain unchanged despiteat-least-oncedelivery and to preserve end-to-end guarantees even when relying on Kafkaexactly-oncefeatures. - When work fails, retry with backoff, and after N tries send to a
dead-letter queue (DLQ)for inspection; many systems also use avisibility timeoutso messages notackedwithin the window are automatically redelivered (classic inSQSor viaack deadlines). - Hands-on demo using
Go channelsto implement in-process queues and fan-out workers, all with no external libraries. - Go
channels are an in-process, typed queue (chan T) provided by the runtime that give compile-time type safety, built-in synchronization via blockingbuffered channel/unbuffered channelsemantics (providing automatic backpressure and per-sender FIFO ordering), are easily composable withgoroutines andselectinto patterns like fan-out/fan-in, pipelines, and worker pools, supportclose/rangeandcontext.Contextfor lifecycle and coordination, and offer low-latency, low-overhead in-process communication — but they are in-process only, lack persistence or distributed delivery and can cause deadlocks or goroutine leaks if misused, so external queues are needed for cross-process or durable workloads. - Pick
Redis Streamsfor simple streams + groups (if already on Redis);Kafkafor massive throughput, replay, partitions, stream processing;SQSfor managed queueing with HTTP + serverless;Pub/Subfor fan-out at global scale with managed subs;RabbitMQfor AMQP routing, varied protocols, easy ops;Pulsarfor multi-tenant systems with storage decoupled from brokers; andNATS + JetStreamfor ultra-low latency control plane with optional durability. - Maintain secure endpoints (use
TLS), enforce credentials/roles and least privilege, monitor lag, consumer errors, andDLQrates, scale viapartitions,consumer groups, andsharding, and ensure backups and retention for compliance and replay.


