Designing Data Structures and Real-Time Market Data Systems for Cryptocurrency Exchanges

    This article explores the engineering foundations behind cryptocurrency market data, covering data modeling, precision handling, indicator calculation methodologies, and the architecture of real-time quote push systems. It provides practical insights into building accurate, low-latency market data pipelines for digital asset trading platforms.

    Starting with a Single Market Data Record

    In digital asset trading, market data is the most fundamental and critical information carrier. Taking a trading pair such as GLM/USDT as an example, a complete market data record typically includes the following fields: name, code, latest price, quote currency, 24-hour price change and percentage change, open price, high price, low price, market capitalization and rank, along with the data update timestamp. For instance, at a given moment, the latest price might be $0.09099, with a 24-hour increase of $0.002297 (approximately +2.59%), a daily high of $0.09369 and a low of $0.0876, a market cap of approximately $91.62 million, and a rank of 166th.

    While these fields appear straightforward, accurately computing and distributing them in real time within a production environment involves a comprehensive technical stack spanning data ingestion, cleaning, aggregation, and push delivery. This article analyzes the topic from three perspectives: data modeling, calculation methodologies, and system architecture.

    Market Data Model Design

    Field Layering

    Market data fields can be categorized into three layers based on their purpose:

    • Identifier Layer: Currency code, trading pair name, market type (e.g., CRYPTO), and quote currency. This layer uniquely identifies a trading instrument and serves as the key for all downstream calculations.
    • Price Layer: Latest price, open price, high price, and low price. These fields originate from matching engine trade records and must be aggregated over time windows.
    • Statistics Layer: Price change, percentage change, market capitalization, and market cap rank. This layer contains derived data that depends on the underlying price data and the circulating supply of the asset.

    Precision and Data Type Selection

    Cryptocurrency prices exhibit extreme variation in precision—some assets trade at tens of thousands of dollars, while others have prices with eight or more decimal places. Using floating-point types can introduce precision loss and rounding errors. A common industry practice is to use fixed-point arithmetic (e.g., storing prices as integers with a defined minimum unit, similar to the concept of satoshis), or to use arbitrary-precision Decimal types in languages that support them. For example:

    1. price_raw BIGINT -- stored as integer with a minimum unit of 1e-8
    2. price_scale INT = 8
    3. display_price = price_raw / 10^price_scale

    This approach ensures that secondary calculations such as percentage change do not produce display anomalies due to floating-point errors.

    Calculation Methodology for Key Indicators

    24-Hour Percentage Change

    The industry-standard calculation for the 24-hour change uses a “rolling 24-hour” window: comparing the current price against the price 24 hours ago (or at UTC midnight). The common formulas are:

    1. Price Change = Latest Price - Price 24 Hours Ago
    2. Percentage Change = (Price Change / Price 24 Hours Ago) × 100%

    Using the earlier data to verify: 0.002297 / 0.0887 ≈ 2.59%, confirming that the open price (which is the price 24 hours ago) serves as the baseline. Development teams must explicitly align on this calculation standard with product stakeholders, otherwise the frontend display may diverge from industry conventions.

    High and Low Prices

    It is essential to clearly define the statistical window (whether rolling 24-hour or the current calendar day) and to apply deduplication and count-limited aggregation over the sliding window. In high-concurrency scenarios, Redis sorted sets or time-series databases are commonly used to maintain windowed extremes.

    Market Capitalization and Rank

    Market Capitalization = Circulating Supply × Latest Price. Note that circulating supply is itself external data that may change due to events such as token lockups, releases, or burns, and therefore requires an independent data source and update frequency. Market cap ranking is derived by sorting all assets in the market by their market capitalization. This is typically computed by a scheduled task running every minute or every few minutes, rather than on a per-trade basis, to reduce computational load.

    Real-Time Quote Push System Architecture

    Data Pipeline

    A typical pipeline is: Exchange matching engine or market data source → Ingestion Gateway → Message Queue → Aggregation Computation Service → Cache Layer → Push Service → Client. The message queue provides burst absorption and decoupling; the aggregation service generates candlesticks and statistical indicators; the cache layer (such as in-memory or distributed cache) stores the latest snapshots to ensure low-latency read interfaces.

    Push Delivery Methods

    • WebSocket Push: Suitable for latency-sensitive scenarios. Clients subscribe to their trading pairs of interest, and the server pushes incremental or throttled updates.
    • REST Polling: Simple to implement and appropriate for list pages with lower real-time requirements, though caching and rate limiting must be carefully managed.
    • Hybrid Mode: Polling with caching for list pages, WebSocket for detail pages—balancing cost and user experience.

    The Significance of Update Timestamps

    Market data must carry an update timestamp (as shown in the example record). Clients can use this to assess data freshness and trigger fallback strategies when stale (e.g., displaying “data delayed” or switching to a backup data source).

    Reliability and Consistency Considerations

    1. Idempotency and Out-of-Order Handling: Market data sources may produce duplicate or out-of-order messages. Consumers should deduplicate and correct using sequence numbers or timestamps.
    2. Reconnection and Snapshot Synchronization: When a push channel disconnects, clients should first fetch a full snapshot upon reconnection, then resume incremental updates, to avoid state inconsistency.
    3. Monitoring and Alerting: Alerts for market data interruptions, abnormal price jumps (e.g., deviation beyond a percentage threshold), and latency exceedance are fundamental safeguards for trading systems.
    4. Display Layer Fallbacks: For extremely small prices, use scientific notation or significant-digit truncation to prevent frontend precision display issues.

    Conclusion

    A seemingly simple market data record is backed by a complete engineering system encompassing field modeling, precision control, indicator calculation standards, real-time pipelines, and reliability design. When building market data systems, developers should prioritize aligning on calculation methodologies, selecting appropriate numeric types, designing robust push and fallback strategies, and ensuring continuous data pipeline availability through monitoring. Mastering these fundamentals enables the delivery of accurate, timely, and trustworthy market data services to users.