Chapter 18.2: Bridge Architectures
18.2.1 The Coupling Challenge
Our integrated urban simulation requires communication between heterogeneous components: SUMO (C++), physics engines (Python/NumPy), MFG solvers (Python), signal controllers (potentially Java), and visualisation dashboards (JavaScript). The bridge architecturedetermines how these components exchange data.
The critical constraint is latency within the simulation loop. At each SUMO timestep (0.5โ1.0 s simulated time), we must:
- Receive vehicle states from SUMO via TraCI
- Compute emissions (HBEFA) and canyon concentrations (OSPM)
- Run MFG routing update if needed
- Send control commands back (reroute, signal changes)
If the total round-trip takes longer than real-time, the simulation slows down. The bridge latency budget is:
$$\tau_{\text{bridge}} < \tau_{\text{step}} - \tau_{\text{SUMO}} - \tau_{\text{TraCI}} - \tau_{\text{physics}}$$
18.2.2 Four Bridge Architectures
1. REST / FastAPI
The simplest architecture: each physics module exposes a REST API. The controller sends HTTP POST requests with JSON payloads. FastAPI (Python) provides automatic OpenAPI documentation and async support.
- Latency: ~8 ms per request (HTTP overhead + JSON serialization)
- Strengths: easy debugging (curl, Postman), self-documenting
- Weakness: HTTP overhead is unnecessary for co-located processes
- Best for: prototyping, <100 vehicles
2. ZeroMQ (ZMQ)
ZeroMQ provides brokerless, asynchronous messaging with multiple patterns (REQ/REP, PUB/SUB, PUSH/PULL). Messages are raw bytesโno HTTP overhead, no JSON parsing.
- Latency: ~0.8 ms per message
- Strengths: 10x faster than REST, multiple messaging patterns, language-agnostic
- Weakness: no built-in serialisation (must choose msgpack, protobuf, etc.)
- Best for: recommended starting point, 100โ1000 vehicles
3. gRPC + Protobuf
gRPC uses HTTP/2 with Protocol Buffers for binary serialisation. It provides type-safe interfaces, bidirectional streaming, and automatic code generation for all major languages.
- Latency: ~0.3 ms per message (binary serialisation, HTTP/2 multiplexing)
- Strengths: type safety, streaming, production-grade, auto-generated stubs
- Weakness: more setup (proto file compilation, generated code)
- Best for: production deployment, >1000 vehicles
4. Subprocess stdin/stdout
The simplest possible approach: launch the physics engine as a subprocess and communicate through stdin/stdout pipes with line-delimited JSON.
- Latency: ~2 ms (pipe overhead)
- Strengths: zero dependencies, trivial to implement
- Weakness: single producer-consumer, no multiplexing, buffering issues
- Best for: single-module integration, testing
18.2.3 Latency Benchmark Comparison
| Architecture | Latency (ms) | Throughput (msg/s) | Serialisation | Type Safety | Scalability |
|---|---|---|---|---|---|
| REST/FastAPI | ~8.0 | ~125 | JSON | Pydantic | Good |
| ZeroMQ | ~0.8 | ~1,250 | Custom (msgpack) | None | Excellent |
| gRPC | ~0.3 | ~3,300 | Protobuf | Full | Excellent |
| Subprocess | ~2.0 | ~500 | Line JSON | None | Poor |
Recommendation:
Start with ZeroMQ for development and testing. Migrate to gRPC when the vehicle count exceeds ~1000 or when you need type safety across Python/Java boundaries. Use REST only for external dashboards and monitoring APIs.
18.2.4 Decision Framework
Choose the bridge architecture based on your deployment scale:
$$N_{\text{vehicles}} < 100 \rightarrow \text{REST} \qquad 100 < N < 1000 \rightarrow \text{ZeroMQ} \qquad N > 1000 \rightarrow \text{gRPC}$$
The total per-step budget for 1000 vehicles at 1-second steps:
$$1000\text{ ms} = \underbrace{20\text{ ms}}_{\text{SUMO}} + \underbrace{5\text{ ms}}_{\text{TraCI}} + \underbrace{0.8\text{ ms}}_{\text{bridge}} + \underbrace{50\text{ ms}}_{\text{physics}} + \underbrace{924.2\text{ ms}}_{\text{headroom}}$$
With ZeroMQ, the bridge adds less than 1 ms of overhead, leaving ample time for physics computations. The bottleneck is typically the physics engine (OSPM + MFG solver), not the communication layer.
18.2.5 Python: ZeroMQ Bridge Implementation
This code implements a complete ZeroMQ bridge with a server (physics engine) and client (SUMO controller), including a latency benchmark.
ZeroMQ Bridge: Architecture & Latency Benchmark
PythonClick Run to execute the Python code
Code will be executed with Python 3 on the server
18.2.6 Key Takeaways
- Four bridge architectures (REST, ZeroMQ, gRPC, subprocess) trade off simplicity against latency by 25x (8 ms vs 0.3 ms).
- ZeroMQ at ~0.8 ms per message is the recommended starting point: 10x faster than REST, much simpler than gRPC, and sufficient for up to ~1000 vehicles.
- gRPC with protobuf provides the lowest latency (0.3 ms), full type safety, and bidirectional streamingโideal for production deployments.
- The bridge is typically not the bottleneck; physics computation (OSPM, MFG) dominates the latency budget.
- Batch requests amortise serialisation overhead: sending 10 edges in one request is 5โ8x more efficient than 10 individual requests.