Latest / CyberSecurity Summary / Functional and Reactive Domain Modeling
Transcript
- 0:00Speaker 1: It's Black Friday. You spent maybe an hour hunting down
- 0:04Speaker 1: deals on this massive retail site and you finally, you know,
- 0:08Speaker 1: you finally click check out.
- 0:09Speaker 2: Oh yeah, the pressure's totally on, right.
- 0:11Speaker 1: The pressure's on, and the shopping cart just freezes, or
- 0:15Speaker 1: even worse, the page refreshes and just drops your items entirely.
- 0:19Speaker 2: Which is just the worst feeling.
- 0:21Speaker 1: It really is. If you're anything like me, you probably
- 0:24Speaker 1: just stare at the screen and pure frustration.
- 0:26Speaker 2: I mean, it is a totally broken digital promise, right,
- 0:29Speaker 2: You're left completely stranded by a system that just couldn't
- 0:32Speaker 2: handle the reality of, you know, a massive traffic spike.
- 0:36Speaker 1: Yeah, exactly. And yet there are apps and systems out
- 0:40Speaker 1: there that actually don't do this, systems that handle millions
- 0:43Speaker 1: of users at the exact same time without even breaking
- 0:45Speaker 1: a sweat.
- 0:46Speaker 2: Yeah, they just keep humming along.
- 0:47Speaker 1: So today our mission is to figure out the secret
- 0:50Speaker 1: architecture behind those rock solid platforms. We are going to
- 0:53Speaker 1: explore the concepts in a deep dive on functional and
- 0:56Speaker 1: reactive domain modeling by Debasish Ghosh.
- 1:00Speaker 2: Such a great source text, it really is.
- 1:02Speaker 1: And the goal today is to give you the listener
- 1:04Speaker 1: a bit of a shortcut to understanding how modern software
- 1:07Speaker 1: is actually built by merging three really powerful ideas.
- 1:11Speaker 2: Right, So that's domain driven design, functional programming, and reactive architecture.
- 1:16Speaker 1: Exactly, Because I mean, while building massive global systems is
- 1:21Speaker 1: inherently complex, the big secret in software engineering right now
- 1:25Speaker 1: is that if we start treating our code a little
- 1:28Speaker 1: bit more like pure.
- 1:29Speaker 2: Mathematics, it can actually save us from the total chaos
- 1:32Speaker 2: of the real world, which sounds crazy, but it's true.
- 1:35Speaker 1: It does sound crazy. But before we can even start
- 1:37Speaker 1: talking to pure math or you know, preventing server crashes,
- 1:41Speaker 1: we kind of figure out what it is we are
- 1:42Speaker 1: actually building, right. You need a blueprint, We need a blueprint,
- 1:45Speaker 1: and in this architecture, that blueprint is called a domain model,
- 1:49Speaker 1: which is built using domain driven design or DDD.
- 1:52Speaker 2: Yeah, so think of a domain model as a direct
- 1:54Speaker 2: translation of how a business actually works in the real world,
- 1:57Speaker 2: but translated into software.
- 1:58Speaker 1: Okay, makes sense.
- 2:00Speaker 2: But to do that translation accurately, Domain driven design says
- 2:03Speaker 2: you absolutely must start with what they call a ubiquitous language.
- 2:07Speaker 1: Ubiquitous language meaning meaning everyone involved from the CEO to
- 2:11Speaker 1: the junior developer speaks the exact same way.
- 2:14Speaker 2: That's the core of it. Yeah, it means using actual
- 2:17Speaker 2: business terms directly in the code itself. So if you
- 2:20Speaker 2: are building a banking app, developers shouldn't be naming things
- 2:23Speaker 2: with like generic programmer jurgen.
- 2:27Speaker 1: Like user data object or whatever.
- 2:29Speaker 2: Exactly, or transaction hash. You use the vocabulary of the domain.
- 2:33Speaker 2: You use terms like debit, credit, portfolio account.
- 2:37Speaker 1: Right.
- 2:38Speaker 2: The real acid test is whether a business expert, like
- 2:41Speaker 2: an actual working banker, could sit down, look at the
- 2:44Speaker 2: code structure and theoretically understand the.
- 2:46Speaker 1: Workflow because you eliminate that really dangerous translation layer between
- 2:51Speaker 1: the business folks and the programmers, which.
- 2:53Speaker 2: Is where so many bugs usually hide. To be honest,
- 2:55Speaker 2: we totally.
- 2:56Speaker 1: And to build this domain model we rely on a
- 2:58Speaker 1: few specific building blocks, the main ones being entities and
- 3:01Speaker 1: value objects.
- 3:02Speaker 2: Yeah, and the distinction between those two is one of
- 3:05Speaker 2: the most fundamental concepts in domain driven design. So let's
- 3:08Speaker 2: look at entities first.
- 3:09Speaker 1: Okay.
- 3:10Speaker 2: An entity is an object that has a unique identity
- 3:13Speaker 2: that never ever changes, even if its internal attributes due
- 3:18Speaker 2: like a bank account. An account is a perfect example.
- 3:20Speaker 2: It has a unique account number. You might change the
- 3:22Speaker 2: name on the account, or you know, the balance will
- 3:25Speaker 2: obviously fluctuate but it is still fundamentally the same account
- 3:28Speaker 2: because the ID remains the same.
- 3:30Speaker 1: Okay, let's unpack this for a second. An entity is
- 3:33Speaker 1: like it's like a car with a VEN number.
- 3:35Speaker 2: Oh I like that.
- 3:36Speaker 1: Yeah, you can paint the car neon green, you can
- 3:38Speaker 1: completely change the tires, you can swap out the radio,
- 3:42Speaker 1: but legally, because of that gin, it is still the
- 3:45Speaker 1: exact same car.
- 3:46Speaker 2: That is a perfect analogy. Now contrast that car with
- 3:49Speaker 2: a value object.
- 3:50Speaker 1: Right.
- 3:50Speaker 2: A value object has absolutely no identity of its own.
- 3:53Speaker 2: It is defined entirely by its value, which means it
- 3:56Speaker 2: is completely immutable. You cannot change it.
- 3:58Speaker 1: So a value object would be like a twenty dollars bill.
- 4:01Speaker 1: You don't care which exact twenty dollars bill you have
- 4:03Speaker 1: in your wallet. I mean, you aren't tracking the serial number.
- 4:06Speaker 1: You only care that the value is twenty dollars.
- 4:08Speaker 2: Right, if you exchange it for two tens, you haven't
- 4:11Speaker 2: altered your twenty dollar bill. You've swapped it for something
- 4:14Speaker 2: entirely new, got it? Or think about a residential address
- 4:18Speaker 2: in a banking system. An address is really just a
- 4:21Speaker 2: collection of values street, city, zip code. If you change
- 4:25Speaker 2: the zip code on an address, you haven't updated the.
- 4:28Speaker 1: Address, You've created a completely different address exactly.
- 4:32Speaker 2: Because value objects are defined by their values and can't
- 4:35Speaker 2: be modified, they're incredibly safe. You can pass them around
- 4:39Speaker 2: the system freely without worrying about keeping track of some
- 4:42Speaker 2: unique ID or accidentally altering them.
- 4:45Speaker 1: Okay, so we have the raw materials, the entities and
- 4:48Speaker 1: the value objects, but they just sort of sit there, Right,
- 4:52Speaker 1: how do we actually make them do something, like, say,
- 4:55Speaker 1: transfer money between two accounts.
- 4:56Speaker 2: That's where the third element comes in, which are services. Yeah,
- 5:00Speaker 2: services are the higher level actions. They represent the actual
- 5:04Speaker 2: behaviors where multiple entities and value objects interact.
- 5:08Speaker 1: So like the actual transfer process.
- 5:10Speaker 2: Right, a service would be the action of transferring funds.
- 5:13Speaker 2: It's a coordinated operation that involves, you know, pulling from
- 5:16Speaker 2: one account entity, adding to another account entity, and swapping
- 5:19Speaker 2: out the money value objects inside them.
- 5:22Speaker 1: The implication of that structure is really clean, but there
- 5:25Speaker 1: is a nuance to it that caught my eye in
- 5:27Speaker 1: the book. The definitions of these objects aren't actually universal,
- 5:31Speaker 1: are they.
- 5:32Speaker 2: No, not at all.
- 5:33Speaker 1: They depend heavily on something called a bounded context.
- 5:36Speaker 2: The bounded context is so crucial. The meaning of an
- 5:39Speaker 2: object changes entirely depending on the specific module or context
- 5:44Speaker 2: you are working in. Right, Take that address we just
- 5:46Speaker 2: talked about. In our personal banking context, an address is
- 5:50Speaker 2: just a value object. We only care about the text
- 5:52Speaker 2: to print on a bank.
- 5:53Speaker 1: Statement, right, just a string of letters and numbers.
- 5:56Speaker 2: But imagine you are building a mapping application, like a
- 5:58Speaker 2: GPS routing system.
- 6:00Speaker 1: Oh wow, In that context, the address isn't just text
- 6:03Speaker 1: on the page anymore.
- 6:04Speaker 2: Exactly, in a mapping context, that same address suddenly becomes
- 6:09Speaker 2: an entity. It gets its own unique idea in the database,
- 6:12Speaker 2: it gets GPS coordinates, it gets a whole life cycle.
- 6:15Speaker 2: Context totally dictates the design.
- 6:17Speaker 1: So now we have our blueprint, we have our language,
- 6:20Speaker 1: our context, and our objects. But if we go back
- 6:22Speaker 1: to that Black Friday scenario.
- 6:24Speaker 2: Freezing shopping cart.
- 6:25Speaker 1: Yeah, how do these entities interact without the server just
- 6:29Speaker 1: catching on fire when a million people log in at once?
- 6:32Speaker 1: I think we need to talk about the shift from
- 6:34Speaker 1: traditional object oriented design to functional programming. Yeah.
- 6:39Speaker 2: To understand why that shift is happening across the industry,
- 6:42Speaker 2: we have to confront the ghost in the machine of
- 6:44Speaker 2: older architectures, which is mutable state.
- 6:47Speaker 1: Mutable state.
- 6:48Speaker 2: Right, for decades, the standard way to write software relied
- 6:51Speaker 2: heavily on changing data in place. You have an object,
- 6:54Speaker 2: say an account with a balance of one thousand dollars.
- 6:57Speaker 2: When someone withdraws money, the code reaches into that exact
- 7:00Speaker 2: object memory and mutates the balance down to nine hundred, which.
- 7:04Speaker 1: Honestly, it sounds totally logical. You take money out, the
- 7:06Speaker 1: number goes down.
- 7:07Speaker 2: It does until you introduce concurrency. Think about Payday. Millions
- 7:13Speaker 2: of users are accessing the same banking system simultaneously. If
- 7:17Speaker 2: you rely on mutable state, you have an absolute concurrency nightmare.
- 7:21Speaker 1: Because everyone is trying to change things at once.
- 7:23Speaker 2: Imagine two separate server threads trying to access and mutate
- 7:27Speaker 2: that exact same account balance at the exact same millisecond.
- 7:31Speaker 2: Thread A tries to withdraw one hundred dollars, while THREADBE
- 7:34Speaker 2: tries to deposit five hundred. Oh Now, if they're both
- 7:36Speaker 2: trying to overwrite the state in place, they can collide,
- 7:40Speaker 2: overwrite each other and cause massive data corruption.
- 7:43Speaker 1: The system panics, the math gets wrong, and somebody loses
- 7:46Speaker 1: a lot of money.
- 7:47Speaker 2: Exactly, It is incredibly difficult to reason about code when
- 7:51Speaker 2: the data is constantly shifting underneath you. So the functional
- 7:54Speaker 2: programming approach takes a pretty radical stance. We kill the
- 7:57Speaker 2: mutable state entirely. We completely separate state from behavior. State
- 8:02Speaker 2: is stored in what are called algebraic data types or ADTs,
- 8:06Speaker 2: which are strictly immutable. Behavior is handled separately by pure functions.
- 8:10Speaker 1: Okay, let me stop you right there, because algebraic data
- 8:13Speaker 1: types sounds incredibly intimidating. What makes a piece of data algebraic?
- 8:18Speaker 2: I know it sounds super academic, but it just means
- 8:20Speaker 2: you can struct your data types using the mathematical concepts
- 8:24Speaker 2: of sums and products.
- 8:25Speaker 1: Sums and products.
- 8:26Speaker 2: Yeah, So, a product type is an A and D relationship,
- 8:29Speaker 2: a user profile is a name and D an email
- 8:33Speaker 2: and the a password that makes sense, and a sum
- 8:36Speaker 2: type is an ore relationship. A payment method is either
- 8:40Speaker 2: a credit card or a bank transfer or a gift card,
- 8:43Speaker 2: oh icee. By defining data this strictly, the compiler can
- 8:46Speaker 2: actually check your logic mathematically to ensure you've handled every
- 8:49Speaker 2: possible scenario. And crucially, once these ADTs are created, they
- 8:54Speaker 2: cannot be changed.
- 8:55Speaker 1: Wait, hang on, I'm not letting that go. If I
- 8:57Speaker 1: deposit one hundred dollars into my account and the account
- 9:00Speaker 1: ADT is mathematically immutable, meaning the code literally cannot change it.
- 9:04Speaker 1: How does my balance actually go up?
- 9:06Speaker 2: It's a great question.
- 9:07Speaker 1: I might have stuck with my old balance forever.
- 9:09Speaker 2: No. No, The answer is that instead of mutating the
- 9:12Speaker 2: existing account object, the function uses a smart constructor to
- 9:17Speaker 2: generate a brand new copy of the account, but with
- 9:19Speaker 2: the updated balance.
- 9:20Speaker 1: I still don't get how that works at scale, though,
- 9:22Speaker 1: How do you mean? Well, if Amazon is processing a
- 9:25Speaker 1: billion transactions an hour and they have to generate a
- 9:29Speaker 1: brand new clone copy of a massive shopping cart object
- 9:33Speaker 1: every single time I add a single pair of socks,
- 9:36Speaker 1: that has to blow up the servers. Oh right, The
- 9:38Speaker 1: memory consumption would be astronomical. What is the code actually
- 9:42Speaker 1: doing behind the scenes.
- 9:43Speaker 2: You're touching on Honestly the biggest hurdle for people moving
- 9:47Speaker 2: to functional programming. It would blow up the memory if
- 9:50Speaker 2: it were doing a naive total clone, right, But it
- 9:53Speaker 2: doesn't do that. It uses something called structural sharing.
- 9:56Speaker 1: Structural sharing, how does that work?
- 9:57Speaker 2: Think of your shopping cart data as a giant tree
- 10:00Speaker 2: with many branches. You have branches for electronics, clothing, shipping details,
- 10:05Speaker 2: and so on.
- 10:05Speaker 1: Okay, if you.
- 10:06Speaker 2: Add a pair of socks, the system doesn't copy the
- 10:09Speaker 2: whole tree. It creates a new root of the tree,
- 10:12Speaker 2: and it creates the one new tiny branch for the socks.
- 10:15Speaker 1: Oh wow.
- 10:16Speaker 2: But for all the other branches, the electronics, the shipping,
- 10:19Speaker 2: the new tree just points back to the exact same
- 10:21Speaker 2: branches from the old tree in memory.
- 10:24Speaker 1: Oh so, because nothing in the old tree can ever
- 10:28Speaker 1: be mutated, it's perfectly safe for the new tree to
- 10:31Speaker 1: share those older parts exactly.
- 10:33Speaker 2: It's incredibly memory efficient. You are only allocating memory for
- 10:37Speaker 2: the tiny piece that actually changed.
- 10:39Speaker 1: That is brilliant.
- 10:40Speaker 2: But the massive benefit is concurrency. Because that old tree
- 10:44Speaker 2: is never overwritten, it remains perfectly safe. Any number of
- 10:48Speaker 2: concurrent threads can be reading that old state at the
- 10:51Speaker 2: exact same time without any fear of data corruption. The
- 10:55Speaker 2: whole fear of concurrency completely vanishes.
- 10:57Speaker 1: Because nobody is fighting over the same chock. They all
- 11:00Speaker 1: get their own brand new chalkboard and they just share
- 11:02Speaker 1: the erasers.
- 11:03Speaker 2: I love that.
- 11:04Speaker 1: Yeah. So, because our data is totally immutable, something really
- 11:07Speaker 1: magical happens to the functions themselves. They start acting like
- 11:10Speaker 1: high school algebra.
- 11:11Speaker 2: Yes, and this brings us to a concept called referential transparency.
- 11:15Speaker 1: Which sounds intense.
- 11:16Speaker 2: Again, it's simpler than it sounds, I promise. Think back
- 11:18Speaker 2: to basic math y equals f of x. If your
- 11:23Speaker 2: function is to square a number and you input three,
- 11:26Speaker 2: the output is always nine.
- 11:27Speaker 1: Right.
- 11:28Speaker 2: It doesn't matter how many times you run it, what
- 11:30Speaker 2: time of day it is, or what other equations are
- 11:32Speaker 2: running next to it. A pure function will always yield
- 11:35Speaker 2: the exact same output for the same input, and crucially,
- 11:39Speaker 2: it has no secret side effects.
- 11:41Speaker 1: Side effects like secretly writing a log to a database,
- 11:45Speaker 1: or throwing an unhandled exception that crashes the program, or
- 11:48Speaker 1: changing some global variable exactly.
- 11:51Speaker 2: A pure function does nothing except take an input and
- 11:53Speaker 2: return an output.
- 11:54Speaker 1: And because there are no side effects, you get the
- 11:57Speaker 1: substitution model. You can mentally or programmatically just replace the
- 12:01Speaker 1: function call with its output. Right.
- 12:02Speaker 2: If I see square of three in the code, I
- 12:04Speaker 2: can literally substitute it with nine, making equational reasoning possible.
- 12:08Speaker 2: You can logically prove your code works.
- 12:10Speaker 1: The implication here is massive for building large architectures, because
- 12:14Speaker 1: if your functions are pure mathematical expressions, they give you
- 12:17Speaker 1: the ultimate superpower, which is compositionality. You can literally snap
- 12:22Speaker 1: small functions together to build larger behaviors like fusion exactly.
- 12:28Speaker 2: Say you need to map a list of accounts to
- 12:31Speaker 2: calculate their interest, and then map that list again to
- 12:33Speaker 2: deduct taxes. Okay, because those functions are pure, you can
- 12:37Speaker 2: algebraically fuse them together into a single pass. You calculate
- 12:42Speaker 2: the interest and deduct the tax simultaneously, which really optimizes performance.
- 12:46Speaker 1: Here's where it gets really interesting to me. Think of
- 12:48Speaker 1: compositionality like a massive plumbing system.
- 12:52Speaker 2: Okay, plumbing.
- 12:52Speaker 1: If you have a bunch of pure functions, they act
- 12:55Speaker 1: like different pieces of pipe. If the types align, like
- 12:58Speaker 1: if the pipe sizes matge exactly, you can snap these
- 13:01Speaker 1: functional pipes together to create a massive system without needing
- 13:05Speaker 1: to know anything about the actual water flowing inside them.
- 13:08Speaker 2: Oh that's good.
- 13:09Speaker 1: You define what you want to happen, and you completely abstract.
- 13:12Speaker 2: A way to how that analogy holds up perfectly. You
- 13:15Speaker 2: are assembling behaviors based purely on their algebraic contracts.
- 13:19Speaker 1: But pure math assumes a perfect world, a white room
- 13:23Speaker 1: where everything works perfectly. The real world is chaotic. Databases
- 13:27Speaker 1: get unplugged, hard drives get full, network connections drop out
- 13:31Speaker 1: in the middle of a transaction. How do we protect
- 13:34Speaker 1: our beautiful, pure algebra from the messy reality of the internet.
- 13:37Speaker 2: Well, this is where we bring in the third pillar,
- 13:39Speaker 2: which is reactive architecture. Pure logic is great, but software
- 13:43Speaker 2: has to actually survive the real world. A reactive model
- 13:46Speaker 2: relies on a specific three plus one view To achieve
- 13:49Speaker 2: this three plus one yeah. The ultimate goal is that
- 13:52Speaker 2: a system must be responsive.
- 13:54Speaker 1: Meaning bounded latency, like the user gets an answer quickly
- 13:58Speaker 1: no matter what.
- 13:59Speaker 2: Yes. But to guarantee responsiveness, it must be backed by
- 14:02Speaker 2: three interlocking treits. First, it must be resilient, meaning it
- 14:06Speaker 2: responds gracefully to failure.
- 14:08Speaker 1: Okay.
- 14:08Speaker 2: Second, it must be elastic, meaning it scales up and
- 14:11Speaker 2: down based on varying loads. And to achieve both resilience
- 14:14Speaker 2: and elasticity, the foundation must be message driven, relying on
- 14:18Speaker 2: asynchronous communication.
- 14:20Speaker 1: So we have to actively design for failure, which honestly
- 14:23Speaker 1: contradicts how a lot of legacy systems were built.
- 14:25Speaker 2: It does, but at scale, when you have ten thousand servers,
- 14:28Speaker 2: hardware will fail multiple times a day. It is a
- 14:32Speaker 2: mathematical certainty.
- 14:33Speaker 1: You just can't avoid it.
- 14:34Speaker 2: You don't pretend failures won't happen. You design the architects
- 14:38Speaker 2: are assuming they're happening right now.
- 14:40Speaker 1: But how does that work with our pure functional math,
- 14:42Speaker 1: Because in traditional code you see those messy tricatch blocks everywhere.
- 14:47Speaker 1: You know, try to write to the database. If it fails,
- 14:49Speaker 1: catch the error. But earlier you said throwing exceptions breaks
- 14:53Speaker 1: referential transparency. It's a secret side effect that basically ruins
- 14:57Speaker 1: the math.
- 14:57Speaker 2: It does. A tricatch block absolutely destroys the purity of
- 15:01Speaker 2: a function. The function is no longer just returning an
- 15:04Speaker 2: output based on an input. It's blowing up the execution stack.
- 15:08Speaker 2: So instead of mixing messy exception handling into our business logic,
- 15:12Speaker 2: functional programming meets reactive architecture by abstracting failures and latency
- 15:17Speaker 2: into actual data types. We call these monads monads.
- 15:21Speaker 1: Okay, how do they solve the trycatch problem?
- 15:23Speaker 2: Think of a monad as a special wrapper or a
- 15:25Speaker 2: shipping box. Instead of a function directly returning an account balance,
- 15:30Speaker 2: it returns a box called try, a box called try Yeah.
- 15:33Speaker 2: Inside that trybox is either the successful balance or the
- 15:37Speaker 2: failure exception. The function itself never crashes. It always safely
- 15:41Speaker 2: returns the box.
- 15:42Speaker 1: And if the network is just really slow, then.
- 15:44Speaker 2: We use a box called a future. It essentially says,
- 15:47Speaker 2: you know, I don't have the balance right now because
- 15:49Speaker 2: the network is slow, but here is a future box.
- 15:51Speaker 2: Keep executing the rest of the code, and I'll drop
- 15:54Speaker 2: the result in here when I'm done.
- 15:55Speaker 1: Oh wow, That allows the main thread of the application
- 15:58Speaker 1: to just keep moving. The user isn't staring at a
- 16:01Speaker 1: frozen screen because the main thread isn't blocked waiting for
- 16:05Speaker 1: a slow database exactly. But I do have a problem
- 16:07Speaker 1: with this. If we remove all that failure handling from
- 16:10Speaker 1: our pure business logic and we centralize it into a
- 16:13Speaker 1: separate failure handling module, doesn't that just create a massive bottleneck,
- 16:18Speaker 1: like you know, one traffic cop trying to manage all
- 16:21Speaker 1: the crashes for a whole city.
- 16:23Speaker 2: If you used a single global error handler, Yes, the
- 16:26Speaker 2: system would bottleneck immediately.
- 16:28Speaker 1: Okay, so how do they avoid it?
- 16:30Speaker 2: The reactive architecture avoids this by delegating those handlers based
- 16:34Speaker 2: on the specific bounded context we talked about.
- 16:36Speaker 1: Earlier, ah tying it back to the domain blueprint precisely.
- 16:40Speaker 2: The portfolio context has its handler, the transaction context has
- 16:43Speaker 2: its own. Treating exceptions and latency as manageable effects in
- 16:47Speaker 2: their own specific contexts allows our pure functional core to
- 16:51Speaker 2: scale elastically, so it's modular right. The system can spin
- 16:56Speaker 2: up more servers for just the specific context that is
- 16:59Speaker 2: struggling without bringing down the whole application.
- 17:02Speaker 1: To be truly asynchronous and message driven. Like you mentioned
- 17:05Speaker 1: in the three plus one view, it feels like we
- 17:08Speaker 1: have to rethink how our system fundamentally communicates. And I
- 17:12Speaker 1: see developers throw around the terms commands and events all
- 17:15Speaker 1: the time, almost interchangeable now all the time. But in
- 17:18Speaker 1: this architecture they aren't the same thing, are they.
- 17:20Speaker 2: They are drastically different, and understanding that difference is key
- 17:24Speaker 2: to a message driven system. A command is an instruction.
- 17:27Speaker 2: It is an imperative request like debit account, because it
- 17:31Speaker 2: is a request to change the state of the system
- 17:33Speaker 2: in the present or future. It can be rejected the
- 17:36Speaker 2: account might be empty. A command can fail in an event.
- 17:39Speaker 2: An event is a notification of a past change debit occurred.
- 17:44Speaker 2: It is a historical fact because it already happened. It
- 17:47Speaker 2: is completely immutable. You cannot reject.
- 17:50Speaker 1: History, so an event cannot fail exactly.
- 17:52Speaker 2: An event cannot fail, and.
- 17:54Speaker 1: This distinction leads us to one of the most powerful
- 17:57Speaker 1: concepts in modern system design, which is yes.
- 18:01Speaker 2: Think about how traditional databases work. They store the current state.
- 18:05Speaker 2: Let's look at the example of a customer named Bob
- 18:07Speaker 2: Okay Bob. In nineteen eighty nine, Bob opened an account
- 18:11Speaker 2: and lived at address A. In twenty ten, Bob moved
- 18:13Speaker 2: to address B. In a traditional database, you would execute
- 18:17Speaker 2: an update command and overwrite address A. With address B.
- 18:21Speaker 1: You literally erase the history to store the present you do.
- 18:24Speaker 2: But in an event source system, you don't overwrite anything ever. Instead,
- 18:28Speaker 2: the system logs a completely immutable event address.
- 18:31Speaker 1: Change, so it just adds it to a list.
- 18:33Speaker 2: Right, the database is no longer just a snapshot of
- 18:37Speaker 2: the current moment. It is a permanent, append only historical
- 18:41Speaker 2: log of every single thing that has ever happened in
- 18:44Speaker 2: the system.
- 18:44Speaker 1: Wait, if I want to know where Bob lives right now,
- 18:47Speaker 1: does the system have to read his entire forty year
- 18:50Speaker 1: life history just tofind his current zip code?
- 18:53Speaker 2: Theoretically yes. If you take the initial empty state of
- 18:56Speaker 2: the system at times zero and you mathematically apply every
- 19:00Speaker 2: single event in the log sequentially, you will arrive at
- 19:03Speaker 2: the exact current snapshot.
- 19:05Speaker 1: That's crazy.
- 19:06Speaker 2: The entire history of your business model boils down to
- 19:08Speaker 2: one long mathematical equation that.
- 19:11Speaker 1: Is essentially time travel. I mean, if a catastrophic bug
- 19:14Speaker 1: corrupts the current state of the database, you don't panic.
- 19:17Speaker 1: You just wipe the corrupted current state, go back to
- 19:20Speaker 1: the event log, and replay the math right up to
- 19:22Speaker 1: the second before the bug hit.
- 19:23Speaker 2: It makes the system incredibly resilient, and practically systems take
- 19:27Speaker 2: periodic snapshots so they don't have to read forty years
- 19:30Speaker 2: of history every single time you log in. Oh, that
- 19:32Speaker 2: makes sense, but the ultimate source of truth is always
- 19:35Speaker 2: that event log. It completely aligns with the functional programming
- 19:39Speaker 2: ideal of immutable data. You never mutate the past, you
- 19:43Speaker 2: only append new facts to the log.
- 19:45Speaker 1: Let's step back and synthesize all this. Yeah, because we
- 19:48Speaker 1: have covered some serious architectural ground today, we really have.
- 19:51Speaker 1: What we have learned from diving into this domain modeling
- 19:54Speaker 1: approach is that functional programming gives us predictability and the
- 19:59Speaker 1: safety pure mathematical logic. But pure logic isn't enough on
- 20:03Speaker 1: its own. Reactive programming takes that pure logic and gives
- 20:08Speaker 1: it a shield to survive the real world, keeping it responsive, elastic,
- 20:12Speaker 1: and resilient to failure by passing around boxes like try
- 20:15Speaker 1: and future instead of.
- 20:17Speaker 2: Crashing, and by wrapping all of that within domain driven design,
- 20:20Speaker 2: using the ubiquitous language of the business, you end up
- 20:23Speaker 2: with software that accurately reflects the real world and can
- 20:26Speaker 2: scale globally without collapsing under its own weight.
- 20:28Speaker 1: So what does this all mean for us, the people
- 20:30Speaker 1: actually using these systems? I want to leave you with
- 20:32Speaker 1: a final concept to mull over on your own.
- 20:34Speaker 2: Go for it.
- 20:35Speaker 1: We just talked about event sourcing. If the most resilient
- 20:38Speaker 1: modern software is shifting away from just storing our current
- 20:42Speaker 1: status and instead is moving toward building an immutable, permanent
- 20:46Speaker 1: log of every single event and action we have ever taken,
- 20:50Speaker 1: how does an architecture that is designed to remember everything
- 20:53Speaker 1: forever impact our digital privacy and basically the human right
- 20:57Speaker 1: to be forgotten? Wow?
- 20:59Speaker 2: That is a heavy thought.
- 21:01Speaker 1: It's something to think about the next time you update
- 21:03Speaker 1: your address online, or the next time you are sitting
- 21:06Speaker 1: on a retail site on Black Friday and the cart
- 21:08Speaker 1: actually works perfectly processing your order instantly. You'll know it's not.
- 21:12Speaker 2: Magic, it's just really really good math exactly.
- 21:15Speaker 1: Thank you for joining us on this deep dive. We'll
- 21:17Speaker 1: catch you on the next one.