Latest / CyberSecurity Summary / Learn You Some Erlang for Great Good!: A Beginner's Guide
Transcript
- 0:00Speaker 1: Imagine a programming language where, you know, variables aren't actually
- 0:03Speaker 1: allowed to vary, where the number one is mathematically considered
- 0:06Speaker 1: less than the concept of false.
- 0:08Speaker 2: Right.
- 0:09Speaker 1: And where the best way to handle a critical system
- 0:11Speaker 1: error isn't to, like, catch it and fix it, but
- 0:14Speaker 1: to just step back and let the entire process crash.
- 0:18Speaker 2: Yeah, it completely violates almost every established best practice we're
- 0:22Speaker 2: taught about, you know, writing code and managing systems.
- 0:25Speaker 1: It really does. So welcome to today's deep dive. We
- 0:29Speaker 1: are unlearning all those habits today. We're looking at Erlang,
- 0:32Speaker 1: drawing heavily from Fred Haber's highly acclaimed guide, Learn You
- 0:36Speaker 1: Some Erlang for Great Good, which actually features a foreword
- 0:40Speaker 1: by the language's creator, Joe Armstrong.
- 0:42Speaker 2: It's such a great resource.
- 0:43Speaker 1: It is. And our mission here for you listening, it
- 0:46Speaker 1: isn't just to learn some new syntax. We are decoding
- 0:48Speaker 1: a functional language that was engineered from the ground up
- 0:51Speaker 1: to solve one specific massive problem, which is how do
- 0:55Speaker 1: you build a system that never, ever goes down?
- 0:57Speaker 2: Right, because to achieve that kind of legendary five nines reliability,
- 1:01Speaker 2: so meaning 99.999% uptime, you have to completely rethink how
- 1:07Speaker 2: a machine manages information and how it handles failure.
- 1:12Speaker 1: You're essentially adopting a totally new set of physical laws
- 1:15Speaker 1: for your architecture.
- 1:16Speaker 2: Exactly. You really have to unlearn imperative programming where you
- 1:19Speaker 2: tell the computer step by step what to do. and
- 1:21Speaker 2: instead embrace functional programming.
- 1:23Speaker 1: Okay, let's unpack this, because let's start with the immediate
- 1:26Speaker 1: friction point for anyone coming from a background in, say,
- 1:29Speaker 1: C or Java or Python. In Erlang, the concept of
- 1:32Speaker 1: updating state by changing a variable is just strictly forbidden.
- 1:36Speaker 2: Yep, totally illegal.
- 1:37Speaker 1: If you declare that x equals 5, that value is
- 1:40Speaker 1: etched in stone forever. You literally cannot write x equals
- 1:44Speaker 1: x plus 1 on the next line.
- 1:45Speaker 2: Because, I mean, mathematically, that statement is a lie. 5
- 1:48Speaker 2: does not equal 6.
- 1:49Speaker 1: Oh, wow, yeah.
- 1:51Speaker 2: Erlang adheres ruthlessly to this concept called referential transparency. Basically,
- 1:55Speaker 2: a function must evaluate to the exact same result every
- 1:59Speaker 2: single time it's called with the same parameters.
- 2:01Speaker 1: So if you have a function named, like add two
- 2:03Speaker 1: to three.
- 2:03Speaker 2: It must always yield five, no matter what.
- 2:06Speaker 1: But wait, if we strip away the ability to mutate
- 2:09Speaker 1: state and place, how does a program actually do anything?
- 2:13Speaker 1: I mean, tracking a user's web session, a shopping cart,
- 2:16Speaker 1: a live game score, all of that requires state to
- 2:19Speaker 1: change over time.
- 2:20Speaker 2: Yeah, that's the big paradigm shift. If we connect this
- 2:23Speaker 2: to the bigger picture, instead of mutating a value in
- 2:25Speaker 2: a static memory location, functional programming passes the evaluated result forward.
- 2:31Speaker 1: Oh, I see.
- 2:31Speaker 2: So you take x, you add 1 to it, and
- 2:34Speaker 2: you pass that newly calculated value directly into the next
- 2:37Speaker 2: function call.
- 2:38Speaker 1: It's like writing a math test in pen. Once you
- 2:41Speaker 1: declare x is 5, it's permanent. Right. You're just moving
- 2:44Speaker 1: the calculations forward.
- 2:45Speaker 2: Exactly. The state flows continuously through the system rather than
- 2:49Speaker 2: sitting in one place being constantly overwritten. He preserved the
- 2:53Speaker 2: mathematical truth of the original equation while still processing dynamic changes.
- 2:57Speaker 1: Like passing a baton in a relay race. Yeah. Rather
- 3:00Speaker 1: than one runner swapping jerseys mid-stride. It enforces this kind
- 3:04Speaker 1: of strict honesty in the code.
- 3:06Speaker 2: That's a great way to put it. And the reason
- 3:08Speaker 2: this immutable state is so crucial isn't just about mathematical aesthetics.
- 3:13Speaker 2: When variables can't vary, you eliminate entire categories of bugs.
- 3:18Speaker 1: Because things aren't changing under your feet.
- 3:20Speaker 2: Exactly. If a piece of data is read only after
- 3:23Speaker 2: it has bound, you never have to worry about race conditions,
- 3:26Speaker 2: like situations where two different processes are trying to overwrite
- 3:30Speaker 2: the exact same piece of memory simultaneously.
- 3:32Speaker 1: Right, which is a nightmare to debug.
- 3:34Speaker 2: Absolute nightmare. So this absolute guarantee of immutability paves the
- 3:38Speaker 2: way for safe, massive concurrency.
- 3:41Speaker 1: But what about the real world? Like things do change.
- 3:44Speaker 2: Yeah. And Erlang is pragmatic. It adheres to pure functional principles,
- 3:49Speaker 2: but it allows breaking them for real world problems. Like
- 3:52Speaker 2: if a function checks today's date, that changes daily. The
- 3:55Speaker 2: architecture accommodates that reality.
- 3:57Speaker 1: Which brings us to the core architectural solution. Because if
- 4:00Speaker 1: you cannot mutate shared variables to track state across a
- 4:03Speaker 1: massive application, you need an entirely different mechanism for handling
- 4:07Speaker 1: simultaneous tasks. Right.
- 4:09Speaker 2: And that leads us directly to Erlang's defining feature, the
- 4:12Speaker 2: actor model.
- 4:13Speaker 1: Because everything runs inside a specialized virtual machine, right? Yeah.
- 4:17Speaker 1: And every actor is a completely isolated process within that VM.
- 4:21Speaker 2: Yeah. Think of an actor as, well, the book uses
- 4:23Speaker 2: this great analogy, like a solitary person sitting alone in
- 4:27Speaker 2: a pitch black windowless room.
- 4:29Speaker 1: Okay. Kind of spooky.
- 4:30Speaker 2: A little bit. The only connection they have to the
- 4:32Speaker 2: outside world is a small mailbox on the door. So
- 4:35Speaker 2: they just wait in the dark.
- 4:36Speaker 1: Waiting for mail. Right.
- 4:38Speaker 2: And when a letter arrives in the mailbox, the actor
- 4:41Speaker 2: turns on a light, performs whatever specific task that message
- 4:45Speaker 2: asks for, like paying bills, writing a thank you card,
- 4:49Speaker 2: calculating some math, and then turns off the light and
- 4:52Speaker 2: goes back to waiting.
- 4:53Speaker 1: Complete isolation. There's no shared memory pool that they're all
- 4:57Speaker 1: dipping their hands into. They communicate strictly by explicitly passing messages.
- 5:01Speaker 2: And if they don't understand the letter they got, they
- 5:03Speaker 2: just ignore it and go back to sleep. They don't
- 5:05Speaker 2: even need to know that other actors exist.
- 5:07Speaker 1: So what does this all mean for you, the listener? Like,
- 5:10Speaker 1: why should you care about actors in dark rooms?
- 5:12Speaker 2: For the listener, this means a world without cascading errors.
- 5:16Speaker 1: Oh, that's huge.
- 5:17Speaker 2: It is. If we contrast this with standard multi-threaded environments,
- 5:21Speaker 2: threads usually share memory. If one thread hits an error
- 5:25Speaker 2: and corrupts that shared memory, the entire application crashes.
- 5:28Speaker 1: Right, so the whole system just goes down.
- 5:30Speaker 2: Exactly. But in Erlang, if our actor in the dark
- 5:33Speaker 2: room makes a mistake or encounters a fatal error and dies...
- 5:37Speaker 2: It dies completely alone. The memory corruption is physically contained
- 5:41Speaker 2: to that single room.
- 5:43Speaker 1: While the rest of the system just keeps processing mail
- 5:45Speaker 1: completely oblivious to the failure.
- 5:47Speaker 2: Exactly. And the ecosystem extends way beyond just the language
- 5:50Speaker 2: itself to support this. It includes the OTP framework, which
- 5:54Speaker 2: abstracts away a lot of that complex message passing boilerplate.
- 5:58Speaker 1: OTP is like the standardized template for structuring these actors
- 6:01Speaker 1: into massive industrial applications, right?
- 6:03Speaker 2: Yeah, battle-tested libraries. And then there's the Mnesia, the distributed
- 6:07Speaker 2: database built directly into the Erlang ecosystem.
- 6:10Speaker 1: Mnesia is wild because it's essentially a key-value store that
- 6:13Speaker 1: natively replicates itself.
- 6:15Speaker 2: Right, because it's built on top of the Erlang VM
- 6:17Speaker 2: and relies on that same message-passing architecture. It handles network
- 6:21Speaker 2: partitions seamlessly. Like, if a server rack actually catches fire
- 6:25Speaker 2: and goes down... the amnesia nodes on the surviving servers
- 6:29Speaker 2: just keep passing messages.
- 6:30Speaker 1: As if nothing happened. And that structural strict isolation naturally
- 6:36Speaker 1: brings up a terrifying question, which leads to arguably Erlang's
- 6:40Speaker 1: most famous doctrine, the let it crash philosophy.
- 6:43Speaker 2: Yeah, let it crash is where people usually lose their
- 6:45Speaker 2: minds a little bit.
- 6:46Speaker 1: Because in most environments, we are taught to program defensively.
- 6:50Speaker 1: You try to anticipate every bad input, every hardware hiccup,
- 6:54Speaker 1: and you write all this extensive code to catch those
- 6:56Speaker 1: errors and keep the system limping along.
- 6:59Speaker 2: Great. Which just creates these monolithic, deeply entangled code bases where, like,
- 7:04Speaker 2: half the logic is purely for error handling. Erlang developers
- 7:07Speaker 2: just abandon that approach entirely.
- 7:09Speaker 1: Just let it break.
- 7:10Speaker 2: They operate like tightrope walkers who have absolute faith in
- 7:12Speaker 2: their safety net. Instead of exhausting yourself trying to perfectly
- 7:15Speaker 2: balance against every unpredictable gust of wind, you just accept
- 7:18Speaker 2: that falls are inevitable.
- 7:20Speaker 1: You let the error happen.
- 7:21Speaker 2: Let it happen and rely on the net to catch
- 7:23Speaker 2: you and reset your position on the wire. The system
- 7:26Speaker 2: will recover.
- 7:28Speaker 1: Here's where it gets really interesting. It's like having a
- 7:30Speaker 1: restaurant where if a chef burns a dish, you don't
- 7:33Speaker 1: shut down the kitchen to investigate and you don't try
- 7:36Speaker 1: to salvage the burnt food.
- 7:38Speaker 2: No, you just instantly clone a new chef who starts
- 7:40Speaker 2: fresh while the rest of the staff keeps cooking.
- 7:42Speaker 1: I love that. But how does that safety net actually
- 7:45Speaker 1: function mechanically? Because if we let the actor in the
- 7:49Speaker 1: dark room die, whatever task it was managing is gone.
- 7:53Speaker 1: How do we bring it back?
- 7:54Speaker 2: This is where supervision trees come in within the OTP framework. Basically,
- 7:59Speaker 2: processes are organized hierarchically. You have worker processes, the actors
- 8:03Speaker 2: doing the actual math or web requests, and you have
- 8:06Speaker 2: supervisor processes. A supervisor's only job is to monitor its
- 8:10Speaker 2: designated workers. It doesn't do math. It just watches the rooms.
- 8:14Speaker 1: So when a worker crashes, the supervisor spots it.
- 8:16Speaker 2: Instantly.
- 8:17Speaker 1: Yeah.
- 8:18Speaker 2: And it reacts according to a predefined strategy. It cleans
- 8:21Speaker 2: up the dead worker state and spawns a brand new,
- 8:23Speaker 2: pristine instance of that actor to take its place.
- 8:26Speaker 1: In milliseconds.
- 8:27Speaker 2: Exactly. It's why Erlang is used for ultra-high reliability systems.
- 8:32Speaker 2: The system recovers from a catastrophic failure by resetting that
- 8:36Speaker 2: isolated component to a known good state.
- 8:39Speaker 1: Rather than trying to untangle whatever corrupted logic caused the crash.
- 8:43Speaker 1: That is brilliant.
- 8:45Speaker 2: It is, but we do need to issue the book's
- 8:47Speaker 2: Kool-Aid warning here.
- 8:49Speaker 1: Ah, yes. The Kool-Aid warning.
- 8:50Speaker 2: Because Erlang processes are incredibly lightweight. You can spin up
- 8:54Speaker 2: hundreds of thousands of them on a standard laptop. The
- 8:58Speaker 2: temptation is to treat concurrency as a magic wand.
- 9:00Speaker 1: Right. The book warns against madness, like making every single
- 9:03Speaker 1: bullet in a shooter game its own independent actor.
- 9:06Speaker 2: Yeah, please don't do that. The message passing overhead, having
- 9:09Speaker 2: thousands of bullets constantly mailing each other their updated coordinates,
- 9:13Speaker 2: will completely kill your performance.
- 9:14Speaker 1: So it's not a silver bullet. Pun intended.
- 9:17Speaker 2: Good one. No, it's not. It's terrible for heavy state-bound
- 9:20Speaker 2: processing like image manipulation or writing OS device drivers. Evaluate
- 9:25Speaker 2: your specific problem before blindly throwing concurrency at it.
- 9:29Speaker 1: But it's amazing for server middleware and distributed databases, though
- 9:32Speaker 1: it has been used in some really unexpected places.
- 9:34Speaker 2: Oh, definitely.
- 9:35Speaker 1: Like Wings3D. com. which is a 3D modeler written in Erlang,
- 9:40Speaker 1: and the Ayano robot, an autonomous AI that actually won
- 9:43Speaker 1: a silver medal in the Eurobot competition.
- 9:45Speaker 2: Yeah, because robots have to process multiple asynchronous sensor inputs
- 9:50Speaker 2: without locking up, which Erlang handles beautifully.
- 9:53Speaker 1: So to execute this brilliant philosophy of isolated actors and
- 9:56Speaker 1: crashing safely, the language relies on highly specific and honestly
- 10:01Speaker 1: sometimes bizarre syntax rules.
- 10:04Speaker 2: Very bizarre.
- 10:05Speaker 1: Let's talk about the math quirks first. Instead of a
- 10:07Speaker 1: forward slash for integer division, you use the word div. Yep.
- 10:10Speaker 2: Div and rem for remainder.
- 10:13Speaker 1: You can even write in different bases natively, right? Like
- 10:15Speaker 1: two type one zero zero ten is just binary for 42.
- 10:18Speaker 2: It's weird, but it works. And then there are the variables.
- 10:21Speaker 1: Oh, man. The variables. They must start with an uppercase letter.
- 10:25Speaker 1: If you type a word with a lowercase letter, it's
- 10:28Speaker 1: not a variable.
- 10:28Speaker 2: Which is a huge adjustment. But the real mind bender
- 10:31Speaker 2: is the equal sign. In Erlang, the equal sign is
- 10:34Speaker 2: not an assignment operator.
- 10:35Speaker 1: It is a pattern matching operator.
- 10:37Speaker 2: Exactly. So if you write one.
- 10:40Speaker 2: Assuming one is a capitalized, unbound variable, the system successfully
- 10:44Speaker 2: binds it. But if you follow that up with one
- 10:46Speaker 2: equals two.
- 10:47Speaker 1: The system will crash with an error because one does
- 10:50Speaker 1: not mathematically match two. Right.
- 10:52Speaker 2: It leans right back into that let it crash mindset.
- 10:54Speaker 1: OK, so if capitalized words are variables. What are lowercase words,
- 10:59Speaker 1: like if I type cat or atom?
- 11:01Speaker 2: Those are called atoms. They are literals. Their name is
- 11:04Speaker 2: their entire value. They're super efficient for internal routing, like
- 11:07Speaker 2: sending a message that just says update in lowercase.
- 11:10Speaker 1: Wait, I need to push back on this, though. The
- 11:12Speaker 1: book tells us Erlang is built for ultimate safety, but
- 11:15Speaker 1: then casually mentions that generating atoms dynamically can literally tip
- 11:18Speaker 1: the system over and crash it.
- 11:20Speaker 2: Yeah, the atom table vulnerability. Right.
- 11:22Speaker 1: The table isn't garbage collected. If a user dynamically generates atoms,
- 11:27Speaker 1: they can hit the physical limit of 1,048,577 atoms, and
- 11:32Speaker 1: the whole virtual machine crashes. How is that safe?
- 11:34Speaker 2: This raises a really important question about structural safety versus
- 11:38Speaker 2: developer responsibility. Atoms are strictly developer tools.
- 11:41Speaker 1: Okay, so you shouldn't expose them to users. Never.
- 11:44Speaker 2: As long as they are typed directly by the programmer
- 11:46Speaker 2: for internal messages, they are totally safe. It's a harsh
- 11:50Speaker 2: lesson in controlling user inputs. You parse untrusted user data
- 11:53Speaker 2: as strings... Never as atoms.
- 11:56Speaker 1: Got it. Once you understand how these individual values and
- 11:59Speaker 1: atoms work, you have to group them, which exposes some
- 12:03Speaker 1: of Erlang's most infuriating design choices.
- 12:06Speaker 2: You're talking about tuples.
- 12:07Speaker 1: Tuples. They're used to group terms like Celsius 23.213.
- 12:12Speaker 2: And pattern mashing unpacking them is so powerful here. If
- 12:16Speaker 2: a function expects Celsius, T, and you send it Kelvin 23.213,
- 12:19Speaker 2: the pattern fails. It safely rejects the wrong temperature scale
- 12:24Speaker 2: before it does math on it.
- 12:25Speaker 1: That part is great. But the comparison.
- 12:27Speaker 2: Oh, yeah, the comparison.
- 12:28Speaker 1: You have double equals, but then you have for exact equality.
- 12:32Speaker 1: And the less than or equal operator is backwards. I know.
- 12:35Speaker 2: I know. It looks like a typo. But the standard
- 12:37Speaker 2: symbol is reserved for the receipt block, which is how
- 12:39Speaker 2: actors check their mailboxes. The designers had to flip it
- 12:42Speaker 2: so the compiler wouldn't get confused.
- 12:44Speaker 1: Everything serves a concurrency. Even Booleans. Booleans aren't real.
- 12:48Speaker 2: Nope. True and false are just atoms.
- 12:51Speaker 1: Which leads to the total ordering rule. You can compare
- 12:54Speaker 1: absolutely anything. Number is less than atom, which is less
- 12:57Speaker 1: than tuple.
- 12:57Speaker 2: Which is why the statement one false actually evaluates a
- 13:00Speaker 2: true in Erlang.
- 13:02Speaker 1: Because false is an atom, and atoms are inherently greater
- 13:05Speaker 1: than numbers. Right.
- 13:07Speaker 2: It sounds crazy, but it's a completely pragmatic choice. Total
- 13:10Speaker 2: ordering ensures that sorting algorithms never crash. No matter what
- 13:14Speaker 2: weird heterogeneous combination of data you feed them, it just
- 13:18Speaker 2: sorts them by type hierarchy and keeps running.
- 13:21Speaker 1: Stability over purity. That is the Erlang way. And nothing
- 13:24Speaker 1: proves that more than the great string illusion.
- 13:27Speaker 2: Ah, the strings.
- 13:28Speaker 1: Erlang has no real string type. Strings are literally just
- 13:31Speaker 1: lists of numbers. The list 97, 98, 99... prints as ABC.
- 13:36Speaker 2: Yeah, it's a hangover from Erlang's telecom origins. They were
- 13:38Speaker 2: building phone switches, not word processors. They didn't want to
- 13:41Speaker 2: waste memory on a dedicated string type when they were
- 13:43Speaker 2: just routing binary payloads.
- 13:45Speaker 1: So when I type ABC, Erlang is just secretly seeing
- 13:48Speaker 1: the ASCII math behind it. It's like looking at the
- 13:50Speaker 1: code in the matrix. There is no string, just numbers.
- 13:53Speaker 2: Exactly. It makes Erlang pretty clunky for text processing compared
- 13:57Speaker 2: to like Python or Perl, but it's brutally efficient for
- 14:01Speaker 2: its intended purpose.
- 14:02Speaker 1: It really is. So just to summarize our journey today,
- 14:06Speaker 1: learning Erlang forces you to abandon flexible variables and defensive programming.
- 14:10Speaker 2: Completely.
- 14:11Speaker 1: You replace them with strict math, referential transparency, pattern matching,
- 14:15Speaker 1: and these totally isolated actors that are actually allowed to fail.
- 14:19Speaker 1: It's a really steep learning curve, but it fundamentally upgrades
- 14:23Speaker 1: how you think about problem solving and system design.
- 14:26Speaker 2: It changes your brain. Once you see systems as isolated
- 14:29Speaker 2: communicating processes, you kind of can't unsee it.
- 14:32Speaker 1: Which leaves you, the listener, with something to chew on.
- 14:36Speaker 1: If Erlang builds the most reliable, fault-tolerant computer systems in
- 14:39Speaker 1: the world by isolating tasks into dark rooms where they
- 14:43Speaker 1: communicate only through strict, written messages, could human organizations benefit
- 14:49Speaker 1: from this model?
- 14:50Speaker 2: Oh, that's an interesting thought.
- 14:51Speaker 1: Right. What if our workplaces operated like an Erlang virtual machine,
- 14:56Speaker 1: where one person's failure didn't cascade into a massive panic,
- 15:00Speaker 1: but was cleanly caught by a safety net without endless
- 15:03Speaker 1: meetings or shared state.
- 15:04Speaker 2: I mean, a self-healing corporate hierarchy, that would certainly save
- 15:08Speaker 2: a lot of headaches.
- 15:09Speaker 1: It really would. Thank you for joining this deep dive.
- 15:11Speaker 1: Until next time, keep questioning the architecture of the world
- 15:14Speaker 1: around you.