Latest / Talk Python To Me / #548: Event Sourcing Design Pattern
Transcript
- 0:00What if your database worked more like Git?
- 0:02Every change captured as an immutable event instead of a single mutating row that quietly forgets its own history.
- 0:09That's event sourcing.
- 0:10And Chris May is back on Talk Python, fresh off our Datastar panel, to walk us through what event sourcing actually looks like in Python.
- 0:18We'll cover core patterns, the libraries to reach for, when not to use it, and why event sourcing turns out to be a surprisingly good fit for AI-assisted coding.
- 0:27This is Talk Python To Me, episode 548.
- 0:29Recorded May 5th, 2026.
- 0:47Welcome to Talk Python To Me, the number one Python podcast for developers and data scientists.
- 0:54This is your host, Michael Kennedy.
- 0:56I'm a PSF fellow who's been coding for over 25 years.
- 1:01Let's connect on social media.
- 1:02You'll find me and Talk Python on Mastodon, Bluesky, and X.
- 1:05The social links are all in your show notes.
- 1:08You can find over 10 years of past episodes at talkpython.fm.
- 1:12And if you want to be part of the show, you can join our recording live streams.
- 1:15That's right.
- 1:16We live stream the raw, uncut version of each episode on YouTube.
- 1:20Just visit talkpython.fm/youtube to see the schedule of upcoming events.
- 1:24Be sure to subscribe there and press the bell so you'll get notified anytime we're recording.
- 1:29This episode is sponsored by Sentry's Seer.
- 1:32If you're tired of debugging in the dark, give Seer a try.
- 1:35There are plenty of AI tools that help you write code, but Sentry's Seer is built to help you fix it when it breaks.
- 1:40Visit talkpython.fm/sentry and use the code Talk Python26, all one word, no spaces, for $100 in Sentry credits.
- 1:49And it's brought to you by Temporal, durable workflows for Python.
- 1:53Write your workflows as normal Python code and Temporal ensures they run reliably, even across crashes and restarts.
- 2:00Get started at talkpython.fm/Temporal.
- 2:04Hey, Chris.
- 2:05Hey there.
- 2:05How's it going?
- 2:06I'm well.
- 2:07How are you?
- 2:08Oh, good.
- 2:08Good to hear it.
- 2:09I'm happy to have you back on the show.
- 2:11Last time we had you on the show, you were part of the panel around Datastar, and that was cool.
- 2:16Now we're going to talk about event sourcing, but we'll find a way to tie it back to Datastar just a bit, I think.
- 2:21I see it on the horizon.
- 2:23Or on the show notes.
- 2:25Sounds good.
- 2:25One of these.
- 2:26Yeah.
- 2:26Well, not everyone listens to every episode.
- 2:28We've got new listeners coming all the time, so give us the quick intro.
- 2:32Who are you, Chris?
- 2:33Who am I?
- 2:33Let's see.
- 2:34I am a Python developer for about 20 years and a long-time listener to the show, so it's a very big privilege to be on, finally, my own self.
- 2:43And by the way, great job continuing going, Michael.
- 2:46You're constantly putting out great content, so I really appreciate all your work.
- 2:50But as far as me, I learned the program as an adult.
- 2:53Friend suggested I learn Python.
- 2:56I hitched my wagon to this engine, and I've loved it ever since.
- 3:01I was a technical coach for a little while.
- 3:04I started the Python group here in Richmond, Virginia, PyRVA.
- 3:09So if you're local, come out.
- 3:10In fact, we're just two weeks away from the next meeting.
- 3:14Awesome.
- 3:14How frequently do you have meetings?
- 3:15Once a month now.
- 3:16In fact, if you look behind me, you can see all these blue dots are the meetings.
- 3:20Okay, excellent.
- 3:22Keep planning.
- 3:22What are some of the kind of topics you all have?
- 3:24Oh, man.
- 3:24We range pretty much whatever we can figure out that people are interested in.
- 3:29We've had a number of AI discussions.
- 3:32In fact, those have been really powerful.
- 3:34We've kind of lined up the chairs in a big circle and just have a discussion.
- 3:38And it's really incredible.
- 3:40You know, you have people on the whole spectrum of opinions about AI.
- 3:47So, yeah, it was very, very...
- 3:49That was one of my favorite...
- 3:50I was going to say episodes.
- 3:52One of my favorite meetings.
- 3:54I think we might bookend this podcast episode with a little AI at the start, a little AI at the end.
- 4:00Sounds good.
- 4:01Cool.
- 4:01So, yeah, people will just go attend that.
- 4:04And I guess probably a meetup.com is where they find you.
- 4:07It is, yeah.
- 4:08And you can go to pyrva.org to get to the meetup page either way.
- 4:12Yeah, easiest way.
- 4:13Because you can find a lot of crazy stuff.
- 4:14You'll probably find people with pet pythons that want to meet up in Virginia as well.
- 4:18And it's not the same thing.
- 4:20Indeed, yeah.
- 4:21Thankfully, and we are, gosh, over 10 years old.
- 4:24So we are one of the first ones to pop up just because of age, I think, sometimes.
- 4:28Although, honestly, some of the newer ones pop up ahead of us sometimes, too.
- 4:32So, you know.
- 4:32It's such a contentious topic, this AI stuff.
- 4:36You know, talking about your meetup.
- 4:37How did that go?
- 4:38Were people frustrated?
- 4:39People excited?
- 4:40Yeah, it was kind of the whole spectrum.
- 4:42You know, I felt like it was great because everybody respected each other.
- 4:45And there were, I'd say, two people anchored on each of the sides.
- 4:51You know, two people who, actually, maybe there were three people who, you know, have bought their own hardware and are really going deep into AI and, you know, encouraging people to, like, really dig into it.
- 5:01And we had two people who were very AI skeptic and very, you know, worried about the environment, you know, all sorts of different things.
- 5:10And so, I thought it was a very healthy, very good discussion.
- 5:14You know, I walked away with, you know, a healthier respect for both sides.
- 5:20Some ideas that I incorporated into my work, especially now that the company I work for gives us a mandate to use AI to write every piece of code.
- 5:28So, that's been a very fascinating transition for me.
- 5:32But it also gives me, you know, a little bit of agency or a little bit of permission to experiment and see what to do, you know, how to actually function in this new paradigm.
- 5:44It is engineering.
- 5:45And we'll talk about it more later, but it's quite wild that your company's, that is the position.
- 5:50I don't necessarily think it's the wrong position.
- 5:52I think it might be the right one, but it's, I think it's got to be brought on from a, from a, this is a skill you need to learn, not just throw stuff at the chat bot and now that's your job.
- 6:03Like, these are not the same things, you know?
- 6:04But I think a lot of people do, especially when they're getting started, treat them as the same thing and then say it doesn't work and then they're frustrated.
- 6:10Yeah, totally.
- 6:11Yeah.
- 6:11Yeah.
- 6:12Yeah.
- 6:13All right.
- 6:14More of that.
- 6:15We'll speak further about certain things, but yes, I agree.
- 6:19Absolutely.
- 6:19Absolutely.
- 6:20All right.
- 6:20Well, we'll, we'll come back to that and, and get there, but let's talk about a little bit of this event sourcing thing.
- 6:27Yeah.
- 6:28What exactly is event sourcing?
- 6:30That's a question.
- 6:30Okay, cool.
- 6:31I wasn't sure if you had more.
- 6:32I saw your.
- 6:33No, no.
- 6:33I think I, when you, when you reached out and said, Hey, let's talk about this.
- 6:36I'm like, I, so let's take a step back.
- 6:38Okay.
- 6:39Design patterns, refactoring ideas, like all of these architecture.
- 6:44I love to talk about this stuff.
- 6:45I love to think about this stuff.
- 6:47I think also, maybe that's a why, one of the reasons I'm not super frustrated with the AI things because I will tell it, I want you to use, like you could say like, I want to build a system with event sourcing and it's going to work this way.
- 6:57Like for me, the fun is like, Oh, I got to build some of the event source and it clicks together with this and it does that.
- 7:02And like, yeah, I didn't have to do all the little checks and details and like the file IO, but whatever.
- 7:07Like I can skip that and build just like, think a little bit bigger and a little more, more big building blocks.
- 7:12So I'm a big fan of design patterns and paying attention to what you're doing, I can tell that you are too.
- 7:18Yes.
- 7:19Especially event sourcing, I would say.
- 7:22So I have been following the topic of event sourcing for over a decade.
- 7:27I listened to a podcast in with a couple of developers in the PHP realm and they were talking about event sourcing and it just inspired me.
- 7:36specifically the things that I really loved.
- 7:39Before I was a programmer, I was a graphic designer.
- 7:42And so creating websites with exceptional user experiences is something that just makes me excited.
- 7:48And at the time, I was working at a creative agency building websites that would just slow, get slower and slower and slower, the more data we put into it or, you know, the more we configure the routing or the navigation and whatnot.
- 8:01And so the thought of never having a slow page load again was intoxicating to me.
- 8:08However, that was in a portion of my career where I was, had so much imposter syndrome, having learned to program in as an adult and I just felt like I couldn't suggest to my lead developer we should lean into this.
- 8:23And even when I was a lead developer, I just didn't feel confident that I could lead a whole team into like a redesign of the code.
- 8:32But a couple years ago, I got laid off and I was like, you know what?
- 8:35I've been wanting to explore event sourcing for 10 years.
- 8:39I'm going to do it.
- 8:40And it turns out it's a lot easier than I thought.
- 8:43But anyway, all this to say, let me define event sourcing since I've kind of danced around it.
- 8:47Event sourcing is how, has to do with how you save data to the database.
- 8:52The best way to contrast it is most apps are kind of CRUD based, right?
- 8:56So you create a table, like let's say you're a shopping cart application.
- 9:00You have a table called carts and you have a bunch of columns and one of the columns has data for the product IDs that are in the cart.
- 9:10And so if you have that kind of situation and you have one user who adds five items to the cart, then removes two and then checks out.
- 9:20And then you have another user who adds just three items to the cart and checks out.
- 9:23Well, if they check out at the same time and you look at the two database rows, they are very similar.
- 9:28They're the same user, basically.
- 9:30They get put in the same cohort from your marketing side, right?
- 9:34Yeah.
- 9:35Both of them checked out exactly the same products.
- 9:39And if you look at the database, you'd have no idea that one of them removed two items from their shopping cart.
- 9:44So event sourcing, so the reason that is because CRUD based applications will mutate state in the database to ensure it's always up to date with the current state and optimize for data integrity.
- 9:58Event sourcing, on the other hand, captures every change that happens within the system.
- 10:04So you would have an event of like cart created, item added to cart, item added to cart.
- 10:10You'd have five item added to cart events and two removals.
- 10:14And so you could have the whole history of how each user interacts with your system in the database.
- 10:19That is essentially the core of event sourcing.
- 10:22Everything else is actually adding more design patterns onto event sourcing, which is really wonderful.
- 10:27But that is what event sourcing is.
- 10:29Right.
- 10:29So your database sort of becomes almost an audit log.
- 10:33It feels very source control-ish.
- 10:35It feels like Git, right?
- 10:36Like what you store is the file and then the diff, then the diff, then the diff.
- 10:41You get back to the file by running all the operations on it, potentially.
- 10:45Yeah, exactly.
- 10:46Yeah, that's exactly what it is.
- 10:47And it's funny because like, I don't know about you, but like when I first heard about this, I'm like, well, isn't it slower?
- 10:52Because it just seems like you're doing so much more work.
- 10:54Every time you're updating the cart, you're pulling out all the events from the event store and building up the state of where it's at right now and saying, okay, you know, does that cart exist in the, does that item exist in the cart?
- 11:06Can I remove it?
- 11:07Okay, let's remove it.
- 11:08You know, and it turns out computers are fast.
- 11:12And so it's negligibly slower depending on the query.
- 11:17Yeah.
- 11:17So I saw you and Bob Belderbos talking about this on YouTube and that was one of my first thoughts too.
- 11:22It was like, this is super cool, but it's kind of sounds a lot slower to answer questions.
- 11:27Yeah.
- 11:28And I think, but then I thought about it and I thought, I think there's actually a decent amount of stuff that you can do to make it quite a bit faster.
- 11:36Right.
- 11:36So let me throw some ideas out to you and then you tell me how they land as somebody who's actually done this stuff.
- 11:43First of all, back to your comment on computers are fast.
- 11:46Computers are so fast.
- 11:47They're so much faster than people realize how fast they are and databases are fast too.
- 11:51If you put indexes on them, then they're so much faster.
- 11:55I just don't understand how websites are slow.
- 11:58It's just, it's not just, oh, I'm a little frustrated.
- 12:01It's like, how is it possible that somebody built this and accepted that it takes three seconds to load this page and yet they do and you just know that it's most likely that there's not a database index somewhere or maybe on an extreme situation there should be better caching,
- 12:16but it's just like, ah.
- 12:18So like when done right, you're right, it's absolutely blazes, right?
- 12:21Yeah, absolutely.
- 12:22But how, you could make it faster.
- 12:25So a couple of thoughts that came to mind.
- 12:26One was, I have three.
- 12:29The one is you could have a operational database which has, for a particular user, it might have the five ads, two removals and that's their shopping cart and the way you get that is either you query it and then code you add it up or you do some kind of aggregation thing
- 12:43that says get all these things and then somehow plus minus them together, right?
- 12:48Yeah.
- 12:49Probably in the shopping cart it feels like you probably need to actually pull them back.
- 12:53But still, you could do that super quick and then just run that bit of code and every time you make a change you could also write that to a second table, second database that doesn't have the, it just has the current state.
- 13:04I just have three for that user shopping cart.
- 13:06That sounds okay to me but it's, I mean databases hate duplication.
- 13:10That's like kind of their third normal form job.
- 13:13Do databases hate duplication or is it people that don't like it?
- 13:18Yeah, that's fair.
- 13:19That's fair.
- 13:19I mean they were built to avoid duplication, right?
- 13:21So in that sense.
- 13:22So, but another, you know, another possibility would just be something like Valkey.
- 13:27You could, I could have said Redis but I'm a fan of Valkey over Redis.
- 13:31I find this is like a little bit nicer project.
- 13:33Are you familiar with Valkey?
- 13:35I am not and I'm taking notes mentally right now.
- 13:37Yeah, here we go.
- 13:38So Valkey, or if I find its repository which is hiding down here, it describes itself.
- 13:45Where does it describe itself?
- 13:47So a fork of the open source Redis project right before the transition to their new source available but not open source models.
- 13:54So if you have say the Redis Python library, you just tell it that it talks to this thing, this endpoint, this IP address and port and it thinks it's Redis, right?
- 14:03But it's like more open source friendly.
- 14:05I'm going to star it for the world.
- 14:06So, and it's got 26,000 stars, right?
- 14:08So it's pretty popular.
- 14:09Not that it's totally really relevant exactly which version this is but just having a cache that has that information of what is, you know, shopping cart is three, period.
- 14:20Yeah.
- 14:20And put something, just put two pieces in place.
- 14:23One, when you do a query, first you check the cache.
- 14:25If it's not there, you get the playback, you compute it and you put it in the cache.
- 14:30And if you make any change to that thing, then you invalidate the cache, right?
- 14:33Those two things would give you basically seamless ephemeral answers to the questions that if you ask them very often, you get super quick responses, right?
- 14:42Absolutely.
- 14:43Yeah.
- 14:43And then the other one is, if you, you know, I always find like these extra caching servers are honestly not necessary.
- 14:49You know, we already talked about FAST.
- 14:50So I'm a big fan of disk cache, which I had Vincent Warmerdom on to talk about and disk cache is awesome.
- 14:57So you could just have like a local file store on your Docker image or volume or whatever.
- 15:02Then you get the same thing, but you don't have to have the infrastructure, right?
- 15:05There's, there's different options.
- 15:06So basically, I guess to sum this up, I'll throw it to you now.
- 15:09Operational database that is also has the answers or some kind of caching story.
- 15:14It could be in memory, it could be a server like Valkey.
- 15:17It could be the disk cache, whatever.
- 15:19What do you think about those?
- 15:20Are you guys all exploring them?
- 15:22I am exploring two, essentially, yeah, I guess you could say all three in a way.
- 15:27I'm, I'm experimenting with NATS to do kind of the, I guess, Redis kind of side of it.
- 15:31But all that to say is all three are viable options depending on your use case.
- 15:36The first thing is like just the event store itself.
- 15:39Like you, getting the current state of any individual item should not take, you know, essentially should take milliseconds if, if that long.
- 15:48One of the, so there are two people that I kind of, who are my North star for all of my event sourcing knowledge.
- 15:54they are Martin Dilger and Adam Dymitruk.
- 15:57And Martin wrote a book called Understanding Event Sourcing, which was huge to helping me go from somebody who created an event sourcing project to adopting event sourcing as my kind of default strategy.
- 16:12And in, in the book, he mentioned that he will tend to use the event store for essentially, like, if you start hitting 2000 events in a stream, then he'll think about optimizing it, you know, changing, going to one of the alternate
- 16:26approaches that we just mentioned.
- 16:28So, like, that's really impressive.
- 16:30I, he uses Java or one of the Java derivative languages.
- 16:34So, chances are, it's slower in Python.
- 16:36And so you need, you know, that we need to adjust that number for Python.
- 16:41But honestly, we should have shorter event streams anyway.
- 16:442000 seems like a lot.
- 16:46It does.
- 16:48Seems like a lot.
- 16:48This portion of Talk Python To Me is brought to you by Sentry and Seer AI.
- 16:54There are plenty of AI tools that help you write code, but Sentry Seer is built to help you fix it when it breaks.
- 17:01The difference is context.
- 17:02Seer isn't just guessing based on syntax.
- 17:05It's analyzing your actual Sentry data, your stack traces, logs, and failure patterns.
- 17:10Because it has the full context, it can, A, spot buggy code in review and help prevent issues before they happen, and B, identify the root cause of production errors.
- 17:20It can even draft a fix and hand the work off to an agent-like cursor to open a PR for you.
- 17:26Seer turns Sentry into a complete loop.
- 17:28You have your traces, errors, logs, and replays to see the problem and now AI to help solve it.
- 17:33Join millions of devs at companies like Claude, Disney+, and even Talk Python who use Sentry to move faster.
- 17:39Check them out at talkpython.fm/sentry and use code talkpython26, all one word, for $100 in Sentry credits.
- 17:49Thank you to Sentry for supporting Talk Python.
- 17:52Kind of the first fallback for me would be what Martin and Adam call the read model, which is essentially one way you can make a read model would be the database-backed read model where you have code that subscribes
- 18:05to only the events it cares about.
- 18:08And so whenever an event comes in, it'll incrementally update that database cache or your file cache or Redis cache or whatever.
- 18:14And then I guess the third thing that I appreciate is using Redis or in my case, NATS, is when you have a front-end, a very high-frequency, high-updating web UI or something
- 18:28that you want to really make sure that the user has up-to-date information, I would lean towards that having ways to push down to the client.
- 18:36Yeah, anytime you've got a live stream, it seems perfect, right?
- 18:39Yeah, absolutely.
- 18:40Yeah, I mean, hook up some JavaScript or hook in some textual or whatever it is you're trying to do and just say, or just write, you know, arbitrary web sockets or server-sent events and just say, when this changes, send me the Delta and then we'll adjust.
- 18:53Yeah.
- 18:54Which is pretty, pretty nice.
- 18:55Yeah.
- 18:55So there's a book that you recommended by one of the guys you mentioned, Martin Dilger.
- 19:00Yeah.
- 19:01Tell people about that real quick.
- 19:02Yeah, this is incredible to me.
- 19:05He realized he needs, there's a gap.
- 19:08One of the biggest problems with event sourcing is that the material is sometimes there's gaps in where there's good material and because event sourcing came out of the domain driven design community, there's a lot of jargon that you have to kind of get through.
- 19:22And they do like their jargon in the DDC space.
- 19:25They really do.
- 19:27And it, once you understand it, it makes perfect sense.
- 19:29But yeah, yeah.
- 19:30Getting onboarded does, does take.
- 19:32Gets your bounded context working and then off the races.
- 19:35Yeah, exactly.
- 19:36And so Martin essentially saw this gap in knowledge and was like, I need to fill this with an e-book.
- 19:44And I can't remember, I want to say in like two months he wrote this thing.
- 19:47And it's so amazing because it introduces the way that the two of them work.
- 19:52Like they both independently kind of came to similar conclusions, which is to use event sourcing as your base, but to also leverage two other or one main other, whatever, a couple of other patterns, vertical slice architecture, CQRS, which is kind of that idea
- 20:07of like having those read models ready to, you know, optimized for you to download and use.
- 20:14And then using a documentation technique called event modeling diagrams.
- 20:18And that, that is a huge key too because as someone who has been on a couple teams to do the event-driven transition to try to really help, you know, do more asynchronously, you need to have a good communication pattern
- 20:33to keep everybody up to date on what does what.
- 20:35And I find that all three, especially event modeling diagram, they have refined this to make it simpler and simpler and simpler to the point where there's really just a few elements put together and you can understand the whole life cycle of a, of an application.
- 20:49Yeah, cool.
- 20:49I'll link to the book on, over on Amazon.
- 20:52You know, another, before we carry on, I thought of another more optimized scenario.
- 20:57What about a document database like Mongo?
- 20:59Yeah.
- 21:00Your top level elements are just like the computed fields like total lifetime value or, you know, cart value or cart count, item count, but then have like maybe a cart item events, which could be a, a nested list
- 21:14of acts of like rich, you know, many documents that are like item added, item added, timestamp, value, like category, all, and actually storing them in the same record.
- 21:24Yeah.
- 21:24I haven't tried that, but I think it's a really interesting approach.
- 21:27You know, I actually use a document database as my fire store, as my, as my event store.
- 21:34And I haven't really kind of dug into like kind of the optimizations I could do.
- 21:38But I find it curious because like the, I mean, what you're suggesting is slightly different than how I think about it because it sounds like you have like a, like a better word, a model that you're storing all of its events in as well.
- 21:50And some of the neat, some of the, it's kind of inside out of what the real design pattern is, right?
- 21:55Well, it's not so much that as much as, one thing that I have found interesting is, you know, since this came out of the domain driven design group, everything is about an aggregate, what they call aggregate.
- 22:07Many people call it a model.
- 22:08And so you, you know, set up boundaries of this is your shopping cart and these are the events that modify the shopping cart.
- 22:15What has been a new movement in event sourcing is to, essentially be model less is to like focus on the events themselves because they are so flexible and so many times we as developers can kind of create
- 22:29boundaries around what we think are the models that, but the models change.
- 22:35Yeah.
- 22:35Yeah.
- 22:36And coupling is like the hardest thing.
- 22:38The bounded context, as I would say, actually changes because the problem you're solving might change and the models don't match.
- 22:44Exactly.
- 22:44Yeah.
- 22:45Yeah.
- 22:45So all that to say is I don't, not that I want to especially say like, I don't think that that's a bad idea.
- 22:50I think it could be really fascinating.
- 22:52especially as like a secondary approach because, you know, well, whatever, you know, like I, one of the things I really find fascinating about this is this is such a flexible pattern that people, I mean, they've done so many different ways of optimizing
- 23:05for their event store or anything like this.
- 23:08So I think that's a very much a valid approach.
- 23:11It's, it's, the reason it came to mind is you can atomically update documents and therefore you could atomically update both the computed value and the series of events as a single action, which is interesting, you know?
- 23:25Yeah, absolutely.
- 23:26Very interesting.
- 23:27And they kind of, that, that one thing becomes the source of truth for what you're tracking.
- 23:30I don't, there might be something there.
- 23:32I don't, but it does sound a little bit too focused on the model.
- 23:35I do think.
- 23:36It's worth experimenting with for sure.
- 23:38So just, to throw out a little street cred there, look at this, purchased April 18th, 20, 2005, the domain driven design by Eric Evans.
- 23:47So this is kind of the greater space, right?
- 23:50Maybe the book is called domain driven design, tackling the complexity in the heart of software.
- 23:54It's pretty interesting.
- 23:55I think it's kind of the follow-on of the refactoring movement that Martin Fowler and all those folks were working on, like in the late 90s, early 2000s.
- 24:05Yeah.
- 24:05Kind of in that space, right?
- 24:06Yeah.
- 24:07I must say, I haven't bought that book.
- 24:08The closest I've come is the Cosmic Python book or the architecture patterns in Python book that Harvey and, oh, I always forget the other guy's name, but that's such an amazing book.
- 24:19So that's the closest I've come to DDD.
- 24:21There's a video.
- 24:24I think, I think you gave me this video, right?
- 24:25Events, we're seeing explain football.
- 24:27Yeah.
- 24:27And this looks like foosball type football, not American football.
- 24:31Indeed.
- 24:32I love American football, but I do believe it's slightly misnamed.
- 24:36Like, you don't use your feet that much, other than the red card.
- 24:38Yeah.
- 24:40True that.
- 24:41I mean, it's like calling Formula One, like, foot car, because you make the car go with your foot, but it's not really the main thing of the sport anyway.
- 24:49Yeah.
- 24:50But yeah, I love this video.
- 24:51Yeah.
- 24:52Okay.
- 24:52Tell me about it.
- 24:53I'll put it in the show notes.
- 24:54Yeah.
- 24:54This is, you know, I feel like event sourcing, it's like one of those things where it's hard to explain until you get it.
- 25:00And then I feel like it's like a good board game.
- 25:02You know, if you have a board game fan in your life, they're like, oh, this is such a great board game.
- 25:06It's so simple.
- 25:07And then they start explaining it and like half an hour later, you're like, are we ever going to actually play this?
- 25:11I don't know that I want to anymore.
- 25:12But I felt like the, this person has done a really good job of kind of really distilling it down and showing like why it matters.
- 25:19And it's a 10 minute video, you know, five minutes at 2X.
- 25:22And it's, it's really kind of charming.
- 25:24Really good.
- 25:25Really well done.
- 25:25Okay, cool.
- 25:25Yeah, yeah.
- 25:26I'll put it on there.
- 25:26Can you watch videos at 2X?
- 25:28Oh yeah, all the time.
- 25:29Yeah.
- 25:29My daughter does that.
- 25:30I'm like, how do you actually take it in?
- 25:33I just, I'm a 1X sort of person.
- 25:34I do slow it down or rewind to, to pull in things, but yeah.
- 25:38Let's see.
- 25:39It's like a, like a seek and then focus sort of deal.
- 25:42Exactly.
- 25:42Exactly.
- 25:43Not that one.
- 25:44This one.
- 25:45I also heard there's this ebook I can get.
- 25:47What's up with this?
- 25:48Yeah.
- 25:49So scheduling this podcast episode, I didn't know, you know, what we're going to talk about.
- 25:55And I know like, there's going to be things I forget.
- 25:57And I feel like part of the reason I am so excited about this is I know that there's someone like me, the 10 year ago version of me who has heard about these things and is curious, but just needs more information.
- 26:08And so I've spent the last couple of weeks creating this ebook and I put the first version up and I'm going to continue to improve it as time goes on.
- 26:18So if anything in this conversation is interesting to you and you just need a little bit more and you want to understand a little bit more what's going on, absolutely download it.
- 26:25And, you know, it's free.
- 26:26So why not?
- 26:27Cool.
- 26:27And then we'll have to make you do a, an audio version, put that on audible or something like that.
- 26:32Sounds good.
- 26:33Yeah, sure.
- 26:34I always want to do audio books for stuff that I'm working on, but just the concept of trying to speak code or config file, I just like, I got to stop.
- 26:42You know, it's, it's tough.
- 26:43It's a tough balance to do with audio books and tech, like developer stuff, but still.
- 26:47For sure.
- 26:48Yeah.
- 26:48Cool.
- 26:49All right.
- 26:49So people can check that out.
- 26:50It's for free.
- 26:50We'll put that in the show notes.
- 26:51Now, I think that this both has really positive or really big possibilities for data science, but also potential challenges.
- 27:02Let me throw it out to you and then you, you take us through it.
- 27:04Super benefits, incredibly obvious.
- 27:06You have an event stream that tells you over time what times the things happened.
- 27:11You have, both the additions and subtractions or the permutations that it goes through until it ends up in its final state.
- 27:17Not just show me all the customers from California who bought this month, but like show me all the Californians who abandoned the cart, but then came back and then did the, you know what I mean?
- 27:26Like you can just answer way more interesting questions.
- 27:29You got time series.
- 27:30On the other hand, maybe I would just want to load up a pandas data frame with the answers of what's the average cart size during checkout.
- 27:38And that, that becomes like a big computation out of an event source based database.
- 27:44If you don't have one of things.
- 27:45Okay.
- 27:45Well, let's hear it.
- 27:46Let's hear it.
- 27:47I'm going to say it.
- 27:47Like if you, if you don't do one of those caching or multi-database things or the QRS, I don't remember the patterns.
- 27:56Anyway, that looks like it maybe is a little bit of a challenge.
- 27:59You could do it.
- 27:59I think you could do it more easily in pandas, but like maybe I just, you know, some people do data science just through SQL and they just, I'm just going to write queries against a warehouse database, you know?
- 28:09Yeah, absolutely.
- 28:10Why not have it both ways?
- 28:11For example, on my, okay, so let's start the, for the hardcore data scientist, actually, you know, I don't even think the event store is the right format for them.
- 28:21You know, I would definitely have some kind of script that would run on some kind of loop that, you know, maybe every day or every couple hours or whatever would transform the raw events into some format that would be great for.
- 28:34Sure.
- 28:35And you hear about all these like OLAP cubes and all these other like super BI type of systems.
- 28:40None of those, no, no, I can't say none of those.
- 28:42Many of those are not running out of the operational database.
- 28:45They're like a, some kind of like warehouse data lake.
- 28:49We've transformed this so you answer questions.
- 28:51So it's not necessarily just event sourcing.
- 28:52Like we just want to avoid five joins so we can just ask the question directly, right?
- 28:57Yeah.
- 28:57Yeah.
- 28:57And in fact, my current project that I have in production at work, it is a service that multiple other services use to process items.
- 29:07And the project manager of one of these services reached out to me and said that they have a BigQuery table that has all this analytical information and they wanted to add the information we have to theirs.
- 29:19And so, you know, we set up a conversation.
- 29:22I created the code and every day I'm sending information to their BigQuery instance.
- 29:26And three days after we did a go live, you know, I created a meeting to like kind of circle back with them to make sure everything's working the way they wanted.
- 29:34And when they opened up the BigQuery database, they were shocked because they were expecting three days worth of data.
- 29:40I had, I set every piece of data I had for months, which was how long they've been sending things to my service.
- 29:48And so, like, I was just, I just, you know, this person was elated because they were like, they knew their data scientists wanted this information and they, now they have all this information going back to day one, so to speak.
- 29:59And also just recently, my boss asked me, like, I have a reports view that's just a webpage that has like how, stats on how my service is doing.
- 30:07And he's like, it'd be nice to have like, some, like a table of some of these, you know, last few days or whatever.
- 30:12And I was like, okay, how many, how many days would you like?
- 30:15And he's like, 30 days.
- 30:16So I created, the HTML was pretty easy and I just created a script to like pull the events out of the event store and populate this table, you know, as exactly as we needed.
- 30:25It went into production live and we were immediately there with 30 days of history.
- 30:28It was, it was so exciting.
- 30:29And, and like, this is what I get to experience every week is like, you know, having the ability to like, go back into history and answer questions that we've had that we didn't even think we knew.
- 30:38We didn't have any idea that we would want to know, you know, a month ago.
- 30:42And to be able to answer those questions with precision is, is intoxicating.
- 30:46Yeah.
- 30:46I certainly see the value.
- 30:47Like you don't necessarily know the questions you're going to ask.
- 30:50And if you don't have enough data or you don't store it in the right way, you literally can't answer them.
- 30:56Yeah.
- 30:56Right.
- 30:57But with, it sounds like with, events or so, you can go back and like, well, what if we ask this over time instead of by region?
- 31:02Like, okay, slightly different query, no problem.
- 31:04Exactly.
- 31:05Yeah.
- 31:05Yeah.
- 31:05It's, it's really quite something.
- 31:07I had no idea.
- 31:09Like my, I kind of mentioned earlier, my biggest thing was, I was, I can't wait to have fast UI.
- 31:15And now that I realized that I feel like our applications obviously serve the primary purpose of whatever it is that the business needs, but I didn't realize how much there was a secondary need of understanding how it works and enabling the business to make decisions
- 31:29based on how customers are actually using the application.
- 31:32We're going to talk about the AI side later, but I do just want to throw out as different constituents who might care to answer these questions.
- 31:38Like I was just thinking, you've got, you've got the operational side of say the website or app or, you know, driving an API for the app or something like that.
- 31:46That's one view.
- 31:47That's kind of the traditional view, but now you have this much more increasingly popular view of like data scientists and BI tools and the CEO wants a dashboard that updates live type, you know, so events are a clear trigger for those kinds of things, right?
- 32:01Absolutely.
- 32:01But then also you might ask your AI Opus or Codex or whatever, hey, find me some trends or let's look at this and, you know, it has more to work with as well, right?
- 32:11Yeah.
- 32:12Just thinking of the different constituencies, yeah?
- 32:14Totally.
- 32:14In fact, just today I was looking into a bug that was happening in production and I asked Claude, hey, can you query the GCP logs?
- 32:23Can you query the event store and help me understand what's going on?
- 32:26And it was like, sure enough, here you go and made fixing the bug much easier.
- 32:32This portion of Talk Python is sponsored by Temporal.
- 32:36Ever since I had Mason Egger on the podcast for episode 515, I've been fascinated with durable workflows in Python.
- 32:43That's why I'm thrilled that Temporal has decided to become a podcast sponsor since that episode.
- 32:48If you've built background jobs or multi-step workflows, you know how messy things get with retries, timeouts, partial failures, and keeping state consistent.
- 32:56I'm sure many of you have written brutal code to keep the workflow moving and to track when you run into problems, but it's trickier than that.
- 33:03What if you have a long-running workflow and you need to redeploy the app or restart the server while it's running?
- 33:09This is where Temporal's open-source framework is a game-changer.
- 33:13You write workflows as normal Python code and Temporal ensures that they execute reliably, even across crashes, restarts, or long-running processes while handling retries, states, and orchestrations for you so you don't have to build and maintain that logic yourself.
- 33:27You may be familiar with writing asynchronous code using the async and await keywords in Python.
- 33:33Temporal's brilliant programming model leverages the exact same programming model that you are familiar with but uses it for durability, not just concurrency.
- 33:42Imagine writing awaitworkflow.sleep timedelta 30 days.
- 33:47Yes, seriously, sleep for 30 days.
- 33:49Restart the server, deploy new versions of the app.
- 33:51That's it.
- 33:52Temporal takes care of the rest.
- 33:53Temporal is used by teams at Netflix, Snap, and NVIDIA for critical production systems.
- 33:58Get started with the open-source Python SDK today.
- 34:01Learn more at talkpython.fm/Temporal.
- 34:04The link is in your podcast player's show notes.
- 34:07Thank you to Temporal for supporting the show.
- 34:09Yeah, I guess you know why.
- 34:11You have more granularity on what, if the thing in the database doesn't look like you expected, you much, have a much more granular way of knowing like it was this step that made it look like that because I've had problems before where I completely
- 34:26upgraded, swapped out the data access layer for Talk Python training for the courses.
- 34:32Yeah.
- 34:32And for the website, it was perfect.
- 34:33Everything was great.
- 34:34But under certain circumstances on Android, the app was resulting in something, it was sending in something that would make the data not right, right?
- 34:43Like there was some field that was null instead of just taking on the default value.
- 34:47Oh, man.
- 34:48Which is fine.
- 34:49But then when the person logged in on the website, the website didn't assume that that thing could be null because it was, at a minimum, had a non-nullable default value.
- 34:57I'm like, why do we need to check this for null?
- 34:59How did it get to be null?
- 35:01It makes no sense.
- 35:01It took forever to figure that out.
- 35:04Oh, wow.
- 35:04But with event sourcing, you could see this was the event that made it null.
- 35:08Not just, it is null.
- 35:09What in the world is going on?
- 35:11Why could it, how could it possibly be null?
- 35:13Yeah, absolutely.
- 35:14So I think it's got some interesting debugging.
- 35:17And one more thing, like I know this is quite the data science side, but another constituency could be PCI, HIPAA, GDPR, like all the compliance frameworks you got to deal with for auditing or sort of audit trail
- 35:32or something that happens.
- 35:33I mean, a lot of times logs serve that value, but that might be a, they updated the record like, oh, what?
- 35:38Yeah, totally.
- 35:39Yeah.
- 35:40And that, you know, I, even though I've been in insurance and I've been in healthcare, I haven't had anything where I have to certify these things, but like you've, the audit log is the way you interact with everything.
- 35:51It is the source of truth.
- 35:53And so, but what's funny is I have worked on teams that created history tables to try to essentially do that work.
- 36:00and it was like two or three months after I started working there before I learned that that table existed.
- 36:06And so, there were two or three months of work I should have been putting in the history table that I didn't.
- 36:12And from what I hear among other developers, a lot of teams work that way.
- 36:17Like, only a few people really know and understand how to maintain that history table.
- 36:22And a lot of times, like when they try to replay it, it just doesn't work.
- 36:25And it's, it's unfortunate.
- 36:26Yeah, it's like, well, there is history in the history table.
- 36:30When we run it again, we don't get the same output as the final database.
- 36:34What's going on?
- 36:34Yeah, true.
- 36:36Yeah, but with the event sourcing, it reverses it.
- 36:38Basically, the events are the source of truth and the other one is some kind of dynamically generated sort of deal, yeah?
- 36:44Yeah, yeah.
- 36:45And it's a lot like, you know, a backup strategy.
- 36:49You know, if you never test your backup strategy, you don't really.
- 36:52Exactly.
- 36:52And I feel like it's the same thing with the history table.
- 36:54And honestly, to be totally honest, event sourcing is similar in that it's easy to accidentally migrate event versions.
- 37:01You know, like for myself, I was working on a new event to kind of, you know, on my app and I introduced a new attribute or actually, I guess it was a full, whatever the point being is, at some point I decided I wanted to change
- 37:16the name of the attribute because it would reflect better what it meant in the domain and not realizing that I had already published that event to production.
- 37:24And so at one point I was, I don't know, I don't remember what I was doing, looking up issues or honestly, it might have been a view that it was rendering that was supposed to be for with throwing errors and I couldn't understand why and I looked at it and sure enough, it was because I accidentally created a different version
- 37:39of the same event.
- 37:40Thankfully, all I had to do was change the code to say, well, if this attribute doesn't exist, look for this attribute and everything was fixed.
- 37:47But, you know, you can honestly fall into some of those things with event sourcing too if you're not careful.
- 37:51But the nice thing is because the events are still there, you have the ability to recover from them.
- 37:56Let's talk about versioning for a little bit.
- 37:58Sure.
- 37:59On a sort of operational third normal form type of database, you know, you might run a migration.
- 38:04One of the reasons I really like using MongoDBs because I almost never have to run migrations, but that's a different, it's a different debate.
- 38:11However, you might run the migration to say like, okay, we're going to add a column or we're going to split this data apart and move this stuff over here and that over there and then create a foreign key relationship or whatever.
- 38:23Yeah.
- 38:23But I can see if you've got this kind of history of things.
- 38:26Like, let's say, I don't know, how do you deal with versioning, right?
- 38:29Like, I've got these old events and the way you're not storing the current state.
- 38:34So with the migration or something like that, you're like, well, let's just transform the current state into the new state.
- 38:38With these, you've got like old events and new events and they might be in a real way incompatible.
- 38:45Yeah.
- 38:45Yeah, sure.
- 38:46What do you think about with that?
- 38:47You have so many strategies.
- 38:50You just have to choose which one works for your situation.
- 38:52So the first one is kind of what I mentioned just a minute ago, kind of like the MongoDB or I should say document database way of working with things where if you're adding things to an event, adding fields, then if, you know, if the code, you know, most code will be blissfully ignorant
- 39:07that you're adding new attributes to it.
- 39:09So it doesn't really matter.
- 39:11And then those that do care can kind of have fallbacks.
- 39:14And the way that Adam and Martin suggest, you essentially have like upcasters or some kind of code that essentially says like, okay, the previous version, actually, that's two different things.
- 39:25I'm sorry.
- 39:25The code that does care about the new attribute, if it encounters an older event that doesn't have that attribute, then you can have a default fallback.
- 39:32And it's best to have those close to, you know, the domain logic that you want to update.
- 39:37This is a vertical slice architecture approach as opposed to having like a global upcaster.
- 39:41Again, I realized that I want to keep going to the upcasting, which is the second option that I've been trying to get to.
- 39:47But so essentially one is, you know, have a default fallback.
- 39:50Second one is to have an upcaster that says, okay, you know, especially if they're two different versions, like truly different versions of the event, you know, you have add to shopping or item added and item added version two.
- 40:02You know, you might have completely different fields.
- 40:03It's good to have a piece of code that can, you know, upcast to the second one.
- 40:08You put that in your, like your data access layer.
- 40:11It might write a thing and say, give me all the items of the cart and it looks at the type or the version flag and then does a little processing or would you rewrite the database?
- 40:20Oh, well, that's, I was going to say that's your third option.
- 40:22So let me take a step back and ask your first, your last question, which was like, at least to me, the tent, what you're doing with your events is essentially rebuilding state.
- 40:30And so wherever you're in that loop to rebuild the state, you would probably say like, oh, is it this event or is it this event or whatever behave this way?
- 40:37Right there.
- 40:38Like in my story of using one of the data caches, you do a request, it's not in the cache.
- 40:44So you've got to run your build it up code and put it in the cache.
- 40:46And that just, I mean, that part right there would be the part that goes, okay, we're going to transform it differently now and then we'll still store the answer in the cache and the next time they ask, it's just fast.
- 40:55Yeah.
- 40:56So yeah, you would have, the way I would code it is you'd have a piece of code that's listening into the events.
- 41:02So you have, it knows about the old version of the event and the new version of the event.
- 41:06And it knows what format that data needs to be in in that Redis cache or whatever it was that you suggested.
- 41:13Yeah, Valkey.
- 41:13Come on, Valkey.
- 41:14Valkey.
- 41:14Let's go, Valkey.
- 41:15I'm going to remember this by the end of the conversation.
- 41:18So yeah, so what it does is it will be able to say like, okay, this event is an old one, but I can upcast it to a new one and convert it into the format or however we need to save it to Valkey.
- 41:28And then if it's a new one, it also knows, okay, I'm going to convert it this way to save to Valkey.
- 41:33So it's nice that it's kind of localized to, you know, the process that you need to update, upcast.
- 41:39And then we've touched on like kind of the quote unquote nuclear option, which is to apply a transform to your entire event store.
- 41:46And so you would, by doing that, you would create a new database from your database table, which would be your new event store.
- 41:50And you, for each event, you just copy it over, do some kind of map or transform to upcast the entire, your entire history.
- 41:58Yeah.
- 41:59I see values, value in all of them.
- 42:01You know, if you're doing a lot of direct SQL data science-y stuff, you probably want to transform the database.
- 42:06If it's primarily coming out of an API, like just let that thing handle it as it reads them, you know, computers are fast.
- 42:12Keep it in mind.
- 42:13Indeed.
- 42:13And I forgot to mention that, oh, his name escapes me right now.
- 42:16The guy who popularized event sourcing back in the 2000s wrote a book on this topic specifically.
- 42:22So he kind of listed out all of your strategies and when you would want to choose them and why.
- 42:27Are we talking Martin Dilger?
- 42:29No.
- 42:30Greg Young.
- 42:31Greg Young.
- 42:32Okay.
- 42:32I think it's on LeanPub.
- 42:34Yeah, that sounds about right.
- 42:35In fact, Martin Dilger's book is cheaper on LeanPub as well and you git additional content there too.
- 42:39Okay.
- 42:40Yeah, very interesting.
- 42:42Let's, let's make this a little bit concrete for people.
- 42:46Sure.
- 42:46We talked about how you might, in theory, architect some software, could be Python or something else to follow these patterns, but there is a Python library, right?
- 42:55Yeah, absolutely.
- 42:56Do you recommend it?
- 42:57I do.
- 42:58You know, I, it's funny, I don't have any production code with it, but I have used it a lot over the years.
- 43:04John Bywater has done an incredible job maintaining this, this repo and, you know, all his, all the people who have contributed as well.
- 43:12He has shepherded this through and is, has been really making it such an incredible application.
- 43:18When I wrote my applications, you know, I, the first one I did, I was like, I want to do it myself so I can understand it.
- 43:26And so I can really understand and respect what he's done.
- 43:29But also, you know, there's a part of me that really, like a lot of the, you know, essentially I feel like it depends on who you are.
- 43:35If you're somebody who wants to grab a framework and run with it, this is an exceptional one to do it with.
- 43:40It, essentially, you just write Python classes and you decorate them or subclass from some of his classes and all the magic of event sourcing happens for you.
- 43:50And it just make, leaves you with really readable, understandable code.
- 43:54And then you'll have other people who, you know, a lot of people in the event sourcing space says it's actually not that complicated.
- 44:00You can write your own code to do it.
- 44:02And I did.
- 44:03And I recommend it for the right type of person.
- 44:06For me, it was hard because there's so many decisions that you need to make.
- 44:09And I am not the best Python programmer.
- 44:11I do not know all the concurrency issues and all these things.
- 44:15I'm getting to learn them more.
- 44:17But, you know, I have software that's running in production that's, that's doing well.
- 44:20So all that to say is, yeah, I highly recommend this package, especially if you're new to it.
- 44:25It can really show you, you know, how you, one option of, of things can work.
- 44:30And I love that it, by default, it uses, well, you can, you can use SQLite, Postgres, or one of the, a couple of the doc, databases that are optimized for event sourcing.
- 44:42And so you can kind of see how it's, you know, some of the many ways you can pattern things to make it easy for you.
- 44:48Interesting.
- 44:49So I probably need to file a PR or something here.
- 44:52It says, the way you get it is pip install event sourcing.
- 44:56I feel like it should be pip install event sourcing.
- 44:58No, just kidding.
- 44:59But that's cool.
- 45:00However, I am, you know, so it's, you know, you can get it off PyPI, but I'm having a hard time resisting pressing this.
- 45:07Ask DeepWiki.
- 45:08Have you ever gone, what is DeepWiki?
- 45:10Yeah.
- 45:11It's a AI powered document thing that he opted into, which I thought was very fascinating.
- 45:16I, I was following, I was at the time he did it.
- 45:19I was, I think I was writing my own code.
- 45:21And so I had Slack open.
- 45:22He has a good Slack channel and was like showing all the things that he was able to, to all the insights that was able to be gleaned from it.
- 45:30This is epic.
- 45:31I love it.
- 45:31So it had, the DeepWiki apparently is like, and knows the source and the docs.
- 45:37And then it's just a chat.
- 45:38And even on fast, I asked, I said, give me an example of using this library.
- 45:41So sure.
- 45:42Here's a complete example of dog school application, all the code using event source, the light, the package.
- 45:48This is nuts.
- 45:50Yeah.
- 45:51Side quest unlocked.
- 45:53Must figure out how to get my packages into DeepWiki.
- 45:55This is nice.
- 45:57And I also want to add, he has other companion packages that for example, connect into Django and I believe Flask and some other ones too.
- 46:05So one of my side projects, I'm leveraging this with Django and it's really cool because one of the things that enables you to do is configure your events table to be similar to your,
- 46:19I guess in the same database as your Django table or at least configurable from the way Django would do it.
- 46:24And yeah, so getting all these read models are very easy with all the migrations.
- 46:28You just say, this is what I want my data to look like with Django and of course apply migrations and there it is in production.
- 46:34So that's really nice.
- 46:35Yeah, cool.
- 46:36It also has extension projects.
- 46:38What are these?
- 46:39The Django one, the KurrentDB, K-U-R-R-E-N-T.
- 46:43I imagine KurrentDB is probably a...
- 46:45I believe that is, yeah, it's one of the first event sourcing specific databases.
- 46:51I think it was called...
- 46:51Oh, it's for intelligent and responsive systems.
- 46:55I don't know, Chris, I just got to rant a little bit.
- 46:57Like there's all these projects that are cool and they do neat stuff and now I feel when I go to them that it's like, this is the AI compute data frame or this is the AI, the intelligent AI.
- 47:06It's like, it's just a database or just a data frame that AIs can use.
- 47:10That doesn't make it an AI data frame, you know, it's like, but they all want to capture the excitement.
- 47:14It drives me crazy.
- 47:15Yeah.
- 47:16And what's worse is when they don't even say exactly what they do.
- 47:18It is, it is your answer for AI-ing the thing that we're not going to tell you.
- 47:22I hate it.
- 47:23Yeah.
- 47:24Yeah, exactly.
- 47:24And it just obscures what the heck it is, but it's the H1 and the H2.
- 47:27You're like, oh my gosh.
- 47:29Yeah.
- 47:29Yeah.
- 47:30Okay.
- 47:30But that does look pretty interesting.
- 47:32Like, yeah, look, it's example is create a client, new event data, new UUID, et cetera, order place, serialize.
- 47:39So right here, this example is basically it's got primary key, a category or type of event, like a, just an event, I guess is the way you would call it.
- 47:48But then it has JSON serialized, like a JSON blob.
- 47:53That is the details of the event.
- 47:55Is this how you typically do it?
- 47:56I would say so.
- 47:57Or is it more column oriented where like this one has an order ID and a total.
- 48:01So you might have an order ID and a total in the data structure or is it in a blob level?
- 48:06Yeah.
- 48:06The implementations I've seen generally will have some kind of blob or JSON serialized or bytecode serialized optimization of it.
- 48:17You know, because each event, you know, when you're saving things to the database, you know, you're going to save an event.
- 48:22It's going to have an event stream.
- 48:23It's going to have generally speaking, there's probably an event version.
- 48:26Like there's all these specific things, but the actual payload of the event.
- 48:29If there's not an event version, you're going to wish there was an event version at some point probably.
- 48:33Exactly.
- 48:34Yes.
- 48:34And so the payload is usually some kind of blob or JSON body or something like that.
- 48:39It sounds very good to be a document database.
- 48:41Indeed.
- 48:42Yeah.
- 48:43Yeah.
- 48:43Because you can put indexes on like the sub items and then if they're not in that event, it just doesn't use the index for those particular ones.
- 48:50I mean, it's a lot of things.
- 48:52Yeah.
- 48:52Yeah, exactly.
- 48:53It's pretty sweet.
- 48:54Yeah.
- 48:54And again, well, I haven't said it quite, but like one of the things I have been thinking about for a decade is the more I kind of thought about it, the event sourcing and the patterns and unlocks really gives you so much flexibility.
- 49:07You know, you can use document data stores and like really take those, the power of that.
- 49:13You know, if you have, and part of this is too is just data, you know, vertical slicing or whatever, it's both multiple patterns put together.
- 49:19But like, you know, if you have a view that would be so much better served by having a graph query, a graph database, then use it.
- 49:28You know, it's, I remember at one time it took me a while, but like somebody told me that they were using, I can't remember their, their open query.
- 49:39Is that what it's called?
- 49:40The, I forgot the old name of what I'm trying to think of, but essentially it's, you know, like a database that optimizes for saving text.
- 49:47So you can like search for it.
- 49:49You know, they use that as just for one, you know, to serve the purpose of one item.
- 49:55And honestly, this isn't unique to event sourcing.
- 49:57You can do this with event-driven architecture as well.
- 50:00But what I love about event sourcing is like you have the benefits of event-driven architecture and the benefits of a monolith in one if you choose to go that way.
- 50:08And yeah, it's just, it's, I guess really what it comes down to is what I love about it and was surprised by is how flexible it gives you the ability.
- 50:16Yeah, in the book, it reminds me, one of my users was complaining about the status screen that I show for the users and he had all these great ideas and I was like, you know what?
- 50:27I want to take advantage of that.
- 50:29So I actually cloned my, the vertical slice for that view and created a new database column or collection for that, to power that view.
- 50:39and we iterated and iterated to make this thing better and with each iteration, sometimes I needed to change how the read model reacted to events and so I could just blow away the read model, regenerate it from events and we ended up with something really great so that when it
- 50:54was time to go live, I just changed which, where the URL went, pointed to the new one and was able to delete the old code and delete the database table and it was wonderful.
- 51:03Yeah, it just gives you so much flexibility to do whatever you need.
- 51:08So, a couple more, one more, I guess one more really relevant thing, two more things to give a shout out with this event sourcing.
- 51:14There's event sourcing Django which is Python package for it with Django.
- 51:18Imagine that probably somehow it upgrades with the ORM, don't know.
- 51:20But also event sourcing SQLAlchemy which is kind of cool.
- 51:23So if you use SQLAlchemy, yeah, very nice.
- 51:25All right, so this stuff is great but I imagine that it has times you should use it maybe more and times you should go, well, square peg round hole maybe not this time.
- 51:36Sure, yeah.
- 51:37What do you think?
- 51:38For me, I feel like it's usually the way I think about it first is because most people are very comfortable with not using event sourcing, right?
- 51:45And so I usually answer it the opposite way which is when should you?
- 51:48Sure, exactly.
- 51:49The two biggest, the best piece of advice that I heard over the last decade was number one, use it, a good opportunity to use it is if you have a database column called status because if you have a columnated status then that means that
- 52:03one item can be in multiple different statuses, right?
- 52:07Different states and if you're having different states each states behave different in some way or form and so you are definitely not dealing with true CRUD create, read, update, delete patterns and so event sourcing would be a great option for that.
- 52:20The second piece of advice is do you ever, are you ever concerned about losing data?
- 52:25Because by default event sourcing does not and what it enables you to do is choose when to lose data, right?
- 52:30Because you don't have to keep every event around forever.
- 52:32You can just say like after 90 days let's just put it to cold storage or just delete it, you know, it depends.
- 52:37Yeah, exactly.
- 52:38Out in the audience Mike says, I'm scared of the physical storage requirements of this potentially.
- 52:42I guess it depends how many data, how many events make up a final state in your system.
- 52:47Like a cart checkout, big deal?
- 52:49No, probably not.
- 52:50Like if used as an app log, that might be a problem.
- 52:54Yeah, yeah.
- 52:54Most, I'll say models, will have maybe a dozen events, maybe two, depends on your, obviously depends on your domain.
- 53:03But ideally, you will keep your events short and they have practices called closing the books where you will use events in your domain to kind of keep it short.
- 53:12So like, for example, a store will want to know their revenue across the entire year, but every night they shut down, they get their cash registers or if they still have cash registers and they kind of reconcile how much money they made that day.
- 53:25And so, you know, kind of keeping your event stream short really helps.
- 53:29If you're going to go back, you would just say, well, we'll just read the daily summary and then add today's events or something like to get the final output, something like that.
- 53:37Yeah.
- 53:38Unless you want to go all the way back to day one, in which case you can, you know, read and say like, okay, the, you know, it all depends on how you want to do it, right?
- 53:46This is again, the flexibility side of it because you could just like, say, start from today and read forward or you can start from today and say, okay, what was the event stream before this and read that and keep going back to the originating?
- 53:57And I think you put up Mike's comment that said, I'm afraid of the physical storage requirements.
- 54:02And it's like, that is the trade-off.
- 54:04There is, it will take more space, but thankfully, storage space is the cheapest commodity in all of online or, you know, in today's world.
- 54:15And it's, and most expensive is memory and then compute and then storage and then bandwidth and then storage.
- 54:20I think that's probably the breakdown, right?
- 54:22Yeah, I think so.
- 54:24And why things like disc cache are awesome versus like another thing that's just in a memory cache, but another process, right?
- 54:29Like, exactly.
- 54:30Yeah.
- 54:31And having the ability to say like, you know, we, you know, like my current application, I have not yet deleted any events, but truly like the only reason we have events older than even a week are just for analytical purposes and, and just me understanding how our system works.
- 54:45And so I'm planning to make a way to offload that event or those events.
- 54:49And a lot of people just put them straight to cold storage, you know, just so that they always have a backup just in case, but, you know, chances are they rarely ever use it.
- 54:57Interesting.
- 54:58And one other thing I did want to add is to go answer your question when not to use event sourcing.
- 55:02exactly.
- 55:03Would be essentially like, you know, so let's say you don't care about losing data.
- 55:08There are just a number of just simple applications that are truly crud, right?
- 55:12Like I've worked on a number of these where they're just forms over data.
- 55:16It's exactly the term I was thinking, forms over data.
- 55:18Defining that for people if they don't know.
- 55:20Yeah, it's essentially something where like in my case, one of the first ones I worked on is like you have a web page that almost exactly mirrors the database table that you're saving the data to.
- 55:30You know, maybe it's a contact form.
- 55:32Who knows what it could be?
- 55:33You know, the idea is like there is so the web UI or whatever you're building is just an easy way to get data into the database.
- 55:41And chances are you don't have status field.
- 55:44You don't have all these different ways of different rules for how things behave.
- 55:49And in fact, in my event source application, I have a model that is not event sourced.
- 55:54It is truly a crud model.
- 55:56And so just by saying, you know, adopting event sourcing doesn't mean you have to do it for everything.
- 56:00You can use it for even just a small bit of your project, especially if you want to try it out and see how it could be.
- 56:05That's a really good point.
- 56:06It's not an all or nothing sort of thing.
- 56:07Because you have a properly factored data access layer and you're not doing that inside of your Jinja template, are you?
- 56:15No?
- 56:16Right.
- 56:17Not even in your view, but like you've got just an opaque layer of actions.
- 56:22Some of those actions can be driven by events.
- 56:25Some of those actions can just be straight crud.
- 56:27Create, read, update, delete for those who don't know.
- 56:29One of the people who inspired me to really dig into event sourcing, he has a line of business that, well, he'll go to a company who is struggling because their database schema is holding them back.
- 56:42You know, for whatever decision they made, they cannot, they're having such a hard problem, hard time creating a new feature because of their database schema.
- 56:50So he goes in, teaches them event sourcing and uses the event sourcing event store to publish both the dream schema that they wish they would have and the old schema.
- 57:01And they live side by side and the event, you know, once the features are complete, they'll, you know, put it up and they'll start slowly migrating traffic over to the new event source version and, you know, eventually they can delete the old database table or schema, you know, database.
- 57:15And most of the teams he's worked with have kept with the event source version and gone on from there.
- 57:21Yeah.
- 57:21Oh, and then finally, one other thing I want to mention too is when not to use is it's up to your teammates because, you know, I am sold.
- 57:30I think this is such an incredible pattern.
- 57:33It is just unlocking so much joy again and so much flexibility as I've said before that I cannot imagine having to go back.
- 57:41That said, if I join a new team and my team members are like, I don't know, I'm going to go with them, you know.
- 57:48Yeah.
- 57:49One thing is to not use an optimal pattern.
- 57:52What's worse is to try to use an optimal pattern but have nobody else want to do it and then they work around it and you, you know, it sounds a little similar to like people who don't want to do unit testing.
- 58:02Yeah.
- 58:02So some of the people write the unit test and they set up CI/CD that'll fail if the unit test failed but then the other people will check and work without running the test at all and then they break it and you're like, what are you doing?
- 58:11Like, well, I don't want to run these crappy tests.
- 58:13You're like, well, now the whole CI/CD is not just not helpful.
- 58:16It's inhibiting me working because you won't even, you know what I mean?
- 58:19It's just like, and it seems like you do need a certain level of buy-in for this to make sense.
- 58:24Absolutely.
- 58:24Yeah.
- 58:25And maybe they should listen to this podcast and they can see it.
- 58:28And maybe you create an example of one feature in an event sourced way so they can see some of the benefits.
- 58:36But, you know.
- 58:37Yeah, yeah, yeah.
- 58:37Like your partial example, indeed.
- 58:39Yeah.
- 58:39All right.
- 58:40We're getting short on time here, Chris, but let's talk this AI flow.
- 58:45First of all, let's circle back to your comment of your company having a mandate to use AI.
- 58:52What the heck is going on here?
- 58:54How is this received and how are you receiving it?
- 58:56And also tell us, are you actually writing, you know, make, shipping more features and be more productive or not?
- 59:03Like what, give us your assessment as much as you're willing to share.
- 59:06Like you don't have to like.
- 59:08Yeah, yeah.
- 59:09I will, I will hide certain things, but to say.
- 59:13Names and places have been changed to protect the parties.
- 59:16Yes, and emotions and conversations with multiple other people.
- 59:20I would say at times I am so much more productive.
- 59:23At times it has brought down the production, you know.
- 59:30So it is a mixed case.
- 59:33I see that Mike in the chat said it's an overconfident intern and I'm like 110%.
- 59:39Like this is exactly what it is.
- 59:40But very smart intern.
- 59:41It is.
- 59:41Oh, absolutely.
- 59:42Very confident.
- 59:43Oh, well, he said that.
- 59:43Yeah, overconfident.
- 59:44And I find this fascinating because my production app is actually three services in one monorepo and I'm responsible for one and, you know, a couple other people are responsible for the other ones, but, you know, we're all interacting.
- 59:59And so my, when I need to change something on my code, you know, and I'm required to use Claude Code, I say this is what I need to do and generally it does a really great job.
- 1:00:08And I think a lot of this has to do with the vertical slice architecture because vertical slices only hold code that is responsible for one feature.
- 1:00:15And so that really fits very nice into a context window.
- 1:00:18Yeah.
- 1:00:19It doesn't have to scan 200,000 lines and 100 files.
- 1:00:23It looks at five.
- 1:00:24Yeah.
- 1:00:24And it knows event sourcing.
- 1:00:26So it knows, okay, I'm subscribing to events.
- 1:00:28These are the events.
- 1:00:29I know where they are, you know, all these different things.
- 1:00:31When I work on one of the other services, it takes a lot more context to understand the state of the code.
- 1:00:38And I really have to work harder to do, to do what I need to do in those, in those parts of the code base.
- 1:00:46Yeah.
- 1:00:46So it's been a very interesting experiment.
- 1:00:48And additionally, kind of when I am, I had to curb this, but when I have been more productive is creating get work trees.
- 1:00:56So it's like, I have a kind of main repo that I work out of.
- 1:00:59And then I, if I have a feature that I, you know, I'm like looking at the code base and like, oh, or the web app or the logs.
- 1:01:04And I'm like, oh, it'd be good to like optimize this.
- 1:01:07Then I create a new work tree and set cloud up in there and get it working on a thing.
- 1:01:11And so I have found that I can only, I need to limit myself to two or three work trees because any more than that, I start losing context.
- 1:01:18And now I know what the LLM is.
- 1:01:20Yeah, exactly.
- 1:01:21If you over, overdo it, it's, you just send off five agents and don't look like that's how you end up like, oh, we have kind of like bugs in our code about architecture.
- 1:01:28Well, you never look at it.
- 1:01:30It's like, we got the super energetic, super smart intern and we kicked him off and said, go on that feature.
- 1:01:35But you know, they need guidance, right?
- 1:01:38All the tests are passing because I changed all the tests to pass.
- 1:01:40I know.
- 1:01:41The problematic data has been removed from the database.
- 1:01:43It works now.
- 1:01:45Why is it empty?
- 1:01:46Yeah.
- 1:01:48Back to your backup comments.
- 1:01:50Honestly, I'm having like an insane amount of productivity with Claude Code and with AI and stuff.
- 1:01:55But it's an engineering skill.
- 1:01:57It is not just, let's fire it up and ask for it.
- 1:01:59Like one of the things I'm doing lately that I'm really appreciating is going through like a planning session, which I know a lot of people do that and like talk about it.
- 1:02:07But, and now, if you have the GitHub CLI installed, just the GH thing, you can tell it, hey, create, you know, instead of just running this plan, create a GitHub issue of this plan, write all the details in GitHub and then your next comment
- 1:02:22can be, let's work on issue 127 and it'll go work on it when it gets done.
- 1:02:26Like, let's make a retrospective comment on the issue.
- 1:02:29Let's create a PR that closes that issue.
- 1:02:32That's, you're like, there's some really interesting team dynamics that you can put in there that, you know, talking to a chat box is not covered, but if you know what, you know what to ask for.
- 1:02:41Yeah, I'm really inspired by Martin and Adam because both of them in one way or another have, let me take a step back.
- 1:02:48They, I mentioned the event modeling diagram.
- 1:02:51It is a visual diagram that really has a reduced visual language and what was mind-blowing to us a couple years ago was that AI understands it.
- 1:03:01And so, the fact that you can essentially say like, here's the diagram, can you implement the slice and it can get you from, well, let me take a step back.
- 1:03:08Martin and Adam have both had successful research spikes where they took an event modeling diagram.
- 1:03:16Actually, no, they even did, what they did even worse was they started with a conversation with a client and recorded it, created the transfer.
- 1:03:23They generated the diagram.
- 1:03:24Generated the diagram and then generated code from the diagram that didn't solve everything but got it, I think, 80 or 85% the way they're in hours from, you know, like cutting months of work down to weeks is impressive.
- 1:03:38And I've had some similar, you know, I'm still working on my personal one because like, I just, you know, after work, I just tend to shut down my computers and I'm not like dedicated to like really going at it but like, I found some really incredible benefits
- 1:03:52of doing something like that.
- 1:03:53that's awesome.
- 1:03:54I created this open source project.
- 1:03:56I mean, it's more source open, whatever, it's not really a project but I called it Python Package Guides for Agents and all the projects that I work on, I'll go and download the source and the documentation.
- 1:04:05So like, if I'm working on disk cache, I'll like literally clone it, clone the documentation and then make Claude write a super detailed, like, not use their documentation or its old version but like, have it like legit, write down examples, study the source code, study the documentation,
- 1:04:20source code like trumps documentation because if the docs are out of date and so on and then I'll drop, you know, if I'm using like two of these like maybe data classes and disk cache, I'll drop those things into my project and tell Claude about them and that's been a pretty neat thing to do as well.
- 1:04:34Yeah.
- 1:04:34But I want to leave this portion of our conversation with an incredible joke.
- 1:04:38Okay.
- 1:04:39Okay.
- 1:04:40Just because I feel like this has to be said right now.
- 1:04:42It's just so, the joke, this is the word copilot, it could be Claude, it could be Codex, it could be Chat, whatever, like just AI, right?
- 1:04:48Friends outside of tech.
- 1:04:50Lol, copilot is dumb.
- 1:04:51Friends in tech.
- 1:04:52In tech.
- 1:04:52I just bought iodine tablets and I've made an offer in land of state.
- 1:04:55My supplies of antibiotics and potable water are sufficient but I need to set up for the hydroponics to make it through the first few years.
- 1:05:01Like I feel like that's where we are, you know?
- 1:05:03Yeah, yeah, totally, totally.
- 1:05:06And that maybe also sums up your meetup as well.
- 1:05:10Yeah, yeah, yeah.
- 1:05:11Yeah, absolutely.
- 1:05:12It's quite a spectrum both of Friends outside tech and inside tech.
- 1:05:17Yeah, exactly.
- 1:05:17Like it's more like believers and non-believers.
- 1:05:19I'm not really sure.
- 1:05:20All right.
- 1:05:21Final Call to Action.
- 1:05:22What do we got here?
- 1:05:24People are interested.
- 1:05:24They want to get started.
- 1:05:25What do you tell them?
- 1:05:26Get your ebook, your free ebook that you put up?
- 1:05:28That's right.
- 1:05:29Yeah, so my website is everydaysuperpowers.dev.
- 1:05:32If you want an ebook that kind of introduces you into event sourcing and kind of gives you kind of this kind of fundamental background and a couple other things, go to everydaysuperpowers.dev slash es intro and it'll take you right there.
- 1:05:46I'm on Mastodon mostly, but I'm also watchbluesky and sometimes x underscore chrismay on all of those.
- 1:05:55With Mastodon, I think I'm on fosstodon.org.
- 1:05:57What else?
- 1:05:59I mentioned everydaysuperpowers, so that's...
- 1:06:01I also have a Discord from there too, so if you go through my website, you can see how you can join that.
- 1:06:07Sweet.
- 1:06:07Maybe check out the event sourcing library.
- 1:06:09100%.
- 1:06:09For them people.
- 1:06:10Yeah.
- 1:06:10Yeah.
- 1:06:11And if you...
- 1:06:12Oh, oh, the...
- 1:06:13Martin and Adam have a podcast called the Event Modeling and Event Sourcing Podcast, which is verbosely named, but it also is really great.
- 1:06:21You know, this is how I learn just from these great people, you know.
- 1:06:25There you go.
- 1:06:26Just kind of every...
- 1:06:27Almost every week talking about patterns they do and stuff like that.
- 1:06:30They also talk about a bunch of other stuff that isn't relevant, but I've learned so much from listening to them and they also have Discords.
- 1:06:37So go ahead.
- 1:06:37Cool.
- 1:06:37I'm honestly impressed.
- 1:06:39Like an entire podcast on a single design pattern.
- 1:06:41Let's go.
- 1:06:42That's commitment to it.
- 1:06:43Yeah.
- 1:06:44Yeah.
- 1:06:44And it's incredible.
- 1:06:45I mean, as someone who has played the board game and really enjoys it, it's amazing.
- 1:06:51Well, Chris, I really appreciate you coming on here and sharing all your experience and excitement and all the things.
- 1:06:56Great to talk to you.
- 1:06:57Likewise.
- 1:06:58Thanks for having me.
- 1:06:59This has been another episode of Talk Python To Me.
- 1:07:02Thank you to our sponsors.
- 1:07:03Be sure to check out what they're offering.
- 1:07:04It really helps support the show.
- 1:07:06This episode is sponsored by Sentry's Seer.
- 1:07:09If you're tired of debugging in the dark, give Seer a try.
- 1:07:12There are plenty of AI tools that help you write code, but Sentry's Seer is built to help you fix it when it breaks.
- 1:07:18Visit talkpython.fm/sentry and use the code talkpython26, all one word, no spaces, for $100 in Sentry credits.
- 1:07:27And it's brought to you by Temporal, durable workflows for Python.
- 1:07:31Write your workflows as normal Python code and Temporal ensures they run reliably, even across crashes and restarts.
- 1:07:38Get started at talkpython.fm/Temporal.
- 1:07:41If you or your team needs to learn Python, we have over 270 hours of beginner and advanced courses on topics ranging from complete beginners to async code, Flask, Django, HTMX, and even LLMs.
- 1:07:54Best of all, there's no subscription in sight.
- 1:07:57Browse the catalog at talkpython.fm.
- 1:07:59And if you're not already subscribed to the show on your favorite podcast player, what are you waiting for?
- 1:08:04Just search for Python in your podcast player.
- 1:08:06We should be right at the top.
- 1:08:08If you enjoyed that geeky rap song, you can download the full track.
- 1:08:11The link is actually in your podcast blur show notes.
- 1:08:13This is your host, Michael Kennedy.
- 1:08:15Thank you so much for listening.
- 1:08:16I really appreciate it.
- 1:08:18I'll see you next time.
- 1:08:18to the show
- 1:08:48Thank you.