Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 79 ยท Application Layer: Names and Web

Application Layer Protocols and Architectures

Understand the application layer: client-server vs peer-to-peer, pull vs push, text vs binary protocols, stateless vs stateful design, with a toy protocol in Node.js.

Everything below the application layer exists to move bytes from one process to another. The application layer is where those bytes get meaning. Your browser, mail client, game and phone apps all speak application-layer protocols: HTTP, SMTP, DNS, SSH, MQTT, and thousands of private ones. Each protocol is just an agreement about who speaks first, what messages look like, and what each message means.

This chapter walks through the protocols you use daily. First, in this lesson, comes the vocabulary: the architectures applications use, the choices every protocol designer faces (text or binary, stateless or stateful, pull or push), and a small protocol of our own that runs live on a loopback connection. After it, the same questions will make every real protocol easier to understand.

What an application protocol defines

Application protocols run on top of the transport layer and use sockets to reach it. A protocol specification answers these questions:

  • Message types. For example request and response, or command, event and error.
  • Syntax. How fields are laid out: lines of text, fields with delimiters, JSON, or fixed binary structures.
  • Semantics. What each field and each message means, and what the receiver must do.
  • Rules. Who may send what and when, including timeouts, error handling and how a conversation ends.
  • Transport. Whether it runs over TCP, UDP or QUIC, and on which port.

Protocols published as open standards (HTTP, SMTP, DNS) are called public protocols; anyone can write a client or server that interoperates. Many applications also use proprietary protocols, which only their own software understands.

Architectures

Client-server

In the client-server model one side, the server, runs permanently at a well-known address and waits. Many clients start conversations when their users need something. Clients do not talk to each other; the server is the hub.

  • Easy to reason about, secure, and manage: one place holds the data and enforces the rules.
  • The server is a single point of failure and a bottleneck, so big services run many servers behind load balancers and CDNs.

The web, email and most mobile apps work this way.

Peer-to-peer

In a peer-to-peer (P2P) system, every participant is both client and server. Peers find each other and exchange data directly. File sharing like BitTorrent, some video-call media paths and cryptocurrency networks use this model.

  • Capacity grows with the number of peers, and there is no central point to overload.
  • Harder to build: peers come and go, sit behind NAT, and must discover each other. Security and fairness are harder to guarantee.

Real systems are usually hybrids. A video call may use a central server to introduce the two parties and then send media directly between them; a chat app might send messages through servers but files peer to peer.

flowchart LR
    subgraph CS["Client-server"]
      C1["Client"] --> S["Server"]
      C2["Client"] --> S
      C3["Client"] --> S
    end
    subgraph P2P["Peer-to-peer"]
      P1["Peer"] --- P2["Peer"]
      P2 --- P3["Peer"]
      P3 --- P1
      P1 --- P4["Peer"]
    end

In client-server every conversation goes through the server; in P2P any peer can talk to any other.

Request-response and REST

Most client-server protocols are request-response: the client sends a request, the server sends a reply. HTTP is the model case, and it inspired REST, a style for designing web APIs. REST treats everything as a resource named by a URL (/users/42), and uses a small, fixed set of verbs on it: GET to read, POST to create, PUT or PATCH to change, DELETE to remove. Responses carry a representation (usually JSON) and a status code. The next lessons look at HTTP closely; here, remember that REST is a convention on top of HTTP, not a protocol of its own.

Design choices every protocol makes

Pull or push

With pull, the client asks whenever it wants data. With push, the server sends data as soon as it is available. Plain HTTP is pull: a server cannot start talking. To get fresh data, a client has to poll repeatedly, which wastes requests when nothing changed and still delays updates by up to one polling interval. The simulation below compares the two for four events during 3 seconds.

Node.js Polling (pull) versus push

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Faster polling lowers the delay but multiplies the requests. Push removes the trade-off, and the web has several ways to get it: Server-Sent Events, WebSockets and WebRTC.

Text or binary

A text protocol sends human-readable lines: GET /index.html HTTP/1.1, MAIL FROM:<[email protected]>. You can debug it with telnet or nc, and it is easy to extend. A binary protocol packs fields into bytes with fixed layouts, as in DNS, TLS or HTTP/2. It is smaller and faster to parse, but needs a specification and a tool to read it.

