Multi-Protocol APIs: Designing a Unified Interface for EDA using Async APIs

Presenter: Naresh Jain
Event: Apidays Singapore
Location: Singapore

Presentation summary

How to use AsyncAPI to manage multiple protocols so that you can define a unified interface for your event- driven architecture.

Transcript

 

Table of Contents

Multi-Protocol Async APIs: Designing a Unified Interface for Event-Driven Architecture

In today’s fast-evolving technology landscape, the need to manage multiple communication protocols seamlessly is more critical than ever. Asynchronous communication plays a vital role in modern distributed systems, particularly in event-driven architectures (EDA). This talk explores how to leverage Multi-Protocol Async APIs to create a unified interface that bridges diverse messaging systems effectively.

Drawing insights from industry standards and real-world examples, we will uncover the essential components of async API design, demonstrate practical implementations, and delve into contract-driven development to optimize API collaboration and testing. This article is inspired by the expert talk by Naresh Jain, Founder and CEO at Specmatic, who shares invaluable knowledge on how to harness async API specifications to manage multiple protocols such as Kafka, Google PubSub, and MQTT.

Introduction to Multi-Protocol Async APIs and event-driven architecture

Understanding the Importance of Multiple Protocols in Asynchronous APIs

Before diving into the technical details, it is crucial to understand why supporting multiple protocols matters. Various organizations, including standards bodies like TM Forum, continually strive to define API specifications that enable interoperability among their members. These specifications often cover both synchronous REST APIs (OpenAPI) and asynchronous communication protocols (AsyncAPI).

While the specification defines the interface, the implementation is left to the individual members. This approach allows different players in the ecosystem to maintain their infrastructure choices while adhering to a unified contract. AsyncAPI, in particular, shines in the asynchronous communication space by offering a standardized way to describe interactions across multiple messaging protocols like Kafka, MQTT, Google PubSub, JMS, and others.

The unified interface provided by AsyncAPI enhances interoperability, reduces integration complexity, and ensures consistent communication patterns across diverse systems.

Standards bodies and API specifications for interoperability

Core Elements to Standardize in Async API Specifications

Designing an effective AsyncAPI specification requires careful consideration of several key aspects to ensure clarity and interoperability. Below, we explore these critical elements:

Schema Formats

The schema format defines the structure and format of the messages exchanged between producers and consumers. AsyncAPI supports multiple schema formats including:

  • JSON Schema: The most commonly used format for describing message payloads.
  • Protobuf: A compact binary format often used in high-performance systems.
  • Avro: Another binary serialization format popular in big data applications.
  • XSD (XML Schema Definition): Recently added support for XML-based schema definitions.

By specifying the schema format clearly, publishers can communicate the exact structure of the messages expected by consumers, avoiding misunderstandings and runtime errors.

Schema formats supported by AsyncAPI including JSON Schema, Protobuf, Avro, and XSD

Messaging Patterns

Asynchronous communication supports various messaging patterns, and it is crucial to specify which pattern an API uses for a particular interaction. Common patterns include:

  • Request-Reply: A client sends a request message to a topic and expects a reply message on a separate reply topic. This pattern is built into AsyncAPI.
  • Publish-Subscribe (PubSub): A message is broadcasted to multiple subscribers who independently process it.
  • Event Streaming: Continuous flow of event messages to interested consumers.
  • Subscribe-Notify: Subscribers receive notifications when specific events occur.

Clearly communicating the messaging pattern used in the API specification helps consumers understand how to interact with the service and what behavior to expect.

Different messaging patterns such as request-reply and pubsub

Topic Names and Naming Conventions

The topic name is the channel on which messages are exchanged. Clearly defining topic names and adhering to naming standards is vital for effective communication and message routing. Organizations often enforce naming conventions to ensure consistency, discoverability, and easier maintenance.

Specifying topic names explicitly in the AsyncAPI document ensures that everyone involved knows where to send and receive messages, which reduces integration errors and confusion.

Importance of clearly communicating topic names in AsyncAPI specifications

Security Schemes

Security is paramount in asynchronous communication. Different protocols support varying security mechanisms, and these need to be defined in the AsyncAPI specification. Common security schemes include:

  • OAuth: Token-based authorization often used in cloud services.
  • Username and Password: Basic authentication mechanisms.
  • Certificate-based Authentication: Using TLS certificates for secure communication.

Security implementations are protocol-specific. For example, Kafka authenticates at the topic level, while Google PubSub can authenticate individual messages within a topic based on subscription permissions. The AsyncAPI specification must capture these nuances so that consumers are fully aware of authentication and authorization requirements.

Security schemes in AsyncAPI like OAuth and certificate-based authentication

Practical Example: Designing an Async API for an Order Service

