Introducing ‘Hive’
By: Kasper Rasmussen
Tags: Technology, Nodejs, Trading, Cryptocurrency, Bitcoin

Introducing ‘Hive’

A distributed matching engine built to scale

This week’s performance update was focused around the deployment of Hive — a distributed, multi-threaded & multi-process matching engine built around speed, stability and scalability.

Electronic Matching Engines

A matching engine is the core software behind an electronic, computer-driven trading platform like Bitfinex. A far cry from a hall packed with rowdy floor traders, cryptocurrency trading (and that of most other financial instruments) is now almost entirely enabled by electronic matching engines and the software supporting this.

Matching engines work by matching up trades on a platform, allocating bids and offers amongst competing traders by means of certain set of rules (algorithms). Through an increasingly efficient order allocation process, electronic matching engines have worked to drive down transaction fees to pennies whilst improving market quality (lower spreads, improved order book depth).

“Time is money”

As a by-product of this, the speed at which one can receive, interpret and react to market information, in both relative and absolute terms, is now a fundamental consideration for both traders and exchanges seeking a competitive advantage.

Introducing ‘Hive’

Recent platform updates on Bitfinex have focused on transitioning away from a single-threaded matching engine towards a distributed, high-performance, multi-process system.

Referred to as Hive, this system allows for increasingly efficient order execution & improved scaling capabilities by separating workloads amongst multiple ‘instances’, or workers. Instances communicate via Grenache to allocate and execute trades in a dynamic manner, spawning new workers in times of high load (and vice versa).

Engine Performance Improvements

Due to improvements brought on by Hive, traders on Bitfinex now react more quickly to price movements and changing market conditions. The new architecture enables Bitfinex to maintain high performance in the face of a growing user base, new tradable assets and unforeseen spikes in trading activity.

The distribution of the matching engine ensures that order matching capacity (and latency) remains unaffected by periods of high order throughput, resulting in a stable trading environment for Bitfinex traders. This minimises chances of order rejection due to system overload, even under heavy load.

Based on internal testing, Hive has successfully scaled up to 100x the number of orders handled in a current sustained market. The deployment of Hive has resulted in a complete order execution time of 14ms, down 10x relative to pre-deployment. This includes the pre-risk check (heaviest part), order matching (microseconds) and execution reporting. Theses results are based on initial tests and are likely to improve further as we work to optimise Hive integration.

Hive also comes with full support for new order types (e.g. position reduce-only margin orders), order level flags to prevent self-trading, FX virtual books and support for a number of additional gateways, including Ethfinex DEX gateway and FIX.

The Decentralisation of Ethfinex

On the topic of Ethereum, Hive puts the groundwork in place for the decentralisation of Ethfinex. Through adding support for expiry times for orders into the engine — a necessary component for the matching of 0x format, non-custodial orders — as well as adding several other flags, the road is paved for the launch of the Ethfinex non-custodial API and decentralised trading portal.

A new gateway API will allow these Ethereum based order promises (signed and authenticated using a trader’s private key) to be submitted into Hive and treated agnostically like any other order, up until the point of execution when they are settled with atomic swaps on Ethereum.