Designing Message Queue Architecture

1. Understanding Message Queue Patterns

PatternUse Case
Work queue1 of N consumers gets each msg
Pub/subAll subscribers get each msg
RoutingTopic/header-based filtering
RPCReply queue + correlation_id
StreamsReplayable log (Kafka)

2. Designing Message Broker Architecture

BrokerStrength
KafkaHigh throughput, replayable log
RabbitMQRich routing, AMQP, work queues
NATS / NATS JetStreamLow latency, simple
PulsarTiered storage, geo-replication
SQS / SNSManaged, simple
Redis StreamsLight, in-memory

3. Designing Message Ordering and Delivery Guarantees

GuaranteeMechanism
FIFOPartition by key (Kafka), FIFO queue (SQS)
At-most-onceFire-and-forget; possible loss
At-least-onceack required; possible duplicate
Exactly-onceIdempotent producer + transactions

4. Designing Dead Letter Queue Strategy

SettingRecommendation
TriggerAfter N failed attempts (3–5)
DLQ retention14 days+ for triage
AlertingPage on DLQ depth > 0
Replay toolingMove back to main queue after fix

5. Designing Message Idempotency

TechniqueDetail
Dedup table(message_id, processed_at)
Conditional updatesUse UPSERT / CAS
Producer-assigned IDStable across retries
Bloom filter windowMemory-efficient recent-dup check

6. Designing Message Schema Evolution

FormatEvolution Rules
AvroDefault values for new fields
ProtobufDon't reuse field nums; reserved
JSON SchemaOptional fields; ignore unknown
Schema registryEnforce compatibility on publish

7. Understanding At-Least-Once vs Exactly-Once Delivery

ModeTrade-off
At-least-once + idempotentPractical "exactly once" outcome
Exactly-once (Kafka EOS)Higher latency, transactional overhead
At-most-onceBest perf; OK for metrics, logs

8. Designing Message Priority Queues

ApproachDetail
Multiple queueshigh/medium/low; consumer drains in order
Native priorityRabbitMQ x-max-priority
Weighted fairAvoid starvation of low priority
CaveatStrict priority can starve; cap budget per tier

9. Designing Message TTL and Expiration

SettingDetail
Message TTLDrop if older than X
Queue TTLAuto-delete idle queue
Expired actionDrop, DLQ, or callback

10. Designing Message Acknowledgment Strategy

ModeDetail
Auto-ackAck on receive; risk of loss
Manual after successSafe; standard
Negative ack (NACK)Requeue or DLQ
Visibility timeout (SQS)Hide while processing; redeliver if not acked

11. Designing Message Retry Strategy

MechanismDetail
In-memory retryFast, but lost on crash
Delay queueVisibility delay or scheduled topic
Exponential backoff queuesretry-1m → retry-5m → retry-30m → DLQ
Max attempts3–5 then DLQ

12. Designing Queue Partitioning

ConceptDetail
PartitionsParallelism unit (Kafka)
Partition keyHashed to choose partition
Consumer groupOne consumer per partition (within group)
RebalanceOn consumer add/remove
Hot partitionAvoid skewed keys