How vorellana99/io-card-issuing works
1. Repository shape
- The repo contains two independent NestJS services: card-issuer and card-processor.
- They do not share a runtime; each has its own package.json, Dockerfile, tests, and SQLite data directory.
- The top-level docker-compose.yml wires both services together with Kafka and Kafka UI.
2. Primary system boundary
- card-issuer is the only HTTP-facing entry point for clients.
- card-processor is not exposed over HTTP; it reacts only to Kafka events.
- This creates a clean split between synchronous admission/queries and asynchronous card issuance.
3. card-issuer responsibilities
- src/cards/cards.controller.ts handles POST /cards/issue and GET /cards/:requestId/status.
- src/cards/cards.service.ts owns request validation, uniqueness checks, persistence of card requests, and publishing the initial Kafka event.
- src/consumer/card-events.controller.ts consumes Kafka responses and updates request status when issuance completes or fails.
4. card-issuer persistence model
- src/cards/entities/card-request.entity.ts represents the local SQLite table of issuance requests.
- The issuer stores request lifecycle state such as PENDING, ISSUED, or FAILED.
- The README indicates a unique constraint on documentNumber, which enforces the “one client = one card” rule before any Kafka publish happens.
5. card-processor responsibilities
- src/consumer/card-requested.controller.ts consumes io.card.requested.v1.
- src/consumer/card-issuance.service.ts performs the issuance workflow, including retries and result publication.
- src/cards/card-generator.ts and src/cards/entities/card.entity.ts model the generated card data stored by the processor.
6. Event-driven data flow
- A client submits card issuance to card-issuer.
- card-issuer persists a request and publishes a CloudEvents-style message to Kafka with type io.card.requested.v1.
- card-processor consumes that event, simulates or performs issuance, then publishes either io.cards.issued.v1 or io.card.requested.v1.dlq.
- card-issuer consumes the result event and updates the original request row in SQLite.
7. Event contract and correlation
- Both services use a shared CloudEvents-like envelope defined in src/common/cloud-event.ts.
- The event source is the requestId, which acts as the correlation key across logs, Kafka messages, and database records.
- This is the main traceability boundary in the architecture.
8. Kafka and integration pattern
- card-issuer runs as a hybrid Nest app: HTTP server plus Kafka consumer in the same process.
- card-processor is a Kafka-only microservice.
- Both use a dedicated Kafka module and an event publisher service, indicating messaging is a first-class internal subsystem rather than a transport detail.
9. Reliability and retry boundary
- The processor has explicit retry policy logic in src/retry/retry.policy.ts.
- The issuance flow retries multiple times with backoff, and after exhaustion emits to a DLQ topic.
- forceError: true in the request DTO is a deliberate test hook to deterministically exercise the failure/DLQ path.
10. Validation and API contracts
- card-issuer defines request/response DTOs under src/cards/dto.
- Validation is domain-specific: document number format, adult age, supported product and currency values.
- Swagger documentation exists for the issuer API, which matters because the issuer is the external contract boundary.
11. Observability and operational tooling
- Both services use structured logging via Pino (nestjs-pino), with the request source/requestId carried through logs.
- Kafka UI is part of the deployed stack for inspecting topics, consumer groups, and message flow.
- This makes the system operable as an event-driven pipeline rather than just two isolated applications.
12. Runtime and infrastructure
- The whole stack is containerized with Docker Compose.
- Kafka runs in KRaft mode, and each service uses its own SQLite database stored under its data/ directory.
- The architecture is therefore local-first and self-contained: HTTP ingress, Kafka backbone, and per-service embedded persistence.
Main entry points: card-issuance.service.ts Issuance service · cards.service.ts Card service · app.module.ts Issuer module · cards.module.ts Cards module
Components 19
Infra
Containerized runtime and messaging backbone
- Compose · Wires both services with Kafka and Kafka UI
docker-compose.yml
card-issuer
HTTP-facing issuance service
- Issuer app · Starts the hybrid HTTP + Kafka runtime
card-issuer/src/main.ts - Issuer module · Composes cards, Kafka, consumer, and persistence
card-issuer/src/app.module.ts - Cards module · HTTP API and request state management
card-issuer/src/cards/cards.module.ts - Cards API · Issue card and status endpoints
card-issuer/src/cards/cards.controller.ts - Card service · Validates, persists, and publishes issuance requests
card-issuer/src/cards/cards.service.ts - Request row · Local issuance lifecycle record with unique document number
card-issuer/src/cards/entities/card-request.entity.ts - Result consumer · Applies issuance success or failure events
card-issuer/src/consumer/card-events.controller.ts - Event publisher · Emits the initial requested event
card-issuer/src/kafka/event-publisher.service.ts - Cloud event · Shared envelope with requestId correlation
card-issuer/src/common/cloud-event.ts
card-processor
Kafka-driven issuance worker
- Worker app · Starts the Kafka-only processor runtime
card-processor/src/main.ts - Worker module · Composes consumer, Kafka, retry, and card generation
card-processor/src/app.module.ts - Request consumer · Consumes card requested events
card-processor/src/consumer/card-requested.controller.ts - Issuance service · Runs retries and publishes outcomes
card-processor/src/consumer/card-issuance.service.ts - Retry policy · Backoff and exhaustion rules for issuance
card-processor/src/retry/retry.policy.ts - Card generator · Builds generated card data
card-processor/src/cards/card-generator.ts - Card store · Persisted generated card record
card-processor/src/cards/entities/card.entity.ts - Event publisher · Emits issued or DLQ events
card-processor/src/kafka/event-publisher.service.ts - Cloud event · Shared envelope with requestId correlation
card-processor/src/common/cloud-event.ts
Connections 22
- Compose → Issuer app: runs · Docker Compose starts the issuer service
- Compose → Worker app: runs · Docker Compose starts the processor service
- Issuer app → Issuer module: loads · Bootstrap wires the issuer modules
- Issuer module → Cards module: includes · Registers the cards boundary
- Issuer module → Result consumer: includes · Registers Kafka result handling
- Cards module → Cards API: exposes · HTTP entry points for clients
- Cards module → Card service: uses · Coordinates validation and persistence
- Card service → Request row: stores · Persists issuance request state
- Card service → Event publisher: publishes · Sends the initial request event
- Card service → Cloud event: formats · Builds the shared event envelope
- Result consumer → Request row: updates · Marks requests issued or failed
- Worker app → Worker module: loads · Bootstrap wires the worker modules
- Worker module → Request consumer: includes · Registers Kafka request handling
- Worker module → Issuance service: includes · Registers issuance workflow
- Issuance service → Retry policy: applies · Uses retry and backoff policy
- Issuance service → Card generator: builds · Creates generated card data
- Issuance service → Card store: stores · Persists issued card data
- Issuance service → Event publisher: publishes · Emits issued or DLQ events
- Issuance service → Cloud event: formats · Builds the shared event envelope
- Request consumer → Issuance service: drives · Starts the issuance workflow from Kafka
- Result consumer → Event publisher: consumes · Receives completion or failure events
- Event publisher → Request consumer: requests · Publishes card requested events to Kafka