Archyno
UMLPraxe modelování

Mikroslužby nakreslené poctivě

Každý diagram mikroslužeb vypadá stejně a téměř žádný neodpoví na jedinou důležitou otázku: dá se kterákoli z nich nasadit samostatně? Dvě uspořádání týchž čtyř služeb - jedno, kde to jde, a jedno, kde ne.

8 min čteníUML 2.5.122 z 23

«use»«send»«subscribe»«subscribe»API gateway«interface»IOrdersOrder service«signal»OrderPlacedShipping serviceBilling service
Four services, two kinds of coupling. The gateway calls an interface because it needs an answer; shipping and billing subscribe to a signal because they do not.

01The question a microservice diagram must answer

Not "what services are there" - a list does that, and the list is usually in the repository names. The question is: can any one of these be deployed on its own? Everything people hope to get from microservices - independent releases, independent scaling, a team that can ship without a coordination meeting - follows from the answer being yes, and no other property of the drawing matters if it is no.

A component diagram answers it, provided you draw the right things. Every deployable service is a component; every published contract is an interface; every event is a «signal». Then cover a service with your hand: if what remains fully specifies its replacement, that service can be released on its own schedule.

02Two kinds of arrow, deliberately

On the figure above, the gateway depends on IOrders - a synchronous contract, because the gateway cannot answer the user without an order. Shipping and billing point at OrderPlaced, a signal the order service sends and does not wait on.

Drawing the two differently is the entire value of the picture. The synchronous edges are where availability composes: if the order service is down, the gateway returns an error. The event edges are where it does not: shipping being down delays a parcel and takes nothing else with it. A diagram that draws both as plain arrows has erased the distinction that decides how the system fails.

03The arrangement to recognise

syncsyncsyncAPI gatewayOrdersPricingInventory
The same services, wired as a synchronous chain. One user request now crosses four processes and three networks, and every one of them has to be up for any of it to work.

This is a distributed monolith, and it is the most common shape in production. Nothing on it is illegal, nothing is badly named, and it will pass review because it looks like every other microservice diagram. What it says, if you read the arrows rather than the boxes, is that the four services must be deployed together, tested together and be up together - so the team has taken on every cost of distribution and kept every constraint of a monolith.

The tell is countable: the number of synchronous hops on one request. One is fine. Two is usually fine. Three or more and the availability of the whole is the product of the parts, so four services at 99.9% each answer about 99.6% together - which is three hours a month of nobody being able to order anything.

The fixes are visible on the same drawing. Let orders publish an event and let pricing and inventory react to it. Or merge two of the boxes, which is a legitimate answer that rarely gets proposed. Or cache what pricing needs, so the hop stops being on the request path. All three are edits to this diagram, which is why drawing it before building is worth the twenty minutes.

In one line each

  1. 01One component per deployable service, one interface per published contract, one signal per event.
  2. 02Draw synchronous and asynchronous coupling differently - it is how the system fails.
  3. 03Synchronous when the caller needs the answer; an event when it does not.
  4. 04Count synchronous hops per request: three or more is a distributed monolith.
  5. 05Availability multiplies along a synchronous chain, and four nines become three.
  6. 06Cover any service: what remains must specify its replacement, or it cannot ship alone.

The notation is covered in the component diagram guide, and five more arrangements - including the two-way cycle this article does not repeat - are in component diagram examples.

04Časté dotazy

Jak se mikroslužby kreslí v UML?

Jako komponenty, jedna na každou nasaditelnou službu, s rozhraním pro každou publikovanou smlouvu a signálem pro každou událost. Diagramem mikroslužeb ho dělá to, že každý box by šlo vydat ve svůj vlastní den - a rozhraní kolem něj toto tvrzení buď podporují, nebo mu protiřečí.

Jak z diagramu poznám distribuovaný monolit?

Sledujte synchronní šipky a spočítejte skoky na jeden požadavek. Jestli jedna uživatelská akce projde přes tři a více služeb, které musí všechny běžet, distribuovali jste monolit místo toho, abyste ho rozložili - a diagram vám to řekl dřív než produkce.

Mají spolu služby mluvit synchronně, nebo přes události?

Synchronně tehdy, když volající bez odpovědi nemůže pokračovat, a přes událost tehdy, když může. Kreslit to dvěma různými způsoby - rozhraní pro volání, signál pro události - mění tento úsudek v něco, co recenzent vidí a může s tím nesouhlasit, místo konvence schované v kódu.

Kam patří databáze na diagramu mikroslužeb?

Jedna na službu a mimo tento diagram. Pohled na komponenty je o smlouvách mezi službami a nakreslení pěti databází přidá pět boxů, které říkají totéž; kde běží, patří do diagramu nasazení a co obsahují, do vlastního modelu každé služby.

Všechny články