Skip to content

About

Finds the fastest Binance edge IP for your machine and can pin it into /etc/hosts.

Topics

Resources

Stars

46 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

bn-edge-bench

This tool finds the fastest Binance edge IP for your machine. It can pin that IP into /etc/hosts.

Each Binance host has many edge IPs, and DNS gives you a random one. The tool finds the edges through DNS, measures each edge, and reports the fastest.

Install

cargo build --release
./target/release/bn-edge-bench --help

Usage

bn-edge-bench                                     # all venues
bn-edge-bench --venue futures-ws-api -n 20        # one venue, more samples
bn-edge-bench --discover-secs 30 --scan-width 24  # a larger search
bn-edge-bench --json > run.json                   # JSON report on stdout
bn-edge-bench --venue spot-fix-oe,spot-fix-md \
    -k "$KEY" --private-key-file ed25519.pem      # FIX
bn-edge-bench --venue spot-fix-sbe-oe,spot-fix-sbe-md \
    -k "$KEY" --private-key-file ed25519.pem      # FIX SBE
bn-edge-bench --venue spot-sbe,spot-fix-md,spot-fix-sbe-md --race 60 \
    -k "$KEY" --private-key-file ed25519.pem      # race three feeds for 60 s
sudo -E bn-edge-bench --update-hosts              # pin the winners, each with a backup
sudo -E bn-edge-bench --pin-count 1               # pin the winners only
sudo bn-edge-bench --clear-hosts                  # remove the pins
sudo bn-edge-bench --no-update-hosts              # run as root, do not change /etc/hosts

For all options, run bn-edge-bench --help.

Venues

Venue Endpoint Metric Credentials
spot-sbe stream-sbe.binance.com:9443 1-way lag API key
spot-ws-api ws-api.binance.com:443 round-trip none
spot-fix-oe fix-oe.binance.com:9000 round-trip API key, private key
spot-fix-md fix-md.binance.com:9000 1-way lag API key, private key
spot-fix-sbe-oe fix-oe.binance.com:9002 round-trip API key, private key
spot-fix-sbe-md fix-md.binance.com:9002 1-way lag API key, private key
futures-ws fstream.binance.com:443 1-way lag none
futures-ws-api ws-fapi.binance.com:443 round-trip none
futures-api fapi.binance.com:443 round-trip none

The default is all venues. To select venues, use --venue, for example --venue futures-ws,futures-ws-api. If a credential is missing, the tool skips the venues that need it. The run does not fail.

The 1-way lag venues measure trades. futures-ws also measures bookTicker. For a symbol with few trades, increase --idle-timeout.

Order entry and market data use different edges. A pin for market data does not make your orders faster. If you send orders, also benchmark the order-entry venue.

Credentials

spot-sbe and the FIX venues accept only an Ed25519 API key.

  • Supply the API key with -k or BINANCE_API_KEY.
  • The FIX venues also need the private key of that API key to sign the Logon. Supply an unencrypted PKCS#8 PEM file with --private-key-file or BINANCE_PRIVATE_KEY_FILE.
  • FIX order entry needs the FIX_API permission. FIX market data needs FIX_API or FIX_API_READ_ONLY.

The tool reads the private key only if you select a FIX venue. If the tool cannot read the key file, the run stops before it measures. If the gateway refuses the key, the report shows the Binance error code. Code -2015 shows a bad key, a bad IP or a missing permission.

Read the report

Example from Tokyo, market-data venues (abridged):

Venue            | Edge IP         |       TCP |       TLS | Metric     |   p50 ms |   p90 ms |   p99 ms |   max ms |    n
--------------------------------------------------------------------------------------------------------------------------
Spot SBE         | 35.79.145.xxx   |     234us |    1.89ms | 1-way lag  |    1.114 |    1.536 |    1.981 |    1.981 |   90
Spot FIX MD      | 18.178.146.xxx  |     916us |    2.19ms | 1-way lag  |    1.466 |    2.144 |   45.436 |   45.436 |   90
Spot FIX SBE MD  | 35.75.126.xxx   |    1.29ms |    2.75ms | 1-way lag  |    1.415 |    2.189 |   10.035 |   10.035 |   90
Futures WS       | 3.115.150.x     |     269us |    1.73ms | 1-way lag  |    1.608 |    2.129 |   20.683 |   20.683 |   90

