microservice.guru
← All posts

Sync or Async: When Should a Call Become an Event?

· Architecture · Microservices · Messaging · Events

By Alain Cyr, Founder, Koesion

microservice.guru — Sync or async? Four calls in a row taking 900 ms, versus one published event and a 60 ms response.

A claim is registered. Four things have to follow: money is reserved, the claim is scored for fraud, an adjuster is assigned, and the customer gets an acknowledgement by e-mail.

The first version of this is always the same. The handler for POST /claims saves the claim and then calls the four services, one after the other.

Sequence: the client posts a claim; Claims calls Reserving, Fraud and Assignment in turn, the call to Mail times out, and the client receives a 500

The customer did not get a claim number because an e-mail could not be sent.

It works in the demo, and in production too, most days. Then the mail gateway slows down one Monday morning, and claim intake goes down with it.

Three problems in one handler

The latency adds up. The user waits for the reserve, the fraud score, the assignment and the e-mail, although none of them changes what the screen will show next.

The availability multiplies. Four dependencies in the request path give four more ways for the request to fail.

The third problem is the quiet one. Claims now knows four other services by name. When Compliance asks for large claims to be reported to the regulator, somebody opens the Claims codebase to add a fifth call. Every new reaction to a claim is a change in Claims, made by a team that has nothing to do with claim intake.

The one question

For each call, ask whether the caller needs the answer in order to continue.

“Is this policy in force on the date of loss?” Yes. Claims cannot accept the claim without knowing. That is a call, and it should stay a call.

“Send the acknowledgement.” No. Nothing in the rest of the handler depends on whether the e-mail has left. The same is true of the reserve, the fraud score and the assignment. Claims does not need those to happen now. It needs them to be set in motion.

So Claims does its own work, records the fact that a claim was registered, and answers.

Sequence: Claims stores the claim and the event, answers 201 in 60 ms, and afterwards Reserving, Fraud, Assignment and Mail each consume ClaimRegistered at their own pace

If Mail is down for an hour, the acknowledgements go out an hour late. Nothing else notices.

Queries, commands and events

It helps to be strict about three words.

A query asks for information and changes nothing. A command asks one specific receiver to do something, and the sender cares whether it was done. An event states that something happened. The sender does not know who is listening and has no stake in what they do with it.

The useful difference between a command and an event is the direction of the dependency.

With a command, Claims depends on Reserving. With an event, Reserving, Fraud and Mail depend on Claims, which knows none of them

Same services, same data on the wire. The arrow of “who has to know whom” points the other way.

With an event, adding the regulator report means deploying a new consumer. Claims is not touched and its team does not have to be in the meeting.

One trap is worth naming. If you publish ReserveFundsRequested and exactly one service is expected to act on it, you have written a command and dressed it as an event. You carry the indirection of a broker and still have the coupling. Call it ReserveFunds, send it to Reserving, and let Reserving tell you if it cannot.

Table comparing query, command and event by meaning, number of receivers, failure handling and a claim example

Most systems need all three. Trouble starts when one of them is used for everything.

What you pay for it

Going asynchronous is a trade, and the bill has four lines.

The user can now see the gap. For a few seconds the claim exists and its reserve does not. The screen has to say “reserve pending” instead of showing an empty field, which means somebody has to design that state.

Messages arrive twice, and sometimes out of order. Every consumer has to cope with that. Idempotency gets its own post.

Following one claim through the system takes tooling. A stack trace stops at the broker, so you need a correlation id on every message and somewhere to search for it.

And the flow disappears from the code. With four calls in a handler, you could read what happens when a claim is registered. With four subscribers, that knowledge is spread over four repositories. For plain reactions this is fine. For a process with steps that depend on each other, it is how you lose track of what the system does, and it is the reason sagas exist.

Where I would draw the line

Keep the call when the caller needs the answer to decide something, and when a human is waiting for exactly that answer. Give it a timeout and a plan for when it fails.

Publish an event when your part is done and others merely need to know.

Use a command over a queue for the case in between: one known receiver, and work that must happen but can happen later.

And do not make this decision once for the whole system. “We are event-driven” is how a policy lookup ends up taking three topics and a correlation table.

Call when you need the answer to continue. Publish when it only needs to be known.

Where to go next

References