Topic Purpose: One Datamodel, Several Flows
A topic on the Klok broker is named after the datamodel it carries. Often the same datamodel is used in several flows that have nothing to do with each other: new invoices and invoice reminders, or the answers an AI service gives to the documentation service and to an invoice coding process. The topic purpose keeps those flows apart without inventing a new datamodel for each of them.
The purpose is an optional suffix, and it is the publisher's. The solution requires it so that one general datamodel serves several flows; the producer sets it when it publishes, and the consumer sets the same purpose when it subscribes. Each configures it on its own end, and the framework adds it to the topic name on both sides, so they meet on the same topic. In older code, and in the method names of the naming library, it is called the producer intention.
persistent://<tenant>[_<env>]/<domain>/<Model>[_<purpose>]
persistent://acme/finance/InvoiceV1 invoices, no purpose
persistent://acme/finance/InvoiceV1_created the flow of new invoices
persistent://acme/finance/InvoiceV1_reminder the reminder flow, same datamodel
Why It Matters
- The datamodel is reused, and stays typed. Every topic still carries exactly one datamodel, so every consumer gets
the same typed objects and one change to the model reaches all flows. You do not copy the model into
InvoiceCreatedV1andInvoiceReminderV1, and you do not fall back to a generic model with atypefield or to untyped JSON, which gives up the type safety. - A consumer reads only its own flow. Without a purpose, every consumer of a shared model receives every message and has to find and throw away the ones that are not its own, often by looking something up for each message. With a purpose, the broker does that separation for free.
- Each flow can be run on its own. A purpose topic has its own subscriptions, backlog, retention and dead letter topic
(
InvoiceV1_reminder_DLQ). A burst of reminders does not queue up in front of the consumer of new invoices, and a dead letter tells you which flow failed. - Access follows the flow. Grants are given per topic. Another domain or tenant can be granted
consumeon one flow without seeing the others.
The CloudEvent type (ce-type) still says what happened to each message. The purpose decides which topic the message
travels on, and so who receives it.
Example: A Shared AI Service
A generic service that calls an AI model is useful to many processes. Say it reads CallAiRequestV1 and answers with
CallAiResponseV1. If every answer is published on the one topic CallAiResponseV1, every process that uses AI must
subscribe to it, receive every answer on the platform and discard those that are not its own.
With a purpose, each process agrees on its own flow with the service:
documentation service publishes CallAiRequestV1_documentation reads CallAiResponseV1_documentation
invoice coding publishes CallAiRequestV1_invoicecoding reads CallAiResponseV1_invoicecoding
The documentation service now receives only documentation answers, and a burst of invoice coding does not queue up in front of it. Both flows use the same two datamodels.
What The Framework Does Today
A route's purposes are fixed when the route starts: in its code or in its configuration. That covers every flow in which
each route has one purpose, such as an ingress that publishes InvoiceV1_created and an egress that consumes it, or an
egress with result that answers on the purpose of the request it consumes.
A shared service that answers many callers, each on the purpose the caller chose, is not yet supported by the route
patterns: one route has one purpose. The platform's AI workers in klok-egress-ai therefore still publish all results
on one KlokAiTaskResultV1 topic, and a caller such as the documentation service filters them. Support is tracked in
klok-quarkus-extension#32; this section is
updated when it lands.
Setting The Purpose
The purpose can be set in code, by overriding a method of the route pattern, or in configuration. Configuration wins over code.
| Pattern | In code | In configuration | Applies to |
|---|---|---|---|
KlokOnrampRoute, KlokOnrampRouteSync, KlokWebhookOnrampRoute, KlokChannelAdapterSourceRoute |
topicPurpose() |
<RouteId>.topic.purpose |
the topic the route publishes on |
KlokOfframpRoute, KlokOfframpRouteSync, KlokChannelAdapterSinkRoute |
topicPurpose() |
<RouteId>.topic.purpose |
the topic the route consumes |
KlokOfframpRouteWithResult |
topicPurpose(), resultTopicPurpose() |
<RouteId>.topic.purpose, <RouteId>.topic.result-purpose |
the request topic, and the result topic (by default the request's purpose) |
KlokProcessingRoute |
incomingTopicPurpose(), outgoingTopicPurpose() |
<RouteId>.topic.purpose, <RouteId>.topic.outgoing-purpose |
the consumed and the published topic, each on its own |
KlokAsyncTaskWorkerRoute |
taskType() |
the request topic: the task type is the purpose | |
| REST handler that starts a task | the klok-task-type header, set in transformBody |
<RouteId>.task.type |
the request topic the task is published on |
Synchronous patterns put the purpose inside the request/reply pair: ReqRes_<Request>_<Reply>_<purpose>_Request and
_Response.
The producer side:
public class ACM001I1_InvoiceImport extends KlokOnrampRoute<ErpInvoice, InvoiceV1> {
@Override
public String topicPurpose() {
return "created";
}
...
}
The consumer side, when the purpose belongs in configuration because the same code serves more than one flow:
ACM001E1_InvoiceToArchive.topic.purpose=created
Use code when the purpose is part of what the route is: a documentation service's AI answers are always its own. Use
configuration when the same route is deployed for different flows. A key at solution level,
<SolutionId>.topic.purpose, applies to every route of the solution, so set it there only when all of them use the same
flow.
Clients outside the Quarkus extension take the same purpose and name the same topic: sendMessage(Class, payload, purpose, ...) and subscribe(Class, purpose, ...) in the Java broker client, topic(Model, purpose) in the Python and
TypeScript clients.
Agreeing On A Purpose
The purpose is part of the contract between producer and consumer, just like the datamodel.
- Write it down on both sides: in each project's
klok-docs/, next to the route, name the purpose and the project on the other end. - A mismatch does not fail. A producer that publishes
InvoiceV1_createdand a consumer that subscribes toInvoiceV1_neware simply on two different topics, and the consumer waits forever. Every route logs its topics when it starts (Route ACM001E1_InvoiceToArchive subscribes to persistent://acme/finance/InvoiceV1_created (subscription ...)), which is the first place to look when nothing arrives. - Changing a purpose moves the flow to a new topic, and what is left in the old one stays there. Treat it like any topic change: start the consumers on the new purpose first, then move the producers, then let the old topic drain.
Naming A Purpose
- Name the flow as both sides understand it:
created,reminder,documentation,bulk. - Use letters, digits,
-and., never_. The underscore separates the purpose from the model and from_DLQ, and a topic has at most one purpose, never a second suffix. - Task types are purposes too, named
<domain>.<verb>-<object>(barnbok.illustrate-page,ai.chat). - Do not put a version, an environment or the publishing tenant in the purpose. The major version is in the model name, and the environment and tenant are in the namespace.
- Readers are not part of the name: a reader is known by its subscription, named after its route id. When the content
belongs to one reader in another tenant, the publisher may choose that tenant's slug as the purpose
(
persistent://northwind/logistics/DeliveryNoticeV1_acme); see Tenant Bridges.
When Not To Use A Purpose
- Every consumer wants every message: publish without a purpose.
- The flows carry different shapes: that is two datamodels, not two purposes.
- The split follows a value in the data, such as one topic per customer or per country: that multiplies topics and grants. Use the message key, or let the consumer decide from the data.
Checklist
- [ ] Does more than one flow use this datamodel? Give each flow a purpose.
- [ ] Do producer and consumer use exactly the same purpose, and is it documented in both projects?
- [ ] Is the purpose set in code when it is part of the route, and in configuration when the route serves several flows?
- [ ] Does the purpose name the flow, without underscores, versions or environments?
- [ ] Is a consumer reading a shared topic and throwing most messages away? Then the flow needs a purpose.
The framework reference for topic names is Topics and CloudEvents, and every route key is listed in Configuration.