You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加密货币交易所无需setInterval实现数据/图表实时更新方案咨询

Better Real-Time Update Alternatives to setInterval for Crypto Exchanges

Hey there! I totally get your frustration with setInterval—constant polling every second is a massive drain on resources, especially when dealing with crypto order books and price charts that don’t need updating every single millisecond. Since your server can’t initiate pushes to users, let’s break down practical, efficient approaches that platforms like Bittrex likely use to keep data fresh without wasting bandwidth or processing power:

1. Long Polling (Persistent Polling)

Instead of spamming requests every second and getting empty/unchanged responses, long polling works by keeping the connection "hanging" until there’s actual new data:

  • Your client sends a request to the server
  • The server holds onto that request until new data is available or a timeout hits
  • Once the server responds (with updates or a timeout), the client immediately sends another request to maintain the connection

This way, you only get updates when something actually changes—no wasted requests during quiet market periods. Here’s a quick frontend example using fetch:

function startLongPoll() {
  fetch('/api/get-active-orders')
    .then(response => response.json())
    .then(data => {
      // Update your order book table or charts here
      refreshOrderBook(data);
      // Immediately start a new long poll
      startLongPoll();
    })
    .catch(error => {
      console.error('Long poll failed:', error);
      // Retry after a short delay if there's an error
      setTimeout(startLongPoll, 3000);
    });
}

// Initialize the long poll when the page loads
startLongPoll();

2. Conditional Requests with ETags

This approach cuts down on bandwidth by only sending full data when it’s actually modified. Here’s how it works:

  • When the server sends a response, it includes an ETag header—a unique string that identifies the current state of the data (e.g., ETag: "order-book-v42").
  • On subsequent requests, the client sends an If-None-Match header with the last received ETag value.
  • The server compares the request’s ETag with the current data’s ETag:
    • If they match (no changes), it returns a 304 Not Modified response with no body—your client skips updating the UI.
    • If they don’t match (new data exists), it sends the full updated data with a fresh ETag.

Most backend frameworks have built-in support for generating ETags, so this is easy to implement without major overhauls.

3. Polling with Exponential Backoff

If long polling isn’t feasible for your server setup, you can make your existing polling smarter with exponential backoff:

  • Start with a short interval (e.g., 1 second) when the market is active (high trading volume).
  • If multiple consecutive requests return no changes, gradually increase the interval (e.g., 2s → 4s → 8s, up to a max like 30s).
  • As soon as new data comes in, reset the interval back to the initial short value.

This drastically reduces unnecessary requests during quiet periods while keeping data responsive when it matters. Example code:

let pollInterval = 1000; // Start with 1 second
const maxInterval = 30000; // Cap at 30 seconds

function pollForMarketUpdates() {
  fetch('/api/get-market-data')
    .then(response => response.json())
    .then(data => {
      if (data.hasUpdates) {
        refreshCharts(data);
        pollInterval = 1000; // Reset interval when new data arrives
      } else {
        // Double the interval (up to max) if no changes
        pollInterval = Math.min(pollInterval * 2, maxInterval);
      }
      // Schedule the next poll
      setTimeout(pollForMarketUpdates, pollInterval);
    });
}

pollForMarketUpdates();

What Bittrex Likely Uses

From observing their real-time behavior, Bittrex almost certainly combines long polling with ETag-based conditional requests. This lets them push updates instantly when data changes, while minimizing redundant data transfer and server load—it’s a perfect middle ground when server-side push (like WebSockets) isn’t feasible or cost-effective.


内容的提问来源于stack exchange,提问作者Marco Vinicius Castellari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:56:41