AsyncAPI: The Key to Effective Asynchronous Flow Management in Microservices Architecture

  • Home /
  • AsyncAPI: The Key to Effective Asynchronous Flow Management in Microservices Architecture

Today, let’s dive into the fascinating world of AsyncAPI, a principle that revolutionizes how we design and document our asynchronous APIs. I’m truly excited to share with you how we implement AsyncAPI at PMU with our internal and external consumers.

Reminder: What is AsyncAPI?

AsyncAPI is an open-source specification for describing, producing, consuming, and visualizing asynchronous APIs. Think of it as the counterpart to OpenAPI (formerly Swagger) for event-driven architectures. It allows standardized definition of message schemas, communication channels, and protocols used in your asynchronous systems.

In our PMU context, we use it in various systems, particularly around our horse racing information system: Global flow diagram

Usage of AsyncAPI

The main use of AsyncAPI is to provide clear and readable documentation of your asynchronous APIs. This includes:

  1. Defining published and consumed messages
  2. Specifying communication channels (Kafka topics, RabbitMQ queues, etc.)
  3. Describing used protocols (AMQP, MQTT, Websockets, etc.)
  4. Automatic generation of client and server code
  5. Creation of interactive documentation

Here’s a simple example of an AsyncAPI specification:

asyncapi: 2.0.0
info:
  title: Racing Data Async
  version: Schema_Prod
  description: "The racing data AsyncAPI provides PMU partners with real time data on races and odds in order to support their bets on French races.\n\n### Check out its awesome features:\n\n* Consume and updated schedule for ongoing and upcoming races.\n* Consume live events from french races. \n* Compatible with racing data API to access referential data."
    ##termsOfService: https://asyncapi.org/terms/
  contact:
    email: xxx
servers:
  staging:
    url: 'api.stage.pmu.event.com:{port}'
    protocol: Kafka
    description: Test broker to get sample data for partner to develop their data collect system.
    variables:
      port:
        description: Secure connection (TLS) is available through port XXXX.
        default: 'XXXX'
        enum:
          - 'XXXX'
          - 'YYYY'
    security:
      - certificates:  []
  production:
    url: 'api.pmu.event.com:{port}'
    protocol: Kafka
    description: Production system which retrieve real data.
    variables:
      port:
        description: Secure connection (TLS) is available through port XXXX.
        default: 'XXXX'
        enum:
          - 'YYYY'
          - 'YYYY'
    security:
      - certificates:  []

channels:
  '{partnerKey}-race':
    description: The topic will expose a full view of a race raised by a specific event.
    parameters:
      partnerKey:
        $ref: '#/components/parameters/partnerKey'
    subscribe:
      summary: >-
        Receive information about all races.
      operationId: race
      traits:
        - $ref: '#/components/operationTraits/kafka'
      message:
        $ref: '#/components/messages/race'

One of the significant advantages of this definition mode is its format identical to OpenAPI. Indeed, the schema referenced in this code snippet is identical to a schema from the OpenAPI standard.

Advantages and Disadvantages

Advantages in a microservices architecture

  1. Standardization: AsyncAPI offers a common language to describe asynchronous interactions between microservices.
  2. Error reduction: Clear specification of messages reduces misunderstandings and bugs related to data formats.
  3. Versioning: Associated with a registry such as Kafka Registry, AsyncAPI is great for better managing and communicating API versions.
  4. Living documentation: Facilitates maintenance by keeping documentation always up-to-date.
  5. Improved testability: Allows generation of mocks and automated tests based on the specification.

Potential disadvantages

  1. Learning curve: Requires adaptation time for teams accustomed to REST APIs.
  2. Increased complexity: May seem more complex for simple systems.
  3. Evolving tooling: The ecosystem of tools is still developing compared to OpenAPI.

Governance and flow management in enterprise

In an enterprise context, AsyncAPI becomes an indispensable tool for good governance of asynchronous data flows. Here’s why:

  1. Visibility: Offers a clear overview of all data flows in the company.
  2. Compliance: Facilitates compliance with regulations by precisely documenting data exchanges.
  3. Onboarding: Accelerates the integration of new developers by providing clear documentation.
  4. Scalability: Allows for efficient planning and management of distributed systems evolution.
  5. Collaboration: Improves communication between teams by providing a common reference.

Conclusion: AsyncAPI in action at PMU

PMU recently adopted AsyncAPI to manage its real-time data flows, particularly for updating odds and managing live betting.

Thanks to AsyncAPI, PMU was able to:

  • Standardize communication between its betting and race management microservices with its international partners and internal developers.
  • Improve communication between development teams.
  • Facilitate the integration of new partners thanks to clear documentation of its APIs.

To date, I consider this implementation a success at PMU for the benefits it has brought. However, we shouldn’t underestimate the change management required. It’s an excellent example of how good governance of asynchronous APIs can lead to significant improvements in terms of efficiency, reliability, and innovation.

In conclusion, if you’re working on distributed systems or event-driven architectures, I strongly recommend exploring AsyncAPI. It’s an investment that will pay dividends in the long term in managing your asynchronous data flows. Happy coding!