Skip to content

🌟 A simple project to learn more about message queues

Notifications You must be signed in to change notification settings

kmlyteixeira/message-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Message Queue


Message queue is a form of asynchronous service-to-service communication. Messages are stored on the queue until they are processed and deleted and each message is processed only once, by a single worker (or consumer).

📌 Some important terms

1. Worker or Consumer

A component that receives and processes messages from the message queue

2. Sender or Producer

A component that creates and sends messages to the message queue

3. FIFO (First-In, First-Out)

The principle where the first message to enter the queue is the first to be processed

4. Message Time-To-Live

Defines how long a message can remain in the queue before being deleted

5. Message broker

An intermediary software component that facilitates communication between different systems or different threads within the same system. Ex: Azure Service Bus, Apache Kafka and RabbitMQ

❓ Why use queues?

Long processing time

When there are tasks that require a significant amount of time to complete, put it in a queue allow the system to continue operating as usual.

💥 Actions prone to failure

When an action has a high probability of failure, put it in a queue can be a good strategy for handling these failures asynchronously. Ex: If the action fails, it can be placed back in the queue to retry later without broke the normal flow of the system.


✨ Hands on!

This simple project simulates a messaging service using Azure Service Bus. Here you'll find:

message-queue-project
|
├── sender
│   ├── sender.ts
|   ├── index.ts
|   ├── interface.ts
|   ├── routes.ts
|   ├── service.ts
|   ├── .env-example
|   ├── package-lock.json
│   └── package.json
|
├── worker
│   ├── commons
│   |   └── ServiceBusCommon.cs
│   |
│   ├── dtos
│   |   ├── MessageBodyDto.cs
│   |   └── MessageResponseDto.cs
|   |
│   ├── Program.cs
│   ├── Startup.cs
│   ├── ServiceBusController.cs
│   ├── ServiceBusMessageProcessor.cs
│   ├── ServiceBusMessageReceiver.cs
|   ├── appsettings-example.json
|   ├── messages.txt
│   └── worker.csproj
|
└── README.md

📁 SENDER

Create and send messages to the queue.

🔨 Built with Node v.18 and Typescript w/ axios

🚥 ENDPOINTS

{POST} /receive-messages

📰 Uses Spaceflight News API to retrieve articles and create a message - it enqueue five articles

{POST} /send-message

☝️ Create a personalized message to enqueue

Request params
{
    "title": "string",
    "imageUrl": "string",
    "summary": "string",
    "url": "string"
}
Payload sample
{
    "title": "New message??",
    "imageUrl": "https://wallpaperaccess.com/full/90977.jpg",
    "summary": "Yes...",
    "url": "https://github.com/kmlyteixeira"
}

🏃 Installing and Running

After create your Service Bus queue (you'll find more here: Azure Service Bus | Microsoft Learn):

  1. Clone this repo https://github.com/kmlyteixeira/message-queue.git
  2. Enter in the sender folder: cd sender
  3. Use the .env-example file to assign your connection string and queue name
  4. Run npm install to install the dependencies
  5. Run npm start
  6. The console should display this message: API de envio de mensagens iniciada em http://localhost:3001

📁 WORKER

Receives and processes messages from the queue.

🔨 Built with .NET 8

🚥 ENDPOINTS

{POST} /api/servicebus/start

⌚ Starts the processor and waits for new messages to be added to the messages.txt file

{GET} /api/servicebus/messages

🔍 Get first 50 enqueued messages from the queue in PEEK MODE

Response
[
    {
        "id": "uuid",
        "enqueuedAt": "timestamp",
        "state": "string ('Active' | 'Deferred')",
        "bodySize": "number",
        "message": {
            "title": "string",
            "url": "string",
            "imageUrl": "string",
            "summary": "string"
        }
    }
]

🏃 Installing and Running

After create your Service Bus queue and clone this repo:

  1. Enter in the worker folder: cd worker
  2. Use the .appsettings-example.json file to assign your connection string and queue name
  3. Run dotnet build worker.csproj to build this project
  4. Run dotnet run worker.csproj
  5. The console should display this message: Now listening on: http://localhost:5000

📚 Learn more

1️⃣ Quickstart - Use Azure Service Bus queues from .NET app - Azure Service Bus | Microsoft Learn

2️⃣ Create a Queue Service - .NET | Microsoft Learn

3️⃣ Introduction to Message Queuing. What is a Message Queue? | by Kabilesh Kumararatnam | Medium

4️⃣ FIFO (computing and electronics)

5️⃣ Learn Queue data structures in 10 minutes 🎟️ - YouTube

6️⃣ Queues in 3 minutes - Youtube