Trimming a stream approximately, because exactly is O(n)

A stream does not empty itself, and exact trimming has to walk to find the boundary — which on a large stream is a foreground operation on a single-threaded server.

# exact, and O(n). not on a hot path.
XTRIM orders MAXLEN 100000

# approximate: removes whole macro nodes, effectively free
XTRIM orders MAXLEN ~ 100000

# or on write, which is where it belongs
XADD orders MAXLEN ~ 100000 '*' order_id 91204

The tilde is not optional in practice: approximate trimming stops when the next node would take the stream below the limit, so the result is slightly longer than asked for, which is exactly what anyone wants. The important caveat is that trimming ignores the pending entries list — a stream trimmed aggressively can remove entries a dead consumer had claimed, which is the same message loss the list had, arrived at differently.