Overview

Get started today
Replay past traffic, gain confidence in optimizations, and elevate performance.

Developing APIs can be a complex process, particularly when you want to ensure they perform as expected under various real-world conditions. API mocking offers a streamlined, effective way to simulate these conditions during the testing phase. But how can you apply it to gRPC APIs, which are inherently more complex due to their use of Protocol Buffers for data transfer?

This post covers the differences between HTTP and gRPC protocols and their impact on mocking, before presenting the key steps of gRPC mocking. Notably, this guide uses production traffic replication to create highly realistic mock servers. This technique uses actual user interaction data to expose potential bottlenecks and elusive bugs that may remain hidden during traditional testing.

Mocking HTTP APIs vs gRPC APIs

The protocol type dictates how data is transferred, what kind of data format it uses, and consequently, how we can mock these APIs.

HTTP is a stateless, request-response protocol designed for transferring data on the web. HTTP-based APIs usually send and receive data in a text-based format like JSON or XML. This text-based nature means you can easily inspect the headers and body of an API response, making it straightforward to generate these mock responses.

On the other hand, gRPC is an open-source, high-performance RPC (Remote Procedure Call) framework developed by Google. Unlike HTTP, gRPC APIs uses Protocol Buffers (protobuf) to transfer data. This enables gRPC to offer some significant advantages like efficient serialization, language neutrality, and type checking.

However, the use of Protocol Buffers presents challenges when it comes to mocking. Since it’s not as readily interpretable or manipulable as JSON or XML, gRPC requires a more sophisticated approach to decode and encode messages, making gRPC APIs more complex to mock.

Mocking gRPC APIs With Production Traffic Replication

The 4 steps of API mocking

Below, we discuss the general steps and considerations required to implement mock servers for gRPC API. Comprehensive step-by-step instructions on mocking APIs in Kubernetes can be found in the traffic replay tutorial. Moreover, this guide focuses on highlighting the use of production traffic for automatic mock creation.

Production traffic replication imitates actual user interactions by capturing their data in a live environment and replaying it in a testing environment. This gives you a unique opportunity to uncover potential performance bottlenecks and elusive bugs that might stay hidden using traditional testing strategies.

For instance, If you’re developing an online bookstore, conventional test scenarios might cover rudimentary functionalities such as searching for a book, adding it to the cart, and checking out. However, they might not encapsulate real-world complexities like multiple users accessing the site concurrently, searching for non-existent books, or unexpectedly dropping transactions. A mock server replicating production traffic captures these intricacies, thus providing an accurate model of your service under real-world conditions.

Step 1: Capture Traffic

The first step requires setting up a Speedscale instance (as detailed in the traffic replay tutorial) to record the intercommunication between your gRPC service and its users. There are a few considerations you need to bear in mind when capturing traffic.

Selecting Traffic Recording

Ensure you get a varied sample of your production traffic. If, for instance, your gRPC service facilitates different procedural calls or varying message structures, include them in your recorded traffic. The power of traffic replication lies in the accurate simulation of real-world conditions, so don’t confine yourself to just the most common operations or simple request types.

The duration of the recorded traffic also plays a pivotal role in building representative mock servers. Too short, and you might miss out on variations in usage patterns; too long, and you might drown in a sea of data. You’ll need to strike a balance to ensure your mock servers accurately mirror your live service.

Speedscale captures traffic continuously, allowing you to focus on the amount of traffic to store in a snapshot, with the possibility of creating snapshots with overlapping traffic. This means you can experiment with different snapshots as mock servers use a snapshot-id to determine what traffic should be used, which you can easily switch. There are also filters available to narrow down the traffic to just the calls you’d like to include in the snapshots.

Data Sensitivity

Production traffic might contain sensitive user information, making it crucial to anonymize or eliminate such data during traffic capture. For instance, if your gRPC service handles user authentication, you’ll need to mask sensitive data such as passwords and tokens to keep in line with regulations like GDPR.

