gRPC Flaws​ – The Illusion of Safety & Frustrating DevEx in Proto3’s Type-Safe Contracts​

By Naresh Jain

Share this page

Understanding the Shortcomings of gRPC and How Contract Testing Can Bridge the Gap 

In the ever-evolving world of API design, development, and testing, the pursuit of a seamless developer experience (DevEx) remains a constant. This article sheds light on some of the overlooked pitfalls of gRPC, a popular choice for its performance capabilities and type-safe contracts. Discover the significant benefits of contract testing in addressing common issues and enhancing developer experience. 

Understanding gRPC and Its Promises – Type-Safety and Schema Enforcement 

gRPC has gained traction in the development landscape due to its promise of performance and the enforcement of type-safe contracts via protocol buffers (protobuf). It leverages proto files to define schemas, ensuring that data types and structures remain consistent across different services and platforms. This theoretically eradicates a multitude of problems, promoting backward and forward compatibility while automatically generating client and server code in various languages. 

The Reality Check: Identifying gRPC’s Flaws – Handling Requests and Responses 

Despite gRPC’s promise, we can demonstrate several important flaws in its operation. For example, let’s use a proto file called order BFF proto, that exhibits three RPC methods: FindAvailableProducts, CreateOrder, and CreateProduct. We can seamlessly create a product using the gRPC web UI. However, omitting crucial information prompts an unexpected response— the product is created successfully despite missing mandatory details. This revelation underscores an overlooked pitfall in gRPC: the lack of built-in support for mandatory parameters enforcement. 

Employing Validation Libraries 

Proto 3 dropped required fields here is the link to the related Github discussion. However, we can leverage external libraries such as buf.validate to define mandatory parameters. Updating the proto file to validate mandatory fields ensures that requests lacking essential information trigger appropriate validation errors. However, the scenario evolves in our demonstration when a product manager imposes a new constraint: inventory can only be in multiples of three. 

The Developer’s Dilemma – Code Changes and Proto File Mismatch 

A developer can incorporate this constraint directly into the code, checking if the inventory value adheres to the new rule. While the immediate implementation seems effective, a discrepancy arises—the constraint is absent from the proto file. This highlights a critical issue: the proto file is now out of sync with the actual implementation, leading to potential misunderstandings and errors for downstream consumers. 

The Role of Contract Testing 

Contract Test Implementation 

Contract testing is an effective remedy for such issues. Using the Specmatic framework, developers can write executable contract tests that verify the alignment between the proto file and the actual implementation. Specmatic automatically generates these tests, and upon execution in our demonstration, it reveals two critical test failures. One involves sending a request with an inventory not being a multiple of three, highlighting the previously unnoticed mismatch. 

Immediate Feedback and Resolution 

Specmatic’s feedback loop proves invaluable, instantly notifying developers of the discrepancy. This immediate feedback ensures that the proto file is updated in tandem with the implementation, preventing downstream misunderstandings and ensuring a consistent developer experience across the board. 

Conclusion: Embracing Contract-Driven Development 

A Better Developer Experience 

So, it is evident that while gRPC offers advantages, it is not without its flaws. The manual nature of validating mandatory fields and the potential misalignment between proto files and implementation can lead to a subpar developer experience. However, integrating contract-driven development practices, exemplified by Specmatic, can bridge these gaps. 

Collaboration and Consistency 

Maintaining a central Git repository for API specifications is essential. This ensures that all stakeholders—be they providers or consumers—operate with an up-to-date version of the specification. This approach eradicates discrepancies and fosters a culture of collaboration and consistency. 

The Path Forward 

Ultimately, embracing contract testing in conjunction with gRPC’s strengths paves the way for a more robust and developer-friendly API ecosystem. By addressing the flaws we have highlighted, developers can build more reliable, consistent, and efficient microservices, leading to enhanced productivity and faster deployment. 

Final Thoughts 

This demonstration serves as a reminder that even the most promising technologies can benefit from supplementary practices. Contract testing is not just a tool; it’s a paradigm that aligns implementation with intent, transforming gRPC’s potential into a reality that developers can rely on. 

Related Posts

Specmatic vs WireMock

By Hari Krishnan

Comparison: Specmatic vs WireMock

API mocking is only effective if the mocks are truly representative of the provider / services they are emulating. Deviations between mocks and providers can
Read More
Specmatic vs Pact & Pactflow comparison

By Hari Krishnan

Comparison: Specmatic vs Pact.io and Pactflow.io

Specmatic and Contract Driven Development differ significantly from Pact. We built Specmatic out of our own necessity where Pact did not fit the bill for
Read More

By Naresh Jain

Contract Testing using OpenAPI Specs as Executable Contracts

Demonstration video showing OpenAPI specifications being leveraged as executable contracts with Specmatic Sample projects with OpenAPI https://docs.specmatic.io/sample_projects.html#openapi
Read More
Contract Testing with Specmatic - Markus Oberlehner

Stubbing an HTTP end-point with Specmatic

Markus Oberlehner gives a brief overview on the topic of Contract Testing with Specmatic and his experience using it for stubbing an HTTP end-point.
Read More

By Joel Rosario

TMForum ODA CTK API specification conformance testing with Specmatic

We recently discovered some major issues with the TMForum Conformance Test Kit (CTK) v5.0.0 and will demonstrate how using Specmatic can address these problems.
Read More
Specmatic API Coverage Report

Early detection of mismatches between your API specs and implementation: Exploring Specmatic’s API Coverage Report

Specmatic’s API coverage report helps identify any mismatches between an OpenAPI specification and an application's endpoints early in the development lifecycle. The report lists all
Read More
Dave Farley pointing to the Specmatic tool for easy microservice testing, featured on Dave Farley's Continuous Delivery channel.

“Easy Microservice Testing” – Specmatic featured on Continuous Delivery channel

Dave Farley is a respected author and widely recognised proponent of Continuous Delivery. In this video on his Continuous Delivery YouTube channel he discusses why
Read More
JDBC stubbing with Redis and Specmatic contract testing.

Break the Chains of Database Dependencies: Leveraging Specmatic for JDBC Stubbing

With Specmatic JDBC stub, you can easily test APIs without the need for a complex database setup. By switching out the real database with a
Read More

JMS Mocking with AsyncAPI using Specmatic

The JMS mock is wire compatible and can be controlled entirely from within the test. This means you can run the test locally or also
Read More
Specmatic + Kafka demo video thumbnail

Kafka Mocking with AsyncAPI using Specmatic

The Specmatic Kafka mock is wire compatible and entirely within the control of the test, the test can run locally and in CI and deliver
Read More