# ashishkosana/ledgerline architecture

> Architecture diagram of the GitHub repository ashishkosana/ledgerline, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ashishkosana/ledgerline
- Repository: https://github.com/ashishkosana/ledgerline

## Overview

Ledgerline is a payments backend built to keep payment state and downstream event delivery correct across retries and failures. A client submits or transitions a payment through the FastAPI API; the service enforces idempotency, validates lifecycle changes, and commits payment changes with ledger postings and outbox events. A relay publishes outbox events for consumer processing, where inbox deduplication, retries, dead-lettering, and counters support resilient delivery. The graph follows the shown calls and documented capabilities; no concrete broker integration or relay scheduler is shown.

Main entry points: [`app/models.py`](https://github.com/ashishkosana/ledgerline/blob/main/app/models.py) (Payment & Event Tables), [`app/service.py`](https://github.com/ashishkosana/ledgerline/blob/main/app/service.py) (Payment Service), [`app/main.py`](https://github.com/ashishkosana/ledgerline/blob/main/app/main.py) (Payment API), [`app/consumer.py`](https://github.com/ashishkosana/ledgerline/blob/main/app/consumer.py) (Event Consumer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_http_api["Payment API<br/>[main.py]"]
end

subgraph group_payments["Payment Core"]
  node_service["Payment Service<br/>[service.py]"]
  node_state_rules["Payment State Rules<br/>[states.py]"]
end

subgraph group_accounting["Persistence &amp; Ledger"]
  node_session["DB Sessions<br/>[db.py]"]
  node_tables[("Payment &amp; Event Tables<br/>[models.py]")]
  node_ledger["Double-Entry Ledger<br/>[ledger.py]"]
end

subgraph group_events["Event Processing"]
  node_outbox["Outbox &amp; Relay<br/>[outbox.py]"]
  node_consumer["Event Consumer<br/>[consumer.py]"]
  node_retry["Retry Backoff<br/>[retry.py]"]
  node_metrics["Pipeline Metrics<br/>[metrics.py]"]
end

node_client(("API Client"))
node_publisher["Event Publisher"]

node_client -->|"sends requests"| node_http_api
node_http_api -->|"depends on"| node_session
node_http_api -->|"invokes operations"| node_service
node_http_api -->|"reads payments"| node_tables
node_session -->|"initializes tables"| node_tables
node_service -->|"reads and writes"| node_tables
node_service -->|"checks transitions"| node_state_rules
node_service -->|"posts entries"| node_ledger
node_service -->|"enqueues events"| node_outbox
node_ledger -->|"stores ledger rows"| node_tables
node_outbox -->|"stores and reads events"| node_tables
node_outbox -->|"publishes events"| node_publisher
node_publisher -.->|"delivers events"| node_consumer
node_consumer -->|"records inbox and dead letters"| node_tables
node_consumer -->|"increments counters"| node_metrics
node_consumer -.->|"uses backoff"| node_retry

click node_http_api "https://github.com/ashishkosana/ledgerline/blob/main/app/main.py"
click node_service "https://github.com/ashishkosana/ledgerline/blob/main/app/service.py"
click node_state_rules "https://github.com/ashishkosana/ledgerline/blob/main/app/states.py"
click node_session "https://github.com/ashishkosana/ledgerline/blob/main/app/db.py"
click node_tables "https://github.com/ashishkosana/ledgerline/blob/main/app/models.py"
click node_ledger "https://github.com/ashishkosana/ledgerline/blob/main/app/ledger.py"
click node_outbox "https://github.com/ashishkosana/ledgerline/blob/main/app/outbox.py"
click node_consumer "https://github.com/ashishkosana/ledgerline/blob/main/app/consumer.py"
click node_retry "https://github.com/ashishkosana/ledgerline/blob/main/app/retry.py"
click node_metrics "https://github.com/ashishkosana/ledgerline/blob/main/app/metrics.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_http_api,node_client toneBlue
class node_service,node_state_rules toneAmber
class node_session,node_tables,node_ledger toneMint
class node_outbox,node_consumer,node_retry,node_metrics toneRose
class node_publisher toneIndigo
```

## Components

### HTTP API

- [Payment API](https://github.com/ashishkosana/ledgerline/blob/main/app/main.py): `app/main.py`

### Payment Core

- [Payment Service](https://github.com/ashishkosana/ledgerline/blob/main/app/service.py): `app/service.py`
- [Payment State Rules](https://github.com/ashishkosana/ledgerline/blob/main/app/states.py): `app/states.py`

### Persistence & Ledger

- [DB Sessions](https://github.com/ashishkosana/ledgerline/blob/main/app/db.py): `app/db.py`
- [Payment & Event Tables](https://github.com/ashishkosana/ledgerline/blob/main/app/models.py): `app/models.py`
- [Double-Entry Ledger](https://github.com/ashishkosana/ledgerline/blob/main/app/ledger.py): `app/ledger.py`

### Event Processing

- [Outbox & Relay](https://github.com/ashishkosana/ledgerline/blob/main/app/outbox.py): `app/outbox.py`
- [Event Consumer](https://github.com/ashishkosana/ledgerline/blob/main/app/consumer.py): `app/consumer.py`
- [Retry Backoff](https://github.com/ashishkosana/ledgerline/blob/main/app/retry.py): `app/retry.py`
- [Pipeline Metrics](https://github.com/ashishkosana/ledgerline/blob/main/app/metrics.py): `app/metrics.py`

### Other components

- **API Client**
- **Event Publisher**

## Connections

- API Client → Payment API: sends requests (evidence: `README.md`)
- Payment API → DB Sessions: depends on (evidence: `app/main.py`)
- Payment API → Payment Service: invokes operations (evidence: `app/main.py`)
- Payment API → Payment & Event Tables: reads payments (evidence: `app/main.py`)
- DB Sessions → Payment & Event Tables: initializes tables (evidence: `app/db.py`)
- Payment Service → Payment & Event Tables: reads and writes (evidence: `app/service.py`)
- Payment Service → Payment State Rules: checks transitions (evidence: `app/service.py`)
- Payment Service → Double-Entry Ledger: posts entries (evidence: `app/service.py`)
- Payment Service → Outbox & Relay: enqueues events (evidence: `app/service.py`)
- Double-Entry Ledger → Payment & Event Tables: stores ledger rows (evidence: `app/ledger.py`)
- Outbox & Relay → Payment & Event Tables: stores and reads events (evidence: `app/outbox.py`)
- Outbox & Relay → Event Publisher: publishes events (evidence: `app/outbox.py`)
- Event Publisher → Event Consumer: delivers events (evidence: `README.md`)
- Event Consumer → Payment & Event Tables: records inbox and dead letters (evidence: `app/consumer.py`)
- Event Consumer → Pipeline Metrics: increments counters (evidence: `app/consumer.py`)
- Event Consumer → Retry Backoff: uses backoff (evidence: `README.md`)
