A single order in a clinic can set off a chain of work: a task for a nurse, a message to a lab, a reminder for the patient and an alert if nothing happens in time. When that chain lives in one large codebase, every change risks breaking a step nobody was thinking about.
This post is for architects and backend teams building clinical workflow automation in NestJS. It explains which workflows suit events, how to choose a transport, how to keep multi-step workflows consistent, and how retries and idempotent handlers stop a patient getting the same message twice.
What is event-driven clinical workflow automation with NestJS microservices?
Automating work for clinical teams?
See how we build workflow automation that removes manual steps from provider operations.
Event-driven clinical workflow automation with NestJS microservices is a design in which small services for orders, tasks and notifications publish and react to events through a message broker, so each step of a clinical workflow runs, retries and recovers on its own. Agnotic Technologies builds these systems for provider operations.
Events let teams add a step without rewriting the others. A new reminder or escalation listens for an existing event instead of changing the order service.
Which clinical workflows suit events?
Workflows suit events when one action triggers several independent follow-ups, some of which may happen minutes or hours later. Workflows that need an instant yes or no answer usually stay as direct calls.
- Good fit: an order that creates tasks, notifies a lab and schedules a patient reminder.
- Good fit: a result that must reach the ordering clinician and escalate if unread.
- Good fit: a referral that moves through intake, review and scheduling steps.
- Poor fit: a dose check that must answer before the clinician can sign.
- Poor fit: a lookup that a screen needs before it can show anything.
Draw the workflow with clinicians before writing code. The events you need usually match the moments they already talk about, such as ordered, collected, resulted and reviewed.
Which transport should the services use?
Use a broker that stores messages and supports acknowledgements, such as RabbitMQ, for clinical workflows. Losing a message silently is worse than delivering it late.
In NestJS, a microservice is an application that uses a transport layer other than HTTP; most built-in transporters support both request-response and event-based message styles, and the docs recommend the event-based style for notifying another service that something has happened.
- RabbitMQ: queues with acknowledgements and dead-letter routing, a sound default for task workflows.
- Kafka: a durable event log, useful when many consumers replay history for analytics.
- Redis or NATS: light and fast, but check persistence settings before trusting them with clinical events.
Because NestJS hides most transporter details behind one interface, the choice can change later. Pick the one your operations team can run well today.
Events, commands and the outbox
Separate commands, which ask a service to do something, from events, which state that something happened. Name events in the past tense, such as order.placed, and keep each event small.
- Event body: ids, a type, a time and a version, not the full clinical record.
- Outbox: the order service writes the order and its event in one database transaction, and a relay publishes the event afterwards.
- Versioning: add fields without removing old ones, and let consumers ignore what they do not need.
- Timestamps: record when the clinical event happened as well as when the message was published.
The outbox closes a common gap. Without it, a crash between saving an order and publishing its event leaves an order that no downstream service ever hears about.
How do multi-step workflows stay consistent?
Multi-step workflows stay consistent through a saga: a coordinator that tracks each workflow's steps, starts the next one when an event arrives and runs compensating steps when something fails. Distributed transactions across services are rarely worth their cost.
In FHIR R4, a Task resource describes an activity that can be performed and tracks the state of completion of that activity, such as a workflow step like fulfilling, signing or cancelling an order.
- Step 1, start: order.placed starts a saga record with the expected steps.
- Step 2, progress: each task.completed event moves the saga forward.
- Step 3, time out: a step that misses its deadline raises an escalation event.
- Step 4, compensate: a cancelled order closes open tasks and stops pending reminders.
Model workflow state in a shape close to FHIR Task where you can. It makes later integration with EHR task lists much easier, and it gives clinicians familiar status names such as requested, in progress and completed.
Delivery, retries and duplicate messages
Reliable brokers deliver at least once, which means a handler may see the same message twice. Design for duplicates instead of hoping they never happen.
The NestJS RabbitMQ transporter acknowledges messages automatically unless its noAck option is set to false, which enables manual acknowledgement; with acknowledgements, RabbitMQ re-queues a message if its consumer dies before acknowledging it.
- Manual acknowledgement: acknowledge only after the handler's work is saved.
- Retries: retry temporary failures with growing delays, using a delay queue rather than a tight loop.
- Dead letters: after the last retry, route the message to a dead-letter queue that someone reviews.
Order matters too. Brokers usually guarantee order only within a queue or partition, so handlers should check the workflow state rather than assume a result never arrives before its order.
How do you make handlers idempotent?
Make handlers idempotent by recording each processed event id and checking it before doing any work. A repeated event then changes nothing.
- Processed table: event id and handler name, written in the same transaction as the handler's change.
- Natural keys: one task per order step, enforced by a unique constraint, not by luck.
- Side effects last: send the message or call the external system after the database change succeeds.
- Provider keys: pass an idempotency key to messaging providers that accept one.
Test idempotency directly. Replay every event in a test run twice and check that the second pass changes no records and sends no messages.
Failure points in clinical event systems
Clinical event systems fail in ways that are easy to miss until a patient or clinician notices. These are the common ones.
- Lost events: an order saved without its event because there was no outbox.
- Double messages: a retried event sends the same reminder to a patient twice.
- Hidden dead letters: failed messages pile up in a queue nobody watches.
- Fat events: full patient records copied into every message and every log.
- Shared databases: services read each other's tables, so the events stop being the contract.
- Escalation storms: a misconfigured deadline pages the covering clinician for every open task at once.
Each one is cheaper to prevent in design than to explain to a care team after go-live. Review this list at every design review for a new workflow, not only at the start of the program.
A worked example: a lab order to result review
A worked example: an outpatient clinic automates lab orders. When a clinician signs an order, the order service saves it and its order.placed event through the outbox.
The task service creates a collection task for the nurse, and the notification service schedules a patient reminder. When the lab result arrives, a result.received event creates a review task for the ordering clinician.
If the review task is still open at its deadline, the saga raises an escalation to the covering clinician. A broker restart during the day redelivers some events, and the processed table stops any duplicate tasks or reminders.
When the clinic later adds a text reminder the day before collection, the new handler subscribes to order.placed. No existing service changes.
Who owns what?
Event-driven systems need clear owners for each service and for the events between them. Unowned events become unchangeable.
- Service teams: their own service, database and the events it publishes.
- Platform team: the broker, dead-letter monitoring and shared libraries for idempotency and tracing.
- Clinical lead: workflow steps, deadlines, escalation rules and wording of patient messages.
- Architecture review: changes to event contracts that more than one team consumes.
Keep a short catalog of every event: its name, owner, fields, consumers and version history. Teams check it before publishing a new event or changing an old one.
How do you test and observe the workflows?
Test each handler alone, then run whole workflows against a real broker in a test environment. In production, trace each workflow from its first event to its last.
- Handler tests: valid, repeated, out-of-order and malformed events for every handler.
- Workflow tests: a full order to review flow, including a timeout and a cancellation.
- Chaos checks: restart the broker and a consumer mid-flow and confirm nothing is lost or doubled.
- Tracing: a correlation id carried on every event so one order's path is visible end to end.
- Contract tests: each consumer proves it can read the current version of every event it uses.
Watch dead-letter queue size and the age of the oldest open saga. Both rise before clinicians start reporting missing tasks.
What are clinical workflow microservice best practices?
Use a broker with acknowledgements, publish events through an outbox, keep events small, and coordinate multi-step workflows with sagas that know how to compensate.
Make every handler idempotent, review dead letters daily, carry a correlation id on every event, and let clinicians own the workflow rules.
How Agnotic can help
Agnotic designs and builds event-driven clinical workflow systems in NestJS: order, task and notification services, outbox relays, sagas, idempotent handlers and the monitoring that shows where every workflow stands.
Plan your event-driven workflow platform
We build cloud-native services that run clinical workflows reliably at scale.
Related reading on Agnotic
Workflow design, integration patterns and backend architecture.


