Ch.11: gRPC API Versioning: Change It Without Breaking Clients

Outline

Transcript

0:00 Somebody on a made-up orders team opens a cleanup pull request. The gRPC service is called OrderService, singular, and the rest of the codebase says orders, plural. So they rename it to OrdersService, with an s on orders. Yeah, honestly, I'd approve that in about 4 seconds. It's one letter. Did they touch any fields? Not one. Every field number untouched, client and server regenerated from the same file, every test green. The server deploys on a Tuesday afternoon. Okay, let's hear it. What broke? Every client still running last week's build starts failing.

0:36 The phone app, a nightly reporting job, another team's checkout service. All with the same error: unimplemented, method not found. Ouch. And get order is still sitting right there in the proto, untouched. Exactly, and that's the trap. Plenty of people think names don't matter on the wire, and for fields, they're right. So here's the plan: what a call actually puts on the wire, which changes survive while two versions overlap, the order to roll them out, and how to ship a real version 2. But first, those green tests.

1:08 So, the passing tests first. Let me guess: not one of them ran last week's client. Yep. The test client and the test server were always the same version. Production never is. And the reviewer on that PR reasonably thought, CI is green, ship it. Right, nothing deploys everywhere at once. A rolling deploy swaps servers a few at a time, so for a while, old and new servers answer side by side. And clients are worse. Take a phone app from last spring. Some people will never update it. Another team's service ships on their schedule, not yours. And if you roll back, an old server meets brand new clients.

1:48 So there are two directions to protect. Old client, new server, that's backward compatibility. New client, old server, that's forward compatibility. And Google's API design guide is blunt about the first 1: old clients must keep working against newer servers, within the same major version. That guide, and the other docs and code we mention, are linked in the description. I think that changes the review question. It's not, does it compile. It's, what happens when last month's client calls this? Oh, I like that. Or when this month's client lands on last month's server, halfway through a rollout.

2:22 That leaves one question: what does that old client actually send? Now, the bytes themselves. Every gRPC call rides on an HTTP/2 request, and that request has a path: slash, the package and service name, slash, the method name. Wait, my code never sees that. It just calls a method on the stub. So, basically a URL for one function. Pretty much. Though if you open a generated Python stub, the path is right there as a plain string. For example, an older client calling get order sends/shop.OrderService, slash GetOrder.

2:59 The server looks up that exact string, and it's case sensitive. And the new server only has a door labeled orders, plural. Exactly, and the old client is knocking on a door that isn't there anymore. A Java server's error says the method wasn't found, plus the full name. Python says much the same, with an exclamation mark. Go says unknown service. And all three send the status code unimplemented. Okay, but then why does renaming a field get away with it? Because inside the binary message, there are no names at all.

3:30 The body carries field numbers and values. Let's say you rename a field from customer to buyer. The bytes don't change. The exception is JSON, say through a gateway, where field names do show up. Sure, although my code that says customer still breaks the moment I regenerate. True, but that's a code problem, not a wire problem. Package, service, and method names are different. It's the address itself. Google's guide puts it plainly: a rename is the same as removing one thing and adding another. So that's Tuesday in one line.

4:04 As far as any old client could tell, that one-letter rename deleted the service. Which means the fix starts with keeping the old name answering. Same idea one level down, then. If the path is the address, field numbers are each field's identity. And reusing one does more damage than people expect. Imagine field 4 used to hold an internal note about a customer. Later, someone frees up number 4 for a display name that's printed on the receipt. Older clients still put that private note in field 4. Oh. So a customer reads your private note at checkout.

4:40 That's a nightmare. Right, and the official protobuf docs list exactly that kind of damage: leaked personal data, data corruption, or a parse error if you're lucky. The guardrail is a reserved line with the old number and name, so the compiler refuses any reuse. Enums have their own version of this. A new enum value is fine on the wire, but an old binary has never seen that number, so its switch statement falls into whatever default branch you wrote, if you wrote one. Mm, good one. And in general, old binaries skip fields they don't know.

5:16 If that old server forwards the message in binary, the unknown field rides along untouched, so a newer service downstream still gets it. Last 1: a field that needs a new type gets a fresh number, not an edit. So field numbers only ever get retired. Hand one to something new, and somebody's private note ends up on a receipt. Now, the flip side of retiring fields: adding one. Old servers skip it, new servers read it. Done? Almost. Turn it around. An old client never sends the new field. So the upgraded server reads the default: zero, an empty string, or false.

5:53 And it just acts on that. Yeah, with full confidence. Let's say we add max items to the list orders request. The app version from March calls list orders without it, the server reads zero, and if zero means return nothing, every customer on that version sees an empty order history, with no error anywhere. Oh, no. And Google's guide has a sneakier cousin. Say a list call used to return everything, and you add page size later with a default of 50. Old clients never ask for page two, so they think 50 is everything.

