What's up coder!

Order Processing System with Message Queue

Word count: 1.4kReading time: 8 min
2025/03/11
loading

Project Name: Microservices-Based Order Processing System with Message Queue

Project Overview

Design and implement a simplified online shopping order processing system using a microservices architecture. The system decouples order, inventory, payment, and notification modules, enabling asynchronous communication between services via a message queue. This enhances systeProject Name: Microservices-Based Order Processing System with Message Queue

Project Overview

Design and implement a simplified online shopping order processing system using a microservices architecture. The system decouples order, inventory, payment, and notification modules, enabling asynchronous communication between services via a message queue. This enhances system scalability and fault tolerance while providing hands-on experience with message queues in real-world business scenarios.


Main Features & Modules

Order Service

  • Functions:

    • Receive user order requests, validate order data, and store it initially.
      • Validation ensures completeness and correctness:
        1. Order amount cannot be negative.
        2. Product ID must be valid.
        3. Purchase quantity must be a positive integer.
      • Frontend data validation methods:
        1. Use token-based authentication.
        2. Implement signature mechanisms.
        3. Prevent CSRF (Cross-Site Request Forgery).
    • Publish order creation events to the message queue.
  • Key Learning Points:

    • How to encapsulate business events as messages and publish them to a queue.

Inventory Service

  • Functions:

    • Listen for order creation messages and check the inventory.
    • Lock inventory if available, then send confirmation or failure messages.
  • Key Learning Points:

    • How to subscribe to a message queue and process messages.
    • Ensure idempotency (avoid duplicate inventory deductions).

Payment Service

  • Functions:

    • Process order payments after inventory confirmation.
    • After payment completion, notify other services via message queue.
  • Key Learning Points:

    • Implement an asynchronous payment process and simulate a payment gateway with callback handling.

Notification Service

  • Functions:

    • Receive payment results or order status updates and send email/SMS notifications to users.
  • Key Learning Points:

    • Message consumption and reliable message delivery.
    • Handling delayed tasks (e.g., order timeout notifications).

Application of Message Queue Technology

  • Choosing a Message Broker:
    You can use RabbitMQ, Kafka, or Redis Streams for message queue implementation.

    • RabbitMQ: Best for understanding traditional messaging patterns, message acknowledgments, and routing.
    • Kafka: Suitable for high-throughput use cases, involving distributed logs and consumer groups.
    • Redis Streams: Provides real-time event streaming while leveraging Redis as an in-memory data store.
  • Core Concepts:

    • Publish/Subscribe Model: Each service subscribes to messages relevant to its functionality.

    • Message Acknowledgment & Retry: Ensure reliable message processing with retries and dead-letter queues.

    • Idempotency Handling: Prevent duplicate processing errors (e.g., avoiding double inventory deduction).

      • Idempotency of Database ID: Using distributed ID(e.g., Snowfllake, Redis increment) when performing INSERT and DELETE

      • Optimistic Occurency Lock: Using version control when performing UPDATE

      • Idempotency of Token: Using Redis verifying token


Project Development Steps

  1. Requirement Design & Technology Selection

    • Define service responsibilities and API design.

    • Select a message broker (e.g., RabbitMQ) and set up the development environment, possibly using Docker.

  2. Microservices Development & REST API Implementation

    • Develop APIs for order, inventory, payment, and notification services.
    • Create a simple frontend or use Postman to simulate user actions.
  3. Integrating Message Queue

    • The order service publishes order creation events.
    • Other services (inventory, payment, notification) subscribe to relevant queues and process messages.
  4. Enhancing Fault Tolerance with Error Handling & Retry Mechanisms

    • Implement message acknowledgment and dead-letter queues for handling failed messages.
    • Add automatic retry mechanisms for message processing failures.
  5. Testing & Debugging

    • Conduct unit testing for individual services and integration testing for end-to-end functionality.
    • Simulate business exceptions (e.g., out-of-stock, payment failures) to validate system robustness.
  6. Documentation & Summary

    • Write project documentation covering service interactions and message queue usage.
    • Reflect on system optimization opportunities.

Key Learning Outcomes

  • Understanding Asynchronous Architectures: Learn how to decouple systems for better scalability and fault tolerance.
  • Familiarity with Message Queues: Master the concepts of publishing, subscribing, acknowledgment, retries, and dead-letter handling.
  • Practical Experience in Microservices Design: Gain hands-on knowledge of service communication and coordination.
  • Real-World Implementation Skills: Apply message queues in business scenarios to optimize system performance.

1. Choosing a Framework

  • Express.js

2. Message Queue Integration

  • Express.js doesn’t have intrinsic mechanism support message queuing. Nevertheless, it is able to support message broker function via middleware such as:

    • RabbitMQ/Redis

3. Key Considerations for Microservices Architecture

  • Service Decoupling:

    • Message queues reduce direct dependencies between services.
    • Implement independent microservices for order, inventory, payment, and notification processing.m scalability and fault tolerance while providing hands-on experience with message queues in real-world business scenarios.

Main Features & Modules

Order Service

  • Functions:

    • Receive user order requests, validate order data, and store it initially.
      • Validation ensures completeness and correctness:
        1. Order amount cannot be negative.
        2. Product ID must be valid.
        3. Purchase quantity must be a positive integer.
      • Frontend data validation methods:
        1. Use token-based authentication.
        2. Implement signature mechanisms.
        3. Prevent CSRF (Cross-Site Request Forgery).
    • Publish order creation events to the message queue.
  • Key Learning Points:

    • How to encapsulate business events as messages and publish them to a queue.

