Performance: TCP transport
This affects the speed and responsiveness of backend communication between microservices or clients and servers using TCP protocol.
const client = new ClientTCP({ host: 'localhost', port: 3000 });
await client.connect();
const response1 = await client.send('event1', { data1 }).toPromise();
const response2 = await client.send('event2', { data2 }).toPromise();
// reuse client without reconnecting
await client.close();const client = new ClientTCP({ host: 'localhost', port: 3000 });
await client.connect();
const response = await client.send('event', { data }).toPromise();
await client.close();| Pattern | Connection Overhead | Latency per Message | Resource Usage | Verdict |
|---|---|---|---|---|
| Create and close client per message | High (new TCP handshake each time) | High (100-200ms extra) | High CPU and network usage | [X] Bad |
| Reuse persistent client connection | Low (single handshake) | Low (minimal extra latency) | Lower CPU and network usage | [OK] Good |
Transport.TCP in a NestJS microservice?transport: Transport.TCP and an options object with port.protocol instead of transport, pass port directly without an options object, use string 'tcp' instead of the Transport.TCP enum, or use the wrong transport type like Transport.HTTP.const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
options: { host: '127.0.0.1', port: 4000 }
});
await app.listen();
console.log('Microservice is listening');await app.listen(), the console logs 'Microservice is listening'. No errors are shown.const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
options: { port: '3000' }
});
await app.listen();await app.listen() is correct.'localhost', omit the host, fail to await app.listen(), or attach the logger to the listen() callback instead of the 'connection' event.Pass the test and this pattern moves unknown