Service communication and contracts
Synchronous service communication waits for a response; asynchronous messaging decouples sender and consumer in time. Choose according to whether the caller needs an immediate result and what delivery guarantees the system can support. 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. Do not treat a 202 response as completion or promise exactly-once processing based solely on a broker or in-memory set. Sources: https://www.rfc-editor.org/rfc/rfc9110.html.
Formal Syntax & Structure
decode(event); receive(event) (application contract)Working Example
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');