Component diagrams: ports

Component diagrams: ports

puml.online

简介 / Introduction

A component on a component diagram is rarely just “a box with one arrow” — it usually exposes a number of ports, each with its own name and direction: inbound ports receive requests from outside, outbound ports publish capabilities to the rest of the system. When a diagram carries a chain like “frontend → gateway → database”, pointing each line at a specific port says far more than putting a label on the arrow.

PlantUML has native port support: the port, portin and portout keywords live inside a component’s { } block, and with an as alias every connection can target an exact port. Mermaid has no component diagram and no notion of a port at all — the closest it gets is a flowchart with subgraph for the component boundary and plain nodes standing in for ports. This article walks one “frontend → API gateway → main database” chain through both engines and spells out what each one gives up.

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@startuml
title Component diagram: ports

component "Web Frontend" as FE {
portin "HTTP" as P_HTTP
portout "REST" as P_REST
}

component "API Gateway" as GW {
portin "REST" as P_GW_REST
portout "SQL" as P_SQL
}

database "Main Database" as DB

FE.P_HTTP --> GW : "forward request"
GW.P_GW_REST --> GW.P_SQL
GW --> DB
@enduml

The three keywords divide the work cleanly: portin marks an inbound port, portout an outbound one, and port is the direction-agnostic generic. Once a port is declared with as, you can address it as component.port. In the diagram above, FE.P_HTTP --> GW attaches the arrow to the HTTP inbound port that the frontend actually declares; GW.P_GW_REST --> GW.P_SQL is a port-to-port line inside the gateway — it never crosses the component boundary and is drawn as a small loop.

Mermaid 版

Mermaid has no component diagram, so the usual workaround is flowchart plus subgraph for the boundary and nodes for the ports:

1
2
3
4
5
6
7
8
9
10
11
12
13
flowchart LR
subgraph FE["Web Frontend"]
P_HTTP["HTTP port"]
P_REST(("REST port"))
end
subgraph GW["API Gateway"]
P_GW(("REST port"))
P_SQL(("SQL port"))
end
DB[("Main Database")]
P_HTTP --> P_GW
P_GW --> P_SQL
P_SQL --> DB

The mapping is: subgraph ≈ component, nodes inside a subgraph ≈ that component’s ports. Port direction has no syntax at all — you can only convey it through the node text (“HTTP port”, “REST port”). Port shape is entirely up to you: the [...] rectangles above stand for the HTTP-style rectangular ports and the ((...)) rounded ones for REST and SQL, but that is a pure visual convention the engine knows nothing about.

If you want a more C4-flavoured look, Mermaid also ships C4Component syntax (Container_Boundary(...) with Component(...)), which reads much more like a real component diagram — yet it has no port concept either, so components can only connect to whole components.

对比 / Side-by-side

Feature PlantUML Mermaid
Component diagram type Native component, optionally with a { } block No component diagram — approximate with flowchart + subgraph
Port declaration port / portin / portout No port concept — plain nodes stand in for ports
Port direction portin / portout distinguish inbound from outbound No syntax support — direction lives in the node label
Port reference Declare an as alias, then address component.port The node id is the reference name
Port-to-port inside one component Supported (GW.P_GW_REST --> GW.P_SQL) Supported, but visually indistinguishable from cross-component lines
Port shape Drawn by the engine as a small square / half-circle Whatever node shape you choose
Alternative syntax — C4Component looks more like a component diagram, but still has no ports

小结 / Wrap-up

The whole point of a port is to turn “a line between two components” into “a line at a specific entry point of this component”, which is what keeps a dense component diagram readable. PlantUML’s component diagram supports ports natively, and portin / portout carry direction semantics for free, which makes it the natural choice for this kind of diagram. Mermaid can only approximate it with subgraph plus nodes: the boundary reads fine, but direction has to be spelled out in the label text. If the diagram is long-lived and the team already lives in PlantUML, use the native ports; if the diagram only ever gets read inside a Markdown viewer, the Mermaid approximation works perfectly well — as long as you accept that “port direction” is a comment, not a diagram element.

  • Title: Component diagrams: ports
  • Author: puml.online
  • Created at : 2026-11-22 01:02:43
  • Updated at : 2026-11-26 01:07:01
  • Link: https://puml.online/blog/component-diagram-port-interfaces-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.