Skip to content

Integrating IBM i with Kafka and message queues

B2Bnetwork team · · 7 min

IBM i, RPG and mainframe

In brief

IBM i connects to Apache Kafka in three ways: change data capture (CDC) from database journals, events the application writes to an outbox table or a data queue that a separate producer sends to Kafka, and IBM MQ with a Kafka Connect connector. CDC requires no changes to RPG programs, but it publishes row changes rather than business events. Whichever path you choose, you have to plan ordering by key, redelivery of the same event and conversion from EBCDIC to UTF-8.

When to use events and when to use REST

REST answers a question asked right now: what is the stock level, what is the order status. Events tell other systems that something happened: an order was accepted, goods were shipped, a payment was posted. There can be many consumers, and IBM i does not need to know who they are or whether they are currently running. According to the Apache Kafka documentation, events go to topics divided into partitions, and order is preserved within a partition.

For IBM i, the difference is practical. Calling someone else's API from an RPG program ties the program's run time to the availability of the receiver. Writing an event that a separate process sends decouples the two: an outage of the receiving system does not stop the nightly batch. Exposing REST services from IBM i is covered in the article IBM i and modern applications: integration through REST APIs, and the first decisions when adopting Kafka in the article Apache Kafka in the enterprise: first architecture decisions.

Three paths from IBM i to Kafka

PathHow it worksAdvantagesLimitations
CDC from journalsA tool reads table journal entries and publishes row changesNo changes to programsAn event is a row change, and consumers know the table structure
Outbox table or data queueThe program writes a business event, a producer in PASE or on a separate server sends it to KafkaEvents in business language, full control over contentChanges to programs, your own sending process
IBM MQ with Kafka ConnectThe program sends a message to an MQ queue, a connector copies it to a Kafka topicAssured delivery, ready-made toolsAnother system to maintain if the company does not run MQ yet

CDC from journals

Journaled files record every change in a journal. A CDC tool reads these entries and publishes them to Kafka, usually as the row image before and after the change. Besides commercial tools, there is an incubating Debezium connector for IBM i which, according to the project description, uses the journal as its source of changes. CDC works well for feeding a data warehouse or a copy of the data. It works less well for events meant for other teams, because every change in table structure becomes a contract change. See the article Change Data Capture: database changes without loading the system for more.

Outbox tables and data queues

The outbox pattern means the program writes the business data and the event to a separate table in the same transaction. A separate process reads the new rows, sends them to Kafka and marks them as sent.

CREATE TABLE ORDERS.OUTBOX (
  ID         BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  TOPIC      VARCHAR(100) NOT NULL,
  MSG_KEY    VARCHAR(50) NOT NULL,
  PAYLOAD    CLOB(1M) CCSID 1208 NOT NULL,
  CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT TIMESTAMP,
  SENT_AT    TIMESTAMP
);

The columns hold the topic, the message key, the UTF-8 payload, the creation time and the time it was sent.

A data queue (*DTAQ) is a faster and lighter mechanism: the program sends an entry and the producer receives it immediately, without polling a table. In SQL you use the QSYS2.SEND_DATA_QUEUE procedure and the QSYS2.RECEIVE_DATA_QUEUE table function, and from RPG the QSNDDTAQ and QRCVDTAQ APIs. However, writing to a table and sending a queue entry are two separate operations. If the program is interrupted between them, the event is lost or appears without the data. When an event must match the database write exactly, an outbox table is the safer choice.

The Kafka producer most often runs in PASE on the same server (the Java Kafka client, or libraries for Node.js and Python) or on a separate server that connects to the database.

IBM MQ and Kafka Connect

If the company already uses IBM MQ, programs on IBM i can send messages to an MQ queue, and the Kafka Connect source connector from IBM copies them to Kafka. In the other direction, the sink connector moves messages from Kafka to MQ. This is a good choice when moving from an integration bus to an event platform, as described in the article ESB or event-driven architecture.

Data, ordering and redelivery

A few decisions determine whether the integration still works after the first failure:

  • Encoding. Data on IBM i is usually in EBCDIC, and numeric fields are often packed. An event in Kafka should be in UTF-8, with numbers and dates in an ordinary format. The simplest way is to build it in SQL, which converts the encoding itself, rather than copying RPG data structures byte by byte.
  • Key and ordering. The message key is the identifier of the object, for example the order number. Then all events for one order land in the same partition, and the consumer sees them in order.
  • Redelivery. A producer that did not receive an acknowledgment will send the event again. Kafka has an idempotent producer, but the consumer should still recognize a duplicate by the event ID.
  • Schema. The event format is a contract. Changes are introduced so that existing consumers keep working, for example by adding fields instead of changing the meaning of existing ones.
  • Monitoring. The number of unsent outbox rows and consumer lag are two indicators that reveal a problem before users report it.

Consuming Kafka events on IBM i

The reverse direction is built in a similar way. A consumer in PASE or on a separate server reads the topic, writes the event to an inbound table or a data queue and calls a procedure that processes it. The offset is committed only after the event has been saved, and processing checks the ID so that a repeated event does not change the data a second time. General rules for this kind of processing are covered in the article Queues and asynchronous processing.

The project on replacing an ESB with a Kafka event platform for payments shows an example of replacing an integration bus with an event platform in banking.

Questions and answers

Can Kafka run on IBM i?

Kafka clients can: a producer or consumer in Java, Node.js or Python runs in the PASE environment on the same server as the database and RPG programs. Kafka brokers usually run on separate servers or as a cloud service, and IBM i connects to them over the network. That way you do not have to maintain a Kafka cluster on IBM i.

Data queue or outbox table?

An outbox table when the event must match the database write exactly, for example accepting an order or posting an entry. The event and the data are written together or not at all. A data queue is faster and simpler, so it fits events whose loss or delay will not change the state of another system, such as notifications and refreshing a preview.

How do I keep the order of IBM i events in Kafka?

Kafka preserves order within a partition, so all events for one object need the same key, for example the order number. On the IBM i side, the producer sends outbox rows in ID order and does not move on until the previous one has been acknowledged for the same key. Ordering across different orders is usually not needed.

Do RPG programs have to change to integrate with Kafka?

Not with CDC from journals: journaling the tables and a tool that reads the journal are enough. Changes are needed for business events, because the program has to write the event to the outbox or a queue. That is usually a few lines where the program commits the operation, not a rebuild of the application.

What it looks like in a project

Developing a system on IBM i or mainframe? Talk to an RPG engineer.

Let's talk

Read more: IBM i, RPG and mainframe