Node.js The same reading as text and as binary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Neither is "better". Text wins for openness and debugging; binary wins when millions of messages per second or tiny IoT radios are involved. JSON over HTTP is a text compromise that most web APIs choose. Note how the binary form had to choose a byte order (big-endian, also called network byte order) and a scaling rule (temperature times ten): every such decision belongs in the specification.

Stateless or stateful

A stateless server treats each request independently: everything it needs is in the request. A stateful server remembers earlier messages in the conversation, such as who has logged in.

Stateless Stateful
Server memory None between requests Per-connection or per-session data
Scaling Any server can answer any request A client must return to the same server (or share the state)
Failure A crash loses nothing A crash loses sessions
Examples HTTP requests, DNS queries FTP, SMTP sessions, SSH, database connections

HTTP is stateless by design, and sites add state on top with cookies and sessions. "Stateless" refers to the protocol; the application behind it usually stores data in a database.

Build a toy protocol

Here is a small stateful text protocol, "TOY/1", with the commands HELLO <name>, WHOAMI, ADD <a> <b>, COUNT and QUIT. Each command is one line ending in \r\n, and each reply is one line beginning with OK or ERR plus a code. The server keeps a small session object per connection, and the client sends a command and waits for the reply before sending the next (a lock-step conversation, like SMTP).

Because TCP is a byte stream, both sides collect incoming text in a buffer and cut it at line endings. This is delimiter framing.

Node.js TOY/1: a stateful line protocol on loopback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The first WHOAMI fails because the session has no name yet, and the second succeeds because the server remembered HELLO Ada. That memory is exactly what makes the protocol stateful: a second connection would start with a fresh session. Also notice that errors are part of the protocol. The client can tell a syntax problem (400) from an unknown command (404) without guessing, a pattern borrowed from HTTP and SMTP.

Protocol design checklist

When you design your own protocol, or evaluate an existing one, ask:

  1. Framing. How does the receiver know where a message ends: a delimiter, a length prefix, or a fixed size?
  2. Versioning. Can you add fields later without breaking old clients? Many protocols start with a version in the first message.
  3. Errors. Are failures reported with machine-readable codes? What happens on malformed input? Never trust a peer's lengths or counts.
  4. Idempotency. If a request is sent twice because of a retry, is the result still correct? GET and PUT can repeat safely; "charge the card" cannot, unless it carries a unique request ID.
  5. Timeouts and keep-alive. How long may a connection sit idle? Who closes it?
  6. Security. Is the connection authenticated and encrypted (see TLS)? Plain text on the open Internet exposes everything.
  7. Reuse before invention. If HTTP, a message queue or an existing binary format fits, use it. Custom protocols need custom tools, libraries and debugging.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which description matches a peer-to-peer system?

    In P2P, peers exchange data directly and each plays both roles, so capacity grows with the number of peers.
  2. 2. A client needs updates from a server within a second. Which approach avoids repeated empty requests?

    Push lets the server send an update the moment it exists. Polling trades delay against wasted requests.
  3. 3. What is the main advantage of a binary protocol over a text protocol?

    Binary encodings are smaller and cheaper to parse. The cost is that you need the specification and a tool to read the bytes.
  4. 4. Why is a stateless server easier to scale?

    With no per-client memory, requests can be spread across any number of servers, and a crashed server loses no conversation state.
  5. 5. In TOY/1 the server splits the incoming text at newline characters. What problem does this solve?

    TCP is a byte stream. A command can arrive in pieces or together with the next one, so delimiter framing turns the stream back into messages.

Summary

  • An application protocol defines message types, syntax, semantics, rules and the transport it uses.
  • Client-server centralizes control and is the dominant model; peer-to-peer shares load among participants; real systems are often hybrids.
  • Protocol designers choose between pull and push, text and binary, stateless and stateful, and must always provide framing, versioning, error codes and security.
  • REST is a style of designing APIs on top of HTTP, based on resources, URLs and a few verbs.
  • Reuse an existing protocol when you can; design your own only for real needs.

Next, learn the system that turns names into addresses for almost every protocol in this chapter: DNS.