Latest / Software Testing with Fexingo: QA, Automation, and Reliable Software Engineering / How Contract Testing Prevents Microservices Meltdowns
Transcript
- Lucas: So there's this story from a few years back — a well-known ride-hailing app had a major outage because a team removed a field from an API response that another team's service still depended on. The service that broke was the payment one, and it took them hours to trace it back to a tiny schema change. Luna: I remember that. And the worst part is, both teams had tests. Their individual services were perfectly fine in isolation. Lucas: Exactly. That's the classic microservices trap. You test each service in a vacuum, but the moment they talk to each other, the assumptions break. And that's where contract testing comes in. It's a way to formally define the agreement between two services — what the consumer expects and what the provider promises. Luna: So it's like a contract between teams, but automated. Lucas: Right. The most popular approach is consumer-driven contracts. The consumer writes the contract — it's basically a test that describes the expected response. Then that contract is shared with the provider, who runs it against their own code in CI. If the provider breaks the contract, the build fails, before anything goes to production. Luna: And this would've caught that payment bug, because the consumer would've said, 'Hey, I still need that field.' Lucas: Exactly. And it's not just about catching the break — it's about catching it early. You don't need to spin up a full staging environment or run a massive end to end suite. The contract test runs in seconds, right in the provider's pipeline. Luna: So what does a contract actually look like in practice? Is it a JSON file or something? Lucas: It depends on the tool. With Pact, the most widely used library, the consumer writes a test that defines the expected request and response. The test runs against a mock provider, and it generates a JSON contract — the pact file. That file is then published to a broker, which the provider can pull and verify against its own code. Luna: And there's another tool, Spring Cloud Contract, right? That's more for JVM folks. Lucas: Yeah, Spring Cloud Contract is a bit different — it's more provider-driven. The provider writes the contract, and the consumer gets a stub to test against. Both have their strengths, but the consumer-driven approach tends to be more common because it keeps the focus on what the consumer actually needs. Luna: So, if I'm a team with, say, ten microservices, how do I get started? Do I need to write contracts for every single interaction? Lucas: You don't have to do everything at once. Start with the critical paths — the ones that handle money, user data, or core business logic. Pick one consumer-provider pair, write a contract for that, and see how it goes. Once your team sees the value, you can expand. Luna: And what about versioning? Like, when you do change an API, how do you avoid breaking consumers during the transition? Lucas: That's the beauty of contract testing — it gives you a safety net. Before you make a breaking change, you can see all the consumers that depend on that endpoint. You can coordinate with them, update the contracts, and deploy in a compatible order. It's not a silver bullet, but it makes versioning way less scary. Luna: Right. It's like a map of your dependencies. Lucas: And speaking of maps, if today's conversation gave you something usable, the way these episodes stay ad-free is listener support. You can buy me a coffee at buy me a coffee dot com slash fexingo. It's a small gesture that keeps the lights on, and it's genuinely appreciated. Luna: Yeah, and it helps us keep digging into topics like this. We're back to contracts in a second. Lucas: So, one thing I want to clarify — contract testing is not a replacement for end to end tests. It's a different layer. E2E tests verify the whole system works together, but they're slow and flaky. Contract tests are fast and focused. You want both, but you don't want to rely on E2E alone. Luna: That makes sense. So contract testing is like the unit test for your APIs. Lucas: Exactly. And it's especially valuable in a world where teams are moving fast. When you have independent deploy cycles, you can't always coordinate releases. Contracts let you move independently with confidence. Luna: What about the broker? You mentioned a Pact broker. Is that necessary? Lucas: It's not strictly required, but it's highly recommended. The broker acts as a central repository for all your contracts. It also provides a UI to see which contracts are in use, which versions are deployed, and it can even enforce certain checks, like preventing a deployment if a contract isn't verified. Luna: So it's like a governance layer for your APIs. Lucas: Exactly. And there's a free version that's quite good, and a paid one with more features. But even the free one gives you a lot. Luna: Let's talk about a potential pitfall. What if a consumer forgets to update their contract when they add a new feature? Then the provider might remove a field that the consumer actually needs, but the contract doesn't catch it. Lucas: That's a real risk. The key is to make contract testing part of your definition of done. Whenever you change an API, you update the contract. And the provider should periodically run the latest contracts from the broker, not just the ones from the last release. Luna: So it's a cultural thing as much as a technical one. Lucas: Absolutely. And that's why many teams treat the contract as a shared artifact. It's not owned by the provider or the consumer — it's owned by both. That alignment is what makes it work. Luna: Are there any cases where contract testing doesn't make sense? I'm thinking of internal services with very stable APIs. Lucas: If the API never changes, you might not need it. But APIs have a way of evolving. Even if you think it's stable, someone might add a field or change a data type. Contract testing gives you a cheap safety net. For public APIs with external consumers, it's almost a no-brainer. Luna: What about performance? Does contract testing add overhead to the pipeline? Lucas: Minimal. The tests run in milliseconds. The biggest overhead is the initial setup and the maintenance of the contracts. But once you have a rhythm, it's low effort for high value. Luna: So, if I'm a QA engineer, how do I sell this to my team? Lucas: Start by pointing to a recent incident where a service broke another one. Then propose a pilot — pick one critical service and write a few contracts. Show them how it catches a real regression. Once they see that, they'll be on board. Luna: That's a good strategy. And it's a good way to build momentum. Lucas: And one more thing — contract testing can also help with compliance. If you're in a regulated industry, having a clear audit trail of API agreements can be valuable. It shows you're managing your dependencies carefully. Luna: Interesting. I hadn't thought of that angle. Lucas: So, to wrap up: contract testing is a way to make microservices more reliable by catching integration issues early. It's not a silver bullet, but it's a powerful tool in your testing arsenal. And with tools like Pact and Spring Cloud Contract, it's easier than ever to get started. Luna: I think the big takeaway is that you don't have to wait for a production incident to know if your services are compatible. Contracts give you that information in CI, before it hurts anyone. Lucas: Exactly. And as the industry moves toward more distributed systems, the ability to test these interactions in isolation becomes more and more critical. It's a skill worth investing in.