No history yet

Saga Pattern Fundamentals

Transcript

Beau

Okay, so Jo, let's just… let's recap where we are, because my head is spinning a little. We've established that thanks to the Two Generals' Problem, we can never be one hundred percent certain a message got through over an unreliable network.

Jo

Correct. There's always that last ounce of doubt.

Beau

And we also decided that for our ERP system, we'd rather it stay online and available, even if that means some parts of it are momentarily out of sync—that whole 'eventual consistency' thing.

Jo

Exactly. We're prioritizing availability.

Beau

So… what do we do? We can't use a giant, single database transaction because that would lock everything up and be super slow. But we also need a multi-step process, like creating a purchase order, to either fully complete or... not. How do we manage that without the safety of a traditional transaction?

Jo

That is the perfect question. And the answer is a pattern with a very cool name: a Saga.

Beau

A Saga? Like… a long, epic story? Are we building a Viking ERP now?

Jo

It kind of fits, actually. Think of a saga as a long story told in chapters. In our world, a Saga is a sequence of local transactions. Each transaction updates its own little corner of the world and then publishes an event that triggers the next transaction in the sequence.

Beau

Okay, hold on. Local transactions. What does that mean? We're using PostgreSQL. A transaction is a transaction, right?

Jo

It is, but it's about the scope. A local transaction is a normal, super-fast, ACID-compliant transaction that only affects one service or one database. It doesn't try to stretch across the unreliable network to another service.

Beau

So instead of one giant, slow, risky transaction that does everything, we're doing... what? A bunch of small, safe ones?

Jo

Precisely. Let's use a better analogy than an ERP for a second. Imagine booking a trip online.

Beau

Okay, I'm with you. A classic.

Jo

The overall business process is 'Book My Vacation'. But it’s made up of several steps. Step one: Book the hotel. Step two: Book the flight. Step three: Rent a car. Step four: Charge the credit card.

Beau

Right. And those are probably all different companies, different systems.

Jo

Exactly. A Saga treats each of those as a separate, local transaction. First, the travel service runs a transaction in its own database: 'Create Hotel Booking for Beau'. That succeeds. It commits. It's done. It's a solid, permanent record in the hotel system's database.

Beau

Okay, so my hotel is booked. But my flight isn't. The overall 'vacation' isn't complete.

Jo

Correct. But once that first local transaction is finished, it sends out a message, asynchronously. It basically shouts, 'Hey everyone, HotelBooked for Beau's trip!' The flight booking service is listening for that specific message.

Beau

Ah, so it's a trigger. The completion of one step kicks off the next one. It's a sequence.

Jo

Exactly. When the Flight Service hears 'HotelBooked', it wakes up and says, 'Oh, that's my cue!' and it runs its own local transaction to book the flight. When it succeeds, it sends out a 'FlightBooked' message. Then the Car Rental service hears that and does its thing, and so on.

Beau

So it's like a chain reaction of small, independent actions. Not one big monolithic action.

Jo

You got it. This is basically a state machine. The 'Vacation Booking' starts in a 'Pending' state. After the first step, it moves to a 'HotelBooked' state. Then 'FlightBooked'. Then finally 'Completed'. Each local transaction is responsible for moving the overall process from one state to the next.

Beau

And the key here is that 'asynchronous trigger' part. The hotel service doesn't wait around to see if the flight booking succeeds. It just does its job, sends its message, and moves on with its life.

Jo

That's the beauty of it. That’s how we achieve availability. The hotel booking system is never blocked waiting for the airline system. It's a huge improvement over that old Two-Phase Commit we talked about, which would have put a lock on everything until the entire process finished.

Beau

This makes so much sense. We're trading that instant, perfect consistency of one big transaction for a series of guaranteed, individual steps that will *eventually* lead to a consistent outcome for the whole process.

Jo

That’s the Saga pattern in a nutshell. It's the practical solution to the theoretical problems we've been discussing. We accept that we can't have perfect knowledge across a network, so we design a system based on a sequence of verifiable, local certainties.

Beau

A sequence of local certainties. I like that. It sounds much less stressful than hoping one giant message gets through.