04-23.eri-test 事件存储:保存正确的字节类型[]

\n

A colleague of mine recently started exploring Event Sourcing.
\nWhile his focus is on .NET and mine is on Java, I was more than glad to help him out.
\nI pointed him to Event Store as a great place to start.
\nA couple of hours later he shared a link to his github, proclaiming he "had done it".

\n\n

The warm feeling you get when helping a colleague discover something new was rising up inside me... until it was taken away again when he dropped the following line on me.

\n\n
\n

It\'s a shame I have to manually serialize my objects to byte[]...

\n
\n\n

I immediately opened the Event Store website only to discover that serializing your Objects into byte[] seems to be the default way of saving events when you use the C# library. While there are other ways to create a byte[], there seems to be no guidance what so ever on what you should be putting in your event store.

\n\n

So... what bytes are a right fit for an event store? To make a decision, lets take a step back to one of the fundamental truths in our sector.

\n

\n \n \n Change as a constant vs Event Sourcing\n

\n\n

Perhaps one of the most powerful lessons I learned in school was this: "Change is the only constant".
\nWriting code is an iterative process where we learn and discover better ways of expressing ourselves. We refactor, we get new insights and sometimes so does the business we are working for.

\n\n

Event Sourcing on the other hand is about writing and never changing events.
\nMost of the time, the way we save our events is referred to as an "immutable sequence of events". This means that if we save something, we have to keep dragging it along until the end of time. It can not change.

\n\n

Immutable Event Store VS The fundamental truth of change
\n\n

The Event Store and the concept of Change may seem like complete opposites.
\nWhat we can do is try to bring the two closer together by choosing an event structure which is flexible enough to allow for some change, without truly breaking the immutability of the Event Store.

\n\n

Lets look at some of the options for creating our byte[] events while keeping change in mind.

\n

\n \n \n Serializing JAVA/C# Objects\n

\n\n

My colleague chose for serializing his C# objects. There are some obvious pitfalls to this choice.

\n\n

Serialization is a very fragile thing. Simple changes like adding or renaming fields will already break the system.
\nYour events will be locked forever to a single version, requiring you to keep them forever and use upcasters to update them to the latest version.

\n\n

Depending on how much you change your events, this may lead to an explosion of versions and an overall increase in complexity.

\n\n

04-23.eri-test 事件存储:保存正确的字节类型[]_第1张图片

\n\n

That being said, there is a bigger issue here. By serializing Objects, you are binding your events to a certain programming language.
\nThis might be a conscious choice but it is not one that should be taken lightly as it will haunt you forever.

\n

\n \n \n JSON\n

\n\n

To remove the dependency on a specific programming language we could use JSON. JSON has been a big success as a communication format in polyglot environments. We are at a point where a public API is expected to return data in a JSON format!
\nBut how does it fare as a format for events in our store?

\n\n

While we can now be polyglot when reading our events, it is far from perfect.
\nFor example, if we discover a more meaningful name for a field, we still can not change it.
\nHave a look at the following JSON structure:
\n

\n\n
{
       \n  "event": "UserAddedEvent",\n  "data": {
       \n    "userid": "123456789",\n    "name": "Tom",\n    "lastname": "Cools"\n  }\n}\n
\n\n\n

Through evolving our code, we notice that we have different kinds of users in our system, which each mean something different.
\nYou want to be able to make the distinction between "Customers", "Employees" and maybe an "Administrator" type of user.
\nAs adding employees is new for the system, the currently saved users are all "Customers".
\nWhat you actually want to do, is change the event to the following:
\n

\n
{
       \n  "event": "CustomerAddedEvent",   <- Change event name\n  "data": {
       \n    "customerid": "123456789",  <- change field name\n    "name": "Tom",\n    "lastname": "Cools"\n  }\n}\n
\n\n\n

Using JSON however, this will not work.

\n\n
  • \n
  • You can\'t change the events in the event store, so now you have two kinds of events.\n
  • Without making two readers for the different types, the reading of the event will fail, either hard with an exception, or soft because the resulting field is empty or null.\n
\n\n

You will be forced to add a new "type" field to the data and your reader must be smart enough to set the type = Customer as a default.
\nThis might not be a big deal for now, but remember, every version of an event must be handled. This means, adding more processors or adding more upcasters to handle the differences.

\n\n

Another downside is that JSON is, at its core, a String. This means you will need to escape special characters.
\nThose of you who have lost some hours with escape sequences should have felt a shiver down your spines reading the last paragraphs...

\n

\n \n \n Data Serialization Libraries\n

\n\n

Data Serialization Libraries are often mentioned in the context of Inter Process Communication. Think about the things you put on Kafka, RabbitMQ or some other message broker. It is commonplace to use a library to transform messages into byte[] to put them on the wire. So why not use those libraries to serialize and save our events?

\n\n

There are some out there, like Avro and Thrift, but my personal favorite is the Google Protocol Buffers Library.
\nUsing Protocol Buffers you declare a schema which is used to generate (de)serialization code for a specific programming language.

\n\n

The serialized result is the same each time, so you could serialize data using the Java library and deserialize that data again with the .NET Library.
\n

\n
message CustomerAddedEvent {
        \n  required string customerId = 1;\n  required string name = 2;\n  optional string lastname = 3;\n}\n
\n\n\n

Important to note is that a Protocol Buffer schema is index based. Because this is the case, there is no impact when changing a field name.
\nAs long as the field index and type never change, you can change the field name as much as you want.

\n\n

Adding and removing of fields can be done without problem as well. The serialization will keep working.
\nWhen you add a new field that didn\'t exist before, you do need to program a sensible default yourself.

\n\n

I have created an example project on github where I use the Maven plugin to generate the Java classes out of the Protobuf Schemas.
\n

\n
\n
\n

\n GitHub logo\n \n TomCools\n / \n protocol-buffers-example\n \n

\n

\n Example repo for protocol buffers\n

\n
\n
\n\n



\nBe sure to have a look. For other programming languages, check out the official documentation.

\n\n

\n \n \n So what do you choose?\n

\n\n

Choosing in which structure you save your events is probably the most significant decision you have to make while starting Event Sourcing.
\nIf you worry about an ever changing world, you may want a mix between the principles of an Immutable store, while still having a little bit of flexibility.

\n\n

Here is a table listing the options I described above and which changes are possible when using each one without adding an extra mapping layer.

\n\n
Example Serialized Object JSON Protocol Buffers
Polyglot \xe2\x9c\x98 \xe2\x9c\x94 \xe2\x9c\x94
Remove field from event \xe2\x9c\x98 \xe2\x9c\x94 \xe2\x9c\x94
Add field to event \xe2\x9c\x98 \xe2\x9c\x94 \xe2\x9c\x94
Change field name \xe2\x9c\x98 \xe2\x9c\x98 \xe2\x9c\x94
Human Readable \xe2\x9c\x98 \xe2\x9c\x94 \xe2\x9c\x98
\n\n

I have a tendency to forgo the human readability in favor of the flexibility and speed of Protocol Buffers.
\nIf you are interested in performance metrics, the guys at auth0 have written a great blog post on this.

\n\n

For now, I will be sticking with Protocol Buffers as the format for my events. Whatever you choose to use, be sure you know the consequences.
\nChoosing an event format is a day 1 decision, so whatever you do, keep in mind:

\n\n
\n

A bad choice now will haunt you for a long, long time.

\n
\n\n\n
\n\n

Don\'t agree or have another idea, please leave your remarks in the comments!

\n\n\n

你可能感兴趣的:(json,c#,开发工具)