Skip to content

Lesson 6 of 9 · 24m

Designing Protocols That Can Evolve

A protocol is a compatibility contract — versioning, errors, idempotency, and capability negotiation decide whether clients survive change.

A network protocol is not the bytes you happen to send today. It is a contract future clients and servers will depend on after everyone forgets why version one looked that way. Good protocol design is mostly humility: assume partial failure, mixed versions, hostile input, and operators reading raw captures at 2 a.m.

Start with the conversation shape

Write down who initiates, who closes, whether requests have responses, whether responses can arrive out of order, and what happens when either side disappears. A request-response protocol with one outstanding request is easier to debug. Pipelining and multiplexing improve throughput but require message IDs and abandonment rules.

client -> HELLO version=2 capabilities=stream,cbor
server -> HELLO version=2 capabilities=stream
client -> REQUEST id=17 method=get key=alpha
server -> RESPONSE id=17 status=ok value=...

Versioning is a negotiation, not a hope

Put a version in the first message and define what mismatches mean. The server can select the highest mutually supported version or reject cleanly. Unknown fields should either be ignored or rejected according to a documented rule; silently truncating them creates corruption. Protobuf uses field numbers for this reason, while JSON protocols often reserve explicit type and version fields.

Errors are part of the protocol

Do not encode failure as a closed socket unless closing is the intended semantic. Send structured errors with stable machine-readable codes and human-readable detail:

{"type":"error","request_id":17,"code":"not_found","message":"key alpha does not exist"}

Stable codes let clients respond programmatically. Human text is for operators and should not be parsed. Avoid leaking internals such as SQL errors, file paths, or private hostnames.

Idempotency decides retry safety

If a request can be retried, either it is naturally idempotent (set key=value) or it carries an idempotency key that lets the server deduplicate. This matters more than throughput in real systems because networks fail after the server processed the request but before the client received the response. Without idempotency, retries can double-charge, double-create, or double-delete.

Small extensibility rules save large rewrites

  • Use explicit message types rather than overloading prefixes.
  • Include lengths or delimiters independently of serialization.
  • Reserve error codes and avoid reusing them with different meanings.
  • Document maximum sizes and timeout expectations.
  • Make unsupported operations return unsupported, not malformed-frame.

Compatibility is tested, not declared

Keep old client fixtures and run them against new servers. Capture real messages and add them as golden tests. Test unknown fields, duplicate fields, oversized frames, and version mismatches. A protocol that only works when both sides were built from the same commit is an implementation detail pretending to be an interface.

Field notes

  • Choose text when humans must debug frequently; choose compact binary when volume justifies tooling.
  • Never change the meaning of an existing field — deprecate and add a new one.
  • Error responses need bounded size too; attackers can trigger amplification through verbose failures.
  • Emit enough logging to correlate request IDs, but never log secrets or authorization tokens.
  • Publish examples that are byte-exact, including hexadecimal where framing is binary.

Was this lesson helpful?

Sign in to rate