To make these concepts more concrete, let’s consider a sample application: an order service that accepts new orders and communicates order updates asynchronously.

Here’s how the interaction might work:

  1. A client publishes a new order message on the new order topic, effectively making a request to the order service.
  2. The order service processes the request and replies with an order ID on a reply topic (e.g., work in progress orders), enabling the client to track order progress.
  3. Backend systems such as the warehouse may notify the order service about inventory status either through another topic or by making an HTTP request.
  4. Upon receiving confirmation (a trigger), the order service publishes a message on the accepted order topic to notify consumers such as a shipping application that the order is ready to be shipped.
  5. The shipping application can also update the order status (e.g., out for delivery) by sending messages to another topic, allowing the order service to stay updated asynchronously.

These interactions illustrate multiple messaging patterns, including request-reply, pub-sub, and event notification, which can be captured in a single AsyncAPI specification to provide a comprehensive contract.

Order service example illustrating async messaging patterns

Exploring the AsyncAPI Specification for Multi-Protocol Support

Let’s delve deeper into how the AsyncAPI specification captures the details of such an asynchronous system supporting multiple protocols.

The specification is typically expressed in YAML or JSON format and includes:

  • Channels: Define the communication channels or topics, e.g., Kafka topics and Google PubSub topics.
  • Operations: Describe the actions on each channel, such as publishing or subscribing, and specify the messaging pattern (e.g., request, reply).
  • Messages: Detail the message format, including the schema and example payload.
  • Security: Define the security schemes per protocol.
  • Schemas: Provide the data structure definitions in JSON Schema or other supported formats.

For example, the order service AsyncAPI specification might define two channels—one for Kafka and another for Google PubSub—with different security schemes (username-password for Kafka and OAuth for Google PubSub). Each channel specifies topics like new orders, VIP orders, and accepted orders, along with the corresponding message formats.

AsyncAPI YAML specification showing Kafka and Google PubSub channels

Operations within the specification detail how messages flow. For instance, the place order operation might receive a message on the new orders topic and reply on the work in progress orders topic. Different operations like accept order and initiate order delivery are also mapped accordingly.

Operations section in AsyncAPI defining message flows and patterns

The schema section defines the message payload structure using JSON Schema, including sample payloads to illustrate the expected data. This clarity helps developers understand exactly what data to send or expect.

JSON schema definitions with example payloads in AsyncAPI

Beyond Documentation: Leveraging AsyncAPI for Code Generation and Contract Testing

While having a well-documented AsyncAPI specification is beneficial for communication, its true power lies in enabling automation and contract-driven development.

Code Generation

Developers can generate boilerplate code, message stubs, and client/server skeletons from the AsyncAPI specification. This accelerates development by providing a solid starting point aligned with the agreed contract.

Contract Testing with Specmatic

Tools like Specmatic allow using the AsyncAPI specification as a contract test to verify if implementations adhere to the defined interface. Contract testing ensures that both providers and consumers conform to the agreed messaging patterns, schemas, and security requirements.

Here’s how contract testing typically works:

  • Setup: Define a test environment with necessary components, such as a Kafka broker or a Google PubSub emulator.
  • Test Execution: The contract test tool publishes messages on specified topics using example payloads from the AsyncAPI spec.
  • Verification: The tool listens for expected replies or side effects, validating message formats and adherence to patterns.
  • Result: Tests pass if the actual implementation behaves according to the contract; otherwise, discrepancies are flagged.

Running contract tests using Specmatic with Kafka broker

For example, a test might publish a new order message on the new orders topic and expect a reply on the VIP orders topic. Specmatic uses the example payload defined in the AsyncAPI specification to simulate the client message and waits for the correct response, verifying schema compliance and message flow.

Specmatic publishing a new order message and validating response

This approach catches schema mismatches and communication errors early in the development cycle, reducing costly integration issues later.

Testing Triggers and Side Effects

Some asynchronous interactions involve triggers or side effects that are not explicitly described in the AsyncAPI specification. To manage these, overlays—an extension concept from the OpenAPI initiative—allow adding orthogonal concerns like testing commands or security details without cluttering the core spec.

For instance, an overlay file can specify that when an order accepted operation occurs, a test trigger should send an HTTP request to update the order status, and then verify that a message is published on the accepted order topic. Similarly, side effects such as updating order status upon receiving a delivery notification can be tested.

Overlay file defining test triggers and side effects for AsyncAPI

Demonstrating Multi-Protocol Testing: Kafka and Google PubSub

One of the strengths of AsyncAPI is its ability to abstract protocol-specific details while maintaining a unified interface. Contract tests can be run against different protocols using the same AsyncAPI specification.

For example, running the contract tests for Kafka involves starting an in-memory Kafka broker, launching the order service, and executing the tests. Similarly, the same tests can be run against Google PubSub by using a PubSub emulator and pointing to the same AsyncAPI specification.

