Outbox Event Router

Example outbox message

To learn about how to configure the Debezium outbox event router SMT, consider the following example of a Debezium outbox message:

  1. # Kafka Topic: outbox.event.order
  2. # Kafka Message key: "1"
  3. # Kafka Message Headers: "id=4d47e190-0402-4048-bc2c-89dd54343cdc"
  4. # Kafka Message Timestamp: 1556890294484
  5. {
  6. "{\"id\": 1, \"lineItems\": [{\"id\": 1, \"item\": \"Debezium in Action\", \"status\": \"ENTERED\", \"quantity\": 2, \"totalPrice\": 39.98}, {\"id\": 2, \"item\": \"Debezium for Dummies\", \"status\": \"ENTERED\", \"quantity\": 1, \"totalPrice\": 29.99}], \"orderDate\": \"2019-01-31T12:13:01\", \"customerId\": 123}"
  7. }

A Debezium connector that is configured to apply the outbox event router SMT generates the above message by transforming a Debezium raw message like this:

  1. # Kafka Message key: "406c07f3-26f0-4eea-a50c-109940064b8f"
  2. # Kafka Message Headers: ""
  3. # Kafka Message Timestamp: 1556890294484
  4. {
  5. "before": null,
  6. "after": {
  7. "id": "406c07f3-26f0-4eea-a50c-109940064b8f",
  8. "aggregateid": "1",
  9. "aggregatetype": "Order",
  10. "payload": "{\"id\": 1, \"lineItems\": [{\"id\": 1, \"item\": \"Debezium in Action\", \"status\": \"ENTERED\", \"quantity\": 2, \"totalPrice\": 39.98}, {\"id\": 2, \"item\": \"Debezium for Dummies\", \"status\": \"ENTERED\", \"quantity\": 1, \"totalPrice\": 29.99}], \"orderDate\": \"2019-01-31T12:13:01\", \"customerId\": 123}",
  11. "timestamp": 1556890294344,
  12. "type": "OrderCreated"
  13. },
  14. "source": {
  15. "version": "0.9.3.Final",
  16. "connector": "postgresql",
  17. "name": "dbserver1-bare",
  18. "db": "orderdb",
  19. "ts_usec": 1556890294448870,
  20. "txId": 584,
  21. "lsn": 24064704,
  22. "schema": "inventory",
  23. "table": "outboxevent",
  24. "snapshot": false,
  25. "last_snapshot_record": null,
  26. "xmin": null
  27. },
  28. "op": "c",
  29. "ts_ms": 1556890294484
  30. }

This example of a Debezium outbox message is based on the default outbox event router configuration, which assumes an outbox table structure and event routing based on aggregates. To customize behavior, the outbox event router SMT provides numerous configuration options.

Basic outbox table

To apply the default outbox event router SMT configuration, your outbox table is assumed to have the following columns:

  1. Column | Type | Modifiers
  2. --------------+------------------------+-----------
  3. id | uuid | not null
  4. aggregatetype | character varying(255) | not null
  5. aggregateid | character varying(255) | not null
  6. type | character varying(255) | not null
  7. payload | jsonb |
Table 1. Descriptions of expected outbox table columns
ColumnEffect

id

Contains the unique ID of the event. In an outbox message, this value is a header. You can use this ID, for example, to remove duplicate messages.

To obtain the unique ID of the event from a different outbox table column, set the table.field.event.id SMT option in the connector configuration.

aggregatetype

Contains a value that the SMT appends to the name of the topic to which the connector emits an outbox message. The default behavior is that this value replaces the default ${routedByValue} variable in the route.topic.replacement SMT option.

For example, in a default configuration, the route.by.field SMT option is set to aggregatetype and the route.topic.replacement SMT option is set to outbox.event.${routedByValue}. Suppose that your application adds two records to the outbox table. In the first record, the value in the aggregatetype column is customers. In the second record, the value in the aggregatetype column is orders. The connector emits the first record to the outbox.event.customers topic. The connector emits the second record to the outbox.event.orders topic.

To obtain this value from a different outbox table column, set the route.by.field SMT option in the connector configuration.

aggregateid

Contains the event key, which provides an ID for the payload. The SMT uses this value as the key in the emitted outbox message. This is important for maintaining correct order in Kafka partitions.

To obtain the event key from a different outbox table column, set the table.field.event.key SMT option in the connector configuration.

type

A user-defined value that helps categorize or organize events.

payload

The representation of the event itself. The default structure is JSON. The content in this field becomes one of these:

  • Part of the outbox message payload.

  • If other metadata, including eventType is delivered as headers, the payload becomes the message itself without encapsulation in an envelope.

To obtain the event payload from a different outbox table column, set the table.field.event.payload SMT option in the connector configuration.

Basic configuration

To configure a Debezium connector to support the outbox pattern, configure the outbox.EventRouter SMT. For example, the basic configuration in a .properties file looks like this:

  1. transforms=outbox,...
  2. transforms.outbox.type=io.debezium.transforms.outbox.EventRouter

Using Avro as the payload format

The outbox event router SMT supports arbitrary payload formats. The payload column value in an outbox table is passed on transparently. An alternative to working with JSON is to use Avro. This can be beneficial for message format governance and for ensuring that outbox event schemas evolve in a backwards-compatible way.

