Skip to content

Tune CPU and memory ​

Bitstream copies streams, it never re-encodes them, so a node is mostly limited by memory (buffers) and network, not CPU. This guide shows the knobs and how to see what a node needs.

Where the memory goes ​

Each running channel on a node has:

  • a TS ring buffer: about 6 seconds of the measured bitrate, capped at 16 MB per channel;
  • an HLS window of 4 segments, kept only while someone watched over HLS in the last 60 seconds.

All buffers on a node share a total cap (node_buffer_mb, 2048 MB by default). When the total is over the cap, every buffer shrinks a little. A new viewer always gets a clean start at the latest keyframe.

As a rough guide: a 5 Mb/s channel needs about 4 MB of ring buffer.

Ask the node ​

Every node runs a small advisor on its own stats. It tells you, for example, how many more channels fit on the node and whether it is limited by CPU or memory, or that the buffer cap is too small for what is running.

bash
curl -s $PANEL/nodes/2/tuning -H "Authorization: Bearer $TOKEN" | jq

Each item in data has an id, a severity and a title with the advice. Today the control passes on the node's own advice as it is: severity is always info and detail repeats the title. Rules over 24 hours of history are planned. The node must be online, or the list is empty.

See the processes on a node ​

The Processes page in the panel lists the processes each online server supervises, with CPU and memory, and refreshes every 10 seconds. With the API:

bash
curl -s $PANEL/nodes/2/processes -H "Authorization: Bearer $TOKEN" | jq '.data[] | {pid, stream_id, kind, cpu_pct, mem_bytes}'

CPU is measured over half a second from /proc. A node older than this feature returns an empty list. The same numbers show per channel in /nodes/{id}/streams and /streams/{id}/stats (pid, cpu_pct, mem_bytes).

Real numbers ​

A 30 minute soak test on a 5 vCPU server ran 3 always-on channels with 20 TS and 10 HLS viewers, plus a bulk restart of all channels every 5 minutes. bs-node stayed flat at about 87 to 90 MB of RAM and 3 % of one core. The 6 ffmpeg processes used 205 MB and 4 to 5 % CPU in total. No slot or memory leak.

Streaming settings ​

These apply to every node. Change them with PATCH /settings/streaming. Buffer changes apply on the next start of each channel.

SettingWhat it does
ts_buffer_secondsSeconds of stream kept in the TS ring buffer. Lower uses less memory. Slow clients that fall further behind are dropped.
max_stream_buffer_mbMemory cap for one channel.
hls_segment_secondsTarget HLS segment length. Segments are cut at keyframes, so the source GOP decides in the end.
hls_window_segmentsSegments in the HLS playlist. Keep it at 3 or more, or players rebuffer on any hiccup.
max_concurrent_starts_per_nodeHow many sources a node opens at the same time. Stops restart storms.
default_mode, default_on_demand, default_idle_timeout_sDefaults for new channels.

Example:

bash
curl -s -X PATCH $PANEL/settings/streaming \
  -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"ts_buffer_seconds": 4, "hls_window_segments": 4}'

Save CPU ​

  • Use llod for HTTP MPEG-TS sources. It reads them in Go without ffmpeg.
  • Turn on on-demand for channels few people watch.
  • Use the server tree so only one node pulls each source.

Cap the memory of the services ​

The installer can put a systemd memory limit on bs-control and bs-node:

bash
sudo ./deploy/install.sh --role both --memory-max 4G

It writes a drop-in such as /etc/systemd/system/bs-node.service.d/memory.conf. Keep the limit above node_buffer_mb plus some room for ffmpeg.

What the installer already tuned ​

  • Kernel: big accept queues, 2 million file handles, a wide local port range, bigger TCP buffers, BBR with fq, keepalive that drops dead viewers in about 4 minutes. See /etc/sysctl.d/90-bitstream.conf. Skip it with --no-sysctl. On a node the values are sized for its RAM and link speed, see below.
  • PostgreSQL: shared_buffers is 1/4 of RAM when the control runs alone and 1/8 when a node shares the machine, between 64 MB and 4 GB.
  • Redis: maxmemory is 1/10 of RAM (64 MB to 2 GB), with noeviction. Evicting a connection slot would break line limits without anyone noticing, so Redis fails loudly instead.

Kernel tuning on nodes ​

On a node the installer computes a profile from the machine's RAM, CPU cores and link speed and writes it to /etc/sysctl.d/90-bitstream.conf, with the reason above each line. Run sudo /opt/bitstream/bin/bs-node tune show to see the current and recommended values.

The panel shows the same thing on the server page, under System tuning. Apply recommended sets the recommended values and saves them, so they survive a reboot.

bs-node never runs as root. When you press the button it writes a request file into /opt/bitstream/data/tune/. A systemd path unit (bs-node-tune.path) then starts a small root helper (bs-node-tune.service, which is bs-node tune apply). The helper only accepts the keys below, each with a fixed range. It ignores anything else, applies the values, saves the file and writes result.json back for the panel. The helper also runs at boot to set the CPU governor again, because sysctl files cannot hold it.