Running contract tests on Google PubSub emulator

Both environments use a configuration file that specifies a central Git repository containing the contract specification. This central repository serves as the single source of truth for all API consumers and providers, ensuring consistency across different protocol implementations.

Configuration file linking to central Git repository for contracts

The tests validate that despite differences in security schemes or protocol-specific details, the overall messaging patterns, schemas, and interactions are consistent and compliant with the AsyncAPI contract.

Contract-Driven Development: Shifting Left and Improving Collaboration

The approach described here fits within a broader methodology known as contract-driven development, which emphasizes designing APIs first, agreeing on specifications, and then building implementations around these contracts.

Key steps in contract-driven development include:

  1. API Design First: Architects, developers, and security teams collaborate to define the API specification, covering schemas, messaging patterns, security, and other concerns.
  2. Centralized Specification Repository: The AsyncAPI specification is stored in a central place, often a Git repository, serving as the single source of truth.
  3. Pull Request Workflow: Changes to the specification go through code review, linting, and validation to ensure quality and compliance.
  4. Backward Compatibility Testing: New specification versions are validated against existing implementations to prevent breakages.
  5. Service Virtualization and Mocking: The specification can be used to generate mocks or stubs, enabling parallel development of providers and consumers.
  6. Contract Testing in CI/CD: Automated tests ensure ongoing compliance throughout the development lifecycle, catching contract violations early.

Contract driven development workflow and benefits

This methodology drastically improves developer experience by providing clear, executable documentation and enabling teams to work in parallel. It enhances governance, reduces time to market, and builds more resilient APIs.

Frequently Asked Questions (FAQ)

What are Multi-Protocol Async APIs?

Multi-Protocol Async APIs refer to asynchronous API specifications that support multiple messaging protocols such as Kafka, MQTT, Google PubSub, and JMS. They provide a unified interface to design, document, and implement event-driven architectures across heterogeneous communication systems.

Why is AsyncAPI important for event-driven architectures?

AsyncAPI standardizes how asynchronous message interactions are described, including schemas, topics, messaging patterns, and security. This clarity enables interoperability, reduces integration errors, and supports automation such as code generation and contract testing, making event-driven systems more reliable and maintainable.

How does AsyncAPI handle different messaging protocols?

AsyncAPI allows specifying protocol-specific details alongside protocol-independent information within the same document. This enables a clear, clean specification that abstracts the differences while detailing necessary configuration such as security schemes and topic naming conventions.

What is contract-driven development?

Contract-driven development is a methodology where API contracts (specifications) are designed first and used as a single source of truth for both API providers and consumers. It supports collaboration, parallel development, automated testing, and governance, ensuring all parties adhere to agreed interactions.

Can I use the same AsyncAPI specification for different messaging platforms?

Yes, AsyncAPI specifications can be designed to support multiple protocols by defining channels and security schemes for each protocol. This allows you to run contract tests and generate code for different messaging platforms while maintaining a consistent interface.

What tools can help with AsyncAPI contract testing?

Tools like Specmatic enable contract testing by using AsyncAPI specifications to automatically generate tests that verify if implementations conform to the contract. These tools simulate message exchanges, validate schemas, and check messaging patterns to ensure compliance.

How does AsyncAPI improve developer experience?

AsyncAPI improves developer experience by providing clear, standardized documentation, enabling automated code generation and testing, supporting service virtualization, and fostering collaboration. It helps developers understand asynchronous interactions clearly and accelerates development cycles.

Conclusion

Designing a unified interface for event-driven architectures using Multi-Protocol Async APIs is a powerful strategy to manage the complexity of modern asynchronous systems. By leveraging the AsyncAPI specification, organizations can define clear, standardized contracts that span multiple messaging protocols, enabling seamless interoperability and robust communication.

Through practical examples like the order service, we see how different messaging patterns, schemas, and security mechanisms are captured comprehensively in a single AsyncAPI document. Tools like Specmatic further amplify the benefits by enabling contract testing and service virtualization, fostering better collaboration and accelerating development.

Embracing contract-driven development and shifting left in the API lifecycle ensures that teams can catch issues early, maintain backward compatibility, and deliver resilient, high-quality asynchronous APIs. This approach ultimately reduces time to market and enhances the overall reliability of event-driven systems.

Whether you are working with Kafka, Google PubSub, MQTT, or other protocols, adopting Multi-Protocol Async APIs and the AsyncAPI specification will empower your teams to build scalable, interoperable, and maintainable event-driven architectures.

Thank you for exploring this deep dive into Multi-Protocol Async APIs. We encourage you to explore AsyncAPI further and consider integrating contract-driven development practices into your API strategy for greater success.

More to explore