Skip to content

Communication Patterns

This document describes the communication patterns used between services in the TradeX platform.

REST APIs are used for:

  • External-facing APIs: Client-facing endpoints
  • Admin APIs: Administrative operations
  • Service-to-service: When real-time performance is not critical

Characteristics:

  • HTTP/HTTPS protocol
  • JSON payloads
  • Stateless requests
  • Standard HTTP methods (GET, POST, PUT, DELETE)

Example:

// Order Service REST API
POST /v1/orders
Content-Type: application/json
{
"instrumentId": "...",
"side": "buy",
"type": "limit",
"quantity": 10.5,
"price": 50000.0
}

gRPC is used for:

  • Internal service communication: High-performance internal APIs
  • Real-time data access: When low latency is required
  • Streaming data: Server-side streaming for real-time updates

Characteristics:

  • HTTP/2 protocol
  • Protocol Buffers (protobuf) serialization
  • Type-safe service definitions
  • Bidirectional streaming support

Example:

// Wallet Service gRPC
service WalletService {
rpc GetBalance(GetBalanceRequest) returns (Balance);
rpc HoldBalance(HoldBalanceRequest) returns (HoldResponse);
}

Kafka is used for:

  • Event-driven architecture: Asynchronous event processing
  • Event sourcing: Maintaining event history
  • Service decoupling: Loose coupling between services

Characteristics:

  • Asynchronous messaging
  • Event ordering per partition
  • At-least-once delivery guarantees
  • Schema registry for type safety

Example Topics:

  • engine.event.v1 - Matching engine events
  • md.instrument.created.v1 - Instrument creation events
  • wallet.balance.changed.v1 - Balance change events

WebSocket is used for:

  • Real-time data streaming: Live market data updates
  • Client push notifications: Real-time notifications to clients

Characteristics:

  • Full-duplex communication
  • Low latency
  • Persistent connections
  • Sub-protocol support

Example:

// WebSocket subscription
{
"op": "subscribe",
"channels": [
{
"name": "book",
"symbols": ["BTCUSDT-PERP"]
}
]
}

Used for synchronous operations where immediate response is required.

Examples:

  • REST API calls
  • gRPC service calls
  • Database queries

Flow:

Client → Service → Response

Used for asynchronous operations and service decoupling.

Examples:

  • Order events
  • Trade events
  • Configuration changes

Flow:

Service A → Kafka → Service B (async)

Used for broadcasting events to multiple consumers.

Examples:

  • Market data updates
  • Configuration changes
  • System notifications

Flow:

Publisher → Kafka Topic → Multiple Subscribers

Used for streaming data from server to client.

Examples:

  • Real-time market data
  • Configuration watch streams
  • Order book updates

Flow:

Client → Service → Stream (continuous)
ServiceMethodTargetPurpose
Order → WalletgRPCWallet ServiceBalance checks, holds
Order → MetadatagRPCMetadata ServiceInstrument validation
Order → Matching EngineKafkaMatching EngineOrder submission
Matching Engine → Market DataKafkaMarket Data ServiceTrade events
Market Data → ClientsWebSocketClientsReal-time updates
Metadata → ServicesKafkaAll ServicesConfiguration changes
Auth → UserKafkaUser ServiceUser events
Wallet → ServicesKafkaAll ServicesBalance changes
  • External-facing APIs
  • CRUD operations
  • When HTTP caching is beneficial
  • When tooling/ecosystem support is important
  • Internal service communication
  • When low latency is critical
  • When type safety is important
  • When streaming is needed
  • Event-driven workflows
  • Service decoupling
  • Event sourcing
  • When eventual consistency is acceptable
  • Real-time data streaming
  • Push notifications
  • When low latency is critical
  • When persistent connections are beneficial
  • Standard HTTP status codes
  • Error response bodies with details
  • Retry logic in clients
  • gRPC status codes
  • Error details in response
  • Retry with exponential backoff
  • Dead letter queues for failed messages
  • Retry mechanisms in consumers
  • Idempotent message processing
  • Error messages in WebSocket frames
  • Connection retry logic
  • Graceful degradation
  • JWT tokens for REST APIs
  • mTLS for gRPC (optional)
  • API keys for service-to-service
  • WebSocket authentication via gateway
  • Role-based access control (RBAC)
  • Service-level permissions
  • Resource-level permissions
  • TLS for all external communication
  • mTLS for internal gRPC (optional)
  • Encrypted Kafka messages (optional)
  • Request latency
  • Error rates
  • Throughput
  • Connection counts
  • Distributed tracing across services
  • Request correlation IDs
  • Span tracking
  • Structured logging
  • Request/response logging
  • Error logging
  • Event logging