Mastering Level 3: Inside the Payment Service Component Diagram

C4 model component diagram showing Payment Service architecture with Spring components

In the world of software architecture, moving from a high-level overview to a detailed technical blueprint is a critical transition. This tutorial explores Level 3 of the C4 modeling technique: the Component Diagram. Using the PayQuick example, we will dissect the internal structure of the Payment Service container, demonstrating how to map out complex logic using visual modeling and C4-PlantUML.

What is a Component Diagram?

A Component Diagram is the “microscope” of software architecture. While a Container Diagram (Level 2) shows you the individual applications (like the Payment Service), a Component Diagram zooms in to reveal the building blocks inside that application. It is used to:

  • Define the internal structure of a single container.
  • Document how responsibilities are split between classes, packages, or modules.
  • Clarify the dependencies and data flows between internal components.

Architectural Deep Dive: The Payment Service

Let’s analyze the specific architecture of the PayQuick Payment Service. The diagram reveals a robust, layered architecture typical of enterprise Spring Boot applications. The central hub of this system is the TransactionManager, which orchestrates the entire payment workflow.

1. The Entry Point: PaymentController

The process begins at the PaymentController (a Spring REST Controller). This component acts as the gateway, responsible solely for handling incoming HTTP requests. It does not process logic itself; instead, it forwards requests to the core business logic layer.

2. The Core: TransactionManager

The TransactionManager is the “brain” of the service. As a Spring Service, it orchestrates the workflow, ensuring that all necessary steps—validation, fraud checking, and balance updates—happen in the correct sequence.

3. The Supporting Services

The architecture relies on several specialized services to maintain separation of concerns:

  • FraudDetectionService: Dedicated to security, validating transactions against known patterns.
  • BalanceCalculator: Handles the financial logic of updating account balances.
  • ValidationService: Ensures data integrity and business rules are met before processing.

4. Persistence and External Integration

The system interacts with data and external systems through specific components:

  • Repositories: The TransactionRepository, AccountRepository, and FraudRulesRepository (Spring Data JPA) handle all database interactions, abstracting the SQL layer.
  • External Clients: NotificationClient (Feign Client) and BankingClient (Feign Client) act as proxies for calling external services, such as sending emails or processing transfers via an external banking API.

Visualizing the Architecture with C4-PlantUML

Using C4-PlantUML allows us to define this architecture declaratively. The code below demonstrates how to structure the diagram using a Container_Boundary to group components logically.

@startuml
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Component.puml

title PayQuick - Payment Service Component Diagram

!define C4ShapeInRow 4
!define C4BoundaryInRow 1

Container_Boundary(payment_service, "Payment Service") {
    Component(payment_controller, "PaymentController", "Spring REST Controller", "Handles incoming payment requests")
    Component(transaction_manager, "TransactionManager", "Spring Service", "Orchestrates payment workflows")
    Component(fraud_detector, "FraudDetectionService", "Spring Service", "Validates transactions for fraud")
    Component(balance_calculator, "BalanceCalculator", "Spring Service", "Calculates and updates account balances")
    Component(validation_service, "ValidationService", "Spring Service", "Validates payment data and business rules")
    
    ComponentDb(transaction_repo, "TransactionRepository", "Spring Data JPA", "Stores transaction records")
    ComponentDb(account_repo, "AccountRepository", "Spring Data JPA", "Manages account data")
    ComponentDb(fraud_repo, "FraudRulesRepository", "Spring Data JPA", "Stores fraud detection rules")
    
    Component(notification_client, "NotificationClient", "Feign Client", "Calls notification service")
    Component(bank_client, "BankingClient", "Feign Client", "Integrates with external banking API")
}

Rel(payment_controller, transaction_manager, "Forwards payment requests to")
Rel(transaction_manager, fraud_detector, "Validates transaction with")
Rel(transaction_manager, validation_service, "Validates data with")
Rel(transaction_manager, balance_calculator, "Updates balances via")
Rel(transaction_manager, transaction_repo, "Saves transactions to")
Rel(balance_calculator, account_repo, "Reads/writes account data to")
Rel(fraud_detector, fraud_repo, "Checks rules against")
Rel(transaction_manager, notification_client, "Sends notifications via")
Rel(transaction_manager, bank_client, "Processes external transfers via")

@enduml

Conclusion

The Component Diagram provides the necessary detail for developers to implement the system. By defining the PaymentController, TransactionManager, and their associated repositories and clients, we create a clear contract for development. This structured approach ensures that the PayQuick service is scalable, maintainable, and easy to understand.