6:28 And they quietly miss the rest. So any field clients fill in has to default to the behavior from before it existed. Zero has to mean, do what you did before. So for max items, zero means no limit. But my first thought is, what if I really need to know the client didn't send it? Mark it optional. Then the generated code gives you a method like has max items, so you can ask whether it was sent. Without that, proto3 can't tell zero from never sent for a plain field. Got it. And the reverse, the server stops filling in a field that old clients still read?

7:06 Keep filling it in, even if it's redundant now. And never make a new request field mandatory, because older clients can't send what they don't know exists. Basically, a new field has to be harmless when it's missing. Now methods, with the same old-versus-new test. Start with the easy case: we add Cancel order. Old clients don't know it exists, so they never call it. And the new client, mid rollout? It calls Cancel order, and half the servers haven't been upgraded yet. Then it gets the same answer our orders team got on Tuesday: unimplemented. That status is gRPC itself saying, no such method here.

7:45 Huh. So the error behind that outage is actually useful here. It is. During the rollout window, the client can catch unimplemented from that one new method and fall back. For example, hide the cancel button for now, or use the old flow. Right. And if get order starts saying unimplemented, that's a real outage, not a stale server. Meanwhile the server side has a matching default. The generated base class, Python's servicer or Java's ImplBase, answers unimplemented for any method you haven't written yet.

8:17 Oh, so I can ship the new proto to every server before the handler's even written, and callers just get unimplemented. Which is why the order matters. Back to that half-upgraded fleet: servers first, then clients. By the time a client ships the new call, every server already has the handler running. And the fallback covers the times that order slips, like a server rollback. So adding is safe. What about changing a method that already exists? Three things are frozen. First, the request type. Second, the response type.

8:48 3rd, the streaming shape. Streaming shape, like turning a normal call into a stream? Yeah. Take a get order history call that returns one big response. Someone turns it into a server stream that sends orders one at a time. Which honestly sounds like an upgrade. For new clients, it is. But a client built for the old shape expects exactly one reply, and now it gets several. The call fails. gRPC's status code table calls that a cardinality violation, and Java's stub reports it as an internal error.

9:22 Hold on, because the backend lead proposing this will say, it's the same method, old clients will just take the first reply and ignore the rest. That's what everyone hopes. But Java's stub doesn't keep the first one. The second message fails the whole call. So leave that method alone and add a new streaming method beside it. So if streaming is out, at least I can swap the request message for a cleaner one? Sadly, no. Older clients keep sending the old one, the server decodes those bytes with a different definition, and fields can land in the wrong slots. Fair. So, back to Tuesday.

9:58 What should that team have shipped? Add the plural OrdersService next to the old singular one. Keep both registered on the server, and point both at the same code. New clients use the new name, and the old one keeps answering until nobody calls it. Same move for a method: a new method name goes in beside get order, with both calling one handler. I love that. Nothing clever, it just keeps both doors open. And behavior counts too. Say a bad order used to get invalid argument, and now it gets unavailable. Clients that retry on unavailable start retrying a request that will never succeed.

10:34 Same name, same bytes, different promise. Still a break. But sometimes the API is just wrong, and adding things beside it forever turns into a mess. When do you actually break it? When the fix can't live beside what's already shipped. Then you build version 2 as its own thing, and leave version one alone. Google's versioning guide puts the major version at the end of the package, so shop becomes shop dot v1, with a sibling, shop dot v2. And the package is in the path. So v1 and v2 are just two different addresses.

11:08 Two doors on the same building. One server can register both services at once. Old clients keep calling v1, new clients call v2, and the guide says both versions have to work at the same time during a transition period. Then comes getting people off v1. You mark it deprecated in the proto, and Java's generated stubs at least show callers a warning. Python's generated code ignores it, like most languages, so you announce it too. And when do you finally delete v1? When its traffic says so. Watch the call count for each method.

11:42 Not almost zero, not a few calls a night from that reporting job. Zero, for long enough to cover your slowest client. Which, for an app on someone's old phone, could be a while. It could. That's the one user who never complains and never updates. Which is why you want the accidental breaks caught before merge. Tools can compare your proto files against the last release in CI and flag them. Buf's breaking check, for example. So that rename gets caught in review instead of on Tuesday afternoon. The max items default and the changed error code still get through, though. To a schema diff, both look perfectly safe.

12:21 So if that orders team pinned one checklist above the proto file, what's on it? The path is an address. Don't rename a package, a service, or a method in place. Register the replacement alongside it. Each field number is an identity. Never change one, never reuse one, and reserve whatever you delete. A missing new field must behave like it was never added. And servers ship before clients. A method's request, response, and streaming shape stay exactly as they shipped. And when you truly have to break things, run v2 beside v1 until v1's traffic hits zero.

12:58 Coming up next, testing and debugging gRPC services, so problems like this show up before your users find them. Yeah. Green tests only count once they've met last month's client. Thanks for listening to Learning Podcasts.