KeyWhat it does
net.core.somaxconnHow many new connections can wait to be accepted. Bursts of viewers (channel zapping, app restarts) need a long queue.
net.ipv4.tcp_max_syn_backlogHow many half-open connections the kernel keeps while a handshake finishes. Same reason as above.
net.core.netdev_max_backlogPackets the network card may queue before the kernel processes them. Fast links need more.
net.core.rmem_maxLargest receive buffer a socket may use. Big buffers keep high bitrate sources smooth over long distances.
net.core.wmem_maxLargest send buffer a socket may use. Same, for sending to viewers.
net.ipv4.tcp_rmemTCP receive buffer: minimum, default and maximum in bytes. The kernel grows each connection up to the maximum when needed.
net.ipv4.tcp_wmemTCP send buffer: minimum, default and maximum in bytes.
net.ipv4.tcp_congestion_controlHow TCP reacts to loss. BBR keeps video steady on lossy last miles (mobile, Wi-Fi).
net.core.default_qdiscQueue discipline for new network queues. fq pairs with BBR and paces packets evenly.
net.ipv4.tcp_fastopenLets repeat clients send data in the first packet (3 = as client and server). Saves one round trip on reconnects.
net.ipv4.ip_local_port_rangeLocal ports for outgoing connections (sources, relays, proxy mode). Starts at 10000 so it never takes a port a service listens on.
net.ipv4.tcp_tw_reuseReuse closed outgoing connections sooner. Only affects connections this server opens.
net.ipv4.tcp_fin_timeoutSeconds a closing connection may linger. Shorter frees memory faster.
net.ipv4.tcp_keepalive_timeSeconds of silence before checking if a viewer is still there.
net.ipv4.tcp_keepalive_intvlSeconds between those checks.
net.ipv4.tcp_keepalive_probesFailed checks before the connection is dropped. With the values above a dead viewer is gone in about 4 minutes instead of 2 hours.
net.ipv4.tcp_slow_start_after_idle0 keeps the connection speed after a pause (ads, still pictures) instead of starting slow again.
net.ipv4.tcp_mtu_probing1 finds a working packet size when a network drops large packets, so some viewers do not freeze.
net.ipv4.tcp_memMemory all TCP sockets together may use, in 4 KB pages: low, pressure, maximum. The helper sets 1/16, 1/8 and 1/4 of RAM. Tens of thousands of viewers need more than the default, or the kernel starts shrinking every connection's buffers.
net.ipv4.tcp_max_orphansClosed connections the kernel keeps finishing after the program let go. A node restart with many viewers creates that many at once.
net.ipv4.tcp_max_tw_bucketsClosed connections kept briefly in TIME_WAIT. Many HLS viewers reconnecting need room, or the kernel drops them early with a warning.
fs.file-maxOpen files for the whole machine. Every viewer, source and ffmpeg pipe is one.
fs.nr_openMost open files one process may have. Must stay above the service limit (1048576) or bs-node cannot start.
vm.swappinessHow eagerly the kernel swaps. Low keeps stream buffers in memory.
vm.dirty_ratioPercent of memory that may hold unwritten file data before writers wait.
vm.dirty_background_ratioPercent of memory with unwritten data before the kernel starts writing in the background.
cpu.governorCPU speed policy. performance keeps the CPU at full speed, so ffmpeg never waits for it to wake up.

The open files limit for bs-node is 1048576 (LimitNOFILE in bs-node.service). The node page shows the limit the process really got. Stream buffers live in the bs-node process, not in /dev/shm, so its size does not matter.

50,000 viewers on one node ​

A node with a 10 to 25 Gbps link can hold 50,000 viewers at 200 to 500 kb/s. In our test (2 cores of a recent CPU, over loopback) bs-node kept all 50,000 viewers at full rate:

50,000 viewersSentCPUbs-node memory
Live TS at 234 kb/s14.5 Gbps2 cores, full2.0 GB (40 KB per viewer)
Movies at 320 kb/s16.2 Gbps1.3 cores2.0 GB (40 KB per viewer)

Loopback tests connections and CPU, not the network card. For 25 Gbps, plan about 2 cores for movies and 3 to 9 for live channels on a recent CPU, and twice that on an old one. Method and all numbers: docs/research/nginx-delivery.md, section 3.6.

What runs out first is the kernel, not bs-node. Check these before such a load:

LimitNeededWhy
LimitNOFILE in bs-node.service1048576 (set by the installer)One file per viewer, plus sources and ffmpeg pipes.
fs.file-max, fs.nr_openabove 1048576The machine limit must stay above the service limit.
net.ipv4.tcp_mem1/16, 1/8, 1/4 of RAM (System tuning sets this)The default lets all sockets use only about 1/11 of RAM. In our test 50,000 viewers passed it: the kernel logged TCP: out of memory and new viewers waited minutes to start.
net.core.somaxconn, net.ipv4.tcp_max_syn_backlog65535Thousands of players reconnect at the same moment after a network blip.
net.ipv4.ip_local_port_range10000 65535Only for connections the node opens (sources, relays, proxy mode). Viewers connect to one port and use none of these.
net.netfilter.nf_conntrack_maxwell above the viewer count, if a firewall such as ufw is on. System tuning does not set it, because the key only exists while the firewall module is loaded. Add it to /etc/sysctl.d/ yourself.The firewall tracks every viewer. When the table is full, new viewers are dropped. Each entry takes about 300 bytes.

Go settings need no change. bs-node uses all CPU cores by default. Do not set a low GOMEMLIMIT: at 50,000 viewers the memory is real connection state, so a low limit only makes the garbage collector run all the time.

To test a node before real viewers arrive, use the load tool in tests/load (see its README). Run it from a second machine if you can. On the node itself the tool and the node share the CPU, and the numbers come out worse than they really are.

Bitstream documentation