Lenguaje Publicado

Escrito por Darjus Kazakevic · revisado el

Put a documented data format on the wire, not your internal model.

The quickest way to publish an event is to serialise the object you already have. It works at once, and it makes every field of your internal model public, including the badly named ones and the one you plan to delete next month.

A published language is a separate, documented format that both sides translate into and out of. In an online shop, Sales publishes order.placed.v1 as a JSON Schema; Shipping and Billing read that, never Sales's Order class. Industry formats like iCalendar or EDIFACT are published languages too.

En esencia — Translate your internal model into a shared, versioned schema before data leaves the context, so consumers depend on a format designed to stay stable.

Cuándo recurrir a él

Recurre a él cuando

  • Events or payloads leave your context for teams you do not control
  • Your internal model still changes, but others need a stable format
  • Several consumers, in different languages, read the same data

Reconsidéralo cuando

  • Data that never leaves the context that owns it
  • A prototype whose one consumer you change in the same commit
  • Calls inside one module, where a shared type is enough

Conceptos clave

The schema
A written definition of the messages: fields, types, meanings and an example. JSON Schema, Avro, Protobuf and AsyncAPI are common ways to write it.
One translation point
A single function maps the internal model to the schema. Internal fields stay private, so the internal model can change freely.
Versions
The version is part of the name, like order.placed.v1. A breaking change ships as v2 next to v1, so no consumer breaks without warning.

Reglas de diseño

Translate at one point; never serialise the model
Map the internal model to the versioned schema in one function. Fields you named badly or plan to delete stay private, and that function is where to look when a consumer asks what a field means.
Put the version in the schema's name
Carry it in the payload and the type, as in order.placed.v1, so a breaking change ships as v2 beside v1 instead of surprising every consumer at once.
Document meaning, not only types
A field named total says nothing about tax or currency. For each field, state the unit, the time zone and what an empty value means, and add one example message.

Errores habituales

  • Serialising the internal class “for now”, until it becomes the contract
  • Changing what a field means without a new version
  • Schemas without units or time zones, so consumers guess
  • One huge schema for every consumer instead of focused messages