Featurewise

Drawing a featurewise service

With featurewise organisation, a service is made up of a number of features. The input to each feature is a link, and the output of a feature may contain links to other features. All of this is within the service's Core: it's independent of any particular way of delivering the service, like an API or a UI.

Putting the links between features in Core makes a service easy and friendly to use, whoever is using it. Some of the things the links do for any consumer are:

Since I started working with featurewise services like these, I've found it useful to draw out the features, along with the links between them, like this:

An online greengrocer drawn as five boats. A customer's dot leads to Produce, a query, which links to Item, a query, which links to AddToBasket, a command. AddToBasket links to Basket, a query, which links to Checkout, a command, and back to Produce. Checkout links back to Produce.
An imaginary online greengrocer: two commands, three queries, and where a customer starts.

Each feature is drawn as a boat. A boat has a clear front and back, which the links need.

Queries get light outlines: they're lighter, in the sense that they don't make changes. Commands get heavier outlines, because commands have an impact. They get different colours too.

Whenever a feature emits a link to another feature as part of its output, we draw an arc from the bow of one boat (the emitter) to the stern of the other (the target of the link).

A feature can have several cases for its output, such as Ordered and BasketEmpty for Checkout. The diagram doesn't show them: if any case contains a link to a feature, the arc is drawn.

For services with users, we usually make one or more of the queries a landing feature, where a user starts. It has to be a query, and it would normally have no required arguments, since the user has nothing to supply yet. A landing is drawn as a dot, as it's reminiscent of the start state in a state machine diagram. Different classes of user may land in different places, like ApplicantLanding and AssessorLanding, and then each dot is labelled with its audience.

What this gives you is the user's way through the service. You can read it like a choose-your-own-adventure story: start at the dot, and follow the arcs.

Workers

That's good for queries and commands. But there's a third family of features, workers. What distinguishes them is that they have no output, and no user invokes them. So no arcs leave a worker's bow, and none of the other features' arcs arrive at its stern.

This made workers look a bit sad on the diagram: they sat at the side, unconnected to anything. But thinking about how they do get invoked suggested a good way to bring them in.

Two ways I often trigger a worker are from a timer and from a queue. HandleCharityCollectionDue might be triggered every morning at six. HandleItemHandedIn might be triggered from a queue, whenever something is put on it.

A timer is drawn as a clock, with an arc from it to the worker it triggers. A queue is drawn as a short stack of boxes, with an arc leading out of it to the worker.

Often, a feature puts work on a queue for one of its own service's workers to do later. In featurewise terms, it does this by enqueueing a link to the worker. That's worth showing. Normally we draw a link when the output of a feature contains it. But a link enqueued as a side effect of invoking a feature isn't in the output. So we draw it through the queue: an arc from the feature into the queue, and one from the queue to the worker.

Here's a message outbox. When you Send a message, it isn't sent straight away. Send puts a link to ActuallySend on a delay queue, where it waits thirty seconds in case you change your mind and CancelSend.

A message outbox. A dot leads to Messages, a query, which links to Compose and CancelSend, commands. Compose links to Send, a command. Send leads into a delay queue, drawn as a stack of slots, which leads to ActuallySend, a worker.
A message outbox. Send doesn't send anything: it queues a link to ActuallySend.

Other systems

Some workers handle events that happen outside the service, like HandleMarketPricesChanged. I normally make these queue workers. The queue is set up to receive an item whenever the external event happens, and each item triggers the handler.

Here the queue is plumbing between two services, and the point is that the work comes from outside. So I leave the queue out, and draw the other system as a box on the edge of the diagram, labelled with its name, with an arc straight to the handler. Here's the greengrocer again, which reprices its produce whenever PricingService says market prices have changed:

The online greengrocer again, with a box labelled PricingService at the edge, and an arc from it to HandleMarketPricesChanged, a worker.
The greengrocer, with its prices coming in from another system.

Emails

Another common case: a user invokes a command, and one of its side effects is to send an email. The email may contain a link to another feature. This is like the queue, except that the target feature won't be a worker. It should be a query, because the link may be followed automatically: mail scanners and link previews often fetch a link before anyone clicks it, and we don't want that to change anything.

An email is drawn as an envelope. The feature that sends the link points to it, and the envelope points to the target feature.

The register-for-updates service on this site works like that. Registering, confirming and asking to leave each email you a link to your registration, a query, where the buttons to confirm or to leave are. It has a second landing, too: the list's owner (me) starts at the list of everyone registered.

The register-for-updates service. A visitor's dot leads to Interest, a query, which links to Register and RequestUnregister, commands. Register, Confirm and RequestUnregister each lead into an envelope labelled Registrant, which leads to Registration, a query. Registration links to Confirm and Unregister. An owner's dot leads to Registrants, a query, which links to Unregister.
The register-for-updates service on this site, drawn from its code. Three commands email links to Registration.

It's already in the code

That last diagram wasn't drawn by hand. In a featurewise service, the information the diagram needs is already in the code. The features and their families are there, right in Core, and so are the links, because they're the types in each feature's output. That's most of what you need, which makes it possible to draw the diagrams automatically.

There are a few gaps. A link that a feature emits as a side effect is hard to find: it goes into a facility, like a queue or an email sender, and the code doesn't say where it ends up. I considered clever ways of finding these, but opted for a simple annotation on the feature. The same goes for a worker that a timer or another system triggers, since the schedule and the other system live outside Core, and for landings:

[EmitsLinksTo<HandleItemHandedIn>(Channel.Queue, "Internal")]
public class HandInItem : Command<HandInItem.Args, HandInItem.Result>

[TriggeredBy(Trigger.Schedule, "Daily")]
public class HandleCharityCollectionDue : Worker<HandleCharityCollectionDue.Args>

[Landing("Traveller")]
public class YourLostReports : Query<YourLostReports.Args, YourLostReports.Result>

The risk is that an annotation drifts from what the code actually does. But there are only a few of them, and each sits on the feature it describes.

Here's the lost property office, one of the featurewise demos, drawn entirely from its code and those three kinds of annotation:

The lost property office. A clerk's dot leads to UnclaimedItems and a traveller's dot to YourLostReports. Queries, commands and links between them fill the middle. HandInItem leads into a queue labelled Internal, which leads to HandleItemHandedIn, a worker. A clock labelled Daily leads to HandleCharityCollectionDue, a worker.
The lost property office, drawn from its code.

Laying it out

Code analysis and a few annotations give you the diagram's content. Laying it out attractively is a different matter.

I tried simulated annealing as a way of doing it automatically. Annealing is what a blacksmith does to metal that has gone brittle from being worked: heat it up, then let it cool slowly. While it's hot, its atoms move about freely, and as it cools they settle. If it cools slowly enough, they settle into a regular arrangement, and the metal comes out softer and tougher.

Simulated annealing borrows the idea. We write down what makes a layout good as a set of rules, each a penalty. We start from a random layout and score it. Then we change it at random - move a boat, turn one round - and score it again. A change for the better, we keep. A change for the worse, we sometimes keep too: often at first, while the temperature is high, and less and less often as it falls. That's what stops it settling on the first layout that no single small change can improve. The changes themselves start big and get smaller, so by the end it's only making fine adjustments.

The rules are whatever you think makes a diagram look good. Mine penalise:

Each rule has a weight, which you can adjust to get good results. The register-for-updates diagram needed a heavier weight on keeping the landings at the edge.

You can try it out for yourself, with each of the services in this post: watch a diagram settle, then change the seed or the weights and start again.

Comments

Comments are GitHub Discussions: sign in with GitHub to join in.