A production-grade, highly scalable implementation of a real-time event transformation and streaming engine featuring robust Finite State Machine (FSM) validation, multi-version Pydantic schema mapping, Prometheus observability, and database persistence.
This project demonstrates the technical depth of enterprise streaming pipelines, designed clean-room style to show advanced architectural patterns without exposing proprietary domain logic.
-
Multi-Version Pydantic Schema Mapping (
app/models/schemas.py)- Utilizes strict Pydantic V2 models for type safety, validation constraints, and serialization.
- Declares incoming source models (
OrderMessageV3) with nested validations and fields mapped via camelCase aliases, alongside subsequent target models representing specific lifecycle events (OrderPendingV3,OrderAcceptedV3,OrderRejectedV3, etc.).
-
Advanced Finite State Machine Validation (
app/core/fsm.py)- Integrates the
python-statemachinelibrary to validate event transitions at run-time. - Enforces a formal order lifecycle:
OrderPending->OrderAccepted/OrderRejected->OrderSettled/OrderRefunded/OrderCancelled. - Handles idempotency (timestamp updates) and guards against out-of-order execution by raising explicit lifecycle violations.
- Integrates the
-
Persistent State Storage (
app/core/db_handler.py&app/models/db.py)- Concrete
DBHandlersubclass extending thePersistenceinterface. - Syncs FSM state directly to the database via SQLAlchemy.
- Cross-compatible with PostgreSQL (using
on_conflict_do_updateupserts) and SQLite (for easy local showcase development).
- Concrete
-
Production-Grade Logger & Cycles (
app/core/logger.py)- Standardized
CommonLoggerclass matching the interface and telemetry output of industrial streaming pipelines. - Automatically tracks transaction boundaries via cycle start/end logs.
- Formats log outputs as structured JSON containing
timestamp,correlation-id,local_time, and execution time (in seconds).
- Standardized
-
Hydration-Mode Runner (
app/services/hydration.py)- Implements a secondary finite processing loop designed to run in batch environments (such as Kubernetes pods scheduled by Airflow).
- Polls the hydration queue/topic with custom timeouts, selectively processes historical messages tagged with a
hydration=trueheader, and exits cleanly after an idle period.
-
Prometheus Metrics & Observability
- Collects throughput, processing status, and error counts categorized by exception reasons using
prometheus_client. - Exposes standard Prometheus metrics via the
/metricsendpoint.
- Collects throughput, processing status, and error counts categorized by exception reasons using
Kafka-Stream-Engine/
│
├── main.py # FastAPI web server & ingestion endpoints
├── requirements.txt # Project dependencies
├── pyproject.toml # Ruff and styling configurations
├── mypy.ini # Mypy type-checking rules
│
├── app/
│ ├── core/
│ │ ├── config.py # Configuration settings and env parameters
│ │ ├── db_handler.py # SQLAlchemy database state persistence
│ │ ├── fsm.py # Order state machine rules & transitions
│ │ ├── interfaces.py # Interfaces for processing, streams, and DB
│ │ └── logger.py # Custom cycle-based structured JSON logger
│ │
│ ├── models/
│ │ ├── db.py # Database schemas and SQLAlchemy helpers
│ │ ├── events.py # API ingestion envelopes
│ │ └── schemas.py # V3 Pydantic schemas (Source & Target states)
│ │
│ └── services/
│ ├── engine.py # High-throughput validation orchestration
│ ├── hydration.py # Simulated hydration-mode execution loop
│ └── message_processor.py# Version/Type validation and mapping functions
│
├── scripts/
│ ├── consume_stream.py # Client subscribing to SSE output stream
│ └── simulate_load.py # Multi-threaded concurrent order ingestion simulator
│
└── tests/
└── test_engine.py # Pytest validation suites (ingest, FSM, hydration)
Create a virtual environment and install all dependencies:
make setupOr manually:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtExecute Ruff lint checks, formatter validation, and Pytest suites:
# Formats code and verifies style rules
venv/bin/ruff format .
venv/bin/ruff check .
# Runs all test suites
venv/bin/pytest testsStart the FastAPI server:
venv/bin/uvicorn main:app --reload --port 8000In secondary terminals, run the load simulator and subscriber stream client:
# Subscribes to Server-Sent Events (SSE) from the processing engine
python scripts/consume_stream.py
# Generates and ingests mock orders of various states
python scripts/simulate_load.pyHydration mode can be triggered and evaluated using the custom showcase endpoint. Send a POST request to trigger the hydration runner, which will consume historical mock events from a simulated queue, filter hydration headers, run validation, and output the processed stream results:
curl -X POST http://localhost:8000/trigger-hydrationOnce running, monitor metrics and endpoint state:
- API Health:
GET http://localhost:8000/health - Prometheus Metrics:
GET http://localhost:8000/metrics