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-EmailX-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โ
idcustomerId(email)orderDatetotalAmountstatus(CREATED, CONFIRMED, CANCELLED)createdAt
OrderItem Entityโ
idorderIdproductIdquantityprice
Each order contains multiple order items.
๐ Order Status Lifecycleโ
CREATEDโ Order placed, payment pendingCONFIRMEDโ Payment successfulCANCELLEDโ Order cancelled / failed
๐ก Kafka Events Publishedโ
| Event Type | Topic | Description |
|---|---|---|
| ORDER_PLACED | order-events | Order successfully created |
| ORDER_STATUS_UPDATED | order-events | Order status change |
These events are consumed by:
- Notification Service
- Analytics Service
- Recommendation Service
๐ APIsโ
Customer APIsโ
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/orders | Place order |
| GET | /api/orders/{id} | Get order by ID |
| GET | /api/orders/my | Get user orders |
Admin APIsโ
| Method | Endpoint | Role | Description |
|---|---|---|---|
| GET | /api/orders | ADMIN | Get all orders |
| PUT | /api/orders/{id}/status | ADMIN | Update 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 APIsOrderServiceโ Core order logicProductClientโ WebClient for Product ServiceOrderRepositoryโ JPA repositoryKafkaEventPublisherโ Order eventsGlobalExceptionHandlerโ 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.