Appearance
Why the node serves video in Go (and when to use nginx)
nginx is a great web server. Many IPTV panels put it in front of everything. So a fair question is: would Bitstream nodes handle more viewers if nginx sent the video?
We measured it. The short answer: no for live channels, and no for movies over plain HTTP. After the fixes below, Go uses the same CPU as nginx for files and holds 50,000 viewers on one node. nginx still wins in one case: movies over HTTPS on a very busy node. The full notes and numbers are in docs/research/nginx-delivery.md.
What the node does for each viewer
Sending bytes is the easy part. For every viewer the node also:
- checks the play token and the viewer's IP address;
- allows one connection per session, so a link cannot be shared;
- starts an on-demand channel when the first viewer arrives;
- counts the bytes each viewer receives, for logs and limits;
- tells the panel every 10 seconds who is watching;
- cuts a viewer at once when the panel kicks them (connection limit).
If nginx sent the bytes, all of this would still need Go, or would have to be rebuilt inside nginx.
Live channels: Go is the right tool
A live channel is not a file. The node keeps the last few seconds of each channel in memory and hands the same bytes to every viewer. nginx cannot read that memory. It would have to fetch the stream from Go and pass it on. That adds work, it does not remove it.
The live path used to cost the most CPU, because every viewer got many small writes. Now each viewer gets one write every 200 ms with everything that arrived meanwhile. Players buffer seconds, so nobody sees the delay. Each viewer has its own queue, so bursty sources and paused players are fine. A viewer that falls far behind jumps to the live picture. It is cut only when it takes no data at all for 60 seconds. One slow phone never slows the channel for others.
| Live TS at 5 Mb/s, 2 cores of our old test server | Before | After |
|---|---|---|
| 1000 viewers: share of the bitrate they got | 54 % | 100 % |
| CPU per Gbps | 62 % of one core | 32 % |
Movies (VOD) and catch-up
Here nginx has a well known trick: sendfile. The kernel sends the file to the network without copying it through the program.
Go can do the same, but the node did not, because of a small wrapper that counted bytes. That is fixed, with a test that fails if it ever breaks again:
| 1000 viewers at 5 Mb/s, plain HTTP | CPU per Gbps | Slowest starts (p99) | Reconnects |
|---|---|---|---|
| node before | 29 % of one core | 2 s | 21 |
| node now | 15 % | 0.13 s | 0 |
| nginx | 15 % | 0.08 s | 0 |
The reconnects came from a timeout. The node dropped a viewer whose player stopped reading for 15 seconds, and players do that when their buffer is full. Now the limit is 60 seconds, like nginx. A kick from the panel now also cuts a long movie download at once, not at the next seek.
50,000 viewers on one node
The question from operators: can one node with a 10 to 25 Gbps link hold 50,000 viewers at 200 to 500 kb/s?
We tested on one machine over loopback, with 2 CPU cores for the node and 2 for the load tool. This tests connections and CPU, not the network card.
| 2 cores, a recent laptop CPU | 10,000 | 25,000 | 50,000 |
|---|---|---|---|
| Live TS at 234 kb/s: Gbps sent | 2.95 | 7.20 | 14.5 |
| Movies read at 320 kb/s: Gbps sent | 3.20 | 8.08 | 16.2 |
| Viewers who lost the connection or fell behind | 0 | 0 | 0 |
| Memory of bs-node per viewer | 40 KB | 40 KB | 40 KB |
So yes: 50,000 viewers fit, with 2 GB of memory for bs-node. nginx needs less memory per viewer (about 13 KB), but 2 GB is small for a node of that size. What runs out first is the kernel. The default TCP memory limit was too small for 50,000 viewers. The tuning guide lists what to set. System tuning in the panel sets most of it.
How many cores a 25 Gbps node needs, from these numbers: about 2 cores for movies and 3 to 9 for live channels, on a recent CPU. On our 2012 test server it is about twice that.
When nginx is worth it
Movies over HTTPS on a busy node. nginx on Ubuntu 24.04 can use kernel TLS. The kernel encrypts and sends the file in one step. Go cannot do that.
| 500 viewers at 5 Mb/s, HTTPS | CPU per Gbps |
|---|---|
| Go | 50 % |
| nginx, normal TLS | 59 % |
| nginx with kernel TLS | 36 % |
That saves about a quarter of the CPU. It starts to matter when one node sends more than about 3 to 5 Gbps of movies over HTTPS and its CPU stays above 60 %.
In that case the plan is a hybrid. Go still checks the token, the session and the limits. Then Go tells nginx "send this file" (X-Accel-Redirect), and nginx sends the bytes.
What the hybrid costs
- One more service to install, update and watch.
- Kernel TLS needs Ubuntu 24.04 or newer, or nginx from nginx.org.
- nginx needs its own copy of the node's certificate, and a reload after each renewal.
- Byte counts arrive when a download ends, not every 10 seconds.
- A kick takes effect on the next request (a seek or a reconnect), not in the middle of a long download. In Go it is instant.
Summary
| Content | Best choice |
|---|---|
| Live TS | Go, always |
| Live HLS and DASH | Go |
| Movies and catch-up, HTTP | Go |
| Movies and catch-up, HTTPS, over about 3 to 5 Gbps per node | Go for checks, nginx for bytes |
| Movies as HLS for web players (nginx-vod-module) | Not needed now |
To serve more viewers, add nodes and use the server tree. It is simpler and safer than putting a second web server in every node.