Skip to main content

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​

ExchangePublished byContent
sessionslive-edgeCommands of a trading app that a service answers
quotesfix-positionsPrices
tradingfix-positions, cashier backendWhat happens on an account
trading.eventsfix-positionsThe record of every committed change
systemfix-positionsSettings that changed
signalssignalsTrends
alertssignalsPrice alerts
notificationssignalsPrice 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:

CommandAnswered by
InstrumentsInfoRequestfix-positions
TradingOptionsRequestfix-positions
AccountBalanceRequestfix-positions
AccountHistoryRequestfix-positions
OpenPositionsListRequestfix-positions
PendingOrdersListRequestfix-positions
PositionCloseRequestfix-positions
PositionCloseAllRequestfix-positions
HistoryQuotesRequesthistory
TrendQuotesRequesthistory
PrimeQuotesRequesthistory
SignalsRequestsignals
CoinsBalanceRequestCashier 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>.

MessagePublished byBody
PositionOpened, PositionUpdated, PositionClosedfix-positions, cashier backendJSON
PositionStatusfix-positions, cashier backendPositionStatus of live.proto
OrderCreated, OrderUpdatedfix-positionsJSON
TransactionCreatedfix-positionsJSON
AccountCreate, AccountUpdatefix-positionsJSON
AccountStatusfix-positionsJSON
BalanceUpdatefix-positionsAccountBalance of live.proto
PropChallengeStatusfix-positionsPropChallengeStatusUpdate of live.proto
PropChallengeProgressfix-positionsPropChallengeProgressUpdate of live.proto
CoinsBalanceUpdate, LoyaltyLevelUpdateCashier backendJSON

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:

  • UpdateMarkups
  • UpdateGroups

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.

QueueExchangeBinding keysConsumer
fix-positions.<routing key>.commandssessions<routing key>.#fix-positions, the engine holding the lease
signals.<routing key>.commandssessions*.SignalsRequestsignals
signals.<routing key>.tradingtrading*.PositionOpened, *.PositionClosedsignals
history.<routing key>.quotesquotes<routing key>.#history
cashier.positions-consumertrading*.PositionOpened, *.PositionUpdated, *.PositionClosed, *.OrderCreatedCashier backend
cashier.accounts-consumertrading*.AccountCreate, *.AccountUpdateCashier backend
cashier.transactions-consumertrading*.TransactionCreatedCashier backend
cashier.coins-consumertrading*.PositionClosedCashier backend
cashier.position-status-consumertrading*.PositionStatusCashier backend, one instance at a time
cashier.notificationsnotifications#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:

QueueA rejected message
cashier.positions-consumerGoes to the queue positions.dead-letters
cashier.accounts-consumerIs dropped
cashier.transactions-consumerIs dropped
cashier.coins-consumerGoes to the queue coins.dead-letters
cashier.position-status-consumerIs 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 queueNow
fix-positions.<routing key>.sessions-consumerfix-positions.<routing key>.commands
signals.<routing key>.sessions-consumersignals.<routing key>.commands
signals.<routing key>.trading-consumersignals.<routing key>.trading
history.<routing key>.quotes-consumerhistory.<routing key>.quotes
cashier-positions.anyGroupcashier.positions-consumer
cashier-accounts.anyGroupcashier.accounts-consumer
cashier-transactions.anyGroupcashier.transactions-consumer
coins.allGroupscashier.coins-consumer
cashier-position-status.anyGroupcashier.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​

MetricMeaning
wintrado_fix_positions_amqp_connected0 while fix-positions has no connection to the broker
wintrado_fix_positions_amqp_publish_dropped_totalMessages 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_totalQuotes and status values dropped because the broker did not take messages fast enough
cashier_amqp_connected0 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).

ExpressionMeaning
wintrado_fix_positions_amqp_connected == 0 for 2 minutesThe engine cannot take requests or publish
increase(wintrado_fix_positions_amqp_publish_dropped_total[10m]) > 0Messages 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 minutesTrading events do not reach the broker
increase(wintrado_fix_positions_outbox_published_total[15m]) > 0 while increase(cashier_gateway_projector_events_total[15m]) is 0The 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]) > 0The projection is behind
cashier_gateway_projector_parked_events > 0An event failed and waits for a person
cashier_unverified_movement_oldest_age_seconds > 900A money movement is neither confirmed nor refused
cashier_amqp_connected == 0 for 3 minutesAn instance of the cashier backend is cut off from the broker