Use case: a realtime risk and P&L loop¶
Goal. A program watches trades as they happen, recomputes a running risk or P&L figure per update, and writes the result back into a Center table, where risk managers, other services, or a dashboard pick it up live. This is the closed loop: subscribe, compute, write.
The shape¶
One client does both halves: a Center config to receive trades and a JDBC config to write the recomputed rows.
Write results with SQL from a worker thread, not publish, and not from the callback
Two rules collide here, and the resolution is the whole point of this use case:
publishdoes not populate a Center table (see the market-data use case), so the result must be written with SQLINSERT/UPDATE.- Do not run the SQL write on the callback thread. The callback runs on the client's I/O (delivery) thread; a slow or same-connection synchronous call there stalls update delivery (and a same-connection call can block). Hand the write to your own thread.
The working pattern: in on_update do the fast arithmetic, then hand the row to your own worker
thread, which runs the sql write. This was confirmed on a live instance.
1. Connect for both directions¶
2. Recompute on each trade, write the result off-thread¶
Keep a running position per symbol. on_update updates it in memory (fast, on the callback thread) and
queues the recomputed row; a worker thread drains the queue and writes each row with sql.
If the RISK table has a primary-key index on symbol, re-inserting a symbol upserts (updates the
row in place); without one, use UPDATE ... WHERE symbol == "..." for existing symbols. Copy the
update's fields before handing them to the worker, they are valid only during the callback.
3. Anyone can watch the result¶
Another program, a risk dashboard, an alerting service, subscribes to RISK the same way the
market-data dashboard does (merging on id), and sees each recomputed
figure as it lands.
Optional: a command to trigger a recompute¶
Register a named command so the platform (or a user in the UI) can ask your engine to recompute or
flatten a book on demand: handle it in on_command and reply with send_command_response. This is how
"kill switch" or "recalculate" buttons reach a native engine.
What this shows¶
- The loop closes in one client: subscribe on the Center connection, write on the JDBC connection.
- Your firm's logic stays in your code, in the language your risk model already uses; the platform carries the data in and the results out.
- The threading model is the crux: compute on the callback thread, and hand the write to a worker thread so a slow or same-connection SQL call never stalls update delivery.