services/inventory/
Purpose
Java + Spring Boot microservice that owns the StockItem aggregate. Manages soft reservations: stock is reserved (soft-locked) during the saga window and only permanently committed after payment succeeds, or released on failure. This prevents oversell without distributed locks. Passive responder — only called by the Ordering saga. Port 5003.
Key Files
| File | Description |
|---|---|
Dockerfile | Multi-stage Maven build → minimal JRE runtime image |
pom.xml | Maven project descriptor with Spring Boot and swagger-request-validator |
readme.md | Service notes: reservation lifecycle, stock schema, environment variables |
src/main/java/com/example/inventory/InventoryApplication.java | Spring Boot entry point |
src/main/java/com/example/inventory/HealthController.java | GET /health → {"status": "ok"} |
src/main/resources/application.properties | Server port, DB datasource settings |
For AI Agents
Working In This Directory
- Passive service — never initiate outbound HTTP calls
- Reservation lifecycle:
POST /reservations(reserve) →POST /reservations/{id}/commit(deduct) orPOST /reservations/{id}/release(return) - Invariant:
reservedQtycan never exceedavailableQty— enforce with a row-level lock orSELECT FOR UPDATE POST /reservationsand commit/release endpoints all requireIdempotency-Keyheader- On insufficient stock, return 409 with
{"error": "InsufficientStock"}
Testing Requirements
docker compose build inventory— verify Maven buildcurl http://localhost:5003/health→{"status": "ok"}- Test reservation → commit flow; verify
availableQtydecreases only after commit, not after reserve - Test reservation → release flow; verify stock returns to available pool
Common Patterns
StockItementity withavailableQtyandreservedQtyfieldsReservationentity with TTL for expiry-based cleanup (future extension)@Transactional(isolation = SERIALIZABLE)orSELECT FOR UPDATEon reservation creation- Environment variables:
DATABASE_URL,DATABASE_USERNAME,DATABASE_PASSWORD
Dependencies
Internal
docs/api-specs/inventory.yaml— request validation contract- Ordering service — sole caller
External
- Spring Boot 3.x
swagger-request-validator(Atlassian)- PostgreSQL — Inventory DB at port 5434
opentelemetry-spring-boot-starter— observability