ventas-kafka-spark · Kafka + Spark

Workflow of a sale

How a purchase travels through Kafka between sales, inventory, order, payment and delivery, and how Spark summarizes the payments at the end.

The journey of a sale

The five “microservices” are classes inside a single Spring Boot application that only talk through Kafka. Each one consumes a topic and publishes the next. Spark is a separate container that reads payment.processed at the end.

Kafka 3.8 · KRaft · 1 partition per topic Salescomprar() Inventorystock 10 · 5 Ordersubtotal Payment16% VAT DeliveryPROGRAMADA Monitorend of flow sale.created inventory.reserved order.created payment.processed delivery.scheduled publishes consumes Analytics · Spark 4.0AvailableNow · at the end reads from earliest → console: per-product summary
Solid arrows: publishes. Dashed: consumes. payment.processed has two readers: EntregaService during the simulation and Spark once the simulation has finished.

How Inventory decides

It is the only place where the sale can stop. The reservation is all or nothing.

alVender(venta)sale.created stock ≥ quantity?for each line stock.merge(-cantidad)every line inventory.reserved all yes log.warn · returnnothing decremented one line does not fit nothing is published · the flow stops here next line
Every line is checked first and stock is decremented only afterwards. If one line does not fit, stock stays the same and no rejection event is published (scenario B).

The sample purchase

ProductSKUQuantitySubtotal16% VATTotal
Blue t-shirt (Playera azul)PLY-AZUL-M2 × $50.00$100.00$16.00$116.00
Blue Levi's 502 jeansLEV-502-AZUL1 × $600.00$600.00$96.00$696.00
Whole sale3$700.00$112.00$812.00
$812.00charged in a single payment · final stock: 8 t-shirts and 4 jeans

The four scenarios

ScenarioWhat happensTopics publishedOutcome
A · Successful saledefault purchasethe sale goes through all five services5 of 5delivery scheduled
B · Out of stock6 jeans, 5 in stockInventory rejects and no one else receives events1 of 5timeout
C · Spark analyticsafter the simulationSpark reads the payments and groups by productreads 1console table
D · Startup with start.sh./start.shcleanup, build, healthy Kafka, simulation and analytics—Kafka stays up

Sequence diagram per scenario

Each scenario step by step, with every message between services and Kafka.

Versión en español