Instruction file imported from Bencev04/andrew-microserivce (
.github/instructions/kafka-events.instructions.md). Copyright stays with the author.
Kafka Event Conventions
Topics
| Topic | Publisher | Purpose |
|---|---|---|
shift-events |
shift-planning | Shift lifecycle changes |
van-stock-events |
van-stock | Stock mutations (via outbox) |
transaction-events |
transaction | Sale transaction lifecycle |
payment-events |
payment-adapter | Payment outcomes |
reconciliation-events |
reconciliation | Reconciliation results |
Event Shape
Every event is a JSON dict with these required fields:
{
"event_type": "ShiftCreated", # PascalCase, descriptive
"shift_id": "uuid-string", # Primary entity ID
"timestamp": "2024-04-09T10:30:00", # ISO 8601 from datetime.utcnow().isoformat()
# ... additional entity-specific fields
}
Event Types by Service
shift-events
ShiftCreated— includes van_id, driver_id, planned_stock, planned_dateShiftLoading,ShiftAwaitingConfirmation,ShiftActivated,ShiftExceptionShiftReturned,ShiftUnderReview,ShiftClosed
van-stock-events
StockLoaded— van_id, shift_id, items loadedStockDeducted— van_id, shift_id, item_name, quantity, current_quantityStockRestored— compensating action for failed sagaWasteRecorded— van_id, shift_id, item_name, waste_quantity
transaction-events
TransactionCreated— shift_id, van_id, items, total_amountTransactionConfirmed— after successful paymentTransactionVoided— compensating action for failed payment
payment-events
PaymentProcessed— transaction_id, amount, status=SUCCESSPaymentFailed— transaction_id, reason
reconciliation-events
ReconciliationStarted— shift_id, van_idReconciliationCompleted— shift_id, matched (bool), result
Consumer Subscriptions
| Service | Consumes From |
|---|---|
| van-stock | shift-events (ShiftCreated), payment-events (PaymentFailed → restore stock) |
| transaction | payment-events (PaymentProcessed → confirm, PaymentFailed → void) |
| reconciliation | shift-events (ShiftReturned), van-stock-events (stock movements) |
| admin-query | All topics (builds read projections) |
| shift-planning | reconciliation-events (ReconciliationCompleted → close or flag) |
| payment-adapter | transaction-events (TransactionCreated → process payment) |
Rules
- Always serialize with
json.dumps(v, default=str).encode("utf-8") - Always deserialize with
json.loads(v.decode("utf-8")) - Use
group_id="<service-name>-group"for consumers - Consumer handlers should dispatch on
event["event_type"] - Never publish events before the DB transaction commits