Service communication and contracts
Prerequisite: node-microservice-trade-offs. Objective: Choose synchronous or asynchronous flow. Tested locally on Node.js 22.16.0 Linux. Each example is independent. Use a fresh disposable working directory, save as main.mjs, then run node main.mjs. The harness uses a five-second timeout and synthetic environment. These are static code blocks, not ByteMind interactive execution.
How it works
An HTTP request can return a final result or a 202 acceptance with a status resource; acceptance is not completion. An event contract should name the event, version, stable ID and data. Prefer additive optional fields for compatible evolution and validate required fields at the receiver. A message may be delivered more than once; store deduplication and business changes together when durability matters. In-memory sets are demonstrations only. Brokers, NestJS transports and gRPC each have distinct delivery, acknowledgement, ordering and failure semantics; no shared abstraction guarantees identical behavior. Test compatibility with old and new messages and handle unknown versions explicitly.
Validate old and additive event versions
import assert from 'node:assert/strict';function decode(e){if(e.type!=='order.created'||e.version!==1||typeof e.id!=='string'||typeof e.orderId!=='string')throw new Error('unsupported event');return {id:e.id,orderId:e.orderId};}const v1={type:'order.created',version:1,id:'e1',orderId:'o1'};assert.deepEqual(decode(v1),decode({...v1,note:'optional'}));assert.throws(()=>decode({...v1,version:2}));console.log('compatible addition; unsupported version rejected');Expected output:
compatible addition; unsupported version rejectedThe receiver ignores optional data and rejects an unknown major contract version.
Demonstrate duplicate event handling in memory
const seen=new Set(),orders=[];function receive(event){if(seen.has(event.id))return 'duplicate';orders.push(event.orderId);seen.add(event.id);return 'applied';}for(const e of [{id:'e1',orderId:'o1'},{id:'e1',orderId:'o1'},{id:'e2',orderId:'o2'}])console.log(receive(e));console.log(JSON.stringify(orders));Expected output:
appliedduplicateapplied["o1","o2"]The set survives only this process. Durable atomic deduplication needs storage and transaction review.
Common mistakes
Do not treat a 202 response as completion or promise exactly-once processing based solely on a broker or in-memory set.
Practice
Design an order accepted/status workflow and an event schema; describe what happens if a message is repeated or the receiver restarts.
Reference candidate: node-service-communication-and-contracts-lookup (route unverified). Documentation: https://www.rfc-editor.org/rfc/rfc9110.html (opened).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service communication and contracts — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What does HTTP 202 indicate?
Which event change can be compatible with the example decoder?
Why is an in-memory Set insufficient for durable deduplication?