Enrichment
Reference for Sequin’s enrichment functionality. Use enrichment to add additional data to your messages before they are sent to sinks.
Enrichment allows you to enrich your messages with additional data from your database before they are sent to the sink destination. This is useful for:
- Adding related data from other tables to your messages
- Reducing the number of queries your downstream systems need to make
- Building composite objects from multiple tables, ie. for search indexing
How it works
To configure enrichment on a sink consumer, you must:
- Create an enrichment function
- Configure the sink consumer to use the enrichment function
When you configure enrichment on a sink consumer, Sequin will:
- Batch messages that need to be enriched
- Execute your enrichment query against your source database with the primary keys of those messages as parameters (ie.
$1
) - Determine which enrichment result corresponds to each message (by matching the primary key values) and set it as the message’s
metadata.enrichment
field - Send the enriched messages through the rest of your sink pipeline, including into transform and routing functions
SQL requirements
Enrichment functions use SQL to query your source database. The SQL query must:
- Use parameterization with the
$1
syntax - Select all primary key columns from the source table
- Select additional fields for enrichment
- Return 0 or 1 row per message
Here’s an example that enriches user messages with their account information:
Primary keys
Your enrichment query must select all primary key columns from the source table. These are used to match the enrichment results back to the original messages.
For example, if your table has a primary key of id
, your query must select id
:
Parameterization
Enrichment functions must use the $1
syntax for parameterization. This allows Sequin to safely batch multiple messages into a single query.
Composite primary keys
If your table has componsite primary keys, you must use a relatively advanced SQL where clause to select rows with those keys. Consider a table with a primary keys of (id, account_id)
:
Sequin will assign the primary key values to two parameters, $1
and $2
. Sequin will assign them in alphabetical order of the column names. In this case, Sequin will assign $1
to a list of all account IDs and $2
to a list of all user IDs for the batched messages.
Results
Your query should return either 0 or 1 row for each message. If your query returns multiple rows for a message, Sequin will raise an error and the entire batch will fail.
The enrichment results are set into the message’s metadata.enrichment
field. This is sent in Sequin’s default message format.
Additionally, the metadata.enrichment
field is available in other functions in your sink pipeline including filters, transforms, and routing functions.
Limitations
When an enrichment function runs, it queries the current state of your database, not the state at the time the message was created. This means the enriched data may have changed between when the original change occurred and when the enrichment executes.
This can lead to race conditions and therefore enrichment should only be used in situations where this race condition is acceptable. For instance in materialization use cases such as search indexing, this is typically acceptable because you want the search index to have the latest data.
In use cases where the point-in-time data is critical we recommend that you enrich your messages in Postgres with either transaction annotations or event tables.
Testing enrichment
When creating or editing an enrichment function, Sequin will automatically capture up to 10 recent events from your database. You can see how your enrichment affects these events in real-time.
When changes occur in a connected database and you have the enrichment editor open, Sequin will capture events and display them in the editor.
Example use cases
Adding related data
Enrich messages with data from related tables:
Enriching multiple rows
Enrichment functions can only return a single row per message. If you want enrichment data from multiple rows, you can use Postgres aggregation functions.
Building a search index
Enrich messages with data from your database to build a search index. This example shows how to enrich messages with materials and colors for faceting:
Formatting data
For formatting data, we recommend using transforms instead of enrichment.
Enrichment functions are designed to query additional data from your database. They make a round trip to your database.
Transform functions are significantly more expressive and performant than enrichment for pure formatting.
Related
Messages reference
Review the structure of a message.
sequin.yaml transforms
Learn about the sequin.yaml file and how to use it to configure your Sequin instance.
Filters
Learn about filters and how to use them to filter messages before they are sent to your destination.
Sinks
Learn how to use sinks to send messages to your destination.