blog by OSO

Real-time Push APIs Using Kafka 

Sion Smith
Sion Smith 14 July 2023
real-time-push-apis-using-kafka

We all love real-world examples of how to use Kafka, so let’s look at building real-time push APIs with Kafka as the customer-facing interface. We cover the trade-offs of push and pull, how authentication and authorisation work, and how to document event APIs.

Is Kafka a push or pull model?

Kafka is a pull model. Consumers call poll() and fetch records at their own pace, and the broker keeps every record for the topic’s retention period. That means a consumer can fall behind, restart or replay history without the broker needing to track delivery for each message.

A push-based approach usually involves a smart broker and a dumb client. It can be harder to run: you need retry strategies, you must size consumers so they are not overloaded, and once a consumer acknowledges a message it normally cannot be received again. That is a problem for B2B customers who need to replay messages after an incident.

Push (broker delivers)Pull (Kafka consumers poll)
Who controls the paceThe brokerThe consumer
Replay after an incidentUsually not possible once acknowledgedYes, reset offsets within retention
Overload riskConsumers can be floodedConsumers fetch what they can handle
Typical fitWebhooks, mobile notificationsB2B streaming, analytics, IoT back ends

Key takeaway: Offer push-style delivery at the edge if customers need it, but keep Kafka’s pull model behind it so replay and back-pressure still work.

How do you build an event-driven API ecosystem like REST?

To match the experience of a REST API, you need three things: authentication that clients already understand, authorisation that limits what each customer can read, and documentation that developers can browse in a portal. Each is covered below.

How do authentication and authorisation work in Kafka?

Kafka supports several authentication mechanisms, including mTLS, SASL/SCRAM and SASL/OAUTHBEARER. With OAuth 2.0 token authentication, clients can use the same identity provider for REST requests and for connecting to the Kafka broker, which keeps the experience consistent across your APIs.

Authorisation is based on Kafka resources such as topics and consumer groups, using ACLs (access control lists). Authorisation applies at topic level, so a consumer authorised for a topic can read every message in it. This needs careful thought when you structure topics for different customers: a shared topic with a customer key is not isolated, whereas one topic (or prefix) per customer is. If you are unsure how to design your topics, speak to one of our experts or see our Kafka services.

How do you document event-driven APIs?

Clear documentation is crucial for any API ecosystem. For REST, OpenAPI is the widely used standard. For asynchronous and push-based APIs, AsyncAPI offers a similar look and feel, and it is designed to be compatible with OpenAPI tooling. Publishing both in a developer portal gives customers one consistent place to learn how to connect.

If you want a full end-to-end example of Kafka with AsyncAPI, take a look at SpecMesh.

Why are event-driven APIs a good match for IoT?

Combining Kafka with OAuth 2.0 and AsyncAPI gives customers an experience close to a REST API, with real-time data on top. It suits IoT well, where devices produce a constant stream of events.

Mind the limits: authorisation is at topic level, which affects topic design, and a pull-based approach may suit B2C scenarios better where device availability and bandwidth are constrained. For B2B scenarios, Kafka’s flexibility and compatibility with existing infrastructure make it a strong choice.

See how German start-up Kugu followed this approach to build a digital platform that lowers the carbon emissions of buildings. For related reading, see Event-driven architecture: a simple guide and Protecting your Kafka cluster. If you need help running Kafka in production, our Kafka support team can help.

Frequently asked questions

Is Kafka push or pull?

Kafka is a pull model. Consumers poll brokers for records at their own pace, and the broker retains data for the configured retention period. This is what makes replay and back-pressure straightforward compared with push-based brokers.

Can you build a push API on top of Kafka?

Yes. You can expose topics to customers directly over the Kafka protocol, or put a gateway in front that delivers events by webhook, WebSocket or server-sent events. The gateway consumes from Kafka and pushes outwards, so you keep Kafka’s replay behind the scenes.

How do you secure a Kafka-based API for customers?

Authenticate clients with OAuth 2.0 (SASL/OAUTHBEARER), mTLS or SCRAM, then restrict access with ACLs on topics and consumer groups. Because authorisation is at topic level, give each customer their own topic or topic prefix.

How do you document an event-driven API?

Use AsyncAPI. It describes channels, messages and security schemes in the same spirit as OpenAPI, and can be published in a developer portal alongside your REST documentation.

Why does replay matter for B2B customers?

Replay lets a customer recover from a bug or outage by re-reading events from an earlier offset. In a push model, acknowledged messages normally cannot be delivered again, whereas Kafka keeps them until retention expires.

Need help with Apache Kafka?

Talk to OSO’s Kafka engineers about your next step.

CONTACT US
OSO
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.