Introducing the new Cumulocity Messaging Service
Tags: iot, cumulocity, iotplatform
Published: 2021-12-14 10:05:00

---
title: Introducing the new Cumulocity Messaging Service
published: true
date: 2021-12-10 14:20:58 UTC
tags: iot, cumulocity, iotplatform
canonical_url: https://tech.forums.softwareag.com/t/introducing-the-new-cumulocity-messaging-service/253841
---

In this second follow-on blog post on Cumulocity messaging architecture, we look behind the new [Notification 2.0 API](https://tech.forums.softwareag.com/t/introducing-the-new-cumulocity-notifications-2-0-api/253669) to introduce the new Cumulocity Messaging Service and then see how it was used to enhance the DataBroker. Unlike for Notifications 2.0, the DataBroker modifications do not introduce new API but enhance the underlying quality of service (QoS) offered by the platform.

Cumulocity, in the cloud and on-premise version, gained an important new platform service in the 10.10 release: the new Cumulocity Messaging Service which is based on the open source [Apache Pulsar](https://pulsar.apache.org/) project. First used for the enhanced DataBroker in 10.10, and now in 10.11, the messaging service underlies the new Notifications 2.0 API. As the following diagram shows, messaging is built-in part of the platform in the logical architecture but deployment is currently optional.

[![c8ylogicalarch](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/2/9/291f5d581fa84e652d6a164dcd1a1e0c03ff27a8_2_690x316.png)](https://aws1.discourse-cdn.com/techcommunity/original/3X/2/9/291f5d581fa84e652d6a164dcd1a1e0c03ff27a8.png "c8ylogicalarch")

The Cumulocity messaging service is deployed in Kubernetes and as a platform service provides for “at-least-once” messaging semantics. In 10.11, we’ve extended it with an WebSocket endpoint so that microservices can access it indirectly but securely to receive notifications. Pulsar is not currently available on Edge deployments but will be soon (targeting 10.13, with lesser message persistence guarantees). We expect the new service to become a required platform component of Cumulocity going forward, so that the enhanced DataBroker and the new Notifications 2.0 API are available everywhere and further allowing leveraging the capabilities of messaging throughout the platform.

Our enhancements mean that the version of Apache Pulsar deployed must be from Cumulocity / Software AG. We provide a Helm chart to install and update deployments (please see the Cumulocity Operations Manual for details).

## Introducing Apache Pulsar

Apache Pulsar provides for both messaging and streaming at IoT / Internet scale and has many advanced features that make it a great basis to build on. Pulsar was selected after an extensive evaluation and comparison with other message broker & streaming solutions (Apache Kafka for messaging and ActiveMQ for queueing were other finalists among the alternatives evaluated). A major primary factor in selecting Pulsar was the [support for both messaging and streaming](https://thenewstack.io/apache-pulsar-a-unified-queueing-and-streaming-platform/?utm_campaign=Monthly%20Newsletter&utm_medium=email&_hsmi=181331693&_hsenc=p2ANqtz--zsusFwHYmjL1oaKniXsK_BHM3_aG4K5b1DC-kQWWp7Ixr6l5Q4FSo-kNagTPIlK__UG8NjWU-5Jn_AVGi80vYvakj2sli5H_hu5OJvX7IaV-HEhA&utm_content=181331000&utm_source=hs_email) with a clean message subscription model that allows both individual (multiple if needed) and grouped subscriptions. Another important consideration is ordering of messages, as published by or resulting from requests from a single device, even in the presence of failures.

[![pulsartopic](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/0/8/087989393d07b220460b4466829dad4e976adf68_2_681x500.png)](https://aws1.discourse-cdn.com/techcommunity/original/3X/0/8/087989393d07b220460b4466829dad4e976adf68.png "pulsartopic")

Apache Pulsar is scalability to potentially millions of topics and has support for multiple tenants (requirements familiar to all users of IoT and Cumulocity). It offers very strong message persistence guarantees, while still providing [low average latency](https://streamnative.io/blog/tech/2020-11-09-benchmark-pulsar-kafka-performance/), supported by a separately scalable storage layer in a tier of “Bookies” behind stateless “Brokers”, which was another main reason for selecting Apache Pulsar. The ability to scale out message consumers dynamically using “partitioned topics” is another important feature that we hope to leverage more heavily in future.  
 ![pulsarlayers](https://aws1.discourse-cdn.com/techcommunity/original/3X/c/a/cace9a6e93997f9d8dd6deb221f44a8844652e6c.png)

The exceptional reliability provided by Pulsar is due to storing 3 separate copies of any message and being able to scale up and recover failed replicates when inevitable failures happen in the cloud or in on-premise dedicated hardware. The default settings used in Cumulocity trade off this fault tolerance for even better latency by requiring only two writes to be acknowledged but this could be dialled up to 3 write acknowledgements if very strong message persistence guarantees are required. The storage layer (see diagram above) is based on Apache Bookkeeper which maintains a separate Journal or write ahead log for recovery with topic data stored in segments of “Ledger” files. Apache Zookeeper is used to both coordinate a Pulsar cluster and store meta data reliably (not shown in diagram but there are at least 3 Zookeeper instances deployed with their own persistent state). On Edge deployments, only one copy is saved to disk using the POSIX fsync system call without replication, so only temporary failures are recoverable.

The message delivery semantics provided is “at-least-once”. Once accepted, each message is delivered repeatedly until the consuming application acknowledges it. This can result in duplicates but ensures that there is no message loss, allowing fault tolerant applications to be constructed relatively simply and to be scaled. Many IoT applications can make do with weak “at-most-once” semantics, where a message is delivered once or not at all. But that is difficult to do in practice and certainly does not cover all use cases. Before the new features that use the new message service, Cumulocity only provided weaker message delivery guarantees, especially under heavy load or in the face off node failures. A contention of the new messaging service is that stronger “at-least-once” guarantees can be provided in a performant, scalable and cost-effective way, making it simpler to build IoT applications and integrations. But applications with state must still deal with potential duplicate or use idempotent messages (such as measurements with a unique client generated timestamp).

In the rest of this blog post, we examine how we have used the new messaging service to enhance the DataBroker feature in Cumulocity, building on the physical architecture diagrammed below:

[![c8yphysicalarch](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/3/6/369676e6caa48c744b9872fc9955f793e46e0f70_2_690x375.jpeg)](https://aws1.discourse-cdn.com/techcommunity/original/3X/3/6/369676e6caa48c744b9872fc9955f793e46e0f70.jpeg "c8yphysicalarch")

## Microservice Enhanced DataBroker

The Cumulocity data broker has been enhanced with a per-tenant Databroker microservice agent. This must currently be subscribed by a tenant explicitly and when subscribed (as is the case for the tenant in the screen shot of the UI below), redirects all DataBroker connections through Pulsar and the per tenant microservice.  
 ![c8yappui](https://aws1.discourse-cdn.com/techcommunity/original/3X/a/5/a55c724e4071c9bbd71c1df58a5398df6d9f2a50.png)

The Cumulocity DataBroker feature allows all API request, whether over REST or MQTT, to be forwarded to a remote tenant, most likely to a remote Cumulocity system, by defining a forwarding “connection” to a remote URL. Without Cumulocity messaging and the new DataBroker microservice agent, this forwarding is best effort only. If the remote system destination is unavailable or responding only slowly then requests will be dropped, and the two Cumulocity Systems (local and remote) would diverge.

Adding the Databroker microservice agent will cause messages to be persisted in a per connector stream in Pulsar for forwarding by the microservice. Using a single microservice agent consumer also allows message ordering to be preserved, which as noted above is important in maintaining synchronized state in some applications. Note that, the microservice can still process different connectors in parallel, if provisioned with multiple CPUs, so some vertical scalability can be achieved even in a single tenant. Performance measurements have shown that Pulsar and the microservice can comfortably forward at a similar rate as the unenhanced Databroker over a typical bandwidth limited wide area connection.

[![DataBrokerWithPulsar](https://aws1.discourse-cdn.com/techcommunity/optimized/3X/3/b/3b39447b5dd7d2ae7a0c564c23607430eebd7b1c_2_690x271.png)](https://aws1.discourse-cdn.com/techcommunity/original/3X/3/b/3b39447b5dd7d2ae7a0c564c23607430eebd7b1c.png "DataBrokerWithPulsar")

The microservice listens for connection activations after reading the current set of connectors on start up and then consumes messages on a per-connector Pulsar topic. Messages read are queued for forwarding, using the normal Cumulocity REST API to the configured remote destination tenant. When a REST call to the destination completes the microservice acknowledges the Pulsar message. This prevents re-delivery of the acknowledged message and processing moves on to the next Pulsar message and corresponding API forwarding request. Crashes of the microservice or underlying node are handled by Kubernetes restarting the microservice.

Note that, over time, the ingress rate must not be higher than the average long-term forwarding rate or eventually (Bookie disk) storage will be exhausted. It is possible to set policies that limit message retention and time to live. These can be configured on a per-tenant basis and are documented in the Cumulocity operations guide. In the unlikely case of Pulsar being unavailable, client/device requests that are routed to the enhanced data broker will fail with a service error, allowing the clients to attempt retries.

The Databroker microservice agent is optionally available in Cloud and on-premise deployments now and will be available in Edge systems soon. With this, Edge deployments will effectively gain a large buffer that can survive temporary crashes or wide area network outages but not disk crashes or other disasters. In future, the enhanced Databroker will be made the default DataBroker implementation. It’s also available for experimentation on [eu-latest.cumulocity.com](https://eu-latest.cumulocity.com) for trial use. Please see the operations documentation on how to enable Cumulocity Messaging and particular of configuring and using the enhanced DataBroker.

We hope this follow up blog post has given an overview of the new Cumulocity Messaging service and introduced the enhanced DataBroker as the first feature to have made used of the new platform service. The new 10.11 Notification 2.0 API is also built on the new messaging service, and the service will become non-optional and central to the Cumulocity platform going forward.

Andre Kramer, Software Architect, Cumulocity Messaging Service.

[Read original article](https://tech.forums.softwareag.com/t/introducing-the-new-cumulocity-messaging-service/253841)

[Part 1 of the blog](https://tech.forums.softwareag.com/t/introducing-the-new-cumulocity-notifications-2-0-api/253669)