Mikroslužby nakreslené poctivo
Každý diagram mikroslužieb vyzerá rovnako a takmer žiadny neodpovie na jedinú dôležitú otázku: dá sa ktorákoľvek z nich nasadiť samostatne? Dve usporiadania tých istých štyroch služieb - jedno, kde sa dá, a jedno, kde nie.
8 min čítaniaUML 2.5.122 z 23
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
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
- 01One component per deployable service, one interface per published contract, one signal per event.
- 02Draw synchronous and asynchronous coupling differently - it is how the system fails.
- 03Synchronous when the caller needs the answer; an event when it does not.
- 04Count synchronous hops per request: three or more is a distributed monolith.
- 05Availability multiplies along a synchronous chain, and four nines become three.
- 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é otázky
Ako sa mikroslužby kreslia v UML?
Ako komponenty, jeden na každú nasaditeľnú službu, s rozhraním pre každú publikovanú zmluvu a signálom pre každú udalosť. Diagramom mikroslužieb ho robí to, že každý box by sa dal vydať vo svoj vlastný deň - a rozhrania okolo neho toto tvrdenie buď podporujú, alebo mu protirečia.
Ako z diagramu spoznám distribuovaný monolit?
Sledujte synchrónne šípky a spočítajte skoky na jednu požiadavku. Ak jedna používateľská akcia prejde cez tri a viac služieb, ktoré musia všetky bežať, distribuovali ste monolit namiesto toho, aby ste ho rozložili - a diagram vám to povedal skôr než produkcia.
Majú služby komunikovať synchrónne alebo cez udalosti?
Synchrónne vtedy, keď volajúci bez odpovede nemôže pokračovať, a cez udalosť vtedy, keď môže. Kresliť to dvoma rôznymi spôsobmi - rozhranie pre volania, signál pre udalosti - mení tento úsudok na niečo, čo revízor vidí a môže s tým nesúhlasiť, namiesto konvencie schovanej v kóde.
Kam patrí databáza na diagrame mikroslužieb?
Jedna na službu a mimo tento diagram. Pohľad na komponenty je o zmluvách medzi službami a nakreslenie piatich databáz pridá päť boxov, ktoré hovoria to isté; kde bežia, patrí do diagramu nasadenia a čo obsahujú, do vlastného modelu každej služby.
Súvisiace články