The Risk Service is responsible for pre-trade validation, real-time exposure monitoring, margin risk management, AML/FIU compliance, and liquidation trigger coordination. It acts as the gatekeeper that ensures all trading activity stays within defined risk parameters.
Pre-Trade Checks : Validate leverage, margin requirements, and exposure limits before orders are accepted.
Exposure Monitoring : Real-time tracking of per-account and per-symbol exposure in USDT terms.
Margin Risk : Monitor margin ratios and emit margin call / liquidation events.
AML / FIU Reporting : Transaction monitoring, suspicious activity detection, and FIU-IND reporting.
Policy Enforcement : Enforce position limits, price bands, and rate limits per risk profile.
Hold Management : Request balance holds via Wallet Service for flagged accounts.
The Risk Service is currently implemented as a gRPC interface consumed by the Order Service, with the actual service implementation planned as a dedicated microservice.
graph TB
OrderSvc[Order Service] -->|gRPC PreTradeCheck| Risk[Risk Service]
Kafka[Kafka] -->|order/wallet/position events| Risk
Risk -->|gRPC| Wallet[Wallet Service]
Risk -->|Events| Kafka2[Kafka]
Risk -->|Stores| PG[(PostgreSQL)]
Kafka2 --> Settlement[Settlement Service]
Kafka2 --> AdminPanel[Admin Panel]
Called by the Order Service before accepting any order:
rpc PreTradeCheck(PreTradeRequest) returns (PreTradeResponse);
message PreTradeRequest {
message PreTradeResponse {
string reject_reason = 2 ;
string available_margin = 3 ;
string required_margin = 4 ;
string current_exposure = 5 ;
The PreTradeCheck validates:
Account Status : User is not frozen, suspended, or under AML hold
Leverage Limits : requested_leverage <= max_leverage_for_symbol
Margin Sufficiency : available_margin >= required_initial_margin
Position Limits : current_position + order_qty <= max_position_for_symbol
Exposure Limits : total_exposure + order_notional <= max_total_exposure
Price Bands : Order price within allowed deviation from mark price (for limit orders)
Rate Limits : Account hasn’t exceeded order submission rate
Field Type Description idUUID (PK) Profile ID user_idUUID Account owner max_leverageINT Maximum allowed leverage max_position_usdtNUMERIC(20,4) Maximum position per symbol max_total_exposure_usdtNUMERIC(20,4) Maximum total exposure max_order_rate_per_secINT Maximum orders per second aml_risk_scoreNUMERIC(5,2) AML risk score (0-100) kyc_tierENUM KYC verification tier updated_atTIMESTAMPTZ Last update
Field Type Description idUUID (PK) Snapshot ID user_idUUID Account owner symbolVARCHAR(32) Instrument symbol long_exposure_usdtNUMERIC(20,4) Long exposure value short_exposure_usdtNUMERIC(20,4) Short exposure value net_exposure_usdtNUMERIC(20,4) Net exposure margin_usedNUMERIC(20,4) Margin allocated margin_ratioNUMERIC(10,6) Current margin ratio captured_atTIMESTAMPTZ Snapshot timestamp
Field Type Description idUUID (PK) Record ID user_idUUID Account owner transaction_typeENUM deposit, withdrawal, trade, transferamountNUMERIC(20,4) Transaction amount currencyVARCHAR(10) Currency risk_scoreNUMERIC(5,2) Computed risk score flaggedBOOLEAN Whether transaction was flagged flag_reasonTEXT Reason for flagging reported_to_fiuBOOLEAN Whether reported to FIU-IND created_atTIMESTAMPTZ Transaction time
Topic Description order.command.v1Order submissions (for rate tracking) engine.event.v1Trade executions (for exposure updates) wallet.balance_changed.v1Balance changes (for margin monitoring) position.update.v1Position changes (for exposure tracking) user.created.v1New users (create risk profile) user.kyc.updated.v1KYC changes (update risk limits)
Topic Description risk.violation.v1Risk limit violations detected risk.margin_call.v1Margin call triggered risk.liquidation.v1Liquidation trigger risk.aml.flagged.v1AML suspicious activity flagged risk.freeze_account.v1Account freeze requested risk.audit.events.v1Risk audit events
Order Service calls PreTradeCheck(user_id, symbol, side, qty, price, leverage)
Load risk_profiles for user
Load current exposure_snapshots for user
Validate all rules (leverage, margin, exposure, position limits, price bands, rate)
If all pass: return approved = true with margin details
If any fail: return approved = false with reject_reason
Target latency : < 50ms (p95)
Consume position.update.v1 events
Recompute per-symbol and total exposure for the user
Update exposure_snapshots
If margin ratio exceeds warning threshold: emit risk.margin_call.v1
If margin ratio exceeds liquidation threshold: emit risk.liquidation.v1
Consume wallet.balance_changed.v1 for deposits and withdrawals
Apply rule-based scoring:
Large transaction threshold (INR 10 lakh+)
Velocity: > N transactions in M hours
Pattern matching: structuring, round-tripping
If score exceeds threshold: flag transaction, emit risk.aml.flagged.v1
Compliance team reviews flagged transactions
If confirmed suspicious: report to FIU-IND, freeze account
Method Endpoint Description POST/v1/risk/pretrade-checkManual pre-trade check (admin) POST/v1/risk/holdPlace risk hold on account GET/v1/risk/exposure/{user_id}Get user exposure snapshot GET/v1/risk/aml/flaggedList flagged transactions PUT/v1/risk/rules/{symbol}Update risk rules for symbol
Metric Target PreTradeCheck latency (p95) < 50 ms Exposure update latency (p95) < 500 ms Liquidation trigger accuracy >= 99.99% AML false positive rate < 5% Margin call delivery < 1 second