Message bus
The gateway is a group of services. fix-positions is the trading engine: it keeps accounts, orders and positions and connects to the market data sources and liquidity providers configured for it. live-edge holds the WebSocket connections of the trading apps and of the Manager API. history stores quotes and answers requests for candles. signals publishes trading signals and price alerts.
The services and the cashier backend talk to each other over RabbitMQ topic exchanges. This page lists the exchanges, what is published on each, and the queues that read from them.
Binding
A binding key on a topic exchange can use two wildcards:
*stands for exactly one word.#stands for zero or more words.
*.PositionClosed matches the closed positions of every account, 42.# matches everything about account 42.
In the keys below, <routing key> is the ROUTING_KEY of a service. live-edge always publishes with WINTRADO, so the other services are configured with the same value.
Exchanges
| Exchange | Published by | Content |
|---|---|---|
sessions | live-edge | Commands of a trading app that a service answers |
quotes | fix-positions | Prices |
trading | fix-positions, cashier backend | What happens on an account |
trading.events | fix-positions | The record of every committed change |
system | fix-positions | Settings that changed |
signals | signals | Trends |
alerts | signals | Price alerts |
notifications | signals | Price changes |
alerts and trading.events are durable. The other exchanges are declared again by the services that use them after a broker restart.
sessions
The commands of a trading app that live-edge does not answer itself. They are the messages of commands.proto in the message schema:
| Command | Answered by |
|---|---|
InstrumentsInfoRequest | fix-positions |
TradingOptionsRequest | fix-positions |
AccountBalanceRequest | fix-positions |
AccountHistoryRequest | fix-positions |
OpenPositionsListRequest | fix-positions |
PendingOrdersListRequest | fix-positions |
PositionCloseRequest | fix-positions |
PositionCloseAllRequest | fix-positions |
HistoryQuotesRequest | history |
TrendQuotesRequest | history |
PrimeQuotesRequest | history |
SignalsRequest | signals |
CoinsBalanceRequest | Cashier backend |
Login, Pong and the four commands that subscribe to quotes or cancel a subscription never reach the bus. live-edge handles them.
live-edge publishes a command with the key <routing key>.<MessageType>, for example WINTRADO.AccountBalanceRequest. The body is the JSON the client sent. The headers accountId and customerId name the logged in user.
The service that handles the command publishes its answer to the default exchange with the reply_to of the command as the key and repeats the correlation_id. reply_to is a direct reply-to address, so the answer reaches the live-edge replica that asked, and live-edge passes it on to the client. It uses the first answer to a command and ignores any other.
live-edge waits 10 seconds for an answer and then sends the client a TimeoutError. A command expires after the same time, so a service that was down does not find commands nobody waits for anymore. A command to close positions is also answered when it worked, with PositionCloseResponse or PositionCloseAllResponse.
quotes
Prices, published as Protobuf Quote messages of live.proto with the key <routing key>.<symbol>, for example WINTRADO.EURUSD.
Quotes are dropped when the broker does not take messages fast enough.
trading
What happens on an account, for the trading apps, the Manager API and the cashier backend. The key is <accountId>.<MessageType>.
| Message | Published by | Body |
|---|---|---|
PositionOpened, PositionUpdated, PositionClosed | fix-positions, cashier backend | JSON |
PositionStatus | fix-positions, cashier backend | PositionStatus of live.proto |
OrderCreated, OrderUpdated | fix-positions | JSON |
TransactionCreated | fix-positions | JSON |
AccountCreate, AccountUpdate | fix-positions | JSON |
AccountStatus | fix-positions | JSON |
BalanceUpdate | fix-positions | AccountBalance of live.proto |
PropChallengeStatus | fix-positions | PropChallengeStatusUpdate of live.proto |
PropChallengeProgress | fix-positions | PropChallengeProgressUpdate of live.proto |
CoinsBalanceUpdate, LoyaltyLevelUpdate | Cashier backend | JSON |
The cashier backend publishes the position messages of MetaTrader 5 accounts. Their key starts with the MT5 login instead of an account id.
fix-positions sets the header group to the group of the account. The Manager API uses it to decide who may see a message and leaves out messages without it.
PositionStatus, AccountStatus and BalanceUpdate only report a current value. fix-positions drops them when the broker does not take messages fast enough, and the next one replaces them.
A message on this exchange can be lost, so do not build a copy of the data from it. Use trading.events for that.
trading.events
Every committed change to positions, transactions, accounts and orders, in the order the changes were committed, as Event messages of events.proto. The stream trading.events.log keeps them for 7 days. See Trading Events.
system
Tells live-edge to reload what it caches from fix-positions. The key is the message type:
UpdateMarkupsUpdateGroups
signals
The trend of a symbol as JSON, with the key TrendMessage. live-edge sends it to every connected client. See Trend Indicator.
alerts
Price alerts as JSON, with the key MarketAlert. live-edge sends an alert to the connections of the account named in it.
notifications
Price changes of a symbol as JSON, with the key PriceChange. The cashier backend sends a push notification to the customers who have the symbol on a watchlist.
Queues
One queue per instance
Every live-edge replica has to see every message, because any of them can hold the connection a message is for. Each replica therefore binds its own queue to trading, quotes, signals, alerts and system with #. The queue is exclusive, named by the broker, and gone when the replica disconnects.
history and the cashier backend answer their commands from queues the broker names as well, one per command and instance: <routing key>.HistoryQuotesRequest, <routing key>.TrendQuotesRequest and <routing key>.PrimeQuotesRequest for history, *.CoinsBalanceRequest for the cashier backend. Every instance gets the command and answers it.
Shared queues
The instances of the other consumers share a queue, so a message is handled once. These are quorum queues: the broker keeps them on three nodes, and they stay available while one node is down.
| Queue | Exchange | Binding keys | Consumer |
|---|---|---|---|
fix-positions.<routing key>.commands | sessions | <routing key>.# | fix-positions, the engine holding the lease |
signals.<routing key>.commands | sessions | *.SignalsRequest | signals |
signals.<routing key>.trading | trading | *.PositionOpened, *.PositionClosed | signals |
history.<routing key>.quotes | quotes | <routing key>.# | history |
cashier.positions-consumer | trading | *.PositionOpened, *.PositionUpdated, *.PositionClosed, *.OrderCreated | Cashier backend |
cashier.accounts-consumer | trading | *.AccountCreate, *.AccountUpdate | Cashier backend |
cashier.transactions-consumer | trading | *.TransactionCreated | Cashier backend |
cashier.coins-consumer | trading | *.PositionClosed | Cashier backend |
cashier.position-status-consumer | trading | *.PositionStatus | Cashier backend, one instance at a time |
cashier.notifications | notifications | # | Cashier backend |
fix-positions receives every command of its routing key and ignores the ones another service answers. A stopping engine stops consuming before it hands the lease over; commands that arrive meanwhile wait in the queue for the next one.
fix-positions, signals and the notifications consumer of the cashier backend consume without acknowledgement. The others acknowledge a message once it is handled.
history.<routing key>.quotes holds at most 100000 quotes. When it is full, the oldest are dropped. The replicas of history share it, so each quote is stored once.
A PositionStatus in cashier.position-status-consumer expires after 10 seconds. The queue has a single active consumer: the broker delivers to one instance of the cashier backend and moves on to another when that one disconnects. An instance writes the statuses it received once a second in one statement, and two instances writing the same positions would block each other.
signals.<routing key>.quotes-consumer on quotes with # is exclusive: it belongs to the connection of signals and is gone with it.
Dead letters
The cashier backend rejects a message it cannot read or fails to handle:
| Queue | A rejected message |
|---|---|
cashier.positions-consumer | Goes to the queue positions.dead-letters |
cashier.accounts-consumer | Is dropped |
cashier.transactions-consumer | Is dropped |
cashier.coins-consumer | Goes to the queue coins.dead-letters |
cashier.position-status-consumer | Is dropped |
Nothing reads positions.dead-letters and coins.dead-letters.
Queues of earlier versions
The type of a queue cannot change, which is why the quorum queues have new names. Where a classic queue of an earlier version exists, the service that consumed it takes it off the exchange when it starts and handles what is left in it. A message published in between is in both queues and handled twice. Delete these queues by hand once they are empty:
| Earlier queue | Now |
|---|---|
fix-positions.<routing key>.sessions-consumer | fix-positions.<routing key>.commands |
signals.<routing key>.sessions-consumer | signals.<routing key>.commands |
signals.<routing key>.trading-consumer | signals.<routing key>.trading |
history.<routing key>.quotes-consumer | history.<routing key>.quotes |
cashier-positions.anyGroup | cashier.positions-consumer |
cashier-accounts.anyGroup | cashier.accounts-consumer |
cashier-transactions.anyGroup | cashier.transactions-consumer |
coins.allGroups | cashier.coins-consumer |
cashier-position-status.anyGroup | cashier.position-status-consumer |
positions.dlx and coins.dlx are only taken off their exchanges, so the dead letters in them can be looked at before they are deleted. cashier.notifications-consumer disappears by itself when the last instance of an earlier version disconnects.
The queues of single instances described above are classic queues. The stream trading.events.log is described in Trading Events.
Monitoring
| Metric | Meaning |
|---|---|
wintrado_fix_positions_amqp_connected | 0 while fix-positions has no connection to the broker |
wintrado_fix_positions_amqp_publish_dropped_total | Messages fix-positions could not publish because the broker was unreachable. Events of trading.events are not among them, they wait in the outbox |
wintrado_fix_positions_amqp_ticks_dropped_total | Quotes and status values dropped because the broker did not take messages fast enough |
cashier_amqp_connected | 0 on an instance of the cashier backend without a connection to the broker |
The readiness probe of the cashier backend is GET /api/health/ready. Besides the database it checks the broker: it fails on an instance that has not connected to the broker yet, and on one that has been without a connection for 2 minutes. Such an instance gets no requests. The liveness probe GET /api/health does not look at the broker.
What to alert on
Every expression is meant per deployment, for example with max by (namespace).
| Expression | Meaning |
|---|---|
wintrado_fix_positions_amqp_connected == 0 for 2 minutes | The engine cannot take requests or publish |
increase(wintrado_fix_positions_amqp_publish_dropped_total[10m]) > 0 | Messages of the engine were lost. The trading apps and the queue handlers of the cashier did not get them |
wintrado_fix_positions_outbox_oldest_pending_age_seconds > 60 for 2 minutes | Trading events do not reach the broker |
increase(wintrado_fix_positions_outbox_published_total[15m]) > 0 while increase(cashier_gateway_projector_events_total[15m]) is 0 | The engine publishes events and the CRM projection handles none |
cashier_gateway_projector_lag_seconds > 60 for 10 minutes, while rate(cashier_gateway_projector_events_total[5m]) > 0 | The projection is behind |
cashier_gateway_projector_parked_events > 0 | An event failed and waits for a person |
cashier_unverified_movement_oldest_age_seconds > 900 | A money movement is neither confirmed nor refused |
cashier_amqp_connected == 0 for 3 minutes | An instance of the cashier backend is cut off from the broker |