Events schema versioning, or in other words, events evolution, is one of the first questions I get when explaining Event Sourcing. The topic may look scary, as how to evolve data that, by definition, is immutable. I won’t lie; migrations are never easy., even in relational databases. You always have to think about the following: * What caused the change? * What are the possible solutions? * Is the change breaking? * What to do with old data? When working in event schema evolution, you must ask yourself the same questions. Based on the answers, you need to choose your strategy. During the webinar, I showed simple techniques that should take you far enough: * simple mapping scenarios, where serialiser features are good enough, * upcasting and downcasting, so how to transform our events on the fly to the latest schema or to the schema that’s compatible with previous versions, * explicit (de)serialisation to handle the payload transformation without additional magic, * more advanced scenarios. All of that was put into a broader context together with considerations on how to apply that: * inside module, * cross-module communication, * business logic and projections, * differences between environments like C#, Java, TypeScript, * using Marten and EventStoreDB, but also messaging tooling. In the end, in Grand Finale, we also discussed the trick of not needing to do versioning at all! You can also read my articles: * Simple patterns for events schema versioning * How to (not) do the events versioning? * Explicit events serialisation in Event Sourcing * Fun with serial JSON * Mapping event type by convention * Event Versioning with Marten * Let's take care of ourselves! Thoughts on compatibility * Internal and external events, or how to design event-driven API Source codes are available here: * C#, * Java, * Typescript. Or just watch the webinar, as it’s all there and more! For the advanced scenarios, read an excellent book by Greg Young, “Versioning in an Event Sourced System” (it’s available for free). Become a paid subscriber and access the Miro board from the webinar! Webinars: * #1 - From CRUD to Event Sourcing * #2 - Keep your streams short! Or how to model Event-Sourced systems efficiently * #3 - Implementing Distributed Processes * #4 - From CRUD to CQRS in practice * #5 - Architecture Weekly 100 Edition - Live Q&A * #6 - Alexey Zimarev - You don't need an Event Sourcing framework. Or do you? * #7 - Design and test Event-Driven projections and read models * #8 - Slim down your aggregates! * #9 - Radek Maziarka - Modularization with Event Storming Process Level * #10 - PostgreSQL Superpowers in Practice * #11 - Maciej "MJ" Jędrzejewski - Evolutionary Architecture: The What. The Why. The How. * #12 - Jeremy D. Miller: Simplify your architecture with Wolverine * #13 - Yves Goeleven - The Fantastic 9 * #14 - Mateusz Jendza: Why Verified Credentials is the Future of Digital Identity! * #15 - Mário Bittencourt: Leveraging BPMN for Seamless Team Collaboration in Software Development * Papers We Love #1 - Sagas (Hector Garcia-Molina, Kenneth Salem) * #16 - Simple patterns for events schema versioning This is a free preview of a paid episode. To hear more, visit www.architecture-weekly.com
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Okay. uh hey all welcome to our next webinar so as i mentioned before starting recording i would like to make it as much interactive as possible so when i will be explaining you the concepts then feel free to interrupt me you can ask questions or put your comments via voice or you can place them on chat window don't be shy if something will be
SPEAKER_00planned to show you later on then I will be try to be assertive and and and tell you that I will be discussing that a bit later so I have a specific plan on how to proceed but still let's make it fun So before I go to the code, because we will see a lot of code, let me start with a brief introduction to the
SPEAKER_00topics and to the issues that we might be facing while working on the event sourcing but also event driven system. The focus will be on event sourcing but definitely the similar patterns can be used in general for EDA. so let's say that we have this boring domain but i think that this domain is actually pretty good on discussing those topics so shopping carts
SPEAKER_00and let us focus on the small part of the process that we might have in the in the shopping cart management so the the final confirmation so once we added some product to our shopping cart Then before we make the final ordering process then we need to confirm the shopping cart and then the other parts of the process needs to start.
SPEAKER_00So typically we are sending some command which is like intention of what we would like to be doing. and then we we need to run the business logic so we need to check some invariants like for example is shopping cart open do we have any products and we are making those decisions based on state and here there is no difference from the classical state-based approach to the event sourcing if we are using event sourcing then we are getting one more component that is a bit different than regular not so different as event stores are also databases but the critical part that we are doing here before we even run the business logic is that we need to read the events from our event store
SPEAKER_00and this is the really significant difference between the streaming and event sourcing because we are not subscribing to events we are reading them but so actually we are reading a stream of events and stream of events contain all the events and all the facts that were registered for this shopping cart so it will probably have events like shopping cart open product item added to shopping cart maybe again the same event and maybe removed and based on those events we are building this specific state so the state that we will be making our decision
SPEAKER_00and in the end once we run the business logic and let's say that we had the happy path so our business logic succeeded then as a result we need to store those events and so far so good and the next phase that can happen after we store those events is of course that we can do something with those events.
SPEAKER_00So what we can do with those events is that for example we can run the subscribe to the notifications and based on those notifications run so-called projection logic so transform those events into some read models. and all of that we are still speaking about staying inside the specific module so those read models logically keeping the logical boundaries are still inside of our module scope
SPEAKER_00so I'm not talking about publishing yet to some messaging system just we are storing that into our event stores event stores are giving us possibility to subscribe to the notifications and based on those notifications we can build read models and then our user can read them and it can query them to get the data.
SPEAKER_00So this is what our user is doing. Okay, and the other scenario that we might have with our events is that we can subscribe to those events and do one more step. So use those events to integrate and to forward them to some messaging system. It can be Kafka, it can be SQS, it can be Google PubSub, what RabbitMQ whatever we prefer but we can subscribe to those events and based on those events we can trigger some other business logic in some other module so for example in shipments so we can push those messages to our to some messaging queue and then subscribe run some business logic
SPEAKER_00or we can also run a similar thing as we have in our module so build the local representation of our process inside this other module so yeah so we can do it like that and All of those scenarios require a bit more thinking and a bit more scenarios because there are some patterns when our model is changing that we can handle inside of our module and usually handling model changes inside the modules are easier than doing them in terms of integrating that with other modules.
SPEAKER_00So we will start with the simplest stuff. So this, actually this specific part. So we will be starting with reading events, appending events, and then we will discuss how to move forward and how to apply patterns to some other specific parts of our systems and other integrations. so the starting point will be this one so running the business logic so how to how to handle if our model changes and we need to read events and we might have multiple version of the specific events and then how to handle event appends and then we will see maybe those patterns can be also reused in other places
SPEAKER_00Any questions so far or to this diagram? I take the silence as a no but feel free to interrupt me. Thanks Zach for the feedback. So let's have a look on how we can handle this versioning. So let's say that we have
SPEAKER_00some current existing schema so our schema is really and events definition is really basic so we have the shopping cart open product item added removed confirmed and so on and so forth so i'm sure that you are buying even too much stuff on the internet so you know this process but let's say that we have the following event the first event in the shopping cart journey so a shopping cart opened contains information about the shopping cart id and the client because we need to know precisely for which client so customer we are opening our shopping cart and let's have a look how we can start with the really simple scenarios
SPEAKER_00so the most obvious scenario is the font size okay or should I make it a bit bigger just double checking so So thanks Martin, thanks Bartek. Okay, luckily we are not on the conference because then that would be too small but sorry for the side note. So the most obvious example of the scenario that we and the easiest scenario to handle is that when we are adding a new not required property
SPEAKER_00and the new not required property is for example that let's say that in the initial event we just had those two properties and then we realized that gosh we are so dumb we forgot to put the date down with the information when we actually opened the shopping cart and those information can be any type of additional data and in general the best practice if we are handling the schema migration and schema evolution is to include our changes in a non-breaking manner so if we don't know
SPEAKER_00how which which data we can use as a default then we should always try to be explicit and say that this information could have been not provided and if we are using the default scenarios for handling JSON schema um then most of the serializers are in that case will assign a null value in case of c-sharp or java or undefined value for javascript of course we can turn on the strict mode and fail if we have some additional property or that we didn't know but if we provide this field as non as a nullable
SPEAKER_00then it means that it will be both backward and forward compatible and we will discuss later what does that actually mean that it's forward compatible so and also my recommendation is never for events schema definition assume that we have a strict and a strong schema I think that weak schema is actually the wrong name because weak schema is really powerful because it allows to easier evolve if we are doing the
SPEAKER_00the changes and handling our engineering hygiene because I think that not doing breaking changes is part of the proper engineering culture and and in general engineering hygiene. so here this is an example that we had the original event so the one that didn't have this initialized at and we are serializing this event and trying to deserialize it into the new definition i'm using here the system text json which is the dotnet serializer
SPEAKER_00and then as we can see we are expecting that this should be now I could even try to run it we'll see just to prove you but I'm not sure we are doing live webinar so that might not be the smartest move but we'll see and in the other way around we would like those our events with the new schema so the one with the opened ad We would like to make this
SPEAKER_00also backward compatible so if we are reading events and we are expecting to have the old schema then it also shouldn't fail and in this case as you can see this shopping cart doesn't even have this property so if we are using the the weak schema so if we are not using the strict mode then we are fine to serialize it in both ways so that's that's the easiest example and the backward compatible
SPEAKER_00Backward compatibility is especially important here if we are reading events because I mean the forward compatibility because we can get events with the old schema but we would like to use in our code events with the new schema because maybe we don't want to maintain the multiple version of the same stuff and maintaining the forward compatibility is especially important for those integrations because if we are publishing event with the new schema
SPEAKER_00and our shipment modules doesn't know that yet that this new schema even exists then it's really important to to publish our event with the definition that can be backward compatible because let's say that we are publishing here the version 2 but shipments module didn't yet update their code base to have even the knowledge about the new version
SPEAKER_00then if we publish that event with the version 2 that is still compatible with version 1 then this module doesn't have the implicit coupling because if we were publishing schema definition v2 that is not backward compatible with the v1 then we would need to deploy both shopping carts and shipments at the same time which can be of course possible if we are doing modular monolith but if we having the micro services work then typically that's not the smartest idea but and even if we are doing monolith
SPEAKER_00then typically we would like to use some load balancing or we still might have some multiple instances of our system because we would like to make it resilient so if one copy fails then the other wakes up then there might be this blind spot where we have both versions so that's why it's extremely important to not do breaking changes.
SPEAKER_00So if you want to take at least one lesson from this webinar, not making breaking changes, because then life becomes much, much easier. Okay and but let's have a look on how we could proceed with potentially breaking changes.
SPEAKER_00So the simplest example of that is that we have the new required property and the new required property might mean that let's say that Initially, when we were opening the shopping cart, then our status was always opened. Because like Captain Obvious, if the event is named shopping cart open, then typically the status of our shopping cart is open. But let's say that we found out that there are some users that are trying to cheat our process.
SPEAKER_00And there are some people that either are spamming our system or trying to find some leaks and doing some breaches. And we would like to for some specific types of behavior. Sure, I can do it. Is it better Paweł? Yeah so for some specific type of behavior we would like to run some fraud detection mechanism and then for that part
SPEAKER_00we don't want to put this shopping cart open in the open state but maybe we would like to put it into some pending or maybe explicitly say that this is just for under fraud detection so in that case we can assume that for those payloads that didn't have this status defined so if we got the JSON without the status property then in this case because we know why we made that change then we can assign a default value
SPEAKER_00for the for this particular case so if it doesn't have then it means that it was stored under the old business logic and that means that all those statuses will be shopping cart open and in C sharp and Java I will show you later on that in Java it really looks almost the same the serializers are smart enough to to do it to assign this default property in TypeScript it's not that easy but we will get to that later but still you can and you need to if if you want to make
SPEAKER_00this new required property really required and not nullable then you really need to provide a new specific value and if we have a look and if we think about the migrations that we are doing in the traditional systems so for example relational one then if we are Then if we want to add new column to the database, then the safest option is to do as we did here.
SPEAKER_00So make this new column nullable, right? But if we want to make this newly created column not nullable, then we need to provide the default value. And we can do it here. so for example under the serialization and then we are adding something that is required but with the default value and now the tricky part is that we are actually not updating events because our payload will still look like it will look something like that
SPEAKER_00let's say that this is unique ID so our payload will still look like that and some may say that well but we didn't do any migration and we will always need to do this on the flight And that's correct. That's some additional mechanism and we will see some more advanced transformation.
SPEAKER_00But if we think about what are events, then events are facts. They already happened. And there is a big value in knowing precisely how we are, how we recorded those events. because let's say that we try to do migration and now adding this status and set it to open right for those old values and we run the migration but then we broke the migration
SPEAKER_00or we accidentally put it too close because we wrote the wrong migration script or actually we broken the initial data and now from the auditability perspective we are not sure what were the precisely recorded events so having those events as they were is really powerful because then we can interpret them in multiple ways and doing this default value is some sort of basic interpretation that happens automatically during the deserialization so really I think that it
SPEAKER_00It might look at the beginning counterintuitive that we need to always do it but on the other hand it's really really close to any other type of migration that we would be doing. And in this case, if we are doing them, those are the similar tests. So we are serializing with the old schema, deserializing and we see that it should be open. And here it's also backward compatible because we are just adding new property.
SPEAKER_00And if it's new property, then typically those deserialization will just ignore this property and not assign it. and what might be surprising for some of you but introducing new property is also the rename to the property is also the breaking change we as developers really love to do some renames and refactorings so for example previously we had the shopping cart id but now we've said that okay i have this shopping prefix then i don't want to have this previously i had it like that so i don't want to have this prefix because it's redundant then i will just remove it
SPEAKER_00and that is already a breaking change such change is already making you unable to deserialize the old value probably if you would do it like that then if you try to again deserialize it then it would assign the default value of the unique identifier so it would silently cause some issues so but most of the serializers can also handle that so if we want to rename our property and we would like to keep it like in code in a different way but in the serialization in a different way
SPEAKER_00then we can use for example some attributes or annotations on the on our properties and then say that if this property is serialized then it will be serialized to shopping cart id and if we are deserializing then try to get it from this shopping cart id and Again, let me get this JSON.
SPEAKER_00So the tricky part is that if we serialize it in this way, so we are saying that you need to use the shopping cart ID, then it will not serialize the new events with using the cart ID, but using the old one so if we had typo because for example we had instead of shopping cart id then we had sopping cart id then unfortunately that means that we still need to keep this sopping cart id because then it's a it's a potentially the wrong way
SPEAKER_00so the alternative to that is to add additional property so have store it like that so store it like cart id and also have the old version of that so once we we have those two properties this is the rename one this one we might name as obsolete and then we would be keeping those values together so we could use it like that some serializer allow us to do it like this way but i'm not sure if system text json is smart enough so it will be like that so
SPEAKER_00so either use it from the cart ID or use from the shopping cart ID so then we will be storing in the new format and in the old format and I will tell you later on how you can get rid of those potentially old formats that you don't want to keep forever but still the the simplest option and I think that potentially the best one is to keep this old naming as it was and we can live with this old schema potentially so just to prove you that in java it looks almost the same let me
SPEAKER_00I never remember how to zoom Paweł in IntelliJ, so sorry for that. It will be a bit smaller. So yeah, so this is the new not required property. So we are again adding it. In Java, everything can be nullable, what is class. So this means that this is nullable in Java.
SPEAKER_00and let me show you more so new required property we need to do it we can do it in a bit different way because we don't have those default values in java but we can have the constructor for the record and we can say that if i got null then assign the default value so it's almost the same and for renamed using jackson serializer it's uh it's the same so we are using json property in this case and for typescript it's almost the same so new not required we can define as nullable
SPEAKER_00so that means that it can be undefined the new required property is not so trivial because if we have type script then in type definition we cannot provide the default value but we can do it like that so we can stringify the new schema and do some additional mapping so JSON parse give us possibility to provide the additional mapping and we can do while parsing that we can do some additional mapping so if the status was null
SPEAKER_00then if it was undefined then use the shopping cart open so we still can provide some specific set of mappings and for rename property we would need to do it also a bit manually so for parse we would need to use the map as we had and we are storing both old schema so shopping cart id and the cart id to keep it backward and forward compatible and the other way around we would also need to store it manually so so at least that part in TypeScript is requires much more manual intervention
SPEAKER_00but I'm showing that to you to show that not all environments are super smart and maybe that's cool because too much magic is also not helping. So do you have any questions around this simple transformation and simple mappings? because really using non-breaking changes policy and those simple mappings can be good enough for really a lot of scenarios.
SPEAKER_00I would say majority of simple scenarios and then we will go to the a bit more advanced version when we want to do a bit more magic. okay i don't see questions so i hope that this means that it's quite clear so let's have a look on the different strategy so let's say that we had our shopping cart open and previously it had only client id
SPEAKER_00so just to remind you the the the old event had just the client id and for some reason we decided that we want to add additional information so the client name I don't want to discuss whether this is a good or bad practice. In general, of course, we should try to avoid putting too much into events, but sometimes it's fine to capture also additional data, potentially redundant.
SPEAKER_00But, for example, it can be useful for the projections. It might make projection handling easier because you won't need to read additional data. Let's say that this is a trade-off that we decided to do. and now we are saying that because we are really we want to be object oriented then we decided that instead of just keeping it as like that like client ID and having additional property with the client name
SPEAKER_00then instead of having it like that we decided that well if it's client then we would like to group it and that type of change is already a breaking change because we cannot serialize it we cannot deserialize event in old schema if we serialize it as it is then it will be again not working correctly because it won't be neither backward nor forward compatible
SPEAKER_00so how to deal with that and the potential example of how to deal with that with the transformation when those simple techniques that I showed you before won't work is to use so-called upcasting so we are taking the old event and we are transforming it into the new schema so this event could come from the initial deserialization if we want to keep in our code base also the old event type then that's okayish solution the only downside potentially is that we have more allocation because first we need to deserialize it to the old event and then transform it into the new one
SPEAKER_00The benefit of that is that we have this strong typing and that we can do those transformation using our favorite language, so shopping, so like that. But if we want to be super performant, then we can take the also write the upcasting logic in a way that we are taking the old event json just the json we got or byte array because those techniques that i'm showing i'm showing using json but also other serializers like protobuf and so on and so forth give you a similar capabilities
SPEAKER_00so then most of the advanced serializers give you the option to do the manual and row parsing and benefit of using stuff like this json document parse or if we are using java then then in java it would be like that so the the serializer json node then those types give you possibility that here we are not yet parsing information we are only parsing when we are calling for example get property
SPEAKER_00And I'm calling that caveman deserialization. Personally, if I'm using C sharp or Java, then I would prefer to benefit from using this environment. But definitely, this can be more performant, reduce allocation, and it can give us more better flexibility, because we can do whatever we want with this JSON. So even if we have some weird or potentially broken JSON, then this is the possibility to somehow fix it or do any type of manipulation that we want.
SPEAKER_00So for example, it might be in the wrong format. It might be located in many different properties. Sometimes that happens and doing this scenario also helps. and that so as i said we can just take the old event which may come from the serialization or we might have the row json and call the upcast logic and transform on the flight the our processing
SPEAKER_00So if we go to our MyRobot then this transformation will happen somewhere here. So if we are reading events then this will be our upcasting. so it's it will be still part of the infrastructure we might inject this function i will show you a few ways how to do it but this happens in between so we are reading reading events in the old schema from our database so as they are
SPEAKER_00and then event by event if we need to do it then we are calling the downcasting the upcasting and then using in our business like the the building this the current state using just the latest schema potentially and this also gives us possibility like for example to reuse some of the metadata from our event so let's say that we forgot to put information like who initialized the shopping cart or maybe this approver if we had the fraud detection or it might be some time stamp like this missing initialized ad data
SPEAKER_00and if we have this information in our metadata because event stores are not only keeping our data but also stream metadata so timestamp usually so when it was added some position we can put some custom metadata like this user id so if we had that then we can potentially use that also in the upcasting logic so we not only have to pass just the road just the event data but we can use also the potentially the event metadata and then we can use those of course if we have those information in our upcasting processing
SPEAKER_00and we can also both have the metadata which is that the class which is strongly typed or we can just have the row json or row bytes and parse it and use it in our logic so any questions to that part should i explain something better or is it clear
SPEAKER_00okay martin thanks for feedback glad that you're good if really if you have any questions feel free to ask them via voice or on chat um so right now what we discuss is this upcasting we and those simple techniques that we used what the serializer is giving us is also some sort of upcasting but upcasting that is happening automatically thanks to our serializer capabilities
SPEAKER_00and the other way is as I mentioned because here we are taking events in the old schema so let's say that this is the v1 let's do it like that so we are taking event in v1 and transforming it into potentially like v2 or v3 right so this is our transformation so this is what is upcasting but as I mentioned we not only want to be forward compatible so we are getting old schema and be able to deserialize it to the new one but we also want to make to make it backward compatible
SPEAKER_00even if we are of course if we are thinking about the business logic then if we start even in the old schema in the new schema then we of course will be able to get it because this upcasting will handle that but not always we are a
SPEAKER_00sometimes we are splitting and write model part is deployed as one version as one one service and read models are also published as a separate one because and that's quite common because this part can be run asynchronous like grid model processing and it might require different scaling capabilities than this one so it would be good if we were publishing and appending events that also our internal module could keep it up so if they got the events in the new schema then they could be also using with the old one
SPEAKER_00And for that part, we will be using downcasting. And downcasting is the other way around of serializing our data. So for example, if we are storing events in the new schema, Then potentially we could downcast it into the the old schema or maybe if we are Downcasting then maybe we would downcast that to the type that would had both old and the new schema So downcasting is happening here so We are taking event and
SPEAKER_00that is in the new version and best if we are downcasting it to the event that is that that is both forward and backward compatible so it's like a union of those two versions because if we are storing that as an event that is compatible both with the old and new schema then all also our services and external services will be able to catch it up
SPEAKER_00and I will show you a different technique than those one like that is quite common in other environments than Java or C sharp because Java and C sharp serializers are as you saw enough powerful to plug in and they can do a lot of magic actually they are not only serializer but also some sort of mappers but the other quite common case is that using explicit serialization and deserialization class that is called usually SerDe.
SPEAKER_00Serialization, deserialization. So let's say that we have our shopping cart event and this shopping cart event is the event with it's a union type so we are saying that this will our shopping cart event will be either of those types If we are using C Sharp or Java, we could define it in a similar way. So write our classes as records and implement the same interface.
SPEAKER_00So it's just grouping all of the event types that we can register for our stream. So all the facts that can be registered. And here we would like to maintain just the latest schema. because then if we are doing this evil function so building the state from our events then we don't want to have here in this evil function all versions of our processing like shopping cart v1 v2 v3 v4 v5 and and the same for other events this would be just a lot of noise
SPEAKER_00and the potential option for that is to, in JavaScript at least, we can define a type that contains information about all potential payloads we had in the past. And in the C Sharp or Java, we could define it in a similar way. So have a separate grouping that contains all the schema that we had.
SPEAKER_00So in this case, we had three types of events definition. The first one that just had shopping cart ID and client ID. The other one that had client grouping. and see here i'm keeping it both backward compatible and forward compatible because i'm not only publishing the the new version the the new grouped client but also the old way so this is like the the obsolete as you see here so i'm keeping it as an obsolete but for the compatibility
SPEAKER_00and I'm giving also this new required property and let's say that we only had those schema migration only for the shopping cart open and for the so how to proceed with that if we have such a class which is the shopping cart event Serde then we could have the two methods one is the serialization and the other is deserialization so serialization takes the the shopping cart event so the one that we will be appending in the new schema so the in other words also like a domain event
SPEAKER_00and it's returning the type that is representing all potential permutations of payload and versions that we had in past and having such methods it might seem that it's a bit redundant and there is a lot of manual stuff needed but see like here we are having a clear type definition and we especially in the deserialization it's it's really great because let's say that we introduce the new type
SPEAKER_00the or forgot to handle the initial shopping cart opened and here we will get the compiler warning a compiler will say like hey you forgot to implement the initial one and only if I handle all the versions then I will have the type checks uh corrected and compiler will tell me yes now it's okay or for example if I forgot to assign the client name for the event where we didn't have this name then it will also tell me that this is the wrong value
SPEAKER_00And here, I'm also seeing in this deserialization method, I'm clearly seeing all the schema evolution in one place. And I also have the clear mapping and explicit mapping of what I'm doing. And in JavaScript, that looks quite nice in C sharp. it would look a bit different let me find it
SPEAKER_00so in c-sharp typically in other places what i saw is that you are typically people are doing that they are using those so-called caveman manipulation but also more performant so see i'm shaping here if i'm serializing then i'm building the json object manually so I'm clearly can do whatever I want and shape the JSON structure as I want or maybe in the serialization I could use just the regular serialization if I don't want or have the fallback and here
SPEAKER_00In the deserialization I can do the same so if I'm getting the specific event type then I can deserialize it in a form that is the most suitable for me. So in that case we are getting with the serialization and deserialization both upcasting and downcasting and it's quite easy or easier to handle that.
SPEAKER_00However, as I said, you don't need to do those types of manipulation if you are adding new like if you are adding new property or maybe if you are changing schema, so for example If we have something like here, then maybe we could just keep the previous information as it was And mark it as obsolete
SPEAKER_00and then if we do it like that then we have event even if we introduce this client id then we still have something that is both backward and forward compatible so that's my uh really that that's why i'm saying that not introducing breaking changes and taking care about that stuff really makes stuff much much more easier because then maybe we don't even need to use or write those custom upcasters or downcasters because our schema by definition is both backward and forward compatible.
SPEAKER_00Okay so getting back to our TypeScript example in Java it's almost the same like in C Sharp so I won't be showing that but if we have if we have this our real event store then let's have a look on how we could how we could use our third class in that type of real world usage so let's say that we have method aggregate stream an aggregate stream is a way that we are building the state from events
SPEAKER_00so the first thing that we are doing is that we are creating the default object it might be created by default constructor or some function that we provide whatever we do it's we are creating the default state and then if we are reading events then for each of those events we need we can provide this parse method and either if we provide this parse method then we are doing that so this is like it can be upcaster it can be
SPEAKER_00It can be our third class and in that case if we provided that method then we are trying to parse it. and in other words we are just using what we got and using the default deserialization mechanism and then once we had this parsed event which is already upcasted then we can use it to evolve and we are passing to the evolve function so the function that is building our state evolving our state based on events already upcasted event
SPEAKER_00and then once we went through all the events in the stream we are getting the new state so this is the really the general approach that you can use and in uh aggregating stream so see we are saying that if if i have method get shopping cart then i'm get i'm i'm trying to read events from this stream this is the you know i'm passing a default object as this shopping cart i'm passing the evil function that you saw before and i'm passing our sparse i'm passing this deserialized method from from the
SPEAKER_00so in this case I'm just you know passing and using it while aggregating stream and in the same way while appending events to stream I could use the downcasting. So I can also provide the serialize method. And if I provided this serialize method, then I'm using it and returning the data.
SPEAKER_00And if I did not, then I'm just using the default serialization mechanism. So this is quite flexible. And this is quite powerful mechanism. and this is how in in nutshell you can do it in your event store but if you are not using event sourcing but you are for example just using Outbox or passing messages through messaging system like Rabbit or Kafka then you also while receiving events for example from Kafka you also need to map them and potentially you might get those messages from other systems
SPEAKER_00in the multiple schemas. So if we are doing in the messaging system, then here we could also put precisely the same upcasting logic. and it can be either upcasting or downcasting depending on if we don't know yet the new schema then probably we would have some sort of downcasting so we can do the same transformation on the receiving end so this is like potentially the ACL
SPEAKER_00or we might use the same technique and use upcasting on this side so if we are getting events from event store in the new schema in the in the sorry if we are getting in the events in the new schema from event store then we could also do the downcasting and publish events with the schema that is compatible for both old one and the new one so in that case we also have option to provide ACL on both sides so it's typically I would recommend start doing with this one
SPEAKER_00so doing here because then if we are ensuring that we are publishing messages in the format that is other subscriber can use then then we are ensuring that we won't break and we won't force others to immediately deploy the new version because this downcasting needs to happen of course on the receiving side so conceptually it's here so we need to deploy new events to be able to do downcasting or upcasting
SPEAKER_00okay but if we don't want to do this caveman manipulations then there is also other option to do to make it more generic and let's say that we have a set of upcasting logic because we have this initial version then we have the version with the client so the grouped client and we also have the third version with the status so we have three different version of the shopping cart open and we have a three different upcasting logic one is upcasting version one and this is doing the manipulation just for example with the JSON row JSON then we have this additional upcasting for the version with the client but without the status
SPEAKER_00and we could define a serializer that is capable of having multiple upcasting registered so if we are trying to deserialize then we don't need to provide a specific method how to handle parsing but have it done automatically So how to do it? So let's say that we have some event transformations.
SPEAKER_00An event transformation would be a class that contains all the upcasters that we registered. So all potential upcasters that can happen in our system. And then if we have the deserialization logic then we could try to transform so upcast our event so we are providing event type name and json and if we manage to upcast so there was define an upcasting logic for the following event type
SPEAKER_00then we are just returning this transformed value so see a similar pattern like I show you in the TypeScript and if I didn't have any transformation then I'm just using the default transformation and call it a day right I have just event type mapping just to know how to map the string event type into the CLR so the C-sharp type but this is a default deserialization
SPEAKER_00Let's have a look how event transformation looks like. So we have a dictionary of transformations and this dictionary contains this is the key is the event type name and the other is the function that takes string so JSON and returns the specific upcasted object and try transform works like so the method
SPEAKER_00that we saw here so this one try transform try to search and find the potential upcasting if there is no upcasting then it just returns null and doesn't do anything if it found the transformation then it just runs the transformation and returns the result simple as that And the registration of this transformation can be done in two ways.
SPEAKER_00One is just taking the row JSON, so event type name and transformation from the row JSON into event. And we are just storing that for this particular event type name, just transformed it in this way. and we can also register that for the regular types but in this case we are first doing the serialization to the old event type and then using it in the transformation
SPEAKER_00so this can this such registration can happen this way so we have event transformation and we are registering that for event type name so shopping cart initialized v1 use this upcasting logic and for this type if once you have it for this v2 then just do the clr transformation and use this upcasting function And then we can inject those transformation into our event serializer and just use such serializer, you know, in the regular way, no need to inject anything more.
SPEAKER_00So this is done. This is something that we could do in the application startup. This event transformation would be a single tone per our application lifetime. and we have all the transformations and then we can just use the serialize and such logic is pluggable into any potential way so this can be this can be plugged into this part or that part so it doesn't needs to be made only for event sourcing but it can be also made for
SPEAKER_00for like handling many multiple versions coming from messaging system so this is quite quite powerful technique And let me show you because in Martin, because what you see is also on my blog, but I decided to include that directly in Martin. So if you have, for example, if you'd like to upcast, let me, let me just scroll to the upcasting logic.
SPEAKER_00So see you have a multiple ways options are our Martin configuration and you can say that I would like to upcast the old event into the new one and just using the the following transformation there are multiple ways like we can define a specific event type name Martin is handling event time mapping automatically
SPEAKER_00or we could even use this json serialization so say like i'm using the json event and i'm doing this caveman caveman deserialization and in the same way there are also multiple ways you can also define class if you prefer to use class instead of injecting lambda then you can also derive from the event upcaster and do it the same way. So Martin, I know that Axon has also built-in capabilities for defining upcasters.
SPEAKER_00EventStoreDB doesn't have such, but I show you how it is really simple to do it. and to get back to the TypeScript code in the same way you can use the the transformations yes this one so you can define get shopping cart function and call c the same way so i'm using precisely the same aggregate stream method but for the parse method i'm sending it from event parser which is the class that is almost the same as i show you like here with the with our deserializer logically and conceptually it's precisely the same part
SPEAKER_00So in really those simple techniques, you can handle most of the scenarios that you have here. And really, I would say that if we are handling and ensuring and trying to model our event model evolution in a non-breaking manner then that's really achievable and those simple techniques should take you really really far.
SPEAKER_00So I want to also show you as the final grand finale I would like to tell you what's the best way of handling versioning and it's not what I show you but before I move on to the grand finale do you have any questions? Should I expand or describe those scenario a bit better or maybe the other way around?
SPEAKER_00Thank you Vendran, happy that it's clear. So see we can use also this upcasting here for read models. There is a question from Paweł I saw that event sourcing systems where events were stored in separate topics by version Is it good practice or not to store publish events in tables queues by version? So if it's event sourcing one then I would say that
SPEAKER_00You typically, I mean, event store is a database, it's not a messaging system. So you store them as they are. Once you store the old version, then you should typically append to the same stream. So in that regard, you will need to keep all the events of the specific stream in the same place. however if you are publishing events to other way to other systems so here then sometimes indeed people are publishing events like here is the topic with the old events and here is for the new one
SPEAKER_00So those advanced techniques I recommend to check Greg Young's book Versioning Event Sourcing System. I'm not sure if I can say if it's good or bad practice. I think that it can be useful to make explicit what you might expect and potentially maybe know whether you have some subscribers to the old topics with the old version but if we are handling our events in the way that they are non-breaking
SPEAKER_00so they are both backward and forward compatible then I would say that probably I would publish them in the same topic and in the same queue however it's a nice idea and Greg is also explaining that stuff but usually There is a challenge because if you have those two topics or some people are publishing two events one in the old schema and one in the new one but then for some specific time quite often you need to subscribe to both topics and then you need to understand and ensure that you are idempotent
SPEAKER_00So from which topic, which event you should use and whether you already handle it or not. so I would say that this is non-trivial but yeah that's that's some potential way I'm not sure Paweł if that answers your question but if not then feel free to follow up and I will try to be I try to explain it in a better way so feel free to follow up
SPEAKER_00Okay, so I see that Martin wants to implement a few app casters after that and Vedran also. Yeah, yeah, nice. But let me show you some other possibility. because in my opinion the best way of handling events versioning is to not need to handle it at all I know that that sounds dumb but that's real thing so let's say that those are our events so like those are our streams that we have in our system
SPEAKER_00and let's say that here this is the perfect scenario where we don't have any new schema or old schema just all using a single specific events so let's say let's make it a bit harder so let's say that this is v2 and this is v3 so we have two streams that are having the old schema so this one let's use like let's make it pink to see it better which one so those are with the old schema and this is like a pinkish because it has this is the latest schema and those are with the old schema
SPEAKER_00and let's say that we are because for such scenarios we of course can use our upcasting logic and once we are reading so let's take this part so once we are reading them let's see if i can rotate it okay the text will be upside down but that doesn't matter much so if we are reading it then we can do the upcasting right so we can handle that with the upcasting maybe there's
SPEAKER_00maybe it's not a big deal for us to maintain multiple version etc so instead of that we will get after upcasting only those events with the with the latest version so that's simple however sometimes our Stream definition changes significantly maybe it's not that simple as I show you so maybe we are missing some data and maybe we would like to have them or maybe it's so annoying for us that we need to always keep that in mind because remember this is not the only place that we need to handle those versions because we need to handle them also in read models
SPEAKER_00We need to read models in projections. We also need to do the upcasting. Of course, we can plug the generic mechanism, as I show you, and that's not getting super hard. But how to get rid of those old versions, especially this is really important for scenarios for integration. so let's say that those events were actually appended in a different way because let's have a look on how they look in the real event store or even in messaging system so this could be
SPEAKER_00like the this for sure was appended as the last one because this is the the latest event then this was maybe appended here and this may be here so those are our streams of course probably they mixed a bit in the order but let's keep it simple so this will be our global lock
SPEAKER_00and if we are using Kafka then you can think that those events could potentially be a partition in Kafka and topic whatever the same stuff so what we can notice here is that if we put a time on on this one so this is how the lifetime of our project look like those are the oldest and those are the newest one and we see that here we introduced the v3 the latest version right all that happened before may contain the old versions
SPEAKER_00And let's say that we would like to, once we deployed this v3, then we want to keep it compatible with the v2 and v1. So this shopping cart event will contain union of all properties from v1 v2 v3 so this is this can be annoying because as I show you here then if we have the union of all possible types then our
SPEAKER_00uh payload has some potential redundant data so this will be looking more or less like that so we see this client id is the same one as here and if we have more changes to our schema then we will have more and more duplication right and this duplication at some point together with the upcasting if we need to constantly do upcasting then of course that decreases our performance and if we are adding this duplication more and more duplication like that then our events are becoming bigger and bigger
SPEAKER_00so how to deal with that so this is like how our payload looks like and this is potentially a duplication because we want to be compatible and now let's say that we would like to get rid of them and how many how long can shopping cart live like what in your opinion what's the typical lifetime of the shopping cart i would say that typical shopping cart just leaves a few hours or maybe day
SPEAKER_00but let's say that we have the let's say that we want to be on the safe side and we would like to like get rid of those potential old data so this client id once like in about two weeks from now because this is our sprint it will be it will look nice in jira etc so we would like to get rid of all the schema versions at that time so potentially at that time probably this will be
SPEAKER_00this will be shopping cart confirmed for this shopping cart this one is already confirmed and maybe and this will be confirmed for this one right so this is how our events will be looking like because in the meantime the lifetime of our application will continue and definitely within those two weeks those shopping carts with the old potentially old schema will be just either confirmed or cancelled.
SPEAKER_00and the new events we will have in the schema that is both forward and backward compatible and maybe if we assume that there are no records that could be used to have those old event types then maybe we can just ignore those old payloads and from now on just proceed with the only v3
SPEAKER_00because technically we won't be doing any operations on those particular streams because you cannot do any business operation on already confirmed shopping carts so there effectively there won't be any leaving shopping carts that will contain the old events with the old schema
SPEAKER_00and then for those events technically we can define some strategy around the archival so we can potentially archive them even move from our event store because we won't need them for the specific logic Of course there is a then question about the projection rebuilds because we won't need those old events from this part so for the business logic but we might need them for the read models and for the read models because we would like to maybe be able to rebuild the read models build new one or on old data then
SPEAKER_00that's quite often the case where we keep the because why we keep those old events and maybe in in that case we can for example append just a summary so a new version like a shopping cart This is a really dumb name. Don't do that on production.
SPEAKER_00But maybe for those events with the old one, maybe we can just provide a summary event that would just have all the information from the past data and introduce that event and use that event for projection rebuilds or for publishing etc. So if we care about the archiving our old events and old schemas then
SPEAKER_00If we are doing that on a constant basis, then this can help us and make us not needing to maintain all of our past event scenarios. And the same goes for integration with other systems. Of course, it's a bit tricky because typically those two modules will be handled by different teams, different deployments.
SPEAKER_00So usually we need to do a bit more time. but if we want then we can plan and discuss with the other team and say like hey publishing those events that are forward and backward compatible is a big effort for me then let's agree that I will be doing that for a next month or two but you need to do you need to handle the new version of our schema the latest one and if you don't then after those two months we will stop producing events with the old schema and they won't be anymore backward compatible
SPEAKER_00So in that regards, even if we are making the breaking change, then we can proceed with that in a non-breaking manner. And the other really, really important part is located here. So if we were publishing all those granular events to messaging system and this shopping cart will need to subscribe to all of those tiny events then each time we are doing breaking change we are doomed because each potentially breaking change to the schema can affect the other modules
SPEAKER_00and this part so doing the ACL on the publisher side is really important and having the clear split of the private and public events because if we have such transformation that once we got the shopping cart confirmed let me just move it a bit more down
SPEAKER_00so let's say that we have the shopping cart module and we have the shipment module and and let's say that we have like v1 v2 someone was really lagging or we were introducing those changes really fast then let's say that we have those events and all of those events if they were if the shipment module will subscribe to that then they will need to handle all the potential versioning that we have
SPEAKER_00However, typically that's not the case because typically shipment module will care only about the confirmed shopping carts. So if we place here the transformation and we will subscribe only to the shopping cart confirmed
SPEAKER_00and we will transform it into the other representation so let's mark it as external because just to to show you that this is different event then if we do this transformation and if we publish that to only this transformed event to the shipments then if they are only subscribing to this event through the messaging system then on this part we can do much more
SPEAKER_00easily the all the other transformation that we have because then if we have such thing then why not we can introduce the version 3 and as we discussed inside the single module and the single deployment it's much easier to handle next versions of the specific thing so here we would just subscribe to the final event and then we are typically either loading all the events and building representation or querying read model whatever we do to enrich and provide the final version like the external events to the outside world then we can do here in this particular place
SPEAKER_00all the ACL logic that we want to and even potentially the shipment module doesn't need to do anything because if we introduced here and we massaged this breaking changes potentially breaking changes with upcasting with other strategies that we discuss then here if we are having this additional stuff then here we are doing upcasting, downcasting, whatever we want but here we are ensuring that the contract and the data that we are pushing outside is compatible to all subscribers
SPEAKER_00and by the simple techniques and as I said hygiene and proper engineering approach we don't need to really do any sophisticated topic management etc. Of course there might be scenarios where we need to do some bigger migration then we might do some stuff like copy and transform so take the this whole stream transform it into different representation and publish it outside but usually from my experience if we care about other people's work if we put thinking about the modeling and versioning and how to tackle that into the design process then we really with those techniques that I mentioned you can handle really most of the stuff
SPEAKER_00and some people are asking me like okay then I have seven version of the specific events and that's annoying and I cannot handle that anymore and in that case to me that's unfortunately a sign that we should get back to the drawing board because that really rarely business process is changing so often. What changes quite often is our understanding of this particular business process.
SPEAKER_00so if we have such a big amount of the different version then that means that unfortunately we are not doing great job on talking with business and maybe we should stop do sanity check and talk more with our business and try to understand why we have such a big amount of changes that we need to provide those different version and that we cannot handle with those simple mappings that I mentioned because of course it might happen that we have new field
SPEAKER_00uh new data that's obvious but as we discussed new field usually is not a big deal we can handle that even on the serializer level accepting that for the old events we might not have this information but um usually i would say that that's an indicator that we are not doing great job on modeling unfortunately and i'm also telling that about myself because i did that in past
SPEAKER_00there is there is comment from martin so there are domains where government's laws come out rather frequent and you have to change your design yes sometimes that happens and especially regulations that are coming from the next year and are announced in december and you need to write in a rush so i'm not saying that this is always the case but from my experience usually the case and if the process is changing so significantly then you can do the mentioned by me copy and transform and copy and transform work works in a way that you take one set of stream one stream
SPEAKER_00and usually the copy and transform you need to do when you cannot just map event one-to-one because maybe you had an issue that you'd like to for example product item added to shopping cart and you had multiple events like that and now you would like to union that into some other then you can take this set of events and also transform them and usually it's done like that so you take those events from event store
SPEAKER_00you read them and you do the transformation and once you got the new shopping the newly transformed stream then you are appending that into some again into either the same stream or usually the different one so maybe that can be like previously you had five events and now you would like to have three and maybe some other or maybe you want you you need to enrich those events and read some additional data from uh from some read model so that may be that may be also the case
SPEAKER_00but then you can do it this way but to be true I wasn't doing that type that type of logic too much but sometimes it happens and that's a valid scenario so it's like a I even have example of that but that's really I would say showing that in the end can be too mind-blowing but we can add additional layer to our deserializer so have provide to deserialize the whole list of the stream
SPEAKER_00then we might have the stream transformation so we are taking all events and in the same way checking if we have if we can transform them so this works this way so we are checking if we have some stream transformation if we don't then we return events as they are but if we have then we can run some transformation for the whole stream and this transformation can be also a function that takes events and returns different set of events so we can do it also the same way and use also the same technique also for this copy and transform
SPEAKER_00So, as I said, based on what we discussed, you can build and you can provide even more fancy way of handling such stuff. Okay, so I will be trying to wrap it up. If you have any questions then feel free to ask them in the meantime. So we discussed today multiple options how to handle our schema versioning and schema migration.
SPEAKER_00So the most obvious way is this one, this ugly one rule. the only rule that I really suggest you to follow do not do breaking changes I'm really usually like well it depends but in that case I really suggest do not do breaking changes for schema and if we are not doing breaking changes if we have our contracts and events are our contracts in a both forward and backward compatible then handling them becomes much much easier
SPEAKER_00and backward compatibility is glossed and explained much more ways and much more often. However, forward compatibility is also important because nowadays we can easily have systems that are capable of handling old schema, new schema at the same time, even the same instances of our service. so if we are introducing our schema that can be handled both by old subscriber and new subscribers then we are just making our life much much easier of course it's like Aaron Stannard said in his optionality talk it's about giving ourself more option giving ourself more option requires a bit more work upfront
SPEAKER_00I wouldn't say that it's extremely a lot work up front, but we need of course to think and understand that our schema is changing, then understand why is it changing, and then choose which technique we will use for this particular case. and of course doing migrations is in general easier if it's handling inside our module inside the boundary so inside bounded context and a single theme but sometimes that might also mean that this actually are more than a single service and we also need to take into consideration some more advanced techniques
SPEAKER_00but the stuff that we discussed so handling simple mappings through serializer transformation so upcasting to the new schema from old one and downcasting from new to the schema that is compatible with all previous schemas It's really essential and it's really simple. Logically, it's just a basic transformation and mapping from either old object or just old payload into the new event type.
SPEAKER_00And that can be also handled not only in event sourcing, but in general also for messaging, handling events coming or comments coming from messaging systems like Kafka, Rabbit, etc. but in general the most important part is here so the the the magic trick to handle versioning is to not need to handle this versioning so in messaging systems it is a bit easier because they have their retention policy and the the events will old schema will just disappear
SPEAKER_00in event sourcing is a bit harder because we are storing those events durable but if we have this archive techniques and if we know this lifetime of our objects then we can have just this period that we will need to support both versions but after that we might just have the new version because all the records and all the streams that have old events in the old schema will effectively be already closed and we won't have those streams accessed anyhow.
SPEAKER_00we can do some summary event and do the mapping and just store the summary if we want to do the rebuilds so i'm not saying that it is easy simple is not always easy but once we know and practice those simple techniques then as you see they are really really composable and quite powerful and I think that the versioning in general shouldn't be that scary if we accept that the change will come because it will
SPEAKER_00So that's all from me. I hope that this will be helpful for you. The Miro board I will send you also later on read-only access and some other reference materials. But if you have any more questions then feel free to ask them now. Again, I'm not great on planning. It was, again, supposed to be one hour and a half, but it's a bit longer. So thank you for staying with me.
SPEAKER_00And if you have any questions, I can answer them. Thanks, Zach. Glad that you like it. Thanks, Vedran. Thanks, Martin. And thanks, Paweł and Daniel and Manuel for staying also with me. okay so cool thank you again if you if something pops out after you rethink what we did and or once you watch again the recording then feel free to ask on discord i will try to cover all of that
SPEAKER_00but yeah for now I would say that again thanks for staying with me and have a great afternoon Manuel have a great rest of the day it's earlier for you and thank you Paweł for the kind words yeah okay guys so thanks bye
Welcome to the new week! “How to document systems build with Event-Driven Architecture?” and “How to keep our understanding about evolving messages definitions?” Are one of the common questions I’m being asked. For many...
Welcome to the new week! Tomasz Ducin is back with us to explain the next parts of Frontend Architecture - this time with a deeper, more hands-on walkthrough of Frontend State Management on the React Query example. And...
Have you considered applying observability but struggled to match the strategy with the tooling? Or maybe you were lost on how to do it? I had a great discussion with Hazel Weakly about Observability beyond Open...
Welcome to the new week! For a long time, I’ve wanted to talk more about Frontend Architecture. In my projects, I always enjoyed delivering features end-to-end the most. That gave me the feeling of completing and...
"My favorite conspiracy theory is that the stuff we make in software actually has any sense. That's a big conspiracy, and you can see people believing that, and that's just not true. I mean, it is in some percentage,...