It’s equally important to secure all captured data as rigorously as production data to avert potential data breaches. If you’re dealing with sensitive data like health records, compliance with regulations like HIPAA is paramount. Speedscale’s single-tenant, SOC2 Type 2 certified architecture ensures safe storage of all your traffic.

Inspecting Traffic

Once the traffic is captured, review the structure of request and response payloads. This analysis can offer you valuable insights into the functioning of the service and the data it expects. For instance, dynamically generated requests may result in the structure of those requests being different than expected.

If your service uses encrypted gRPC traffic, ensure that Speedscale can decrypt it, as the usefulness of traffic is otherwise limited. For instance, if SSL/TLS encryption is used, you’ll need to provide Speedscale with the necessary keys for decryption.

Step 2: Create a Mock Server

In gRPC, correctly decoding protobuf messages is crucial. Improper decoding can result in a less representative mock server, potentially leading to inaccurate testing results. If your gRPC service employs nested protobuf messages, ensure these structures are decoded correctly to maintain identical behavior in your mock server.

While most gRPC mock servers rely on the service definition from your .proto files to understand your gRPC service’s structure, Speedscale decodes gRPC into a general-purpose JSON format, eliminating the need for protobuf access. This grants greater flexibility when filtering out specific transactions for testing, as well as reducing the complexity of creating mock servers.

To test a wider variety of scenarios, consider adding simulated network latencies to your mock server. You may also consider removing the latency to speed up development, like a developer testing locally. While this introduces a certain level of artificiality, it can offer valuable insights into your service’s response to delays and packet loss. And, you’ll still get the benefits of traffic data being realistic.

Step 3: Integrate with Your Test Environment

The service under test can now interact with this mock server as if it were the actual service, providing an environment that closely mirrors your production setup and the behavior of actual users. Ensure that your test environment’s network setup allows for communication with the mock server. This usually involves configuring network rules or setting up service discovery to locate the mock server. If you’re running services in Kubernetes, Speedscale automates this process.

Handling Development Dependencies

If your service is dependent on other services, these dependencies should be appropriately mocked. For instance, if your production environment uses load balancing, simulate it in your test environment by running multiple instances of your mock server and distributing traffic among them. Also, remember to set environment variables and configurations that mimic your production setup—this is also mostly automated when using production traffic.

Step 4: Test and Iterate

You can now start testing by executing your usual suite of tests against the mock server. At this point, there may be gRPC functionality or API usage highlighted by your traffic capture that’ll lead you to consider adding new test cases.

Also, make sure to implement logging and monitoring for your test environment. It’s rarely enough to know that something is broken; you’ll need to determine the cause of the issue in order to fix it. This especially comes into play during performance testing, which involves testing your service under varying levels of load and observing its behavior.

Lastly, your mock server should evolve in parallel with your live service. By continuously capturing traffic, you can ensure that your mock server stays up-to-date with your live service. All that’s needed is to change the snapshot-id during traffic replay executions.


Remember, the goal is to make your test environment as close to the production environment as possible. This means the traffic should be similar, the behaviors should be consistent, and the responses should be representative of what users would actually experience. If you need to test cases that are not in production already, use recorded traffic as the foundation that can then be modified. By doing this, you ensure that your service is ready to handle the real world.

The Importance of Realistic Mock Servers

Despite the complexities of mocking gRPC APIs, the importance of having realistic mock servers cannot be overstated. They play a crucial role in the software development lifecycle, providing numerous benefits that enhance the efficiency, cost-effectiveness, and overall quality of your software.

Additionally, efficient and realistic mock servers can significantly improve the agility of your development teams, as mock servers allow teams to work both independently when fixing a simple bug, and collaboratively when developing a new feature. Realistic mock servers have proven beneficial in various use cases. For instance, they can help in detecting bugs that would only surface under real-world conditions.