Enrich
A shipment event names a carrier and nothing about it, and the customer needs the mode and the tracking link. A Camel Content Enricher asks the owner for every record; a Kafka Streams join reads a local copy of the owner's topic. They differ when the reference changes and when the owner is down.
ShipmentScheduled says which carrier was booked and the tracking number. The customer wants more: does the parcel travel by road or by air, and which link tracks it. shipping-service owns that reference data. notification-service builds the message the customer reads, so it needs the data without owning it. The same enrichment is written twice in notification-service, once as a Camel route and once as a Kafka Streams topology, and both read the same input.
Learning objectives
- Merge external reference data into a message via a Content Enricher call
- Join a stream against a compacted topic’s local copy with a GlobalKTable
- Weigh freshness against availability when choosing a lookup or a local copy
- Trace a stalled consumer back to a failed enrichment call and its lag
Why this matters
When a shipment notification links to the wrong tracking page, the customer is stranded on a stale page. When the notification service blocks on every lookup, a reference-data outage becomes a shutdown of customer messaging. Choosing a live lookup over a local copy decides whether a message reflects what the owner knows now or what it last published, and whether one service’s downtime stalls another’s queue. Get it wrong and stored messages either freeze at values nobody can defend later, or notification delivery depends on the shipping service staying up.
Pattern: Content Enricher
The problem
The sender of a message often lacks data the receiver needs. shipping-service publishes the facts it decided, the carrier and the tracking number, and not the carrier’s tracking page, which changes for reasons that have nothing to do with a shipment. Putting the page into every event would repeat reference data in every record and freeze an old value into each of them. Making the receiver keep the data itself would give two services one truth to maintain.
Two ways of asking exist. The receiver can ask the owner each time, and so see the current row but depend on the owner being up. Or it can keep a copy fed by the owner’s own events, and so work when the owner is down but only know what the owner last published. A fetch-back over HTTP that replaces a thin event with the full order is one such call. Here the answer is merged into the message and the message is kept.
The solution
A Content Enricher uses data in the incoming message to fetch the missing details from an external source and attaches them to the message.
Hohpe and Woolf define the pattern by that source being external to the message. Whether the source is a call or a local copy is a choice the pattern leaves open, and it decides how the enrichment behaves when the world changes.
How Quarkus and Camel model it
ShipmentEnricherRoute is the lookup form. It reads ShipmentScheduled, calls shipping-service with the carrier from the message, and merges the answer:
from("kafka:" + Topics.SHIPPING_SCHEDULED + "?groupId=notification-enricher-camel&sessionTimeoutMs=10000"
+ "&breakOnFirstError={{enricher.break-on-first-error}}")
.unmarshal().json(JsonLibrary.Jackson, ShipmentScheduled.class)
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.enrich().simple("{{shipping-service.url}}/carriers/${body.shipment.carrier}")
.aggregationStrategy(carrierEnrichment)
.setHeader(KafkaConstants.KEY, simple("${body.shipment.orderId}"))
.marshal().json(JsonLibrary.Jackson)
.to("kafka:" + Topics.SHIPPING_SCHEDULED_ENRICHED_CAMEL);
enrich calls the endpoint and hands the original message and the answer to an AggregationStrategy. CarrierEnrichment reads the answer as a CarrierReference and calls Enrichment.merge, the one function both forms use, which fills the carrier mode and replaces {trackingNumber} in the template. The key is set through the CamelKafkaKey header (KafkaConstants.KEY); a key option on the endpoint would take precedence over the header.
A failed call is retried three times, and then the exchange fails. With breakOnFirstError the consumer retries the failed record instead of polling the next one, so the group’s offset stays on it until the call succeeds. The record is neither lost nor skipped, and the records behind it wait.
The same enrichment as a stream-table join
The second form moves the reference data to the receiver. edp.shipping.carriers is a compacted topic with one record per carrier, so it keeps the latest reference for each and nothing older. shipping-service owns the reference. Here the records are produced to the topic directly; in production the owner publishes them from its table with an outbox or log tailing. A Kafka Streams GlobalKTable reads the whole topic into a local store on every instance, and the stream of shipments joins against it:
GlobalKTable<String, CarrierReference> carriers = builder.globalTable(Topics.SHIPPING_CARRIERS,
Consumed.with(Serdes.String(), references),
Materialized.<String, CarrierReference, KeyValueStore<Bytes, byte[]>>as("carriers"));
builder.stream(Topics.SHIPPING_SCHEDULED, Consumed.with(Serdes.String(), scheduled))
.leftJoin(carriers,
(orderId, event) -> event.shipment().carrier().name(),
(event, reference) -> enrichment.merge(Enrichment.KAFKA_STREAMS, event, reference))
.to(Topics.SHIPPING_SCHEDULED_ENRICHED, Produced.with(Serdes.String(), enriched));
The first lambda says where to look: the join key is a field of the value, not the record key, which is the order id. That is what a global table is for. A KTable joins on the record key, so joining shipments on a table keyed by carrier matches nothing, and it would need a re-keying and a repartition topic for two rows. The global table needs no co-partitioning because every instance holds every partition of the reference topic. It also costs a full copy of the topic on every instance, which suits a small table and not a large one. The local copy is the same idea as a table copy of the owner’s rows kept in a service’s own database, held by Kafka Streams instead.
The join is a left join. A shipment whose carrier has no reference is forwarded with an empty mode and link; an inner join would drop it, and nothing would show that a customer was never told.
The topology is a Topology bean and the extension runs it. Readiness waits for the two input topics, liveness follows the state of the Streams instance, and a restarted instance bootstraps the store from the compacted topic before it processes shipments.
When the reference changes
An operator changes FASTLINE’s tracking page. The owner’s row changes first, and the topic changes when the owner publishes it.
A shipment scheduled in between gets the new link from the lookup and the old link from the join. The lookup sees the owner’s current row. The join sees the topic, so it is as fresh as the time the owner takes to publish plus the time a store takes to apply it, which measured 7 to 13 ms once the record was on the topic. After that the two agree.
Neither form revises an earlier output. A message already written holds the link it was enriched with, and the next shipment holds the new one. If outputs must follow the reference, they are not enriched messages but joins that recompute, and that is a table-to-table join with its own output stream.
When the owner is down
With shipping-service stopped, the lookup cannot answer. The route retries, the consumer stays on the failed record, the offset stays put and the group shows a lag of one. Nothing is lost, nothing behind it moves, and when the owner returns the record is enriched about three seconds after the owner was started again. The join does not notice. It enriches from the local copy and goes on, with data as old as the last record it read.
breakOnFirstError=false removes the guard. The consumer commits past the failed record and carries on, and the record is never enriched. The failure is silent and permanent, which is why the guard is the first thing to check in an enricher that calls out.
The trade-offs, measured on the same input:
| Camel lookup | Streams join | |
|---|---|---|
| Where the reference lives | Owner only | Owner, plus a full copy in every instance |
| Freshness | The owner’s current row | The topic’s last record for the carrier |
| Owner down | Stalls on the failed record, catches up, loses nothing | Continues from the local copy |
| Cost per record | One HTTP call; a burst of 500 shipments made 500 lookups | A local store read, no lookups |
| Burst of 500 shipments, producer call to last output | 6.4 s (6.35 to 6.44) | 2.5 s (2.50 to 2.56) |
| Memory | None for the reference | The whole table, per instance |
| Reference deleted from the topic | Not affected while the owner still has it | The record is kept with an empty mode and link |
| Coupling | Availability and latency of the owner | Format of the reference topic |
A real service picks one.
Build, run, observe
(cd examples/domain-model && mvn -B -q install)
cd examples/19-enrich
mvn -B verify
./verify.sh
verify.sh sends the same shipments to both forms, changes a reference, stops shipping-service and recreates the enricher. Watch the two outputs differ for one shipment after the template change, and the lookup’s lag fall back to zero when the owner returns. It prints 20/20 checks passed.
Try one change and rerun:
CAMEL_BREAK_ON_FIRST_ERROR=false ./verify.sh(demo code): the shipment that arrives while the owner is down is committed and never enriched.
When not to use it
Do not enrich when the receiver can ask for the data at the moment it is needed. A consumer that renders a page can call the owner then. Enrichment is the right tool when the enriched message is stored, sent on or replayed, and so must hold what was true when it was made.
Do not call out from a consumer whose availability must not depend on another service. A lookup couples the consumer to the owner’s uptime and latency, and a stall in one record holds up every record behind it in the partition. Use a local copy, and accept that it is as fresh as the owner’s last published change.
Do not hold a copy of data that is large, changes constantly or is private to the owner. A global table is a full copy per instance; for a big reference set a partitioned table joined on a co-partitioned key is the form, and the join key must then be the record key. If the owner publishes no change events, there is nothing to fill the copy from.
Common pitfalls
- Turning off
breakOnFirstError(theCAMEL_BREAK_ON_FIRST_ERROR=falseswitch) lets the consumer commit past a failed lookup, so the shipment is never enriched and nothing marks the record as wrong. - Using an inner join instead of a left join against the reference table drops every shipment whose carrier has no reference row, with no trace left that the customer was never told the tracking link.
- Expecting an already-published enriched message to pick up a later reference change: neither form revises what it already wrote, so a stale link in a stored or replayed message outlives the fix made at the owner.
🤖 Explore with your agent “In examples/19-enrich, change FASTLINE’s template through PUT /carriers/FASTLINE on shipping-service before publishing it to edp.shipping.carriers, schedule a shipment, and compare ShipmentEnricherRoute’s output with ShipmentEnrichmentTopology’s. Then stop shipping-service, schedule another, and state when each form should be chosen.” Check: the shipment scheduled between the change and its publication has the new link on edp.shipping.scheduled-enriched-camel and the old link on edp.shipping.scheduled-enriched, and with the owner stopped only the Streams output grows while the Camel group has lag 1.
References
- Gregor Hohpe and Bobby Woolf, Enterprise Integration Patterns, Addison-Wesley, 2003: Content Enricher
- Apache Camel, Enrich and Kafka component (
breakOnFirstError) - Apache Kafka, Kafka Streams DSL (joins, co-partitioning, the GlobalKTable)
- Quarkus, Using Apache Kafka Streams (topics, health checks, interactive queries)
- Alex Lawrence, Message- and Event-Driven Systems with CQRS and Event Sourcing, Addison-Wesley, 2026, chapter 9: Composing Data from Different Sources
- Adam Bellemare, Building Event-Driven Microservices, 2nd ed., O’Reilly, 2025, chapter 7: Denormalization and Eventification and chapter 8: Stateful Event-Driven Microservices
- Enterprise Integration Patterns with Camel, chapter 12: Transformation Fundamentals (Content Enricher)
- Event notification
- Event-carried state transfer and claim check
- Transactional outbox
- Log tailing with Debezium