Skip to content

feat: add opt-in TCP_NODELAY support for low-latency trading #390

@vjsingh1984

Description

@vjsingh1984

Problem

Trading systems using this library need to minimize latency on order submissions. Currently, the TcpStream connections use Nagle's algorithm (the default), which can buffer small writes (order messages ~100-200 bytes) for up to 40ms before sending them.

For algorithmic trading, this adds unnecessary latency to order routing — a critical path where milliseconds matter.

Proposed Solution

Add opt-in TCP_NODELAY support via:

  1. Environment variable IBAPI_TCP_NODELAY=1 to disable Nagle's algorithm
  2. Apply to all 4 connection paths:
    • TcpSocket::new() (sync initial connection) — transport/sync.rs:704
    • TcpSocket::reconnect() (sync reconnect) — transport/sync.rs:719
    • AsyncConnection::connect_with_callback() (async initial connection) — connection/async.rs:42
    • AsyncConnection::reconnect() (async reconnect) — connection/async.rs:91

Backward Compatibility

  • Default behavior: unchanged (Nagle enabled)
  • Opt-in: Set IBAPI_TCP_NODELAY=1 before calling Client::connect()
  • No API changes required
  • Zero breaking changes

Performance Impact

When enabled:

  • Eliminates up to 40ms delay on small writes (order submissions, market data requests)
  • For trading systems with low message rates (~1-10/sec), bandwidth overhead is negligible
  • Significantly reduces order routing latency

Implementation

I have a fork with this change ready to submit as a PR. The implementation:

  • Adds 4 socket.set_nodelay(true)? calls, one per connection path
  • All 100 existing unit tests pass
  • Environment variable is checked at connection time only
  • ~18 lines of code total

Would you be open to this feature? I can open a PR if this aligns with the project's goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions