Sequence ID validation race condition when requesting snapshot via REST #255
Replies: 1 comment
-
Originally posted by @bmancini55 in #252 (comment) @evan-coygo, thanks for submitting the issue! Yeah, this is sort of a known issue that's shows its ugly head when you try to build certain books. To rehash, the issue is that the socket stream starts after the From a code perspective, to avoid this issue, you need to perform the snapshot after the subscription has been confirmed and you've queued a sufficient number of messages (intentionally ambiguous as the sufficient depth is likely exchange and latency specific). In the past I've recommend monkey patching a timestamp delay over const REST_DELAY_MS = 500
client._originalRequestLevel2Snapshot = client._requestLevel2Snapshot;
client._requestLevel2Snapshot = market => setTimeout(() => client._originalRequestLevel2Snapshot(market), REST_DELAY_MS); However, this solution isn't foolproof and may introduce some funkiness on reconnections. Ideally (as usually), changes after the refactor (#149) which will include subscription success/failure (#103) combined with firing the snapshot request after a delay after subscription success would be best. As a fallback though, I think you need to be prepared to call |
Beta Was this translation helpful? Give feedback.
-
See original discussion here: #252
For exchanges that request the l2 snapshot over REST there is a pretty common race condition issue that can lead to missing order book data.
The problem w/example
I'll use Bittrex as an example since the issue happens often for me with Bittrex. The culprit code looks like this:
The issue is as follows:
subscribeLevel2Updates()
this._requestLevel2Snapshot(market)
"Subscribe"
event is sent to subscribe to l2 updates.l2snapshot
event withsequenceId
of "100".sequenceId
of "105".The snapshot has
sequenceId
of "100" while the first update has asequenceId
of "105". This means you missed four updates and your order book will never be valid.Solutions
sequenceId
before the snapshot'ssequenceId
, but this issue already exists w/snapshots over REST in the current implementation. I imagine something like this below (not run or tested):client._requestLevel2Snapshot(market)
again to get the newer snapshot, then ignore the updates that arrive before the newer snapshot'ssequenceId
. This will work and is what I'm doing right now, but I'm wondering if there's a better way to handle this in a generic way.Beta Was this translation helpful? Give feedback.
All reactions