• Cadence
  • Dev Rel
  • Apache Kafka
  • Technical
Spinning Your Drones With Cadence and Apache Kafka® – Architecture, Order and Delivery Workflows – Part 4

In Part 3 of my Cadence blog series, we introduced the Drone Delivery Demo application, focusing on the Drone Workflow. In this blog we’ll look at the Drone Delivery from the perspective of the Order Workflows, understand how the Drone and Order workflows interact, discover some extra Cadence+Kafka integration patterns, and explore some new Cadence features (e.g. retries, side effects, queries, and continue as new).

1. The Drone Delivery Application Architecture

As we explore the Drone Delivery application further, we’ll use this high-level architecture diagram to see what’s going on in more detail. The top 2 swimlanes are the Apache Kafka® components, and the bottom 2 swimlanes are the Cadence Workflows. I’ve simplified the workflow steps for clarity. Looking at the bottom swimlane, each Drone Workflow maps to an actual physical drone, with steps corresponding to drone delivery lifecycle events. 

From the perspective of Orders, the numbered steps reflect the Order/Delivery lifecycle events as follows:

  1. Create new Order
  2. Create new Order Workflow
  3. Order ready for delivery
  4. Drone waiting for order
  5. Drone allocated an Order
  6. Order picked up by Drone and on way to delivery, location updated while in flight
  7. Order delivered and checked, order completed

Now let’s look at the Order flow in more detail.

2. New Order

A customer orders something from the Drone Delivery App to start the Order and Drone delivery process (Source: Shutterstock)

The first step in the Order lifecycle is a customer actually ordering something from a drone delivery app. We assume this triggers a Create New Order event which is placed on the Kafka New Orders topic. This brings us to the first of our new Cadence+Kafka integration patterns, the “Start New Cadence Workflow from Kafka” pattern (1) below.

This pattern is simple. An independent Kafka consumer runs forever, and picks up the next order from the New Orders topic, and using a Cadence client it creates and starts a new Order Workflow Instance. Here’s some example code:

So this is an example of running some Cadence code in a Kafka consumer. We’ve actually seen something similar to this before in Part 2, where we sent a signal to a running Cadence Workflow in a Kafka Consumer. The difference is that in this example we start a Cadence Workflow.

3. The Order Workflow

The Order Workflow is straightforward. Once started, it generates random order and delivery locations (that are guaranteed to be within drone delivery and return to the base range), sends a message to Kafka in an activity to say it’s ready for delivery (see below), updates location state in a loop (received via a signal from the Drone Workflow), and then waits until the  “orderComplete” state to exit. Other activities are plausible, including checking for delivery violations and sending location updates to Kafka for analysis and mapping.

We’ll explain the Side Effects below.

4. Drone Gets Next Order for Delivery

A taxi rank models people (orders) waiting in line for the next available taxi (drone). (Source: Shutterstock)

Once the order is ready for pickup (potentially after a delay due to order preparation time), we’re ready for the major coordination between the Drone and Order workflows, using the “get the next job from a queue” Cadence+Kafka pattern (2, 3).

The behaviour we want from this interaction is (a) drones become ready to be allocated an order to deliver, (b) orders become ready for delivery, (c) exactly one order is allocated to exactly one drone. That is we don’t want drones “fighting” over orders, drones trying to deliver more than one order, or orders that miss out being allocated a drone.  (a) and (b) can occur in any order, and there may be 0 or more drones or orders ready at any time. 

How does this pattern work in practice? It actually consists of two Cadence+Kafka sub-patterns. 

The first pattern (2) is a simple one-way notification from Cadence to Kafka. The Order Workflow has an activity, readyForDelivery(), that wraps a Kafka producer. This is a remote call so can fail, so that’s why I used a Cadence activity even though it’s not long running, and doesn’t wait for a reply, unlike the Cadence+Kafka microservices pattern we demonstrated in blog 2, which sends a notification, and then waits for a reply from Kafka. The producer sends the Order’s ID to the “Orders Ready” topic, and then the Order Workflow blocks while waiting for a signal from a Drone to say that it has been picked up, using Workflow.await().

But how does a Drone acquire a ready order? This is where the second pattern comes in (3).  The Drone Workflow has a Wait For Order Activity (3). This wraps a Kafka consumer (3a) which actually runs in the Cadence activity thread, so it is transient, lasting only as long as the activity runs. It polls the Orders Ready topic until a single order is returned (3b), resulting in the activity completing (3c).

Kafka consumers are used in a slightly atypical way to normal for this use case. There is exactly one consumer per Drone Workflow in the Wait for Order state, the consumer keeps polling the topic until a single record is returned, and then terminates. To ensure only 1 order is returned, we set max.poll.records to 1.

Each of these consumers share a common consumer group, so orders are distributed among all the waiting Drones, but only one drone can get each order. We don’t use a Kafka key, so records are just delivered to consumers in a round-robin fashion. There may be some overhead due to consumers joining and leaving the group regularly (mainly lag due to rebalancing), and as the number of drones increases the number of partitions on the topic needs to increase to ensure that there are sufficient partitions for the number of consumers. The rule is partitions >= consumers. You may be tempted to set partitions to a very high number to start with, but previous experiments reveal that too many partitions can reduce Kafka cluster throughput, and that there is an optimal number of partitions depending on the cluster size (<= 100 partitions is fine for normal operation). If you do happen to have lots more drones, just increase the size of your Kafka cluster and it will keep pace.  Here’s the waitForOrder() activity implementation:

The code is available in our Github repository.

Well, that’s enough for this blog. In the next part, we’ll continue with a summary of the Cadence+Kafka integration patterns we’ve used, and cover some of the new Cadence features in more detail. 

Discover the incredible benefits Cadence can provide to your organization!

Download white paper

Follow the series: Spinning Your Drones With Cadence

Part 1: Spinning Your Workflows With Cadence!

Part 2: Spinning Apache Kafka® Microservices With Cadence Workflows

Part 3: Spinning your drones with Cadence

Part 4: Architecture, Order and Delivery Workflows

Part 5: Integration Patterns and New Cadence Features

Part 6: How Many Drones Can We Fly?

Other articles
Read All