Message Size Matters
ECM Protocol message size directly impacts network latency and throughput. This guide covers optimization techniques for production deployments.
Serialization Optimization
Compression
Enable transport compression:
// Request with compression
const response = await fetch(endpoint, {
headers: {
'Accept-Encoding': 'gzip, br',
'Content-Encoding': 'gzip'
},
body: gzip(serialize(context))
});
// Typical compression ratios
// JSON text: 70-80% reduction
// Already binary: 10-20% reduction
Binary Formats
Consider binary serialization for internal communication:
// Protocol Buffers definition
message ECMContext {
string context_id = 1;
string context_type = 2;
google.protobuf.Struct data = 3;
map<string, string> metadata = 4;
}
// MessagePack alternative
const packed = msgpack.encode(context);
// 20-40% smaller than JSON
Payload Optimization
Field Selection
// Request only needed fields
{
"operation": "context.get",
"context_id": "ctx-123",
"projection": ["data.preferences", "metadata.updated_at"]
}
// Response excludes non-selected fields
{
"context_id": "ctx-123",
"data": { "preferences": {...} },
"metadata": { "updated_at": "..." }
}
Pagination
// Paginated queries for large results
{
"operation": "context.query",
"filter": {...},
"limit": 100,
"cursor": "eyJvZmZzZXQiOjEwMH0="
}
Connection Optimization
Connection Pooling
const pool = new ConnectionPool({
maxConnections: 20,
minConnections: 5,
idleTimeout: 30000,
keepAlive: true
});
// Reuse connections across requests
async function query(filter) {
const conn = await pool.acquire();
try {
return await conn.request('POST', '/query', filter);
} finally {
pool.release(conn);
}
}
HTTP/2 Multiplexing
Use HTTP/2 for parallel requests over single connection. Multiple streams without head-of-line blocking. Header compression for repeated headers.
Caching Headers
ETag-Based Caching
// Server response
HTTP/1.1 200 OK
ETag: "abc123"
Cache-Control: private, max-age=60
// Client conditional request
GET /contexts/ctx-123
If-None-Match: "abc123"
// Server response if unchanged
HTTP/1.1 304 Not Modified
Batch Operations
Batch Requests
// Single request for multiple contexts
{
"operation": "context.get_batch",
"context_ids": ["ctx-1", "ctx-2", "ctx-3", "ctx-4", "ctx-5"]
}
// vs 5 individual requests
// Reduces round trips from 5 to 1
Conclusion
Protocol message optimization reduces latency and improves throughput. Apply compression, use binary formats where appropriate, paginate large results, and batch operations to minimize round trips.