An Azure Blob storage based solution to store checkpoints and to aid in load balancing when using EventProcessor
from the @azure/event-hubs library
Source code | Package (npm) | API Reference Documentation | Samples
Install the Azure Event Hubs Checkpoint Store Blob library using npm
npm install @azure/eventhubs-checkpointstore-blob
Prerequisites: You must have an Azure subscription, an Event Hubs Namespace to use this package, and a Storage account
If you are using this package in a Node.js application, then use Node.js 8.x or higher.
TypeScript users need to have Node type definitions installed:
npm install @types/node
You also need to enable compilerOptions.allowSyntheticDefaultImports
in your tsconfig.json. Note that if you have enabled compilerOptions.esModuleInterop
, allowSyntheticDefaultImports
is enabled by default. See TypeScript's compiler options handbook for more information.
-
Scale: Create multiple consumers, with each consumer taking ownership of reading from a few Event Hubs partitions.
-
Load balance: Event Processor based application consists of one or more instances of
EventProcessor
which have been configured to consume events from the same Event Hub and consumer group. They balance the workload across different instances by distributing the partitions to be processed among themselves. -
Checkpointing: It is a process by which readers mark or commit their position within a partition event sequence. Checkpointing is the responsibility of the consumer and occurs on a per-partition basis within a consumer group. This responsibility means that for each consumer group, each partition reader must keep track of its current position in the event stream, and can inform the service when it considers the data stream complete.
If a reader disconnects from a partition, when it reconnects it begins reading at the checkpoint that was previously submitted by the last reader of that partition in that consumer group. When the reader connects, it passes the offset to the event hub to specify the location at which to start reading. In this way, you can use checkpointing to both mark events as "complete" by downstream applications, and to provide resiliency if a failover between readers running on different machines occurs. It is possible to return to older data by specifying a lower offset from this checkpointing process. Through this mechanism, checkpointing enables both failover resiliency and event stream replay.
A BlobPartitionManager is a class that implements key methods required by the Event Processor to balance load and update checkpoints.
Use the below code snippet to create a Partition Manager. You will need to provide the connection string to your storage account.
import { ContainerClient } from "@azure/storage-blob",
import { BlobPartitionManager } from "@azure/eventhubs-checkpointstore-blob"
const containerClient = new ContainerClient("storage-connection-string", "container-name");
await containerClient.create(); // This can be skipped if the container already exists
const partitionManager = new BlobPartitionManager(containerClient);
To checkpoint events received using an EventProcessor to Azure Blob Storage, you will need to pass an instance of the Partition Manager to the Event Processor along with the code to call the updateCheckpoint()
method.
In this example, we will use a SamplePartitionProcessor
that extends the PartitionProcessor class in order to checkpoint the last event in the batch.
import { ContainerClient } from "@azure/storage-blob",
import { BlobPartitionManager } from "@azure/eventhubs-checkpointstore-blob"
import { EventHubClient, PartitionProcessor } from "@azure/event-hubs"
const eventhubClient = new EventHubClient("event-hub-connectionstring")
const containerClient = new ContainerClient("storage-connection-string", "container-name");
await containerClient.create(); // This can be skipped if the container already exists
const partitionManager = new BlobPartitionManager(containerClient);
SamplePartitionProcessor extends PartitionProcessor {
async processEvents(events) {
if (events.length) {
/* custom logic for processing events, then checkpoint*/
await this.updateCheckpoint(events[events.length - 1]);
}
}
}
const eventProcessor = new EventProcessor(
EventHubClient.defaultConsumerGroupName,
client,
SamplePartitionProcessor,
partitionManager);
eventProcessor.start();
You can set the following environment variable to get the debug logs when using this library.
- Getting debug logs from the Eventhubs Checkpointstore Blob
export DEBUG=azure:eventhubs-checkpointstore-blob*
- If you are interested only in errors, then you can set the
DEBUG
environment variable as follows:
export DEBUG=azure:event-hubs:error,azure:eventhubs-checkpointstore-blob:error
-
Set the
DEBUG
environment variable as shown above and then run your test script as follows:-
Logging statements from your test script go to
out.log
and logging statements from the sdk go todebug.log
.node your-test-script.js > out.log 2>debug.log
-
Logging statements from your test script and the sdk go to the same file
out.log
by redirecting stderr to stdout (&1), and then redirect stdout to a file:node your-test-script.js >out.log 2>&1
-
Logging statements from your test script and the sdk go to the same file
out.log
.node your-test-script.js &> out.log
-
Please take a look at the samples directory for detailed example.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.