Why streams?

To know what stream in HTTP/2 is all about and why it is required, one needs context on HTTP/2. HTTP/2 was introduced as an improvement over the existing HTTP/1.x. Some of the improvements made in HTTP/2 are

  • Efficient use of network resources
  • Minimal number of connections established
  • request response multiplexing
  • Header field compression
  • prioritisation of requests

HTTP/2 achieves these improvements by handling http messages and connections in certain ways. At its core, there is a framing layer, which structures request/response message into various frames. Before transmission each of them are encoded in a binary format with an embedded frame header and other details which can be uniquely identified to a stream. Frames are the smallest unit of communication in HTTP/2.

We now know that message is split into multiple frames and then transferred between client and server. Note that message is a complete sequence of frames that map to a logical request or response message.

Streams

Stream is a bidirectional flow of bytes within an established connection which can carry one or more messages.

It is worth noting that streams are usually running on an established TCP connection. A single HTTP/2 connection can contain multiple concurrently open streams. This allows the ability to split HTTP message into multiple independent frames, interleave them and reassemble them in the receiver end. Order in which these frames are sent on the stream is important as recipients process frames in the order which they are received.

Having multiple request/response exchanged over a single TCP connection improves overall performance compared to overhead of creating multiple TCP connections per request. Streams have a unique stream ID associated with them. Client initiated stream ID have odd number whereas server initiated stream id’s have even number.

Flow Control in Streams

Having multiple streams in a single TCP connection might cause congestion on the receiver end. This can result in blocked streams. To ensure streams are not blocked, flow-control scheme is introduced.

Receivers can advertise how many octets they are ready to receive on a stream and for the entire connection. This is a directional scheme where the power to choose is given to receiver. A sender must respect the flow control limits imposed by receiver. Flow control cannot be disabled.

Stream prioritisation

Another important feature of streams is stream prioritisation. Each stream can be assigned a weight which ranges from 1-256. Each stream can also be marked dependent on other streams. Streams can add streamid of parent streams in their parent field. Based on the numbers provided for weight and the dependency tree, server can optimise allocation of resources for requests.

Conclusion

We saw how streams are involved in overall working of HTTP/2. With HTTP/2 in place, browsers can do parallel transfer over a single TCP connection. However if a single packet is dropped, entire TCP connection is brought to halt while lost packet is retransmitted. HTTP/2 performs bad under heavy packet loss or under a bad network compared to HTTP/1, which is still a problem in HTTP/2.