Blog

    Event-Driven Apps with AWS SQS and Lambda: FIFO Ordering, Deduplication and Dead-Letter Queues for HL7 v2

    By GauravSeptember 22, 20269 mins read
    On this page

    Event-driven apps with AWS SQS and Lambda suit HL7 v2 feeds well: a hospital sends ADT, order and result messages in bursts, and a queue lets your system accept them quickly and process them at its own pace. The hard parts are keeping each patient's messages in order, ignoring resent duplicates and handling a message that will not process without blocking everything behind it.

    Startup founders and product or engineering leaders receiving HL7 v2 feeds on AWS will find how to structure the flow, choose FIFO message groups, deduplicate and design dead-letter handling that respects order.

    What are event-driven apps with AWS SQS and Lambda for HL7 messages?

    Event-driven apps with AWS SQS and Lambda for HL7 messages accept each inbound HL7 v2 message, place it on an SQS queue, and let Lambda functions process it independently of the sender. A FIFO queue keeps each patient's messages in order, deduplication drops resends, and a dead-letter queue holds messages that repeatedly fail.

    Receiving HL7 v2 feeds on AWS?

    We build the listener, queues, consumers and failure handling for ADT, order and result feeds.

    The queue sits between two systems that work at different speeds. The hospital's interface engine expects a fast acknowledgment, while your processing may call databases, terminology services or APIs that slow down or fail.

    Why put HL7 messages on a queue?

    Putting HL7 messages on a queue separates receiving from processing, so a slow database or a failed deployment does not make the hospital's interface engine back up or start resending.

    • Bursts: admissions and result batches arrive in waves that a queue absorbs.
    • Retries: failed processing is retried from the queue without asking the sender again.
    • Deployments: consumers can be updated while messages wait safely.
    • Scaling: more consumers run when the queue grows, within the limits ordering allows.

    How do messages flow from the sender to the target?

    Messages flow from the hospital's interface engine to a listener, onto a FIFO queue, through a Lambda consumer and into your data store, with a dead-letter queue beside the main queue for failures.

    HL7 v2 message flow on AWS in four stages: the hospital interface engine sends messages over MLLP, a listener writes each one to an SQS FIFO queue before returning the ACK, a Lambda consumer parses and stores it idempotently, and the target data store receives the result, with failed messages moved to a dead-letter queue
    • Interface engine: sends HL7 v2 messages over MLLP and waits for an acknowledgment.
    • Listener: a long-running container or server that holds the TCP connection, since Lambda functions run per event and do not keep a listening socket open.
    • SQS FIFO queue: stores the message with a group ID and a deduplication ID.
    • Lambda consumer: parses, validates, transforms and writes the message to the target.

    When should the listener send the ACK?

    The listener should send the HL7 acknowledgment only after SQS confirms the message is stored. Acknowledging first and queuing second loses the message for good if the queue call fails.

    If the enqueue fails, return a negative acknowledgment or close the connection so the interface engine resends. Store the raw message bytes as received, because a parser fix later may need to reprocess exactly what the hospital sent.

    How does FIFO ordering work for HL7?

    FIFO ordering in SQS works per message group, so the group ID you choose decides which messages are kept in sequence.

    AWS defines the message group ID as the FIFO queue attribute that sorts messages into groups, and says messages within the same group are always processed one at a time, in strict order.

    Choosing a message group ID for HL7 v2 feeds
    Group IDOrder keptTrade-off
    One group for the whole feedEvery message in arrival orderOnly one message processed at a time
    Sending facility plus patient identifierEach patient's messages in orderMerges and identifier changes need care
    Sending facility plus visit numberEach visit's messages in orderPatient-level events across visits can interleave

    Most feeds use the patient as the group: an admission must come before a transfer for the same person, but two different patients' messages can be processed in parallel. Patient merge messages touch two identifiers, so route them carefully and test them.

    How does deduplication handle resent messages?

    Deduplication drops a message SQS has already accepted with the same deduplication ID, which catches most resends from an interface engine that did not receive your acknowledgment.

    Amazon SQS documentation says a FIFO queue treats messages sent with the same deduplication ID within a 5-minute deduplication interval as duplicates and delivers only one copy. With content-based deduplication turned on, SQS builds that ID from a SHA-256 hash of the message body, not its attributes.

    Use the sending facility and the message control ID from MSH-10 as the deduplication ID rather than content-based deduplication. A resent HL7 message can differ slightly, for example in a timestamp, so a body hash may miss the duplicate.

    Five minutes does not cover every case. An interface engine that resends a backlog after an outage can repeat messages hours later, so the consumer must also be idempotent: store the processed control IDs and skip any it has already applied.

    What should the Lambda consumer do?

    The Lambda consumer should turn one queued message into one durable, idempotent change in the target system, and nothing more.

    • Parse and validate: check the message type, required segments and identifiers.
    • Check for repeats: look up the control ID and skip messages already applied.
    • Transform: map fields to your model or to FHIR resources.
    • Write: apply the change and record the control ID in the same transaction where possible.
    • Fail clearly: raise an error with the reason, so the message is retried or moved to the dead-letter queue.

    Keep the function's timeout and the queue's visibility timeout in step, so a message is not handed to a second consumer while the first is still working on it.

    How should dead-letter queues work with ordered HL7?

    Dead-letter queues need a deliberate design for HL7, because moving one patient's failed message aside lets that patient's later messages run out of order.

    The SQS dead-letter queue documentation defines maxReceiveCount as the number of times a consumer can receive a message before SQS moves it to the dead-letter queue, and advises against a dead-letter queue on a FIFO queue when the exact order of messages must never break.

    • Block the group: no automatic move; the failing message is retried, the patient's later messages wait and an alert goes to the support team.
    • Dead-letter and park: the failed message moves aside, and later messages for the same patient are held in a parking table until it is fixed.
    • Dead-letter and continue: acceptable only for message types where order does not matter, such as some standalone results.

    Choose per message type, write the choice down and test it. Whichever option you pick, an alarm on dead-letter queue depth is essential.

    A worked example: an admission, a transfer and a bad update

    A short sequence for one patient shows why the failure strategy matters.

    • An A01 admission arrives, is queued under the patient's group and processes normally.
    • An A08 update follows with a location code your mapping table does not know, so the consumer fails it on every attempt.
    • An A02 transfer for the same patient arrives next and waits behind the failed update in the same message group.
    • With the park strategy, the update moves to the dead-letter queue, the transfer is held in the parking table and an alert names the unknown code.
    • Support adds the code to the mapping table, replays the update and releases the transfer, so the patient's record ends in the right state.

    Messages for every other patient kept flowing throughout, because their groups were never blocked.

    How do you replay messages after a fix?

    Replay messages after a fix by redriving them from the dead-letter queue, or from the stored raw messages, in their original order, then releasing any parked messages for the same patients.

    Because the consumer skips control IDs it has already applied, replaying a wider range than needed is safe. Record each replay with who ran it, why and which messages it covered.

    Test replay before go-live with a recorded feed, including a replay that overlaps messages already processed, so the support team trusts the tool when a real failure happens.

    What should you monitor?

    • Age of the oldest message in the main queue, which shows processing falling behind.
    • Dead-letter queue depth, alarmed at the first message.
    • Time from receipt to acknowledgment at the listener.
    • Consumer errors by message type and by sending facility.
    • Parked messages waiting behind a failure.

    How do teams build the pipeline step by step?

    • Step 1, list the feeds, message types and ordering rules with the hospital's interface team.
    • Step 2, build the MLLP listener with enqueue-before-ACK and raw message storage.
    • Step 3, create the FIFO queue, the dead-letter queue and the redrive policy.
    • Step 4, write the consumer with idempotent writes and clear errors.
    • Step 5, choose and build the failure strategy per message type.
    • Step 6, test ordering, duplicates, poison messages and replay with recorded sample feeds.

    What are the common mistakes with event-driven HL7 apps on SQS and Lambda?

    The common mistakes with event-driven HL7 apps on SQS and Lambda break ordering or lose messages in ways that only show up weeks later.

    • ACK before enqueue: a failed queue call silently drops a message the hospital thinks was delivered.
    • One group for everything: ordering is kept, but throughput collapses during bursts.
    • Relying on the five-minute window alone: late resends create duplicate records.
    • A default dead-letter queue on an ordered feed: later updates overtake the message that failed.
    • No raw message store: a parser bug cannot be fixed by reprocessing.

    What does this mean for your build plan?

    The queue itself takes little time to set up. The work is in ordering rules per message type, idempotent writes, the failure strategy and testing with realistic feeds, so plan those with the hospital's interface team early.

    • Collect sample messages for every type and every sending system before building the consumer.
    • Agree the acknowledgment rules and resend behavior with the hospital's interface team.
    • Assign an owner for dead-letter alerts before go-live, with a documented replay procedure.

    How does Agnotic build HL7 pipelines on AWS?

    Agnotic Technologies builds the MLLP listener, FIFO queues, Lambda consumers, idempotent storage, dead-letter and parking logic, replay tools and monitoring for your HL7 v2 feeds. We test with recorded sample feeds before connecting to a live hospital interface.

    Keep every patient's messages in order

    Ordering, duplicates and failures handled by design.

    Sources

    Frequently Asked Questions

    Teams receiving HL7 v2 feeds from hospitals or labs who already run on AWS and want processing that scales with bursts and survives deployments without losing or reordering messages.

    Talk to an engineer about your project

    Tell us which HL7 feeds you receive and where the data needs to go. We will propose the queue design, ordering rules and failure handling.