How a source application produces Avro formatted content for outbox message payloads is out of the scope of this documentation. One possibility is to leverage the KafkaAvroSerializer class to serialize GenericRecord instances. To ensure that the Kafka message value is the exact Avro binary data, apply the following configuration to the connector:

  1. transforms=outbox,...
  2. transforms.outbox.type=io.debezium.transforms.outbox.EventRouter
  3. value.converter=io.debezium.converters.ByteBufferConverter

By default, the payload column value (the Avro data) is the only message value. Configuration of ByteBufferConverter as the value converter propagates the payload column value as-is into the Kafka message value.

The Debezium connectors may be configured to emit heartbeat, transaction metadata, or schema change events (support varies by connector). These events cannot be serialized by the ByteBufferConverter so additional configuration must be provided so the converter knows how to serialize these events. As an example, the following configuration illustrates using the Apache Kafka JsonConverter with no schemas:

  1. transforms=outbox,...
  2. transforms.outbox.type=io.debezium.transforms.outbox.EventRouter
  3. value.converter=io.debezium.converters.ByteBufferConverter
  4. value.converter.delegate.converter.type=org.apache.kafka.connect.json.JsonConverter
  5. value.converter.delegate.converter.type.schemas.enable=false

The delegate Converter implementation is specified by the delegate.converter.type option. If any extra configuration options are needed by the converter, they can also be specified, such as the disablement of schemas shown above using schemas.enable=false.

Emitting messages with additional fields

Your outbox table might contain columns whose values you want to add to the emitted outbox messages. For example, consider an outbox table that has a value of purchase-order in the aggregatetype column and another column, eventType, whose possible values are order-created and order-shipped. To emit the eventType column value in the outbox message header, configure the SMT like this:

  1. transforms=outbox,...
  2. transforms.outbox.type=io.debezium.transforms.outbox.EventRouter
  3. transforms.outbox.table.fields.additional.placement=type:header:eventType

To emit the eventType column value in the outbox message envelope, configure the SMT like this:

  1. transforms=outbox,...
  2. transforms.outbox.type=io.debezium.transforms.outbox.EventRouter
  3. transforms.outbox.table.fields.additional.placement=type:envelope:eventType

Configuration options

The following table describes the options that you can specify for the outbox event router SMT. In the table, the Group column indicates a configuration option classification for Kafka.

Table 2. Descriptions of outbox event router SMT configuration options
OptionDefaultGroupDescription

id

Table

Specifies the outbox table column that contains the unique event ID.

aggregateid

Table

Specifies the outbox table column that contains the event key. When this column contains a value, the SMT uses that value as the key in the emitted outbox message. This is important for maintaining correct order in Kafka partitions.

Table

By default, the timestamp in the emitted outbox message is the Debezium event timestamp. To use a different timestamp in outbox messages, set this option to an outbox table column that contains the timestamp that you want to be in emitted outbox messages.

payload

Table

Specifies the outbox table column that contains the event payload.

aggregateid

Table

Specifies the outbox table column that contains the payload ID.

Table, Envelope

Specifies one or more outbox table columns that you want to add to outbox message headers or envelopes. Specify a comma-separated list of pairs. In each pair, specify the name of a column and whether you want the value to be in the header or the envelope. Separate the values in the pair with a colon, for example:

id:header,my-field:envelope

To specify an alias for the column, specify a trio with the alias as the third value, for example:

id:header,my-field:envelope:my-alias

The second value is the placement and it must always be header or envelope.

Table, Schema

When set, this value is used as the schema version as described in the Kafka Connect Schema Javadoc.

aggregatetype

Router

Specifies the name of a column in the outbox table. The default behavior is that the value in this column becomes a part of the name of the topic to which the connector emits the outbox messages. An example is in the description of the expected outbox table.

(?<routedByValue>.*)

Router

Specifies a regular expression that the outbox SMT applies in the RegexRouter to outbox table records. This regular expression is part of the setting of the route.topic.replacement SMT option.

The default behavior is that the SMT replaces the default ${routedByValue} variable in the setting of the route.topic.replacement SMT option with the setting of the route.by.field outbox SMT option.

outbox.event​.${routedByValue}

Router

Specifies the name of the topic to which the connector emits outbox messages. The default topic name is outbox.event. followed by the aggregatetype column value in the outbox table record. For example, if the aggregatetype value is customers, the topic name is outbox.event.customers.

To change the topic name, you can:

  • Set the route.by.field option to a different column.

  • Set the route.topic.regex option to a different regular expression.

false

Router

Indicates whether an empty or null payload causes the connector to emit a tombstone event.

warn

Debezium

Determines the behavior of the SMT when there is an UPDATE operation on the outbox table. Possible settings are:

  • warn - The SMT logs a warning and continues to the next outbox table record.

  • error - The SMT logs an error and continues to the next outbox table record.

  • fatal - The SMT logs an error and the connector stops processing.

All changes in an outbox table are expected to be INSERT operations. That is, an outbox table functions as a queue; updates to records in an outbox table are not allowed. The SMT automatically filters out DELETE operations on an outbox table.

tracingspancontext

Tracing

The name of the field containing tracing span context.

debezium-read

Tracing

The operation name representing the Debezium processing span.

false

Tracing

When true only events that have serialized context field should be traced.

Distributed tracing

The extension has support for the distributed tracing. See tracing documentation for more details.