Skip to content

Binary payloads and their schemas

Inside the system every payload is binary: protobuf or FlatBuffers, named by its content type (common AGENTS.md, "Protocols and encodings"). A worker that emits one registers the payload's schema with the gateway, so readers that do not link the producer's types (the TUI, the app, the EventStore) can decode it.

Registering a codec

Language Protobuf FlatBuffers
C# builder.AddProtobuf<T>(contentType, dataSchema) builder.AddFlatBuffers<T>(contentType, read, write, binarySchema, rootTable, dataSchema)
Python codecs.add_protobuf(content_type, MessageType, data_schema) codecs.add_flatbuffers(content_type, payload_type, read, write, binary_schema, root_table, data_schema)
TypeScript codecs.addProtobuf(contentType, MessageDesc, dataSchema) codecs.addFlatBuffers(contentType, typeKey, read, write, binarySchema, rootTable, dataSchema)

A record mapped to its message (devkit payloads)

A devkit's payload is its own record (a candle, an order fill) carried as a protobuf message. Every devkit uses the one registration that maps the two, never hand-written glue (worker-devkit#9, common/AGENTS.md DRY):

Language Registration
C# builder.AddProtobuf<TRecord, TMessage>(contentType, dataSchema, toWire, fromWire)
Python ProtobufPayload(content_type, MessageType, to_wire, from_wire, data_schema): read(bytes), encode(record), schema; codecs.add_payload(payload, RecordType)
TypeScript protobufPayload({ contentType, message, toWire, fromWire, dataSchema }): read, encode, schema; codecs.addPayload(payload, typeKey)

A missing required field is a decode error, never a default. fromWire reads required fields through the shared helpers (C# ProtobufFields.Amount/Time/Id, Python protobuf_fields.required/time/guid, TypeScript required); the codec reports the failure with the content type (C#: PayloadDecodeException with the event id). Exact amounts convert with lib's DecimalAmount (Exact/exact/parse, FromWire/from_wire/fromWire).

Each registration carries its PayloadSchema: the content type, the encoding, the message's full name (a FlatBuffers root table's) and the schema bytes. The codec lists them (ICodec.Schemas, Codecs.schemas).

  • Protobuf: a FileDescriptorSet of the message's file and every file it imports, dependencies first (PayloadSchema.ForProtobuf<T>, PayloadSchema.for_protobuf, protobufSchema).
  • FlatBuffers: the schema's binary form, a .bfbs (flatc --binary --schema).

Registration at start

A worker declares the schemas of the payloads it emits, and WorkerCore.StartAsync registers them through Context.Api (Gateway.RegisterPayloadSchema):

public sealed class MyTradesWorker() : MarketDataWorkerBase("tape", "venue")
{
    private static readonly ICodec Codec = CodecBuilder.Create().AddExecutedTrades().Build();

    protected override IReadOnlyList<PayloadSchema> PayloadSchemas => Codec.Schemas;
}
  • Idempotent: the gateway keys a schema by the SHA-256 of its bytes, so every start may register again; an unchanged schema changes nothing.
  • A new schema for a content type becomes the current one. The old hash stays readable, so events published before the change still decode.
  • A failed registration (the gateway unreachable) is logged as a warning and the worker starts anyway. Until a later start registers the schema, readers show the payload as bytes with its content type.
  • In-process hosts: LocalVirtufinApi keeps the schemas in memory with the same rules, so LocalWorkManager runs and tests can read them back (ListPayloadSchemasAsync, GetPayloadSchemaAsync).
  • Python and TypeScript workers register the same way: WorkerBase.Start (start) passes payload_schemas (payloadSchemas) to ctx.register_payload_schemas (ctx.registerPayloadSchemas), which the WorkManager's Python and TypeScript engines forward to the gateway. A host without the call (an older WorkManager) is skipped. LocalWorkManager keeps the schemas in payload_schemas (payloadSchemas), by content type.
class MyExecutor(WorkerBase):
    @property
    def payload_schemas(self):
        return [orders.FILLED.schema]

Reading a schema

IGatewayApi.GetPayloadSchemaAsync takes a content type (its current schema) or a hash (the current or an earlier one); ListPayloadSchemasAsync lists every content type with its encoding, message name, current hash and earlier hashes. The Python and TypeScript clients have the same calls (get_payload_schema_async, getPayloadSchema, ...).