Azim Haffar
Back to projects

Event-driven backend / Engineering notes

OrderFlow

After a failed event reached the Kafka dead-letter topic, the next order on the same partition succeeded. Only the valid order reduced stock.

Inspect the repository

Java / Spring Boot / Kafka / PostgreSQL / Redis / Testcontainers

What I built

I built an order-processing backend that accepts work while Kafka is unavailable, then recovers without deducting stock twice in the verified local scenario.

Key decision

persist before publishing

Verification

Revision 6eb8a60: 18 backend tests passed. After an exhausted event reached the Kafka dead-letter topic, the next event on the same partition succeeded and only the valid order reduced stock.

Local verification only. Dead-letter publish failure was reviewed in configuration. Single publisher, no authentication or hosted demo.

OrderFlow dashboard showing order activity and infrastructure status
OrderFlow dashboard capture. The Kafka recovery observations below come from separate local checks. Select the image for the full-size capture.

The decision, at a glance

What survives a broker outage?

  1. 01

    Accept durably

    Write the order and outbox together in PostgreSQL.

  2. 02

    Wait & publish

    Pending work can be published after Kafka recovers.

  3. 03

    Guard the transition

    Lock the order and skip terminal state before changing stock.

Recorded evidence

In the recorded local exercise, three orders recovered; stock moved from 27 to 24 and replay left it at 24.

Read the verification scope ↗

One controlled local scenario. Delivery remains at least once.

An interactive engineering story

Pull the plug.
Follow what survives.

Pause Kafka, accept an order, then bring the broker back. Explore why replay does not deduct stock twice in this illustrated scenario.

Illustrative simulation · no backend connection · example stock of 10

Interactive architecture

Follow an order through the system.

Explanation · no backend connection

STEP 01 / 04

Accept an order

The Spring Boot API accepts the order request. This is the entry point to the asynchronous processing flow.

Keep the request path separate from inventory processing.

Select a step to inspect the decision.

OrderFlow transactional outbox, Kafka inventory processing and Redis cache
Order and outbox writes share a transaction. The consumer locks the order, skips terminal states, then locks the product before changing stock.

The problem

Accepting an order and updating inventory cross database and broker boundaries. I wanted to understand what happens when Kafka stops, delivery repeats, or cancellation competes with processing.

My contribution

I built the Spring Boot API, order and inventory workflow, persistence, messaging, caching, React dashboard, and integration tests. Recent reliability work propagates listener failures for retry and exercises duplicate delivery, cancellation races, transaction rollback, and outbox replay.

One decision: persist before publishing

The API writes the order and its outbox event together in PostgreSQL. Publication is separate, so broker unavailability does not require losing the accepted order. Delivery remains at least once: an order-state guard and consistent order-before-product locking prevent repeated stock deduction for this transition.

What happened when Kafka stopped

In the 6 October local experiment, three orders submitted while Kafka was paused remained PLACED, their outbox rows stayed PENDING, and stock stayed at 27. After Kafka resumed, all three orders were observed CONFIRMED and their events PUBLISHED; stock became 24. Explicitly replaying one order left stock at 24. This was one controlled local scenario, not a production availability guarantee.

Verification and evidence scope

Existing XML reports and a fresh Maven build record show 17 local tests, with zero failures, errors, or skips: four unit tests and thirteen container-backed integration tests. Seven reliability tests cover duplicates, both cancellation-race winners, rollback/retry, and outbox replay. A separate baseline used 20 sequential orders at concurrency one. Neither that small sample nor the test count establishes throughput or production tail latency. This October result precedes the 18-test November verification.

Demo

Run Docker Compose from the repository, then execute ./scripts/demo.ps1 in PowerShell. The script checks order confirmation, replay with unchanged stock, cancellation conflict, and insufficient-stock cancellation. It consumes two units per run and does not reset the database. The published repository includes setup and demo instructions. This portfolio walkthrough is an architecture explanation, not a live connection to that backend.

Limits and next work

The outbox assumes one publisher. Consumer retries are bounded before routing exhausted events to a Kafka dead-letter topic. Dead-letter send failure was reviewed in configuration only. Authentication remains absent. Cache eviction is not atomic with the database commit. Process-crash recovery, multi-instance coordination, and measured load behavior need separate verification.

Keep exploring / Next case studyHireLensGet in touch