Multiplex to Run Multiple Services on One Port
Distributed Services with Go — by Travis Jeffery (59 / 84)
👈 Implement Raft in Our Service | TOC | What You Learned 👉
Multiplexing allows you to serve different services on the same port. This makes your service easier to use: there’s less documentation, less configuration, and fewer connections to manage. And you can serve multiple services even when a firewall constrains you to one port. There’s a slight perf hit on each new connection because the multiplexer reads the first bytes to identify the connection, but for long-lived connections that performance hit is negligible. And you must be careful you don’t accidentally expose a service.
Many distributed services that use Raft multiplex Raft with other services, like an RPC service. Running gRPC with mutual TLS makes multiplexing tricky because we want to multiplex the connection after the TLS handshake. Before the handshake, we can’t differentiate the connections; we just know they’re both TLS connections. We need to handshake and see the decrypted packets to know more. After the handshake, we can read the connection’s packets to determine whether the connection is a gRPC or Raft connection. The issue with multiplexing mutual TLS gRPC connections is that gRPC needs information taken during the handshake to authenticate clients later on. So we have to multiplex before the handshake and need to make a way to identify Raft from gRPC connections.