How byte2code/cnkart works
1. Purpose and deployment shape
CNKart is a Java 17/Spring Boot microservices backend for catalog, checkout, stock reservation, and asynchronous notifications. Six independently built services run locally as a Docker Compose cluster: docker-compose.yml, docker-compose.override.yml. Each service has its own Maven build and container image.
2. Service entry points and discovery
Each service starts through its Spring Boot application class. Eureka provides registration and service-name resolution; the gateway and Feign client avoid fixed downstream URLs.
• api-gateway/src/main/java/com/cnkart/apigateway/ApiGatewayApplication.java
• discovery-server/src/main/java/com/cnkart/discoveryserver/DiscoveryServerApplication.java
• order/src/main/java/com/cnkart/order/OrderApplication.java
3. Ingress boundary: gateway
External REST traffic enters the gateway (port 8080), which routes catalog, inventory, and order paths to Eureka-discovered services. It applies Redis-backed IP token-bucket rate limiting and audits request/response metadata before forwarding.
• api-gateway/src/main/resources/application.yml
• api-gateway/src/main/java/com/cnkart/apigateway/config/RateLimiterConfig.java
• api-gateway/src/main/java/com/cnkart/apigateway/filter/SecurityAuditFilter.java
4. Catalog and inventory domains
The item service is an isolated CRUD catalog with a controller/service/repository boundary. Inventory owns stock and performs reservation under a pessimistic database write lock, preventing concurrent oversell. Inventory persists reservation outcomes as outbox records rather than directly publishing to Kafka.
• item/src/main/java/com/cnkart/item/controller/ItemController.java
• item/src/main/java/com/cnkart/item/service/ItemService.java
• inventory/src/main/java/com/cnkart/inventory/service/InventoryService.java
• inventory/src/main/java/com/cnkart/inventory/repository/InventoryRepository.java
• inventory/src/main/java/com/cnkart/inventory/event/InventoryEventPublisher.java
5. Order orchestration and synchronous boundary
Order placement is the core workflow: detect a duplicate idempotency key, persist a PENDING order plus OrderCreated outbox event, call inventory synchronously through OpenFeign, then persist CONFIRMED, REJECTED, or FAILED state plus corresponding events. Database transaction segments deliberately do not span the remote HTTP call. Resilience4j retry/circuit-breaker behavior is applied at the order endpoint.
• order/src/main/java/com/cnkart/order/controller/OrderController.java
• order/src/main/java/com/cnkart/order/service/OrderService.java
• order/src/main/java/com/cnkart/order/feign/InventoryService.java
• order/src/main/java/com/cnkart/order/model/OrderStatus.java
6. Reliable asynchronous events and compensation
Order and inventory implement transactional outboxes: business state and pending event rows commit together; scheduled pollers publish pending records to Kafka topics and update delivery status. Kafka delivery is at-least-once, so consumers must tolerate duplicates. Order also consumes inventory outcomes for saga-style rejection/compensation; notification consumes both event streams.
• order/src/main/java/com/cnkart/order/event/OutboxEventPoller.java
• order/src/main/java/com/cnkart/order/listener/InventoryEventListener.java
• inventory/src/main/java/com/cnkart/inventory/event/OutboxEventPoller.java
• notification-service/src/main/java/com/cnkart/notification/event/KafkaConsumer.java
7. Infrastructure and operational boundaries
MySQL 8 provides separate service-owned schemas for item, inventory, and order; initialization is in docker/mysql/init/01-create-databases.sql. Kafka/Zookeeper carry order and inventory events; Redis is gateway-only rate-limit state; Zipkin/Sleuth provide cross-service tracing. Spring Data JPA/Hibernate owns persistence, with automatic schema updates configured per service. Docker Compose is the integration/runtime definition.
• docker/mysql/init/01-create-databases.sql
• order/src/main/resources/application.yml
• inventory/src/main/resources/application.yml
• notification-service/src/main/resources/application.yml
Main entry points: application.yml API Gateway · OrderService.java Order workflow · InventoryService.java Inventory reservation · DiscoveryServerApplication.java Eureka
Components 17
Edge & discovery
- REST clients
- API Gateway · Routes catalog, inventory, and order traffic by service name.
api-gateway/src/main/resources/application.yml - Rate limit & audit · Redis token buckets and request/response audit run before forwarding.
api-gateway/src/main/java/com/cnkart/apigateway/config/RateLimiterConfig.java - Eureka
discovery-server/src/main/java/com/cnkart/discoveryserver/DiscoveryServerApplication.java
Business services
- Item catalog · CRUD controller and service boundary for catalog items.
item/src/main/java/com/cnkart/item/controller/ItemController.java - Inventory reservation · Pessimistic write locking prevents concurrent oversell.
inventory/src/main/java/com/cnkart/inventory/service/InventoryService.java - Order API · Applies idempotency and Resilience4j retry/circuit breaking.
order/src/main/java/com/cnkart/order/controller/OrderController.java - Order workflow · Persists PENDING, calls inventory, then records terminal state in separate transaction segments.
order/src/main/java/com/cnkart/order/service/OrderService.java
Event delivery
- Order outbox poller · Publishes committed order events and updates delivery status.
order/src/main/java/com/cnkart/order/event/OutboxEventPoller.java - Inventory outbox poller · Publishes committed reservation outcomes and updates delivery status.
inventory/src/main/java/com/cnkart/inventory/event/OutboxEventPoller.java - Kafka / Zookeeper · At-least-once order and inventory event transport.
- Inventory outcome listener · Consumes inventory outcomes for order rejection or compensation.
order/src/main/java/com/cnkart/order/listener/InventoryEventListener.java - Notification consumer · Consumes both order and inventory event streams.
notification-service/src/main/java/com/cnkart/notification/event/KafkaConsumer.java
State & operations
- MySQL service schemas · Item, inventory, and order own separate schemas.
docker/mysql/init/01-create-databases.sql - Redis · Gateway-only token-bucket state.
- Zipkin / Sleuth
- Docker Compose cluster · Runs independently built service containers and infrastructure.
docker-compose.yml
Connections 27
- REST clients → API Gateway: REST
- API Gateway → Rate limit & audit: applies
- Rate limit & audit → Redis: token buckets
- API Gateway → Eureka: resolves services
- Item catalog → Eureka: registers
- Inventory reservation → Eureka: registers
- Order workflow → Eureka: registers
- API Gateway → Item catalog: catalog routes
- API Gateway → Inventory reservation: inventory routes
- API Gateway → Order API: order routes
- Order API → Order workflow: place order
- Order workflow → Inventory reservation: Feign reserve · Synchronous HTTP call outside the database transaction.
- Item catalog → MySQL service schemas: item schema
- Inventory reservation → MySQL service schemas: inventory + outbox
- Order workflow → MySQL service schemas: order + outbox
- Order workflow → Order outbox poller: committed events
- Inventory reservation → Inventory outbox poller: committed outcomes
- Order outbox poller → Kafka / Zookeeper: order events
- Inventory outbox poller → Kafka / Zookeeper: inventory events
- Kafka / Zookeeper → Inventory outcome listener: inventory outcomes
- Inventory outcome listener → Order workflow: reject / compensate
- Kafka / Zookeeper → Notification consumer: both streams
- API Gateway → Zipkin / Sleuth: trace spans
- Order workflow → Zipkin / Sleuth: trace spans
- Docker Compose cluster → API Gateway: runs
- Docker Compose cluster → MySQL service schemas: runs
- Docker Compose cluster → Kafka / Zookeeper: runs