Spot SBE: none of the 16 edges scanned separated from the field (384 samples); picked 35.79.145.xxx on TCP among the leaders, so treat it as a tie
Spot FIX MD: 18.178.146.xxx is measurably fastest of the 2 edges scanned (180 samples)
Spot FIX MD: of that lag, p50 0.985 ms passed inside the exchange (TransactTime to the gateway's SendingTime) and p50 0.423 ms after it
Futures WS: winner's samples contain outliers (sd 2.4 ms vs robust 0.5 ms, max 20.7 ms)

Each row shows the winning edge of a venue. TCP and TLS are its handshake times. The percentiles are in milliseconds. n is the number of samples.

  • round-trip is the time from a request to its reply. The request is a ping, or a FIX TestRequest.
  • 1-way lag is the local arrival time minus the time of the match (TransactTime). All the 1-way lag venues use this time. The lag includes the offset of your NTP clock.
  • Do not compare a 1-way lag with a round-trip.
  • If the report shows a negative lag, your clock is behind the exchange. Synchronize your clock with NTP, then run the tool again.

Each venue has one verdict line:

JSON verdict Report line Meaning
separated is measurably fastest The median of the winner is better than the median of the runner-up by more than their combined sampling error.
inconclusive treat it as a tie The leaders overlap. The tool selected the leader with the lowest TCP time.
sole-candidate was the only edge that stayed up No other edge stayed up.

Other lines:

  • of that lag, ... inside the exchange: FIX and FIX SBE market data only. The first part is from two exchange clocks, so your NTP offset has no effect on it.
  • winner's samples contain outliers: the samples have a real tail.
  • winner has the better median but a worse p99: compare the tails before you pin.
  • REQUEST_WEIGHT ... used: the part of the rate-limit budget that the run used.

FIX

Binance applies these limits to each account, not to each IP. Your live FIX sessions on the same account share them.

Concurrent sessions Logon attempts
Order entry 10 15 per 30 s
Market data 100 300 per 300 s
  • The tool opens a maximum of 5 order-entry sessions and 50 market-data sessions.
  • FIX and FIX SBE share these limits. The tool runs the venues one after the other.
  • Each session has a random SenderCompID, and the tool logs out each session. A run cannot lock the SenderCompID of your live sessions.
  • The FIX SBE venues use the schema spot-fixsbe-1_1.xml.
  • The tool does not benchmark drop copy (fix-dc).

Race

--race SECS compares the spot market-data feeds on the same trades. SECS is 1 to 3600.

After the scan, the tool connects to the winners of spot-sbe, spot-fix-md and spot-fix-sbe-md. It receives trades from all the feeds at the same time and matches them by trade ID. One local clock stamps all the feeds, so the gaps have no NTP offset.

The race needs a winner for spot-sbe, and a winner for spot-fix-md or spot-fix-sbe-md in the same run. Thus, it needs the API key and the private key. If a winner is missing, the report shows Race: skipped. If a FIX feed cannot connect, the race continues without it.

The race lines:

  • first print of an event: the lag of the first trade of each event. One order that sweeps the book is one event with many trades. missed is the number of trades that the feed did not deliver.
  • per event, A behind B: the gap between the first prints of each event, and the percentage of events that each feed delivered first. Use this line to compare the feeds.
  • per trade: the gap for each trade. Each trade of a sweep counts, so bursts control this value.
  • transactTimes of the feeds differ by: if this value is zero, all the feeds use the same exchange timestamp. Only then can you compare their one-way lags.

Rate limits

The *-ws-api and futures-api venues send real requests:

  • Each request is an unauthenticated ping with weight 1. The tool never sends an order.
  • --rps sets the pace for each connection. The default is 4/s. The tool decreases a value above 5/s to 5/s. --rps also sets the pace of the FIX order-entry venues.
  • The tool reads the budget in each reply and stops at 80% of it. Other processes on the same IP use the same budget.
  • A 429 or 418 status stops the scan. Another error status stops only that edge.

The cost increases with --scan-width and -n. With the default settings, futures-api uses about 384 weight, which is 16% of the budget of 2400 per minute. If you trade on the same IP, use a smaller --scan-width.

/etc/hosts

--update-hosts writes pins. As root, the tool writes pins by default. --no-update-hosts stops this. When the tool writes pins, it:

  1. Removes its previous pins before the benchmark.
  2. Removes the untagged entries that conflict with the new pins.
  3. Pins only the edges that it measured end to end.

The tool tags its entries with # BN-EDGE-BENCH AUTO-GEN. --clear-hosts removes them. The writes are atomic, so an interrupt cannot leave a truncated file.

--pin-count is the total number of addresses for each host. The default of 2 is the winner and one backup:

18.180.200.143  fstream.binance.com # BN-EDGE-BENCH AUTO-GEN
13.193.146.230  fstream.binance.com # BN-EDGE-BENCH AUTO-GEN backup 1
  • Backups need multi on in /etc/host.conf. Without it, glibc returns only the first line that matches. If the setting is missing, the run shows a warning.
  • A backup helps only if connect() fails. It does not help if an edge accepts TCP but does not complete TLS.
  • The tool never pins an edge that stopped during the scan. Thus, a venue can get fewer pins than --pin-count.
  • A --pin-count above 2 costs more samples and more rate-limit weight.

FIX and FIX SBE use the same host names. The tool pins the FIX winner for these hosts. To pin the FIX SBE winner, run --venue spot-fix-sbe-oe,spot-fix-sbe-md alone.

To test the write without root:

cp /etc/hosts /tmp/hosts-test
BN_EDGE_HOSTS_FILE=/tmp/hosts-test bn-edge-bench --update-hosts

Output and exit status

Progress goes to stderr. The report goes to stdout. -q stops the progress messages.

The exit status is 0 if at least one venue gave a result and all requested changes to the hosts file were successful.

Development

cargo test                  # unit tests, no network
cargo clippy --all-targets

License

MIT. Refer to LICENSE.

About

Finds the fastest Binance edge IP for your machine and can pin it into /etc/hosts.

Topics

Resources

Stars

46 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages