About Kafka and asynchronous APIs
Apache Kafka is an event-streaming platform. It is a scalable, reliable, and secure way to create asynchronous (or event-driven) APIs.
When a client application sends a request to a RESTful, GraphQL, SOAP, or other synchronous API, it waits for a response. When you work with asynchronous APIs, the client does not need a response, and the server does not send it. Such APIs implement the event-driven architecture.
A typical process looks like this:
A publisher (or producer, in Kafka terms) posts an event (sends a request) to a broker. An event is a piece of data to transmit. It can be a JSON object containing information about a transaction or details of a newly created object in a database, and so on.
To separate one type of events from another, a producer sends it to one of the channels (or topics) specified within a broker.
If another application needs events related to some topic, it subscribes to the needed channel. Once a publisher posts an event to that channel, the subscriber (or consumer) receives the event and can work with it as it is needed by a business logic:
Imagine you have a web store. When a user logs in, a client application posts a login event. When a user creates an order, the client application posts another event with the order details. The client application does not bother about it anymore. In its time, some internal application can get the order’s data from the broker and handle it in some way. For example, store it in a database or send you an email.
|Note:||Here, we use terms used in the AsyncAPI initiative. Kafka uses slightly different terminology: publishers are called producers, subscribers are called consumers, and channels are called topics. Despite their names, they mean the same.|
To add a Kafka API to the project, you need to have any ReadyAPI license.
To test Kafka APIs, you use the API Connection test step. To add it to a test case, you will need a ReadyAPI Test Pro license. If you do not have it, try a ReadyAPI trial.
Working with Kafka in ReadyAPI
To test Kafka-based services in ReadyAPI, you use the API Connection test step. This test step is linked to either the Publish or Subscribe Kafka operation. Depending on a base operation, the test step works as a producer or as a consumer.
Event serialization and schema registry
Kafka provides a way to serialize and deserialize events by using schemas. There are several reasons to do it. First, serialization translates the event data to an array of bytes, so its size is relatively small and requires fewer resources to store and transmit it. Second, the schema guarantees that a producer and a consumer understand each other.
When you use a schema, you bind a schema to a topic, so all the events posted to that topic must comply with that schema.
Schema registry is a dedicated service for storing schemas. The service provides you with schema versioning, and, depending on certain registry settings, it may check compatibility with the previous schema versions.
To serialize events in ReadyAPI:
Open the API Connection test step.
Select the needed format in the drop-down box. Select Custom to specify an Avro or Protobuf schema by using a file.
If you use a schema registry, open Connection Settings and specify the Schema Registry URL and Registry Authorization if needed.
If you use a file, select the needed file in the subsequent dialog and click Add: