-
Notifications
You must be signed in to change notification settings - Fork 12
Order Command Service
tien.nguyen edited this page Jul 30, 2026
·
1 revision
The Order Command Service is the entry gateway for trade command intake in the Emporia Trading Platform. It provides RESTful APIs for placing, modifying, and cancelling orders, validates incoming orders against static exchange listings, and correlates asynchronous Kafka command execution outcomes back to clients.
-
Directory:
order-command-service -
HTTP Port:
8085 -
Database Boundary: Stateless (No database; uses in-memory
ConcurrentHashMapfor command-result correlation). -
Primary Roles:
- Expose REST API endpoints (
POST /orders,PUT /orders/{id},POST /orders/{id}/cancel,POST /orders/cancel-all). - Validate JWT security context and trader role (
ROLE_TRADER). - Fetch instrument listing snapshots from
static-data-service(:8081). - Construct
OrderCommandKafka messages and produce them toemporia.order.commands.v1. - Correlate Kafka processing results from
emporia.order.results.v1viaKafkaCommandGatewaywithin a configurable timeout (default 8s).
- Expose REST API endpoints (
sequenceDiagram
autonumber
actor Client as Trader / React UI
participant Ctrl as OrderCommandController (:8085)
participant StaticClient as StaticDataClient
participant StaticData as Static Data Service (:8081)
participant Gateway as KafkaCommandGateway
participant KafkaCmds as Kafka (emporia.order.commands.v1)
participant OMS as Order Management Service (:8086)
participant KafkaResults as Kafka (emporia.order.results.v1)
Client->>Ctrl: POST /orders (CreateOrderRequest)
Ctrl->>Ctrl: Validate JWT & trader role
Ctrl->>StaticClient: get(listingId, bearerToken)
StaticClient->>StaticData: GET /listings/{id}
StaticData-->>StaticClient: ListingSnapshot
Ctrl->>Gateway: send(OrderCommand)
Gateway->>KafkaCmds: Publish OrderCommand (commandId)
KafkaCmds->>OMS: Consume OrderCommand
OMS->>OMS: Execute OrderCommandHandler
OMS->>KafkaResults: Publish OrderCommandResult (commandId, success, status, payload)
KafkaResults->>Gateway: @KafkaListener result(OrderCommandResult)
Gateway-->>Ctrl: Complete CompletableFuture<OrderCommandResult>
Ctrl-->>Client: 201 Created (OrderView JSON)
| Method | Path | Request Body | Description |
|---|---|---|---|
POST |
/orders |
CreateOrderRequest |
Places a new order. Validates listing, assigns orderId, and routes to DMA, SMART, or VWAP. |
PUT |
/orders/{id} |
ModifyOrderRequest |
Modifies an existing order's requested quantity or limit price with optimistic version check (expectedVersion). |
POST |
/orders/{id}/cancel |
None | Cancels an active order by orderId. |
POST |
/orders/cancel-all |
None | Bulk cancels all active orders for the authenticated trader / desk. |
Because order command processing across Kafka is inherently asynchronous, order-command-service implements an in-memory Correlation Gateway Pattern to provide synchronous HTTP responses to clients:
-
Pending Correlation Registration: When
send(OrderCommand)is called, aCompletableFuture<OrderCommandResult>is registered in a thread-safeConcurrentHashMapkeyed bycommandId. -
Kafka Dispatch: The
OrderCommandis sent to topicemporia.order.commands.v1, partitioned byorderId(oruserSubject). -
Kafka Reply Listener: An
@KafkaListenerlistens onemporia.order.results.v1(with an isolated group IDorder-command-service-${random.uuid}). -
Completion & Timeout: When a matching
OrderCommandResultarrives, the correspondingCompletableFutureis completed. If OMS fails to respond within 8 seconds, a504 Gateway Timeoutexception is thrown.
- Trading Terminology Glossary
- Order Lifecycle & Validation
- Order Routing & SOR
- Market Data & Pricing
- Portfolio & Risk Management
- Architecture & Order Flow
- Static Data Service
- Market Data Service
- Order Command Service
- Order Management Service (OMS)
- Execution Service
- Portfolio Service
- Design Patterns
- Microservices Overview
- Exchange-Core Integration
- Testing & Verification
- Deployment & Operations
- Repository: emporia
- Tech Stack: Java 21 | Spring Boot 4.0.7 | React 19 | Kafka | gRPC
- Coverage: 91.95% JaCoCo