Skip to content

Instantly share code, notes, and snippets.

@samoshkin
Last active February 16, 2024 02:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samoshkin/18e8001715f15cc01f950df835a9d713 to your computer and use it in GitHub Desktop.
Save samoshkin/18e8001715f15cc01f950df835a9d713 to your computer and use it in GitHub Desktop.
Comparison of text and binary protocols

Text protocols

In plain text protocols, the bit stream is organised as a sequence of characters or text strings, e.g. Unicode or ASCII. So the two computers are exchanging textual messages. Example: number 20020 is represented by five characters (5 bytes).

Pros and cons:

  • interoperability between multiple platforms and runtimes that adhere to open and well-known standards: JSON, XML.
  • results in larger size messages
  • can be easily, inspected, read, debugged
  • self-explanatory, data is associated with names, that tell what that data means. Message captures semantics.
  • can be extended in a backward-compatible way
  • can be parsed and decoded by any 3rd party software. Does not need some proprietary program to decode.
  • when multiple messages are sent, it's hard to detect the boundary between individual messages. Usually some sort of text delimiters are used (=====, newlines, etc).

Binary protocol

In binary protocols, the bit stream is organised differently around data structures. You still have a sequence of bits, but they don't correspond to a sequence of characters.

Example: number 20020 is represented by single uint16 data type (2 bytes).

Pros and cons:

  • more compact, saves bandwidth, more performant
  • need to worry about byte order (endianness) when exchanging data on a network
  • message carry only data, but without information what that data means. Semantic is implicit and stay in a program used to decode/encode. So it requires separate program to decode.
  • binary protocols are typically proprietary, it's less likely to be an open standard like XML or JSON
  • hard to maintain backward compatibility as protocol evolves
  • data structure consists of fixed and variable length fields, and always specifies length of the variable segments. It's easy to detect END boundary between packets/messages.

Use when message size is a limitation, for example with UDP protocol, that puts a limit on packet size. Use when performance and bandwith consumption is critical.

HTTP/2 is an example of hybrid text-oriented protocol, that utilizes binary encoding layer to gain performance benefits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment