# Schema Replication

To ensure that replicated data remains usable, K2K can be configured to automatically replicate schemas from the source to the target Schema Registry.

When this feature is active K2K will, for each record's schema, ensure a corresponding schema exists on the target registry and that the written record correctly references that schema.

Enabling schema replication requires providing connection details (see [Connections](/latest/k2k/configuration/connections.md)) for both the source and target registries, in addition to activating the global feature flag.

<table><thead><tr><th width="358.5">Property Path</th><th>Description</th><th>Required to Enable</th></tr></thead><tbody><tr><td><code>features.schemaMapping</code></td><td>The main feature flag that activates the automatic schema replication process. It must be set to the string <code>enabled</code>.</td><td>Yes</td></tr></tbody></table>

> **Schema Replication Behavior**
>
> Schemas identifiers are not guaranteed to be the same between source and target environments.
>
> To handle this potential mismatch, K2K will automatically mutate the replicated record such that it references the correct identifier. The written record will then correctly reference the schema identifier present in the target environment allowing for its correct de-serialization.

***

### Customizing Schema Mapping

By default, when schema mapping is enabled, K2K will assume all replicated topics to have registered schemas for both the value and schema facets.\
If this is not the case, or when fine grain control over which topics and facets should have their schemas propagated is required, rules can be added to enable or disable the behavior:\\

Rules are defined as an array of objects. Each object defines a pattern which matches against a topic name. The first rule to match a topic's name is used in order to determine if a schema should be propagated or not for each facet:

<table><thead><tr><th width="305.66668701171875">Property</th><th>Description</th><th>Required</th></tr></thead><tbody><tr><td><code>schemaMappings.topics[].pattern</code></td><td>A regex used to match against the topic's name.</td><td>yes</td></tr><tr><td><code>schemaMappings.topics[].key</code></td><td>Can be either <code>disabled</code> or <code>enabled</code>. Controls if the key facet of the record should have its schema replicated.</td><td>yes</td></tr><tr><td><code>schemaMappings.topics[].value</code></td><td>Can be either <code>disabled</code> or <code>enabled</code>. Controls if the value facet of the record should have its schema replicated.</td><td>yes</td></tr></tbody></table>

## **Example**

The following example shows the required properties set across the `features`, `source`, and `target` configuration blocks to enable automatic schema replication.

```yaml
# ---------------------------------------------------
# K2K Configuration for Automatic Schema Replication
# ---------------------------------------------------

# 1. Global feature flag to enable the schema replication process.
features:
  schemaMappings: enabled

# 2. Source configuration must include the source Schema Registry URL.
source:
  registry:
    config:
      "schema.registry.url": "http://prod.schema-registry:8081"

# 3. Target configuration must include the target Schema Registry URL.
target:
  registry:
    config:
      "schema.registry.url": "http://dr.schema-registry:8081"
# 4. Fine grain control of 
schemaMappings:
   topics:
     - pattern: "user-.*" #rule only applies to topics matching this regex
       key: enabled #enables schema mapping of key facets
       value: enabled #enables schema mapping of value facets
     - pattern: "transaction-.*" #rule only applies to topics matching this regex
       key: disabled #disables schema mapping of key facets
       value: enabled #enables schema mapping of value facets
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lenses.io/latest/k2k/configuration/schema-replication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
