You are viewing documentation for an outdated version of Debezium.
If you want to view the latest stable version of this page, please gohere.

MongoDB Outbox Event Router

The MongoDB Outbox Event Router SMT is under active development. The structure of the emitted message or other details might change as development progresses.

This SMT is supported only for the MongoDB connector. SeeOutbox Event Routerfor the relational database equivalent to this SMT.

The outbox pattern is a way to safely and reliably exchange data between multiple (micro) services. An outbox pattern implementation avoids inconsistencies between a service’s internal state (as typically persisted in its database) and state in events consumed by services that need the same data.

To implement the outbox pattern in a Debezium application, configure a Debezium connector to:

  • Capture changes in an outbox collection

  • Apply the Debezium MongoDB outbox event router single message transformation (SMT)

De开云体育官方注册网址bezium连接器配置为应用the MongoDB outbox SMT should capture changes that occur in an outbox collection only. For more information, seeOptions for applying the transformation selectively.

A connector can capture changes in more than one outbox collection only if each outbox collection has the same structure.

要使用这个贴片,在实际业务操作collection(s) and the insert into the outbox collection must be done as part of a multi-document transaction, which have been being supported since MongoDB 4.0, to prevent potential data inconsistencies between business collection(s) and outbox collection. For future update, to enable updating existing data and inserting outbox event in an ACID transaction without multi-document transactions, we have planned to support additional configurations for storing outbox events in a form of a sub-document of the existing collection, rather than an independent outbox collection.

SeeReliable Microservices Data Exchange With the Outbox Patternto learn about why the outbox pattern is useful and how it works.

Example outbox message

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

# Kafka Topic: outbox.event.order # Kafka Message key: "b2730779e1f596e275826f08" # Kafka Message Headers: "id=596e275826f08b2730779e1f" # Kafka Message Timestamp: 1556890294484 { "{\"id\": {\"$oid\": \"da8d6de63b7745ff8f4457db\"}, \"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}" }

De开云体育官方注册网址bezium连接器配置为应用the MongoDB outbox event router SMT generates the above message by transforming a Debezium raw message like this:

# Kafka Message key: { "id": "{\"$oid\": \"596e275826f08b2730779e1f\"}" } # Kafka Message Headers: "" # Kafka Message Timestamp: 1556890294484 { "patch": null, "after": "{\"_id\": {\"$oid\": \"596e275826f08b2730779e1f\"}, \"aggregateid\": {\"$oid\": \"b2730779e1f596e275826f08\"}, \"aggregatetype\": \"Order\", \"type\": \"OrderCreated\", \"payload\": {\"_id\": {\"$oid\": \"da8d6de63b7745ff8f4457db\"}, \"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}}", "source": { "version": "1.8.1.Final", "connector": "mongodb", "name": "fulfillment", "ts_ms": 1558965508000, "snapshot": false, "db": "inventory", "rs": "rs0", "collection": "customers", "ord": 31, "h": 1546547425148721999 }, "op": "c", "ts_ms": 1556890294484 }

This example of a Debezium outbox message is based on the默认发件箱事件路由器配置, which assumes an outbox collection structure and event routing based on aggregates. To customize behavior, the outbox event router SMT provides numerousconfiguration options.

Basic outbox collection

To apply the default MongoDB outbox event router SMT configuration, your outbox collection is assumed to have the following fields:

{ "_id": "objectId", "aggregatetype": "string", "aggregateid": "objectId", "type": "string", "payload": "object" }
Table 1. Descriptions of expected outbox collection fields
Field Effect

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 collection field, set thecollection.field.event.idSMT optionin 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 theroute.topic.replacementSMT option.

For example, in a default configuration, theroute.by.fieldSMT option is set toaggregatetypeand theroute.topic.replacementSMT option is set tooutbox.event.${routedByValue}. Suppose that your application adds two documents to the outbox collection. In the first document, the value in theaggregatetypefield iscustomers. In the second document, the value in theaggregatetypefield isorders. The connector emits the first document to theoutbox.event.customerstopic. The connector emits the second document to theoutbox.event.orderstopic.

To obtain this value from a different outbox collection field, set theroute.by.fieldSMT optionin 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 collection field, set thecollection.field.event.keySMT optionin the connector configuration.

payload

A representation of the outbox change event. The default structure is JSON. By default, the Kafka message value is solely comprised of thepayloadvalue. However, if the outbox event is configured to include additional fields, the Kafka message value contains an envelope encapsulating both payload and the additional fields, and each field is represented separately. For more information, seeEmitting messages with additional fields.

To obtain the event payload from a different outbox collection field, set thecollection.field.event.payloadSMT optionin the connector configuration.

Additional custom fields

Any additional fields from the outbox collection can beadded to outbox eventseither within the payload section or as a message header.

One example could be a fieldeventTypewhich conveys a user-defined value that helps to categorize or organize events.

Basic configuration

To configure a Debezium connector to support the outbox pattern, configure theoutbox.EventRouterSMT. For example, the basic configuration in a.propertiesfile looks like this:

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter

Options for applying the transformation selectively

In addition to the change event messages that a Debezium connector emits when a database change occurs, the connector also emits other types of messages, including heartbeat messages, and metadata messages about schema changes and transactions. Because the structure of these other messages differs from the structure of the change event messages that the SMT is designed to process, it’s best to configure the connector to selectively apply the SMT, so that it processes only the intended data change messages. You can use one of the following methods to configure the connector to apply the SMT selectively:

