Skip to content

Commit 6874cdb

Browse files
committed
kafka vs RabbitMQ
1 parent 836e48f commit 6874cdb

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

docs/event_streaming_message/apache_kafka.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,151 @@ RabbitMQ, and Kafka are designed for messaging or communication between systems
122122
| **Kafka** | **High-throughput event streaming**, **log aggregation**, **real-time analytics**| **Overly complex** for simple tasks, lacks **routing** and **priority features**, more setup and management |
123123

124124
RabbitMQ is ideal for cases where **reliability, routing, and discrete task management** are important, while Kafka excels at **high-throughput, durable event streams** where **scalability** and **real-time analytics** are the focus. Each platform serves different types of workloads, and using the wrong one for a particular job can result in added complexity and inefficiencies.
125+
126+
127+
## Point-to-Point and Publish-Subscribe
128+
129+
130+
### 1. **RabbitMQ****Point-to-Point** and **Publish-Subscribe**
131+
RabbitMQ is a message broker that supports both the **Point-to-Point** and **Publish-Subscribe** messaging patterns.
132+
133+
- **Point-to-Point (Queue-based messaging)**: In RabbitMQ, a sender can publish a message to a queue, and the message will be delivered to **one specific receiver** that pulls the message from the queue. This fits the **Point-to-Point** pattern.
134+
135+
- **Publish-Subscribe (Exchange-based messaging)**: RabbitMQ also supports the **Publish-Subscribe** pattern through **exchanges**. A publisher sends a message to an exchange, which routes the message to all the queues bound to that exchange. Subscribers, consuming messages from their respective queues, receive a copy of the message.
136+
137+
### 2. **Kafka****Publish-Subscribe**
138+
Kafka implements a **distributed log-based messaging system** that is primarily designed for the **Publish-Subscribe** pattern.
139+
140+
- **Publish-Subscribe (Topic-based messaging)**: In Kafka, a producer publishes messages to a **topic**, and any number of **consumers** (subscribers) can subscribe to that topic to consume the messages. Kafka ensures durability and allows replay of messages from the log, making it very suitable for event streaming and high-throughput, fault-tolerant use cases.
141+
142+
143+
144+
Here’s a basic overview and code implementation of the **Publish-Subscribe** and **Point-to-Point** design patterns in C++ along with a UML diagram.
145+
146+
### 1. Publish-Subscribe Design Pattern
147+
In this pattern, the *publisher* doesn't send messages directly to specific receivers. Instead, messages are sent to a *topic*, and any number of *subscribers* can receive the messages.
148+
149+
#### Code Example
150+
151+
```cpp
152+
#include <iostream>
153+
#include <vector>
154+
#include <string>
155+
156+
class ISubscriber {
157+
public:
158+
virtual void update(const std::string& message) = 0;
159+
};
160+
161+
class Publisher {
162+
private:
163+
std::vector<ISubscriber*> subscribers;
164+
public:
165+
void subscribe(ISubscriber* subscriber) {
166+
subscribers.push_back(subscriber);
167+
}
168+
169+
void unsubscribe(ISubscriber* subscriber) {
170+
subscribers.erase(std::remove(subscribers.begin(), subscribers.end(), subscriber), subscribers.end());
171+
}
172+
173+
void notify(const std::string& message) {
174+
for (auto* subscriber : subscribers) {
175+
subscriber->update(message);
176+
}
177+
}
178+
};
179+
180+
class Subscriber : public ISubscriber {
181+
private:
182+
std::string name;
183+
public:
184+
Subscriber(const std::string& name) : name(name) {}
185+
186+
void update(const std::string& message) override {
187+
std::cout << name << " received message: " << message << std::endl;
188+
}
189+
};
190+
191+
int main() {
192+
Publisher publisher;
193+
194+
Subscriber sub1("Subscriber 1"), sub2("Subscriber 2");
195+
196+
publisher.subscribe(&sub1);
197+
publisher.subscribe(&sub2);
198+
199+
publisher.notify("Hello, Subscribers!");
200+
201+
publisher.unsubscribe(&sub1);
202+
203+
publisher.notify("Another message!");
204+
205+
return 0;
206+
}
207+
```
208+
209+
#### UML Diagram for Publish-Subscribe
210+
211+
```
212+
+----------------+ +--------------------+ +---------------------+
213+
| Publisher | | ISubscriber | | Subscriber |
214+
+----------------+ +--------------------+ +---------------------+
215+
| - subscribers |<--------> | + update(message) |<------- | - name |
216+
| + subscribe() | +--------------------+ | + update(message) |
217+
| + unsubscribe()| +---------------------+
218+
| + notify() | | + name |
219+
+----------------+ +---------------------+
220+
```
221+
222+
---
223+
224+
### 2. Point-to-Point Design Pattern
225+
In this pattern, a *sender* sends a message directly to a *receiver*. The message is meant for a single specific receiver.
226+
227+
#### Code Example
228+
229+
```cpp
230+
#include <iostream>
231+
#include <string>
232+
233+
class Receiver {
234+
public:
235+
void receiveMessage(const std::string& message) {
236+
std::cout << "Receiver got message: " << message << std::endl;
237+
}
238+
};
239+
240+
class Sender {
241+
public:
242+
void sendMessage(Receiver& receiver, const std::string& message) {
243+
receiver.receiveMessage(message);
244+
}
245+
};
246+
247+
int main() {
248+
Sender sender;
249+
Receiver receiver;
250+
251+
sender.sendMessage(receiver, "Direct Message to Receiver");
252+
253+
return 0;
254+
}
255+
```
256+
257+
#### UML Diagram for Point-to-Point
258+
259+
```
260+
+------------+ +-------------+
261+
| Sender | -------> | Receiver |
262+
+------------+ +-------------+
263+
| + sendMessage() | + receiveMessage() |
264+
+------------+ +-------------+
265+
```
266+
267+
### Summary:
268+
- **Publish-Subscribe**: The publisher notifies all subscribers when an event occurs, and any number of subscribers can react.
269+
- **Point-to-Point**: A sender sends a message directly to a specific receiver.
270+
271+
Let me know if you need further explanations or additional design details!
272+

0 commit comments

Comments
 (0)