Patterncore
Point-to-Point Channel
Also called Point-to-Point Channel pattern, point-to-point messaging, PTP channel, message queue.
A point-to-point channel gives each message to exactly one of its receivers. Many receivers can read the channel to share the work, but no two of them get the same message at the same time.
- Family
- integration
- Intent
- Send the message on a channel that gives each message to only one receiver, so that a command runs once and a document is processed once.
- Also called
- queue, work queue, task queue
Intent
Send the message on a Point-to-Point Channel, "which ensures that only one receiver will receive a particular message".1 Many receivers can read the channel, and they share the messages between them.
Problem
A shop sends a command message to charge a card for each order. Three copies of the payment program read the commands, so that the work goes on when one copy stops. The question is: "How can the caller be sure that exactly one receiver will receive the document or perform the call?"1
The forces are these. A command must run once: a card that is charged by two receivers is charged twice. The receivers must not need to talk to each other to decide who takes a message, because that makes them depend on each other. And the system must be able to add receivers when the load goes up.
A Publish-Subscribe Channel fails the first force: it gives a copy to each subscriber. A list of receivers in the sender fails the second and the third: the sender must know each receiver and choose one.
Solution
The channel keeps one queue of messages. Each message in the queue goes to one receiver. If two receivers ask for a message at the same time, the channel gives it to one of them, so "the receivers do not have to coordinate with each other".1
+--> payment w1 (charge-1, charge-4)
shop --send--> [ charges ] ---+--> payment w2 (charge-2, charge-5)
one queue +--> payment w3 (charge-3)
each message goes to one receiver
The participants are the sender, the channel and one or more receivers. The receivers that read one channel are Competing Consumers in the book.2 Jakarta Messaging states the rule for its queues: "A queue may have more than one consumer." and "Each message in the queue is delivered to only one consumer."3 It does not define how a provider shares the messages between the consumers.3 RabbitMQ sends each message to the next consumer by default, and the tutorial calls this round-robin.4 Azure Service Bus lists the queue as point-to-point, with a single consumer for each message.5
One receiver at a time, not exactly once. A broker cannot know that a receiver has done the work until the receiver sends an acknowledgement. RabbitMQ re-queues a message when a consumer dies without an ack, and it can then "quickly redeliver it to another consumer".4 Service Bus, in peek lock mode, locks a message for one receiver and unlocks it when the lock times out, and its documentation calls this at-least once processing.5 The following program shows the result. Receiver w2 charges a card and loses its connection before the ack, and the queue gives the same command to w3.
from collections import Counter, deque
class PointToPointQueue:
"""Each message goes to one receiver at a time, and stays unacked until the receiver acks it."""
def __init__(self):
self.ready = deque()
self.unacked = {} # delivery tag -> (receiver, message)
self.next_tag = 1
def send(self, message):
self.ready.append(message)
def deliver(self, receiver):
if not self.ready:
return None
tag, self.next_tag = self.next_tag, self.next_tag + 1
message = self.ready.popleft()
self.unacked[tag] = (receiver, message)
return tag, message
def ack(self, tag):
del self.unacked[tag]
def connection_lost(self, receiver):
# As in RabbitMQ: a message with no ack goes back to the queue.
for tag, (who, message) in list(self.unacked.items()):
if who == receiver:
del self.unacked[tag]
self.ready.appendleft(message)
q = PointToPointQueue()
for n in range(1, 6):
q.send(f"charge-{n}")
handled = Counter()
receivers = ["w1", "w2", "w3"]
crashed = set()
turn = 0
while q.ready:
who = receivers[turn % len(receivers)]
turn += 1
if who in crashed:
continue
tag, message = q.deliver(who)
handled[message] += 1 # the side effect: a card is charged
if message == "charge-2" and who == "w2":
print(f"{who} handled {message}, then lost its connection before the ack")
crashed.add(who)
q.connection_lost(who)
continue
q.ack(tag)
print(f"{who} handled {message}")
print("times handled:", dict(sorted(handled.items())))
w1 handled charge-1
w2 handled charge-2, then lost its connection before the ack
w3 handled charge-2
w1 handled charge-3
w3 handled charge-4
w1 handled charge-5
times handled: {'charge-1': 1, 'charge-2': 2, 'charge-3': 1, 'charge-4': 1, 'charge-5': 1}
The channel did its job: no two receivers had the message at the same time. But the card was charged twice. A receiver of a point-to-point channel that gives at-least-once delivery must be idempotent, or it must find the duplicates. Service Bus says: "If your scenario can't tolerate duplicate processing, add extra logic in your application to detect duplicates."5 The other mode, receive and delete, marks the message as consumed when it is delivered. A message is then lost if the receiver fails, and Service Bus calls this at-most once processing.5
Example
Each example file starts three workers on one queue, sends nine jobs, and stops the workers. Each worker records the jobs that it handled. The program then shows that the nine jobs were handled, each one by one worker. Which worker got which job changes from run to run, so the programs print only the sorted list of jobs.
The examples use the queue or channel of the standard library in place of a broker. A Go channel, a .NET channel and a Java BlockingQueue each give a value to one receiver, so a pool of goroutines, tasks or threads that read one of them is a point-to-point channel inside a process.67 The examples have no acknowledgements, because a value that a thread takes from a queue in memory cannot be lost by the network.
import queue
import threading
jobs = queue.Queue()
done = queue.Queue()
STOP = None
def worker(name):
while (job := jobs.get()) is not STOP:
done.put((job, name))
workers = [threading.Thread(target=worker, args=(f"w{i}",)) for i in range(1, 4)]
for w in workers:
w.start()
for n in range(1, 10):
jobs.put(f"job-{n}")
for _ in workers:
jobs.put(STOP) # one stop message for each worker
for w in workers:
w.join()
results = []
while not done.empty():
results.append(done.get())
names = sorted(job for job, _ in results)
print("handled:", " ".join(names))
print("each job once:", len(names) == len(set(names)) == 9)
handled: job-1 job-2 job-3 job-4 job-5 job-6 job-7 job-8 job-9
each job once: True
- Verdict: stdlib.
queue.Queuegives each item to onegetcall, so three threads that read one queue share the jobs. STOPis a stop message. Each worker ends when it takes one, so the program sends one for each worker. With a broker, a worker usually runs until its process stops, and a stop message is not needed.gettakes the item out of the queue at once. A broker client with manual acks, such as Pika withauto_ack=False, keeps the message until the worker acks it.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
static final String STOP = "stop";
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> jobs = new LinkedBlockingQueue<>();
ConcurrentLinkedQueue<String> done = new ConcurrentLinkedQueue<>();
List<Thread> workers = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
String name = "w" + i;
Thread t = new Thread(() -> {
try {
for (String job = jobs.take(); !job.equals(STOP); job = jobs.take()) {
done.add(job + "@" + name);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
workers.add(t);
t.start();
}
for (int n = 1; n <= 9; n++) {
jobs.put("job-" + n);
}
for (int i = 0; i < workers.size(); i++) {
jobs.put(STOP); // one stop message for each worker
}
for (Thread t : workers) {
t.join();
}
List<String> names = new ArrayList<>();
for (String d : done) {
names.add(d.substring(0, d.indexOf('@')));
}
names.sort(null);
System.out.println("handled: " + String.join(" ", names));
System.out.println("each job once: " + (names.size() == 9 && new HashSet<>(names).size() == 9));
}
}
handled: job-1 job-2 job-3 job-4 job-5 job-6 job-7 job-8 job-9
each job once: true
- Verdict: stdlib.
BlockingQueue.takeremoves one element and waits when the queue is empty, so each job goes to one thread. - The stop message is a plain string here. A typed message, for example a sealed interface with a job record and a stop record, is safer when a job can have any text.
- A JMS or Jakarta Messaging client has the same shape: a
MessageConsumerfor each worker on oneQueue, and the provider gives each message to one consumer.
using System.Collections.Concurrent;
using System.Threading.Channels;
var jobs = Channel.CreateUnbounded<string>();
var done = new ConcurrentBag<(string Job, string Worker)>();
var workers = Enumerable.Range(1, 3).Select(i => Task.Run(async () =>
{
// Each item is read by one reader only.
await foreach (var job in jobs.Reader.ReadAllAsync())
{
done.Add((job, $"w{i}"));
}
})).ToArray();
for (var n = 1; n <= 9; n++)
{
await jobs.Writer.WriteAsync($"job-{n}");
}
jobs.Writer.Complete();
await Task.WhenAll(workers);
var names = done.Select(d => d.Job).Order().ToList();
Console.WriteLine("handled: " + string.Join(" ", names));
Console.WriteLine($"each job once: {names.Count == 9 && names.Distinct().Count() == 9}");
handled: job-1 job-2 job-3 job-4 job-5 job-6 job-7 job-8 job-9
each job once: True
- Verdict: stdlib. A
Channel<T>fromSystem.Threading.Channelsgives each item to one reader, and three tasks that runReadAllAsyncshare the jobs. Writer.Complete()ends the loops of the readers when the channel is empty.ConcurrentBagcollects the results from the three tasks with no lock. The order of the bag is not defined, so the program sorts the jobs.
package main
import (
"fmt"
"slices"
"strings"
"sync"
)
type result struct{ job, worker string }
func main() {
jobs := make(chan string)
done := make(chan result, 9)
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range jobs { // each value is received by one goroutine
done <- result{job, fmt.Sprintf("w%d", i)}
}
}()
}
for n := 1; n <= 9; n++ {
jobs <- fmt.Sprintf("job-%d", n)
}
close(jobs)
wg.Wait()
close(done)
var names []string
seen := map[string]bool{}
for r := range done {
names = append(names, r.job)
seen[r.job] = true
}
slices.Sort(names)
fmt.Println("handled:", strings.Join(names, " "))
fmt.Println("each job once:", len(names) == 9 && len(seen) == 9)
}
handled: job-1 job-2 job-3 job-4 job-5 job-6 job-7 job-8 job-9
each job once: true
- Verdict: native. A value sent on a Go channel is received by one of the goroutines that read it, so
for job := range jobsin three goroutines is a point-to-point channel with three competing receivers. jobshas no buffer, so each send waits for a free worker. This is a simple form of the prefetch limit of a broker: a busy worker gets no new job.close(jobs)ends therangeloops. Thedonechannel has room for the nine results, so no worker waits on it.
Consequences
Benefits.
- The work is shared without coordination. Receivers can join and leave, and the channel spreads the messages over the receivers that are there.
- The sender does not know the receivers. It sends to the name of the queue.
- Load leveling. The queue holds the messages when the receivers are slow, and the receivers take them at their own rate.
Liabilities.
- The order is not kept across receivers. With one receiver, a queue gives the messages in order. With many, a later message can finish before an earlier one, because each receiver works at its own rate. Jakarta Messaging adds that a consumer may not receive all the messages on the queue when other consumers share it.3
- Duplicates under at-least-once delivery. As the program above shows, a redelivered message runs again.
- Unfair dispatch. Round-robin dispatch gives each consumer the same number of messages, not the same amount of work. RabbitMQ uses a prefetch limit,
prefetch_count=1, so that a busy worker gets no new message until it acks the last one.4 - A message that always fails. A message that makes every receiver fail goes back to the queue again and again. Service Bus counts the deliveries, and when the count exceeds the limit, which is 10 by default, it moves the message to the dead-letter queue.8 With a broker that does not do this, the receivers must count and move the message.
When not to use it
When each receiver must see each message. An event that many programs react to, such as an order that was placed, needs a Publish-Subscribe Channel. On a queue, each program would get only some of the events.
When the order of all messages matters. A queue with competing receivers does not keep a global order. Use one receiver, or a channel that keeps an order for each key: a Kafka topic gives each partition to exactly one consumer in a consumer group at a time.9
For a request that needs an answer now. A queue adds a delay and needs a reply channel for the answer.
Variants and related patterns
Competing Consumers. The receivers of one point-to-point channel, which share its messages.2 See also Work queue and Producer-consumer, the same structure inside one program.
Publish-Subscribe Channel. The other kind of channel: each subscriber gets a copy.10 A topic subscription with many consumers combines the two. In Jakarta Messaging, a shared subscription gives each message in the subscription to only one consumer.3
Kafka consumer groups. Kafka divides a topic into partitions, and each partition "is consumed by exactly one consumer within each subscribing consumer group at any given time".9 Each group is then a point-to-point reader of the topic, with the order kept for each partition. Kafka 4.1 adds share groups, where the consumers of a group "cooperatively consume records, and partitions may be assigned to multiple consumers".9
Spring Integration channels. QueueChannel wraps a queue and has point-to-point semantics. DirectChannel is also point-to-point, but it calls one subscribed handler in the thread of the sender.11
Retries. A receiver that fails can return the message to the queue at once, or after a delay. See Retry with backoff.
Known uses
- Spring Integration: QueueChannel.java at v6.5.1 puts each message in a
BlockingQueue, and eachreceivetakes a message out, so only one receiver gets it.12 - RabbitMQ work queues: a queue that many workers consume, with acks and a prefetch limit.4
- Azure Service Bus queues: a single consumer for each message, with peek lock or receive and delete.5
- Jakarta Messaging queues, with any number of consumers for each queue.3
References
- Gregor Hohpe and Bobby Woolf, Enterprise Integration Patterns, Point-to-Point Channel (problem and solution under CC BY), problem and solution statements; pattern text
- Gregor Hohpe and Bobby Woolf, Enterprise Integration Patterns, Designing, Building, and Deploying Messaging Solutions, Addison-Wesley, 2003, Chapter 4, Messaging Channels, Point-to-Point Channel; Chapter 10, Messaging Endpoints, Competing Consumers
- Jakarta Messaging 3.1 specification, 4.1.2 Queue semantics
- RabbitMQ tutorial 2, Work Queues (Python), Round-robin dispatching; Message acknowledgment; Fair dispatch
- Azure Service Bus queues, topics, and subscriptions, Choose between queues and topics; Queues; Receive modes
- .NET documentation, System.Threading.Channels library, Producer/consumer conceptual programming model
- Java SE 21 API, Interface BlockingQueue, interface description
- Azure Service Bus dead-letter queues, Maximum delivery count
- Apache Kafka 4.1.0 documentation source, design.html, The Consumer, Consumer Position; Share groups
- Gregor Hohpe and Bobby Woolf, Enterprise Integration Patterns, Introduction to Messaging Channels, Message Channel Decisions, One-to-one or one-to-many
- Spring Integration reference, Message Channel Implementations, QueueChannel; DirectChannel
- Spring Integration 6.5.1 source, QueueChannel.java, class QueueChannel, lines 34 to 46
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag M7J197 Edit this page History Report an error Markdown