Skip to main content

Order Service ๐Ÿงพ

The Order Service is responsible for managing the order lifecycle in ShopVerse โ€” from order creation to status updates.

It coordinates with Product Service for inventory validation and Payment Service for payment processing, while publishing events for asynchronous workflows.


๐ŸŽฏ Responsibilitiesโ€‹

The Order Service handles:

  • Order creation and persistence
  • Order item management
  • Order status transitions
  • Stock validation via Product Service
  • Publishing order-related events
  • Fetching orders for users and admins

๐Ÿง  Why a Separate Order Service?โ€‹

Orders represent a critical transactional domain.

Separating Order Service ensures:

  • Clear ownership of order logic
  • Isolation from product and payment concerns
  • Easier handling of transactional consistency
  • Independent scaling for high order volumes

๐Ÿ—๏ธ High-Level Architectureโ€‹


๐Ÿ” Security Modelโ€‹

  • All requests go through API Gateway

  • Gateway injects:

    • X-User-Email
    • X-User-Role
  • Role-based access enforced inside Order Service

  • Customers can access only their own orders

  • Admins can access all orders


๐Ÿ” Order Placement Flowโ€‹


๐Ÿ—„๏ธ Data Modelโ€‹

Order Entityโ€‹

  • id
  • customerId (email)
  • orderDate
  • totalAmount
  • status (CREATED, CONFIRMED, CANCELLED)
  • createdAt

OrderItem Entityโ€‹

  • id
  • orderId
  • productId
  • quantity
  • price

Each order contains multiple order items.


๐Ÿ”„ Order Status Lifecycleโ€‹

  • CREATED โ†’ Order placed, payment pending
  • CONFIRMED โ†’ Payment successful
  • CANCELLED โ†’ Order cancelled / failed

๐Ÿ“ก Kafka Events Publishedโ€‹

Event TypeTopicDescription
ORDER_PLACEDorder-eventsOrder successfully created
ORDER_STATUS_UPDATEDorder-eventsOrder status change

These events are consumed by:

  • Notification Service
  • Analytics Service
  • Recommendation Service

๐ŸŒ APIsโ€‹

Customer APIsโ€‹

MethodEndpointDescription
POST/api/ordersPlace order
GET/api/orders/{id}Get order by ID
GET/api/orders/myGet user orders

Admin APIsโ€‹

MethodEndpointRoleDescription
GET/api/ordersADMINGet all orders
PUT/api/orders/{id}/statusADMINUpdate order status

๐Ÿ›ก๏ธ Inventory Consistency Strategyโ€‹

  • Stock is validated before order creation
  • Stock reduction happens after order confirmation
  • Prevents overselling
  • Uses synchronous REST call to Product Service
  • Kafka used only for async side effects

โš ๏ธ Failure Scenariosโ€‹

โŒ Insufficient Stockโ€‹

  • Order creation fails
  • Returns 400 Bad Request

โŒ Product Service Downโ€‹

  • Order placement blocked
  • Prevents inconsistent state

โŒ Duplicate Order Requestsโ€‹

  • Handled using transactional boundaries

โš™๏ธ Key Componentsโ€‹

  • OrderController โ€“ Order APIs
  • OrderService โ€“ Core order logic
  • ProductClient โ€“ WebClient for Product Service
  • OrderRepository โ€“ JPA repository
  • KafkaEventPublisher โ€“ Order events
  • GlobalExceptionHandler โ€“ Standard error handling

๐Ÿ“ˆ Scalability Considerationsโ€‹

  • Stateless service โ†’ horizontal scaling
  • Database indexed on customerId
  • Kafka decouples downstream consumers
  • Read-heavy APIs can be cached

๐Ÿงช Testing Strategyโ€‹

  • Unit tests for order creation
  • Integration tests for stock validation
  • Negative tests for unauthorized access
  • Failure tests for external service downtime

๐Ÿ“Œ Summaryโ€‹

The Order Service ensures:

  • Reliable order lifecycle management
  • Inventory consistency
  • Secure user-level access
  • Event-driven extensibility

It acts as the transactional backbone of the ShopVerse platform.