Inventory Service

  • Functions:

    • Listen for order creation messages and check the inventory.
    • Lock inventory if available, then send confirmation or failure messages.
  • Key Learning Points:

    • How to subscribe to a message queue and process messages.
    • Ensure idempotency (avoid duplicate inventory deductions).

Payment Service

  • Functions:

    • Process order payments after inventory confirmation.
    • After payment completion, notify other services via message queue.
  • Key Learning Points:

    • Implement an asynchronous payment process and simulate a payment gateway with callback handling.

Notification Service

  • Functions:

    • Receive payment results or order status updates and send email/SMS notifications to users.
  • Key Learning Points:

    • Message consumption and reliable message delivery.
    • Handling delayed tasks (e.g., order timeout notifications).

Application of Message Queue Technology

  • Choosing a Message Broker:
    You can use RabbitMQ, Kafka, or Redis Streams for message queue implementation.

    • RabbitMQ: Best for understanding traditional messaging patterns, message acknowledgments, and routing.
    • Kafka: Suitable for high-throughput use cases, involving distributed logs and consumer groups.
    • Redis Streams: Provides real-time event streaming while leveraging Redis as an in-memory data store.
  • Core Concepts:

    • Publish/Subscribe Model: Each service subscribes to messages relevant to its functionality.

    • Message Acknowledgment & Retry: Ensure reliable message processing with retries and dead-letter queues.

    • Idempotency Handling: Prevent duplicate processing errors (e.g., avoiding double inventory deduction).

      • Idempotency of Database ID: Using distributed ID(e.g., Snowfllake, Redis increment) when performing INSERT and DELETE

      • Optimistic Occurency Lock: Using version control when performing UPDATE

      • Idempotency of Token: Using Redis verifying token


Project Development Steps

  1. Requirement Design & Technology Selection

    • Define service responsibilities and API design.

    • Select a message broker (e.g., RabbitMQ) and set up the development environment, possibly using Docker.

  2. Microservices Development & REST API Implementation

    • Develop APIs for order, inventory, payment, and notification services.
    • Create a simple frontend or use Postman to simulate user actions.
  3. Integrating Message Queue

    • The order service publishes order creation events.
    • Other services (inventory, payment, notification) subscribe to relevant queues and process messages.
  4. Enhancing Fault Tolerance with Error Handling & Retry Mechanisms

    • Implement message acknowledgment and dead-letter queues for handling failed messages.
    • Add automatic retry mechanisms for message processing failures.
  5. Testing & Debugging

    • Conduct unit testing for individual services and integration testing for end-to-end functionality.
    • Simulate business exceptions (e.g., out-of-stock, payment failures) to validate system robustness.
  6. Documentation & Summary

    • Write project documentation covering service interactions and message queue usage.
    • Reflect on system optimization opportunities.

Key Learning Outcomes

  • Understanding Asynchronous Architectures: Learn how to decouple systems for better scalability and fault tolerance.
  • Familiarity with Message Queues: Master the concepts of publishing, subscribing, acknowledgment, retries, and dead-letter handling.
  • Practical Experience in Microservices Design: Gain hands-on knowledge of service communication and coordination.
  • Real-World Implementation Skills: Apply message queues in business scenarios to optimize system performance.

1. Choosing a Framework

Express.js

Root directory
  • app.js: used to configutr Express server, define middleware, and routes.

  • package.json(core of a Node.js program): contains program details(e.g., name, version), dependencies, scripts, other configurations.

  • bin/: contains executable files like www(defining how to initiate server)

Core directory
  • public/: used to keep static file like CSS, JS, Images

  • routes/: define routes, handling URL requests

    • index.js: define the homepage route(/)

    • users.js: define user-related route(/users)

  • views/: keeps view templates


2. Message Queue Integration

  • Express.js doesn’t have intrinsic mechanism support message queuing. Nevertheless, it is able to support message broker function via middleware such as:

    • RabbitMQ/Redis

3. Key Considerations for Microservices Architecture

  • Service Decoupling:

    • Message queues reduce direct dependencies between services.
    • Implement independent microservices for order, inventory, payment, and notification processing.
CATALOG
  1. 1. Project Name: Microservices-Based Order Processing System with Message Queue
    1. 1.1. Project Overview
    2. 1.2. Project Overview
    3. 1.3. Main Features & Modules
    4. 1.4. Application of Message Queue Technology
    5. 1.5. Project Development Steps
    6. 1.6. Key Learning Outcomes
    7. 1.7. 1. Choosing a Framework
    8. 1.8. 2. Message Queue Integration
    9. 1.9. 3. Key Considerations for Microservices Architecture
    10. 1.10. Main Features & Modules
    11. 1.11. Application of Message Queue Technology
    12. 1.12. Project Development Steps
    13. 1.13. Key Learning Outcomes
    14. 1.14. 1. Choosing a Framework
      1. 1.14.1. Express.js
        1. 1.14.1.1. Root directory
        2. 1.14.1.2. Core directory
    15. 1.15. 2. Message Queue Integration
    16. 1.16. 3. Key Considerations for Microservices Architecture