# Latency Optimization

> A Claude skill from brainbytes-dev/everything-claude-trading.

- **Type:** Skill
- **Install:** `agentstack add skill-brainbytes-dev-everything-claude-trading-latency-optimization`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [brainbytes-dev](https://agentstack.voostack.com/s/brainbytes-dev)
- **Installs:** 0
- **Category:** [Finance & Payments](https://agentstack.voostack.com/c/finance-and-payments)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [brainbytes-dev](https://github.com/brainbytes-dev)
- **Source:** https://github.com/brainbytes-dev/everything-claude-trading/tree/main/skills/execution/latency-optimization

## Install

```sh
agentstack add skill-brainbytes-dev-everything-claude-trading-latency-optimization
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Latency Optimization

> Low-latency trading infrastructure — co-location, FPGA, kernel bypass, network optimization.

## When to Activate

- User is building or evaluating low-latency trading systems
- Optimizing tick-to-trade latency for market making or arbitrage
- Understanding co-location, FPGA-based trading, kernel bypass networking
- Measuring and profiling latency in trading systems
- Evaluating hardware and network topology for trading infrastructure

## Core Concepts

### Why Latency Matters

In competitive trading strategies (market making, statistical arbitrage, index arbitrage), speed determines profitability. A latency advantage means:
- Canceling stale quotes before being adversely selected
- Capturing fleeting arbitrage opportunities
- Being first in the exchange queue (price-time priority)
- Reacting to market data before competitors adjust positions

### Latency Budget

A typical tick-to-trade path and budget:

```
Market Data Feed → NIC → Kernel → User Space → Strategy Logic → Order Generation → NIC → Network → Exchange

Component              Typical Latency      Optimized Latency
────────────────────────────────────────────────────────────────
Exchange → NIC         ~1 μs (co-located)   ~0.5 μs
NIC → Kernel           ~5-10 μs             bypassed (0)
Kernel → User Space    ~2-5 μs              bypassed (0)
Market Data Parse      ~1-5 μs              ~0.2 μs (FPGA)
Strategy Logic         ~1-10 μs             ~0.1 μs (FPGA)
Order Serialization    ~1-2 μs              ~0.1 μs (FPGA)
User Space → Kernel    ~2-5 μs              bypassed (0)
NIC → Exchange         ~1 μs (co-located)   ~0.5 μs
────────────────────────────────────────────────────────────────
Total                  ~15-40 μs            ~1-3 μs (FPGA)
```

### Technology Stack Tiers

| Tier | Technology | Tick-to-Trade | Use Case | Annual Cost |
|------|-----------|---------------|----------|-------------|
| **1** | FPGA + kernel bypass | 1-5 μs | Market making, HFT | $1-5M |
| **2** | C++ + kernel bypass | 5-20 μs | Stat arb, fast alpha | $200K-1M |
| **3** | C++ standard | 20-100 μs | Medium-frequency | $50-200K |
| **4** | Java/C# optimized | 100-500 μs | Slower systematic | $20-50K |
| **5** | Python | 1-10 ms | End-of-day strategies | 
template
class SPSCQueue {
    // Single-producer single-consumer lock-free queue
    std::array buffer;
    alignas(64) std::atomic head{0};  // separate cache lines
    alignas(64) std::atomic tail{0};
    // ...
};

// 2. Memory pre-allocation (no malloc in hot path)
// Pre-allocate all objects at startup, use object pools

// 3. CPU pinning and isolation
// isolcpus=2,3,4,5 in kernel boot params
// pthread_setaffinity_np() to pin trading threads

// 4. NUMA awareness
// Ensure memory is allocated on the same NUMA node as the CPU
// numactl --cpunodebind=0 --membind=0 ./trading_app

// 5. Huge pages (reduce TLB misses)
// echo 1024 > /proc/sys/vm/nr_hugepages
// mmap with MAP_HUGETLB

// 6. Compiler optimizations
// -O3 -march=native -flto -fno-exceptions
// Profile-guided optimization (PGO): -fprofile-generate / -fprofile-use

// 7. Avoid branch mispredictions
// Use branchless code: result = (a > b) * a + (a <= b) * b
// __builtin_expect() for likely/unlikely branches
```

### Step 5: Latency Measurement

```python
def latency_measurement_framework():
    """
    Accurate latency measurement requires:
    1. Hardware timestamping (NIC or FPGA, not system clock)
    2. Nanosecond precision (TSC, RDTSC, or PTP)
    3. Measure at multiple points in the pipeline
    4. Statistical analysis of the distribution (not just mean)
    """
    # Key metrics:
    metrics = {
        'median_latency': 'P50 — typical case',
        'p99_latency': 'P99 — tail latency (critical for HFT)',
        'p999_latency': 'P99.9 — extreme tail',
        'jitter': 'P99 - P50 — consistency matters as much as speed',
        'max_latency': 'Worst case — often caused by OS jitter',
    }

    # Sources of jitter (latency variance):
    jitter_sources = {
        'context_switches': 'Solution: CPU isolation, RT kernel',
        'interrupts': 'Solution: IRQ affinity, move interrupts off trading cores',
        'TLB_misses': 'Solution: huge pages',
        'cache_misses': 'Solution: keep hot data in L1/L2, avoid false sharing',
        'GC_pauses': 'Solution: avoid GC languages, or tune GC (Zing JVM)',
        'page_faults': 'Solution: mlockall(), pre-touch memory',
        'NUMA_remote': 'Solution: NUMA-aware allocation',
        'power_management': 'Solution: disable C-states, set governor to performance',
    }

    return metrics, jitter_sources

# Hardware timestamp capture:
# On Linux with Solarflare NIC:
# - Use SO_TIMESTAMPING socket option for hardware RX/TX timestamps
# - Accuracy: ~5 nanoseconds
# - Compare hardware timestamps to measure true wire-to-wire latency
```

### Step 6: Market Data Feed Optimization

```python
def market_data_architecture():
    """
    Market data feed handling is often the latency bottleneck.
    """
    approaches = {
        'consolidated_feed': {
            'source': 'SIP (US), CTA/UTP',
            'latency': '500 μs - 1 ms (too slow for HFT)',
            'use_case': 'Compliance, slow strategies',
        },
        'direct_feed': {
            'source': 'Exchange proprietary (ITCH, PITCH, XDP)',
            'latency': '1-10 μs (co-located)',
            'use_case': 'Market making, fast strategies',
            'formats': {
                'nasdaq': 'ITCH 5.0 (binary, efficient)',
                'nyse': 'XDP (binary)',
                'bats_cboe': 'PITCH (binary)',
                'cme': 'MDP 3.0 (binary, multicast)',
            }
        },
        'fpga_parsed': {
            'source': 'Direct feed parsed in FPGA',
            'latency': '100-500 ns from NIC to parsed message',
            'use_case': 'HFT, sub-microsecond strategies',
        },
    }

    # Feed handler optimization:
    # 1. Parse only fields you need (skip unused fields)
    # 2. Use fixed-offset parsing (binary protocols)
    # 3. Multicast: join specific groups, filter early
    # 4. Gap detection and recovery without blocking
    # 5. Book building: incremental updates, not full rebuilds

    return approaches
```

## Examples

### Latency Budget for Market Making

```python
budget = {
    'market_data_receive': 0.5,    # μs (co-located, direct feed)
    'md_parse_fpga': 0.3,          # μs (FPGA parsing)
    'strategy_logic': 1.0,         # μs (quote update calculation)
    'order_build': 0.2,            # μs
    'network_to_exchange': 0.5,    # μs (co-located)
    'exchange_processing': 5.0,    # μs (exchange matching engine)
}
total = sum(budget.values())  # 7.5 μs wire-to-wire
# Competitive for equity market making in 2024
```

### Cost-Benefit of Latency Investment

```python
# Is the latency investment worth it?
# Revenue model for market making:
spread_capture = 0.5  # cents per share (half the spread)
volume_per_day = 1_000_000  # shares
trading_days = 252

gross_revenue = spread_capture * volume_per_day * trading_days / 100  # $1.26M/year

# Faster latency → better queue position → higher fill rate → more revenue
# 10 μs improvement might increase fill rate by 5-15% → $60-190K additional revenue
# Co-location cost: ~$150K/year
# FPGA development: ~$500K one-time + $200K/year maintenance

# Breakeven: depends on strategy capacity and competition
```

## Quality Gate

- [ ] Latency measured end-to-end with hardware timestamps (not software timers)
- [ ] Tail latency (P99, P99.9) analyzed, not just median — jitter kills strategies
- [ ] CPU isolation configured (isolcpus, IRQ affinity, disable hyperthreading on trading cores)
- [ ] NUMA topology verified — memory and NIC on same NUMA node as trading thread
- [ ] Kernel bypass technology selected based on NIC vendor and latency requirements
- [ ] FPGA investment justified by strategy revenue (not all strategies need sub-microsecond)
- [ ] Market data feed handler tested under peak load (market open, news events)
- [ ] Clock synchronization (PTP/GPS) accurate to nanoseconds for cross-venue strategies
- [ ] Disaster recovery and failover tested without compromising latency in normal operation
- [ ] Total cost of ownership calculated including co-location, hardware, development, and maintenance

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [brainbytes-dev](https://github.com/brainbytes-dev)
- **Source:** [brainbytes-dev/everything-claude-trading](https://github.com/brainbytes-dev/everything-claude-trading)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-brainbytes-dev-everything-claude-trading-latency-optimization
- Seller: https://agentstack.voostack.com/s/brainbytes-dev
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
