← notes

Aggregating a price from several APIs, and deciding what to do when they disagree

Mar 28, 2021

Pull the bitcoin price from several APIs at once, so one bad feed cannot mislead an app.

The premise is simple and the implementation holds one real decision: what to do when the sources disagree.

A mean is the obvious answer and the wrong one, because it is trivially poisoned, one source returning a stale, zero or wildly wrong value drags the result, and the error scales with how wrong that source is. A median is bounded and survives any single bad source entirely, which is exactly the failure being defended against, at the cost of needing an odd count and discarding information when everything is healthy. A weighted mean needs weights, and any weighting is a defensible claim about which exchange is more trustworthy.

Quorum is the related question. If two of five sources time out, is an answer from three still an answer? At some point the aggregate stops doing its job and the caller has to be told, rather than handed a number identical in shape to a healthy one. Staleness catches people the same way: a cached price from ten minutes ago is worse than none, because the caller cannot tell.

Published to RubyGems. An example wrapper ships in examples/.