README
¶
MessagePack Serializer Example
Store events as compact MessagePack binaries instead of JSON.
By default go-mink serializes events to JSON, which is human-readable but larger and slower to encode. Swapping in the MessagePack serializer yields smaller payloads and faster serialization — valuable for high-throughput or storage-sensitive systems. This example compares the two formats side by side, then wires MessagePack into a real EventStore.
Compatibility: MessagePack is a binary format. It works with the in-memory adapter (used here) or any
BYTEA-backed store, but not the PostgreSQL event store, whoseevents.datacolumn isJSONB. Pairing this serializer with the PostgreSQL adapter makes the firstAppend/SaveAggregatefail fast withmink.ErrBinarySerializerUnsupported(detected at construction, before any write) — use the default JSON serializer there.
What this demonstrates
- Pluggable serialization —
msgpack.NewSerializer()produces a serializer that drops into anEventStoreviamink.WithSerializer(...), no other code changes. - Payload size savings — the same
ProductCreatedand a larger array/map-heavyLargeEventare serialized with both JSON and MessagePack, printing byte counts and the percentage saved. - Typed deserialization — event types are registered with
msgpackSerializer.Register(...), soDeserializereturns the concreteProductCreatedstruct rather than a generic map. - Speed benchmark — 10,000 serialization iterations per format, reporting duration, ops/sec, and the speedup factor.
- Event store integration — three events are appended and reloaded through a store backed by the MessagePack serializer.
Running
go run ./examples/msgpack
No infrastructure required — uses the in-memory adapter.
What happens
- Set up serializers — a JSON serializer (
mink.NewJSONSerializer()) and a MessagePack serializer are created; the four event types (ProductCreated,ProductPriceChanged,ProductInventoryUpdated,LargeEvent) are registered with the MessagePack serializer. - Compare a typical event — a
ProductCreatedis serialized both ways; JSON and MessagePack byte sizes and the savings percentage are printed. - Compare a large event — a
LargeEventwith a 1000-byte blob, a map, a 100-element int slice, and nested data repeats the comparison. - Deserialization test — the JSON bytes deserialize to a
map[string]interface{}(type not registered) while the MessagePack bytes deserialize to a concreteProductCreated; both print the recovered ProductID. - Speed benchmark — each format serializes the event 10,000 times; durations, ops/sec, and the MessagePack speedup are reported.
- Store and load — a store built with
mink.WithSerializer(msgpackSerializer)appends three events withmink.ExpectVersion(mink.NoStream), then loads them back and prints each event's type and version. - Guidance — the program closes with notes on when to prefer MessagePack versus JSON.
Key APIs
msgpack.NewSerializer()— creates a MessagePack-backedSerializer.msgpackSerializer.Register("ProductCreated", ProductCreated{})— registers an event type for typed deserialization.serializer.Serialize(event)/serializer.Deserialize(data, "ProductCreated")— encode and decode event payloads.mink.NewJSONSerializer()— the default JSON serializer, used here as the comparison baseline.mink.WithSerializer(msgpackSerializer)— option that sets the store's serializer.mink.ExpectVersion(mink.NoStream)— optimistic-concurrency option asserting the stream does not yet exist.store.Append(...)/store.Load(...)— persist and read events from the store.
Related
- Examples: protobuf · full-ecommerce · basic
- Docs: Event Store · API reference