简体   繁体   中英

What is expected to be faster when collecting data from multiple apis: websockets or http requests?

I have to collect real timer ticker data on trading pairs (usd/eur etc) from APIs of different sites. The data is usually a small JSON object with mostly ~10 numbers. The naive strategy is to make a request every 5 or so seconds to get the up-to-date ticker data of each of those sites. Some of them, though, provide a websocket option, which allows them to notify me directly when a change occurs, and, I believe, is more efficient. The issue is some of those sites don't offer that option, so the overall code will be simpler to organize and read if I use the same method for all sites (ie, http requests). I'm also not sure the data is heavyweight enough to justify that choice.

From the experts who dealt with similar situations, is this case one where a relevant performance improvement can be expected from using sockets instead of timed http requests when it is available?

It depends:

  • WebSockets make only sense if you keep them open most of the time. If you instead open a new WebSocket connection each time you want to have new data the overhead is larger compared to a simple HTTP request. It is not so much the bandwidth (but this too) but you need more round trips to get your data which makes everything slower.
  • WebSockets take more resources at your end because you have to keep a TCP connection open for each open WebSocket connection. If there are only a small number of sites you need to ask it does not matter, if there are a lot it will matter. While it can be an advantage (less latency) to keep normal HTTP connections alive too you can close them if you have less resources.
  • If most of the time the data you get is the same then WebSockets might be more efficient because you only get send the new data when it actually changes.
  • If you want to be informed of new data as soon as possible then WebSockets perform better. If you only need a 5 second precision anyway it does not matter much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM