Search RU
Tasks / Kafka / Schema compatibility in a registry
Statement medium

Schema compatibility in a registry

`check_compatibility(old_schema, new_schema, mode)` checks whether a new schema
version can be deployed.

A schema is a dict with a `fields` key holding a list of fields. A field is a
dict:

```python
{"name": "order_id", "type": "string", "required": True}
```

`mode` is one of:

- `backward` — the new schema (new consumer) reads data written with the old
schema;
- `forward` — the old schema (old consumer) reads data written with the new
schema;
- `full` — both directions at once.

The function returns a `bool`: `True` is compatible, `False` is not.

Rules:

1. A field with the same name must keep the same `type`. A type change is
incompatible in all modes.
2. `required: true` → `false`: the field became optional. `backward` is `True`
(the new reader accepts the old value), `forward` and `full` are `False`
(the old reader expects the field, the new writer may omit it).
3. `required: false` → `true`: the field became required. `forward` is `True`,
`backward` and `full` are `False` (old data may lack the value).
4. Adding a field: `required: false` is safe in all modes; `required: true`
breaks `backward` and `full`, but not `forward` — the old reader simply
ignores the new field.
5. Removing a field: `required: false` is safe in all modes; `required: true`
breaks `forward` and `full`, but not `backward` — the new reader ignores the
old field.
6. Renames are not supported: if exactly one field disappeared and exactly one
appeared, that is a rename, and it is incompatible in all modes even when
both fields are optional. Aliases are out of scope.
7. Field order does not matter: fields are compared by name.

Errors — `ValueError`:

- `mode` is not one of `backward`, `forward`, `full`;
- a schema is not a dict or `fields` is not a list;
- a field is not a dict;
- `name` or `type` is an empty string or not a string;
- `required` is not a `bool`;
- field names repeat.

Write tests: backward ok and fail, forward ok and fail, full fail, a type
change, a rename, a `required` change, an empty schema, reordered fields and
argument validation.

Python
kafkaschema-registrycompatibilitybackwardforward
test_solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

Which direction does backward mode read: a new consumer reading old records, or an old consumer reading new ones?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts