Latest / CyberSecurity Summary / Functional Web Development with Elixir, OTP, and Phoenix: Rethink the Modern Web App
Transcript
- 0:00Speaker 1: You know, I want you to imagine walking into your
- 0:03Speaker 1: absolute favorite coffee shop. You go to the exact same
- 0:06Speaker 1: barista every single day, Right.
- 0:08Speaker 2: They know your order by heart exactly.
- 0:10Speaker 1: Or at least they should. But imagine every time you
- 0:12Speaker 1: step up to the counter they just stare at you
- 0:15Speaker 1: with this totally blank, uncomprehending expression.
- 0:18Speaker 2: Oh, that would be incredibly frustrating.
- 0:21Speaker 1: It would. They demand to see your ID, They pull
- 0:24Speaker 1: up this massive, heavy filing cabinet, verify all your past orders,
- 0:28Speaker 1: cross reference your credit history, and only then, like after
- 0:31Speaker 1: all of that, do they suddenly remember that you just
- 0:34Speaker 1: want your usual large flat white.
- 0:36Speaker 3: It's exhausting just thinking about it, honestly. But you know,
- 0:39Speaker 3: if we connect this to the bigger picture, what you
- 0:41Speaker 3: are describing is exactly how the modern web operates. Yeah,
- 0:44Speaker 3: we've basically built an entire global infrastructure on this massive
- 0:48Speaker 3: illusion called statelessness.
- 0:50Speaker 1: We really have, so welcome to the deep dives today.
- 0:54Speaker 1: We are starting with this exact premise. Every single time
- 0:57Speaker 1: you click a link or send a request, the serve
- 1:00Speaker 1: basically suffers from total amnesia.
- 1:02Speaker 4: It completely forgets who you are, right, it forgets what
- 1:05Speaker 4: you're doing, and it has to frantically ask a database
- 1:08Speaker 4: to reconstruct your entire identity before can send you a
- 1:11Speaker 4: single web page.
- 1:12Speaker 3: And I mean this approach of going stateless it really
- 1:15Speaker 3: is what allowed the web to scale globally in the
- 1:18Speaker 3: first place.
- 1:18Speaker 1: Sure, because it frees up resources.
- 1:20Speaker 3: Exactly by completely forgetting about you. The second and HTTP
- 1:24Speaker 3: response is sent, servers free up their memory to handle
- 1:27Speaker 3: the next user. But the architectural trade off there is severe.
- 1:31Speaker 1: It really is.
- 1:32Speaker 3: It created massive network latency, and it turned the database
- 1:35Speaker 3: into this unavoidable central bottleneck. Every user action basically requires
- 1:42Speaker 3: an expensive round trip ticket to a hard drive.
- 1:44Speaker 1: Somewhere, which brings us to our mission. For this deep dive.
- 1:48Speaker 1: We are exploring an early beta copy of a book
- 1:50Speaker 1: by Lance Halverson. It's called Functional Web Development with alixer
- 1:54Speaker 1: otp in.
- 1:55Speaker 2: Phoenix, a fantastic read, by the way.
- 1:56Speaker 1: Yeah, and the goal here is pretty radical. We're looking
- 1:59Speaker 1: at how to build blazing fast, stateful applications that remember
- 2:03Speaker 1: everything bypass the database entirely and kind of rethink web
- 2:07Speaker 1: architecture from the ground up.
- 2:09Speaker 3: And to prove this can actually work in production. The
- 2:12Speaker 3: source material walks us through building a complex stateful game
- 2:16Speaker 3: engine entirely in memory, like without touching a single database table.
- 2:21Speaker 1: Okay, let's unpack this. To really understand how this works,
- 2:25Speaker 1: we need to ground it in the game we are
- 2:27Speaker 1: actually building. It's a two player grid based game called Islands.
- 2:31Speaker 2: Yeah, kind of like a classic naval combat.
- 2:33Speaker 1: Game, right exactly. So imagine two players, each with a
- 2:36Speaker 1: ten x ten board cord. It's from a one down
- 2:39Speaker 1: to j ten. Neither player can see the other's.
- 2:42Speaker 3: Board, and you're placing islands of different shapes onto your grid, right,
- 2:45Speaker 3: like an a tall, a single dot, an L shape,
- 2:48Speaker 3: and S shape and a square yep. Once you set them,
- 2:51Speaker 3: you take turns guessing coordinates on your opponent's board. If
- 2:55Speaker 3: you guess right, you plant a palm tree on that island,
- 2:57Speaker 3: get all the coordinates, and you force the island first
- 3:00Speaker 3: to forrest all opponent islands.
- 3:02Speaker 2: Wins.
- 3:03Speaker 1: It is a perfect test case, honestly, because a game
- 3:06Speaker 1: like this is inherently stateful. You have to remember where
- 3:08Speaker 1: the pieces are, whose turn it is, what has already
- 3:11Speaker 1: been guessed.
- 3:12Speaker 2: You can't just forget everything between turns.
- 3:14Speaker 1: Right. If you tried to build this with a traditional
- 3:17Speaker 1: stateless web framework, say standard Rubyon rails or Django app,
- 3:22Speaker 1: every single guess would require a round trip to a
- 3:25Speaker 1: postgress database just to do one move exactly. It would
- 3:28Speaker 1: have to lock the row, check the board, update the score,
- 3:31Speaker 1: save the turn. It'd be incredibly heavy.
- 3:34Speaker 3: Not to mention the concurrency issues if two things happen
- 3:37Speaker 3: at once.
- 3:38Speaker 1: So to build this without a database, the author introduces
- 3:41Speaker 1: us to Alixer and the Erlang virtual machine, which is
- 3:44Speaker 1: known as the Beam. The Beam is just phenomenal, it
- 3:48Speaker 1: really is, and this is where my mind was blown.
- 3:50Speaker 1: The Beam allows you to spawn millions of these ultra lightweight,
- 3:53Speaker 1: long lived processes that hold data right in the server's.
- 3:57Speaker 2: Memory, millions of them.
- 3:58Speaker 1: But I have a major question here. If we have
- 4:00Speaker 1: thousands of different games running simultaneously on one server, spunning
- 4:04Speaker 1: millions of processes, doesn't the memory overhead completely kill the server?
- 4:09Speaker 1: I mean, OS level threads are incredibly heavy.
- 4:11Speaker 2: Well, that's the crucial distinction.
- 4:13Speaker 3: Really, we aren't talking about OS threads here or even
- 4:15Speaker 3: you know, Docker containers. A Beam process is an entirely
- 4:18Speaker 3: different beast. How so, it starts at around two kilobytes
- 4:22Speaker 3: of memory. It is unbelievably lightweight.
- 4:25Speaker 1: Two kilobytes wow.
- 4:27Speaker 3: Yeah, And the Beam has its own internal scheduler that
- 4:31Speaker 3: handles context switching between these processes efficiently. It does this
- 4:35Speaker 3: without ever asking the host operating system for permission.
- 4:38Speaker 1: That's wild.
- 4:39Speaker 3: It allocates a certain number of reductions or function calls
- 4:42Speaker 3: to each process before forcing it to yield. That's actually
- 4:46Speaker 3: how it handles millions of concurrent tasks without blocking the
- 4:49Speaker 3: main thread.
- 4:50Speaker 1: Okay, so they are cheap to make, But how do
- 4:54Speaker 1: we prevent the data from getting scrambled?
- 4:56Speaker 2: What do you mean?
- 4:56Speaker 1: Well, in a normal web app, if two people click
- 4:58Speaker 1: a button at the exact same millisecond, you get race conditions.
- 5:02Speaker 1: Data gets overwritten unless you have a heady database.
- 5:04Speaker 2: Lock right right?
- 5:05Speaker 1: Is this setup sort of like having a goldfish teller
- 5:08Speaker 1: at a bank who has to run back and check
- 5:10Speaker 1: the main vault for every single transaction, versus having a
- 5:13Speaker 1: dedicated teller who just keeps your specific file open right
- 5:17Speaker 1: on their desk the whole time.
- 5:18Speaker 3: Oh I love that analogy. Yes, it's exactly like the
- 5:21Speaker 3: dedicated teller in Alixer. These dedicated tellers are called agents.
- 5:26Speaker 3: Agents are simple, lightweight processes designed specifically to hold state,
- 5:31Speaker 3: so they.
- 5:31Speaker 1: Literally just sit there holding your data in memory exactly.
- 5:34Speaker 2: And what's fascinating.
- 5:35Speaker 3: Here is how they solve that race condition problem without
- 5:38Speaker 3: database locks. They rely on a principle called single representation.
- 5:43Speaker 1: Okay, what does that mean in practice?
- 5:44Speaker 3: Well, a piece of data, let's say the status of
- 5:47Speaker 3: Coordinate A one on Player one's board. It lives inside
- 5:50Speaker 3: exactly one agent process, nowhere else in the entire system,
- 5:54Speaker 3: and every single beam process has an exclusive built in mailbox.
- 5:59Speaker 1: So wait, if fifty network requests arrive at the exact
- 6:02Speaker 1: same millisecond asking to modify Coordinate A one, what.
- 6:05Speaker 3: Happens in mailbox physically cues them up. The agent processes
- 6:09Speaker 3: those messages sequentially, one by one. It processes message one,
- 6:13Speaker 3: updates its internal state, then pulls message too.
- 6:15Speaker 1: So there's no collision at all.
- 6:17Speaker 2: None.
- 6:18Speaker 3: Because the data has a single representation and a sequential mailbox,
- 6:22Speaker 3: it naturally effortlessly prevents race conditions. The architecture itself makes
- 6:27Speaker 3: a race condition mathematically impossible.
- 6:29Speaker 1: That is incredibly elegant. And because we aren't using a database,
- 6:32Speaker 1: we don't have to deal with object relational mapping or ORM.
- 6:35Speaker 2: Right, you get to skip all of that overhead.
- 6:37Speaker 1: There are no foreign keys, no joint tables, no M
- 6:41Speaker 1: plus one cordy problems. The architecture composes the game entities
- 6:45Speaker 1: using pure native Elixir data structures.
- 6:47Speaker 2: Which eliminates an enormous layer of translation logic. It's so
- 6:51Speaker 2: much cleaner.
- 6:52Speaker 1: So an individual coordinate is an Elixir struct It just
- 6:55Speaker 1: tracks a couple of specific things. A key for in
- 6:57Speaker 1: island to say what island it belongs to, and a
- 7:00Speaker 1: booleion for guests to say if it's been hit.
- 7:02Speaker 2: Very simple.
- 7:03Speaker 1: Yeah, then an island is just a list of those
- 7:06Speaker 1: coordinate processes, and a board is a map that connects
- 7:09Speaker 1: string names like A one to the specific coordinate process.
- 7:12Speaker 3: Ye.
- 7:13Speaker 1: You are literally just nesting native data structures.
- 7:16Speaker 3: But here is the critical part, and this is where
- 7:18Speaker 3: it gets really smart. Because each of these entities, the coordinates,
- 7:21Speaker 3: the islands, the boards are individual agent processes.
- 7:25Speaker 2: They are highly decoupled. Okay, if you update the A.
- 7:28Speaker 3: One coordinate through the board process, the island process that
- 7:31Speaker 3: also contains A one instantly sees the update.
- 7:34Speaker 2: Why is that because the.
- 7:36Speaker 3: Island doesn't hold a deeply nested copy of the coordinate data,
- 7:40Speaker 3: It just holds the address the process ID pointing to
- 7:43Speaker 3: that one specific coordinate agent.
- 7:45Speaker 1: Okay, so we've solved the data layer we have perfect
- 7:48Speaker 1: isolation and no race conditions. But having tens of thousands
- 7:52Speaker 1: of isolated islands of data is useless without a logistics.
- 7:56Speaker 2: Network, right, They need to communicate.
- 7:58Speaker 1: Exactly How do they all coordinate if you have independent
- 8:00Speaker 1: agents holding data, How does that not instantly turn into
- 8:03Speaker 1: a tangled, chaotic mess of processes just shouting at each other.
- 8:07Speaker 3: Well, that dilemma leads us directly to the next architectural layer.
- 8:11Speaker 3: Having thousands of independent agents. It's like having thousands of
- 8:14Speaker 3: employees who refuse to talk to anyone but their direct supervisor.
- 8:17Speaker 1: That sounds like a nightmare. It is.
- 8:19Speaker 3: You need a management structure. In the traditional tech world,
- 8:22Speaker 3: when developers want to break up a massive application to
- 8:24Speaker 3: scale it, they usually turn to service oriented architecture or microservices.
- 8:29Speaker 1: Oh boy, micro services. I feel like every time an
- 8:32Speaker 1: engineering team pivots to microservices, it starts with a hyped
- 8:36Speaker 1: up whiteboard session and ends in absolute tears.
- 8:39Speaker 2: The pain points are notorious. For sure.
- 8:42Speaker 3: To get traditional microservices to talk to each other, you
- 8:45Speaker 3: have to deal with incredibly complex network topologies.
- 8:48Speaker 2: Oh the retrilogic alone, exactly. You have to write massive
- 8:52Speaker 2: amounts of retrilogic just to handle network failures. Because the
- 8:56Speaker 2: services are communicating over the open Internet via HTTP, testing
- 9:01Speaker 2: them end to end is a nightmare.
- 9:02Speaker 1: And local development is just as bad.
- 9:04Speaker 3: Just to spin up a dev environment on your laptop,
- 9:07Speaker 3: you might have to run Docker, Kubernetes, a dozen different ports,
- 9:10Speaker 3: and configure external message cues like Kafka.
- 9:13Speaker 2: It's brutal.
- 9:14Speaker 1: But the architecture we're looking at offers a completely different path,
- 9:17Speaker 1: using OTP, the Open Telecom platform, and a specific abstraction
- 9:22Speaker 1: called a GEN server, which stands for Generic server.
- 9:25Speaker 3: Right. A GEN server gives us the exact encapsulation and
- 9:28Speaker 3: separation of concerns of a micro service, but it runs
- 9:32Speaker 3: entirely inside our Elixir application.
- 9:34Speaker 1: That is a huge difference.
- 9:35Speaker 3: It lives inside the Beam virtual machine, so you get
- 9:38Speaker 3: the benefits of a service oriented architecture, but with zero
- 9:42Speaker 3: external infrastructure, no HTTP network ops, no JSON serialization, no
- 9:48Speaker 3: Kubernetes cluster required.
- 9:50Speaker 1: Here's where it gets really interesting. The book insists on
- 9:53Speaker 1: a very specific repeatable code pattern for building these gen servers.
- 9:57Speaker 1: He mandates that a client function wraps a mod function,
- 10:00Speaker 1: which then triggers a callback.
- 10:02Speaker 2: Yep, the standard OTP pattern.
- 10:04Speaker 1: But why go through all these layers why not just
- 10:06Speaker 1: have a function that reaches in and grabs.
- 10:08Speaker 3: The data, because we have to remember how a genser
- 10:10Speaker 3: actually holds state underneath the hood, a gen service basically
- 10:14Speaker 3: just a recursive function running in an infinite loop.
- 10:16Speaker 1: Okay.
- 10:17Speaker 3: It receives a message, computes a new state, and then
- 10:19Speaker 3: calls itself recursively with that new state. If you let
- 10:22Speaker 3: other parts of your app interact directly with that recursive loop,
- 10:26Speaker 3: you break the concurrency model.
- 10:27Speaker 1: Ah see. So the client function is the public API.
- 10:31Speaker 1: It's the safe interface that the rest of the app
- 10:33Speaker 1: uses exactly.
- 10:34Speaker 3: The client function sends a message to the gen server process.
- 10:38Speaker 3: The gen server process receives it, triggers the internal callback function,
- 10:43Speaker 3: processes the state change, and continues its loop safely. It
- 10:47Speaker 3: completely isolates the internal state from the rest of the system.
- 10:51Speaker 1: And as part of this messaging, we dive into the
- 10:53Speaker 1: difference between a call and a cast.
- 10:56Speaker 2: This is a really important distinction.
- 10:57Speaker 1: Yeah. So, if I'm a process sending a message to
- 10:59Speaker 1: a gens wherever, a call is synchronous, I knock on
- 11:02Speaker 1: the door, ask a question, and I wait for the answer.
- 11:05Speaker 1: A cast is a synchronous I throw the message over
- 11:07Speaker 1: the wall and walk away, right, But the author strongly
- 11:10Speaker 1: advocates for using call almost everywhere, even when you don't
- 11:13Speaker 1: actually need the return data. Why why deliberately slow the
- 11:17Speaker 1: system down by making it.
- 11:19Speaker 3: Weight Because call creates a mechanical, unhackable system of back pressure.
- 11:24Speaker 3: When you execute a call, the beam scheduler literally suspends
- 11:28Speaker 3: the execution thread of the sender until the gen.
- 11:31Speaker 1: Server replies, So it's a built in traffic light.
- 11:33Speaker 3: Precisely, if a gen server is getting overwhelmed with messages,
- 11:37Speaker 3: say a massive spike in user traffic, a call physically
- 11:41Speaker 3: forces the senders to pause and wait their turn.
- 11:44Speaker 2: It prevents the system from flooding.
- 11:46Speaker 1: Because with a cast it would just keep piling up.
- 11:48Speaker 3: Exactly with an asynchronous cast, you could bombard a process
- 11:52Speaker 3: with a million messages in a second, completely fill up
- 11:55Speaker 3: its mailbox memory, and crash the process. Using a call
- 12:00Speaker 3: even if the reply is just an empty okay, keeps
- 12:03Speaker 3: the entire memory footprint stable and predictable.
- 12:05Speaker 1: Okay. That makes perfect sense for stability, and it's that
- 12:08Speaker 1: stability that allows us to build out the game logic
- 12:11Speaker 1: using the law of demeter.
- 12:12Speaker 2: A great design principle.
- 12:13Speaker 1: Yeah, it's where a process only talks to its immediate friends.
- 12:17Speaker 1: The main game gen server doesn't reach deep down and
- 12:19Speaker 1: directly tweak a.
- 12:21Speaker 3: Coordinate because that would create brittle, highly coupled code. If
- 12:24Speaker 3: the game process knows exactly how a coordinate works, any
- 12:28Speaker 3: change to the coordinate breaks.
- 12:29Speaker 2: The game right.
- 12:30Speaker 1: So instead the architecture change the functions. The game process
- 12:33Speaker 1: talks to the player process, the player process talks to
- 12:36Speaker 1: the board map, and the board talks to the specific
- 12:39Speaker 1: coordinate agent.
- 12:40Speaker 2: It delegates the work down the chain exactly.
- 12:42Speaker 1: If you want to change how a coordinate works, you
- 12:45Speaker 1: only update the coordinate code.
- 12:46Speaker 2: It's beautifully isolated.
- 12:48Speaker 1: But wait, I have to push back here for a second.
- 12:50Speaker 1: If we have ten thousand active games being played on
- 12:53Speaker 1: our server, we have ten thousand game gen servers running.
- 12:57Speaker 1: How does a player's web request actually their specific game process?
- 13:02Speaker 2: Ah, the routing problem.
- 13:04Speaker 1: Yeah, I know. Erlaying has a built in way to
- 13:06Speaker 1: name processes using atoms, which are basically constant strings. But
- 13:11Speaker 1: there's a massive warning in the source material about using
- 13:14Speaker 1: atoms for dynamically generated games. Why can't we just name
- 13:18Speaker 1: the process with an atom?
- 13:20Speaker 3: Well, it's a fatal trap if you don't understand how
- 13:22Speaker 3: the beam handles memory atoms are incredibly fast because under
- 13:25Speaker 3: the hood, an atom is just an integer pointer to
- 13:28Speaker 3: an entry in a C level string table.
- 13:30Speaker 1: Okay, fast sounds good.
- 13:31Speaker 3: It is, But because they are just pointers, atoms are
- 13:34Speaker 3: never garbage collected. Once you create an atom, it stays
- 13:36Speaker 3: in that table forever. And the Erling virtual machine has
- 13:39Speaker 3: a hard unchangeable limit of just over one million atoms.
- 13:43Speaker 1: Oh wow. So if you dynamically name every new game
- 13:47Speaker 1: with an atom based on a user's ID.
- 13:49Speaker 3: You will eventually hit that one million limit. And when
- 13:52Speaker 3: the atom table fills up, the node doesn't just throw
- 13:55Speaker 3: an exception, it violently crashes.
- 13:57Speaker 2: The entire server process drops dead.
- 14:00Speaker 3: Oh man, Yeah, it's a classic memory leak that causes
- 14:03Speaker 3: three point zero zero AM emergency outages.
- 14:05Speaker 1: Okay, but if atoms are a hard limit and they
- 14:08Speaker 1: crash the node, why use them at all? Why not
- 14:11Speaker 1: just use normal strings for everything? From the beginning? And
- 14:14Speaker 1: what's the actual solution for routing to our game?
- 14:16Speaker 3: We use a global name registry. Instead of registering the
- 14:20Speaker 3: gen server with a primitive atom, we register it using
- 14:22Speaker 3: a standard string like game call in player name.
- 14:25Speaker 1: Because strings are garbage collected.
- 14:27Speaker 2: Exactly, strings are garbage collected. The global registry keeps a
- 14:30Speaker 2: dynamic track of which string points to which internal process
- 14:33Speaker 2: idea across the entire server cluster.
- 14:36Speaker 1: But isn't a global string registry a central bottleneck If
- 14:40Speaker 1: every single request has to query one central registry to
- 14:43Speaker 1: find its destination, aren't we just reinventing the database bottleneck
- 14:47Speaker 1: and memory.
- 14:48Speaker 3: It would be, except for how Elixir implements the registry.
- 14:52Speaker 3: It uses ETS or laining term storage. It's an extremely
- 14:57Speaker 3: fast concurrent in memory to built in c Okay, so
- 15:01Speaker 3: it's optimized, very optimized. Multiple processes can read from an
- 15:05Speaker 3: ETS table simultaneously without blocking each other, so the lookups
- 15:09Speaker 3: are effectively instantaneous and highly concurrent. It completely bypasses the
- 15:13Speaker 3: atom limit crash without introducing a bottleneck.
- 15:16Speaker 1: Okay, so let's zoom out a bit. We have our
- 15:18Speaker 1: data living cleanly in memory using agents. We have perfect
- 15:21Speaker 1: robust communication and coordination using gen servers. But knowing where
- 15:25Speaker 1: the pieces are is really only half the battle.
- 15:28Speaker 2: Because a game has phases exactly, it has.
- 15:30Speaker 1: The dimension of time. If player one tries to drop
- 15:33Speaker 1: a bomb before player two is even joined the server,
- 15:35Speaker 1: our perfect data layer is going to flawlessly record an
- 15:38Speaker 1: illegal move. How do we build a bouncer that enforces
- 15:41Speaker 1: the rules of time and sequence without writing a billion
- 15:44Speaker 1: MESSIFL statements.
- 15:46Speaker 3: This raise is an important question, and to see why
- 15:48Speaker 3: this is so difficult, just look at the nightmare of
- 15:51Speaker 3: modern front.
- 15:52Speaker 2: End state management.
- 15:53Speaker 1: To start on front end state.
- 15:54Speaker 3: Right, You've got react use effect hooks, misfiring reduc stores,
- 15:59Speaker 3: getting tangled components, rendering out of order. State fatigue is
- 16:03Speaker 3: very real. When an application stays alive in memory, you
- 16:07Speaker 3: have to manually track every single user interaction over time.
- 16:10Speaker 1: Yeah, if modal A is open, disable button B, but
- 16:13Speaker 1: only if user C is an admin.
- 16:15Speaker 2: It devolves into a massive, tangled web of conditional logic.
- 16:19Speaker 1: So how does this Elixir architecture prevent that exact same
- 16:23Speaker 1: spaghetti logic on the back end.
- 16:24Speaker 3: It avoids it by using another built in OTP behavior
- 16:27Speaker 3: called gen statum, which stands for a generic state machine.
- 16:31Speaker 3: A state machine acts as the ultimate uncompromising rule enforcer.
- 16:36Speaker 3: It separates the state management logic entirely from the business logic.
- 16:39Speaker 1: Let's walk through exactly how this is implemented for our game.
- 16:42Speaker 1: The game starts in a state called initialized, and the
- 16:45Speaker 1: brilliance of gen statum is how literal the code pattern is.
- 16:49Speaker 1: You don't write a giant switch statement checking a state variable.
- 16:52Speaker 2: No, you don't.
- 16:52Speaker 1: You actually write a callback function and physically name it initialized.
- 16:56Speaker 2: The function name is the state.
- 16:58Speaker 1: So inside that initialized function, the code explicitly says the
- 17:02Speaker 1: only permissible event here is adding a second player. If
- 17:06Speaker 1: player two joins, the function returns a success topple and
- 17:09Speaker 1: then instructs the state machine to transition the entire game
- 17:13Speaker 1: into a completely new state called player's set.
- 17:16Speaker 2: And this is the magic.
- 17:17Speaker 3: Once you are in the players set state, the beam
- 17:19Speaker 3: process actually changes which function handles the message loop. Really, yeah,
- 17:24Speaker 3: the initialized function is no longer active. Any new messages
- 17:28Speaker 3: are now routed exclusively to a function called player's set.
- 17:31Speaker 1: But what if a rogue event tries to sneak in.
- 17:34Speaker 1: What if there's a bug in the front end client
- 17:36Speaker 1: and it sends a command trying to guess a coordinate
- 17:38Speaker 1: while we are still in the initialized phase. If the
- 17:41Speaker 1: initialized function only knows how to handle adding a second player,
- 17:46Speaker 1: won't that unhandled message crash the process?
- 17:49Speaker 3: That is where the catch all clause comes in, and
- 17:50Speaker 3: it showcases the power of Elixer's pattern matching it's less
- 17:54Speaker 3: like a standard else's statement and more like a coin sorting.
- 17:56Speaker 1: Machine the coinsorder. Yeah.
- 17:58Speaker 3: When an event drops in at the top, Elixra tries
- 18:00Speaker 3: to match its shape against the defined rules.
- 18:03Speaker 1: So if it's the exact right size for the ad
- 18:05Speaker 1: player slot, it falls through and updates the state exactly.
- 18:09Speaker 3: But if it's a misshapen rogue event, like guessing a
- 18:12Speaker 3: coordinate too early, it slides past that slot. At the
- 18:16Speaker 3: very bottom of your state function, you define a catch.
- 18:19Speaker 1: All clause, and what does that do?
- 18:21Speaker 3: The rogue event slides all the way down to this
- 18:23Speaker 3: bottom slot, where it's safely rejected into the coin return.
- 18:27Speaker 3: It physically cannot get stuck in the gears. The catch
- 18:30Speaker 3: all simply returns an aerotople, denies the transition, and keeps
- 18:34Speaker 3: the game safely in its current state.
- 18:36Speaker 1: It completely eliminates the need for sprawling nested if statements.
- 18:41Speaker 1: The rules of time and sequence are strictly enforced by
- 18:44Speaker 1: the physical routing of the architecture itself.
- 18:46Speaker 3: And the beauty is that the business logic doesn't have
- 18:49Speaker 3: to care about time anymore. The logic for calculating a
- 18:52Speaker 3: hit or a miss only exists inside the state where
- 18:54Speaker 3: guessing is mathematically allowed.
- 18:56Speaker 1: So what does this all mean? We started by talking
- 18:58Speaker 1: about the amnesia of the mod web, the heavy reliance
- 19:01Speaker 1: on databases just to remember who a user is, and
- 19:04Speaker 1: the immense latency that causes.
- 19:06Speaker 2: We covered a lot of ground today.
- 19:07Speaker 1: We did, but by dissecting this architecture we've seen a
- 19:11Speaker 1: completely different paradigm. We've seen how moving away from the
- 19:15Speaker 1: database as a crutch and embracing stateful, concurrent processes can
- 19:19Speaker 1: result in incredibly fast, robust applications.
- 19:22Speaker 3: It proves that you can have the elegance of tiny,
- 19:25Speaker 3: focused single purpose functions and compose them together to run
- 19:29Speaker 3: massive complex systems entirely natively.
- 19:32Speaker 2: No RMS, no.
- 19:34Speaker 3: HTTP, microservice tangles across external networks, no JavaScript state fatigue,
- 19:39Speaker 3: just clean, highly concurrent in memory processing managed by the beam.
- 19:44Speaker 1: And that leads us to a final provocative thought for
- 19:47Speaker 1: you to take away from this deep dive. We just
- 19:49Speaker 1: walked through how to run a complex real time multiplayer
- 19:51Speaker 1: game entirely in memory, using lightweight processes and state machines
- 19:55Speaker 1: without ever touching a database.
- 19:56Speaker 2: Which is the feat in itself.
- 19:58Speaker 1: It is, but games are really just the ti up
- 20:00Speaker 1: of the iceberg. What other standard notoriously database heavy business
- 20:03Speaker 1: applications can be completely revolutionized by this architecture.
- 20:07Speaker 3: It's a profound call to mullover. Just think about complex
- 20:10Speaker 3: e commerce shopping carts that constantly query the database just
- 20:13Speaker 3: hold five items.
- 20:15Speaker 1: Or highly secure banking sessions where state must be.
- 20:18Speaker 3: Perfectly preserved exactly, or live logistics networks tracking thousands of
- 20:23Speaker 3: delivery trucks in real time. If we can simply allow
- 20:26Speaker 3: the server to remember who we are and hold that
- 20:28Speaker 3: state securely and concurrently in memory, the latency drops to
- 20:32Speaker 3: almost zero.
- 20:32Speaker 1: It's amazing to think about.
- 20:34Speaker 2: The possibilities for scale and speed are enormous.
- 20:37Speaker 1: Imagine your barista not only remembering your name and your
- 20:40Speaker 1: exact order, but having it poured and ready the exact
- 20:44Speaker 1: second you walk through the door without ever having to
- 20:47Speaker 1: look at a ledger. That is the true power of
- 20:49Speaker 1: a staatefle architecture. We want to warmly thank you for
- 20:52Speaker 1: joining us on this deep dive today. Keep questioning the
- 20:55Speaker 1: standard ways of doing things, and we'll see you next time.