A Speedy Introduction To Http/2

Overview of HTTP/2

The Hypertext Transfer Protocol (HTTP) is the foundation of data communication on the world wide web. Since the original version, HTTP/1, was first standardized in 1991, the protocol has evolved significantly to accommodate the demands of the modern internet.

HTTP/2 is the second major version of the HTTP network protocol, used by the World Wide Web. It was derived from the earlier experimental SPDY protocol, developed by Google. It's a binary protocol that provides several significant benefits over HTTP/1, such as multiplexing, server push, and header compression.

Key Features of HTTP/2

Multiplexing

This feature allows multiple requests and responses to be sent concurrently on a single TCP connection. It solves the head-of-line blocking problem in HTTP/1, where TCP connections are blocked by the first request in the queue.

# Python code to demonstrate Multiplexing using selectors module import selectors import socket selector = selectors.DefaultSelector() def accept(sock, mask): conn, addr = sock.accept() # Should be ready print('accepted', conn, 'from', addr) conn.setblocking(False) selector.register(conn, selectors.EVENT_READ, read) def read(conn, mask): data = conn.recv(1000) # Should be ready if data: print('echoing', repr(data), 'to', conn) conn.send(data) # Hope it won't block else: print('closing', conn, 'after reading no data') selector.unregister(conn) conn.close() sock = socket.socket() sock.bind(('localhost', 1234)) sock.listen(100) sock.setblocking(False) selector.register(sock, selectors.EVENT_READ, accept) while True: events = selector.select() for key, mask in events: callback = key.data callback(key.fileobj, mask)

Server Push

In HTTP/2, the server can send resources to the client proactively, without waiting for a request. That reduces latency and improves page load speed.

Header Compression

HTTP/2 uses HPACK compression, which drastically reduces overhead.

Conclusion

HTTP/2 brings multiple benefits, including connection multiplexing, server push, and header compression, which help improve the performance of both websites and APIs. We implemented a multiplexing example above with Python. This version of the protocol is being widely adopted and knowing how it works becomes a necessity for web developers, API developers, network engineers, and pretty much any professional working with network communications.