What is Apache Pulsar ? Installation and Usage Example

Apache Pulsar is an open source data processing software. It is a scalable and high-performance messaging software. It is especially designed for big data processing and real-time data stream processing applications. It receives data from many different data sources, processes that data, and distributes its results to various applications and services. To streamline data, Pulsar uses publish/subscribe and queue mechanisms. In addition, Pulsar has scalable, multi-tenant and multi-cloud support.
Some features of Pulsar include:
- High performance: It can process billions of messages per second and provides millisecond delays.
- Durability: It has multiple copy and data backup features so that messages are not lost.
- Scalable: Easily scales horizontally and can run on thousands of servers.
- Multi-tenant: Can handle messages from different tenants separately.
- Multi-cloud support: It can run on different cloud providers and local data centers.
Apache Pulsar is an open source project managed by the Apache Software Foundation and is used by many institutions and companies.
Let’s build Pulsar and start reviewing:
Make sure Docker is installed:
To check if Docker is installed, open terminal window and run docker -v
command. If Docker is not installed, you can download and install it from Docker's official website.
Download Pulsar’s Docker image:
Run the following command in the terminal window:
docker pull apachepulsar/pulsar:latest
This will download the latest version of Pulsar via Docker Hub.
Start Pulsar:
Run the following command in the terminal window:
docker run -it -p 6650:6650 -p 8080:8080 apachepulsar/pulsar:latest bin/pulsar standalone
This command will start Pulsar on Docker.
Test if Pulsar is working:
Go to http://localhost:8080
in your browser.
The Pulsar web interface will open and you can see if your Pulsar broker is up and running.
Now let’s make a Pulsar communication example using Javascript:
First we need to download the Pulsar NPM package:
npm install pulsar-client
Next we need to create a Pulsar client:
const pulsar = require('pulsar-client');
const serviceUrl = 'pulsar://localhost:6650';
const client = new pulsar.Client({
serviceUrl: serviceUrl,
});
To broadcast messages from your microservice, create a generator using the Pulsar client.
const producer = await client.createProducer({
topic: 'my-topic',
})
await producer.send({
data: Buffer.from('Hello, Pulsar!'),
});
To consume messages from your microservice, create a consumer using the Pulsar client.
const consumer = await client.subscribe({
topic: 'my-topic',
subscription: 'my-subscription',
});
await consumer.receive((msg) => {
console.log(`Received message: ${msg.getData()}`);
consumer.acknowledge(msg);
});
In this example scenario, we implemented messaging between microservices via the Pulsar client. To communicate between microservices, you can broadcast messages to a specific topic (topic) and listen to messages via a consumer. This is a very useful communication method for microservice architecture.
Of course, this example scenario is just a basic explanation. Real-world scenarios can be much more complex and require more code to handle issues such as client configuration, topic naming, and message handling. However, Pulsar’s JavaScript client library is very useful and well-documented, so you can check out the official Pulsar documentation for more information.
Thank you for reading.
Would you like to read my other article about Distributed Message systems? Redis vs Kafka vs RabbitMQ