Using Avro as the payload format

The MongoDB outbox event router SMT supports arbitrary payload formats. Thepayloadfield value in an outbox collection 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 theKafkaAvroSerializerclass to serializeGenericRecordinstances. To ensure that the Kafka message value is the exact Avro binary data, apply the following configuration to the connector:

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter value.converter=io.debezium.converters.ByteBufferConverter

By default, thepayloadfield value (the Avro data) is the only message value. Configuration ofByteBufferConverteras the value converter propagates thepayloadfield value as-is into the Kafka message value.

Debe开云体育官方注册网址zium连接器可能配置的排放heartbeat, transaction metadata, or schema change events (support varies by connector). These events cannot be serialized by theByteBufferConverterso additional configuration must be provided so the converter knows how to serialize these events. As an example, the following configuration illustrates using the Apache KafkaJsonConverterwith no schemas:

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter value.converter=io.debezium.converters.ByteBufferConverter value.converter.delegate.converter.type=org.apache.kafka.connect.json.JsonConverter value.converter.delegate.converter.type.schemas.enable=false

The delegateConverterimplementation is specified by thedelegate.converter.typeoption. If any extra configuration options are needed by the converter, they can also be specified, such as the disablement of schemas shown above usingschemas.enable=false.

Emitting messages with additional fields

Your outbox collection might contain fields whose values you want to add to the emitted outbox messages. For example, consider an outbox collection that has a value ofpurchase-orderin theaggregatetypefield and another field,eventType, whose possible values areorder-createdandorder-shipped. To emit theeventTypefield value in the outbox message header, configure the SMT like this:

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter transforms.outbox.collection.fields.additional.placement=type:header:eventType

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

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter transforms.outbox.collection.fields.additional.placement=type:envelope:eventType

Expanding escaped JSON String as JSON

You may have noticed that the Debezium outbox message contains thepayloadrepresented as a String. So when this string, is actually JSON, it appears as escaped in the result Kafka message like shown below:

# Kafka Topic: outbox.event.order # Kafka Message key: "1" # Kafka Message Headers: "id=596e275826f08b2730779e1f" # Kafka Message Timestamp: 1556890294484 { "{\"id\": {\"$oid\": \"da8d6de63b7745ff8f4457db\"}, \"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}" }

The outbox event router allows you to expand this message content to "real" JSON with the companion schema being deduced from the JSON document itself. That way the result in Kafka message looks like:

# Kafka Topic: outbox.event.order # Kafka Message key: "1" # Kafka Message Headers: "id=596e275826f08b2730779e1f" # Kafka Message Timestamp: 1556890294484 { "id": "da8d6de63b7745ff8f4457db", "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 }

To enable this transformation, you have to set thecollection.expand.json.payloadto true like below:

transforms=outbox,... transforms.outbox.type=io.debezium.connector.mongodb.transforms.outbox.MongoEventRouter transforms.outbox.collection.expand.json.payload=true

Configuration options

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

Table 2. Descriptions of outbox event router SMT configuration options
Option Default Group Description

警告

Collection

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

  • 警告- The SMT logs a warning and continues to the next outbox collection document.

  • error- The SMT logs an error and continues to the next outbox collection document.

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

All changes in an outbox collection are expected to be an insert or delete operation. That is, an outbox collection functions as a queue; updates to documents in an outbox collection are not allowed. The SMT automatically filters out delete operations (for removing proceeded outbox events) on an outbox collection.

_id

Collection

Specifies the outbox collection field that contains the unique event ID. This ID will be stored in the emitted event’s headers under theidkey.

aggregateid

Collection

Specifies the outbox collection field that contains the event key. When this field 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.

Collection

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 collection field that contains the timestamp that you want to be in emitted outbox messages.

payload

Collection

Specifies the outbox collection field that contains the event payload.

false

Collection

Specifies whether the JSON expansion of a String payload should be done. If no content found or in case of parsing error, the content is kept "as is".

Collection, Envelope

Specifies one or more outbox collection fields 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 field 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 field, 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 beheaderorenvelope.

Collection, Schema

When set, this value is used as the schema version as described in theKafka Connect SchemaJavadoc.

aggregatetype

Router

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

(?.*)

Router

Specifies a regular expression that the outbox SMT applies in the RegexRouter to outbox collection documents. This regular expression is part of the setting of theroute.topic.replacementSMT option.

+ The default behavior is that the SMT replaces the default${routedByValue}variable in the setting of theroute.topic.replacementSMT option with the setting of theroute.by.fieldoutbox SMT option.

outbox.event.${routedByValue}

Router

Specifies the name of the topic to which the connector emits outbox messages. The default topic name isoutbox.event.followed by theaggregatetypefield value in the outbox collection document. For example, if theaggregatetypevalue iscustomers, the topic name isoutbox.event.customers.

+ To change the topic name, you can:

false

Router

Indicates whether an empty ornullpayload causes the connector to emit a tombstone event.

tracingspancontext

Tracing

The name of the field containing tracing span context.

debezium-read

Tracing

The operation name representing the Debezium processing span.

false

Tracing

Whentrueonly events that have serialized context field should be traced.

Distributed tracing

The outbox event routing SMT has support for distributed tracing. Seetracing documentationfor more details.