cryptofeed icon indicating copy to clipboard operation
cryptofeed copied to clipboard

Bulk/batch insert in PostgreSQL by one query and AWS vs Vultr perfomance

Open cryptorevizor opened this issue 6 years ago • 4 comments

Hello. Firstly, I would like thanks all contributors for this greatest tool for working with websocket.

My question: Is it possible a bulk/batch insert in PostgreSQL by one query?

Because, we getting time to time something like this, bulk list with trades at the same timestamp:

ID SYMBOL Timestamp Received timestamp Side Amount Price
161878 "BTC-USDT" "2020-10-15 10:20:14.903" "2020-10-15 10:20:14.91396" "sell" 0.366 11316.5
161877 "BTC-USDT" "2020-10-15 10:20:14.903" "2020-10-15 10:20:14.912996" "sell" 0.373 11316.51
161876 "BTC-USDT" "2020-10-15 10:20:14.903" "2020-10-15 10:20:14.912119" "sell" 0.366 11316.71
161875 "BTC-USDT" "2020-10-15 10:20:14.903" "2020-10-15 10:20:14.911127" "sell" 0.183 11316.75
161874 "BTC-USDT" "2020-10-15 10:20:14.903" "2020-10-15 10:20:14.909969" "sell" 0.002 11316.86

Please, look how increasing the received timestamp value. Each writing operation takes 1 ms. And due that, if these trades(at the same ts) will be more than X, we can get latency X*delay for writing in postgres (in my case 1 ms).

And I got situation yesterday, when this lag was more than 1 minute.

I research some cause, that when we have strong move (pair BTC-USDT, 1 minute ohlcv, more than 3k BTC volume) there is some latency, and depend on the server type, cores, DB location there are different values. So, look please on this data:

One good move that was at 12:45 UTC 15.10.2020

Screenshot_2

I have 2 VPS.

Amazon EC2 free tier stage. Second is vultr. These server are in Tokyo.

For amazon, I use EC2 instance for run cryptofeed and store it ro RDS instanse on the same zone that EC2. Delay between EC2 and RDS aprox. 0.1ms.

For vultr I use scheme, where I have postgresql DB on the same instance. So the latency between aprox. is == 0.

AWS VULTR
1 CPU 4 CPU
1 GB RAM 8 GB RAM
Network SSD SSD
PostgreSQL on RDS PostgreSQL on the same VPS
Latency to postgresql< 0.1 ms Latency to postgresql =0
Latency to binance = 10ms Latency to binance = 10ms

SQL query:

SELECT rtimestamp-timestamp as difference FROM public."TRADES-BTC-USDT" WHERE timestamp > timestamp '2020-10-15 09:44:00' and timestamp < timestamp '2020-10-15 09:47:00'
ORDER BY difference DESC

Got 5360 records.

Metrics AWS VULTR Perf. AWS/VULTR
Total records 5360 5360 The same
Max lag 1090ms or 1.09s 453 ms 2.42x
Min lag 1ms 2 ms 0.5x
AVG lag 153ms 40 ms 3.8x

Screenshot_4

And the second situation 2020-10-15 09:55:00 to 2020-10-15 10:03:00:

Screenshot_5

SQL:

SELECT rtimestamp-timestamp as difference FROM public."TRADES-BTC-USDT" WHERE timestamp > timestamp '2020-10-15 09:55:00' and timestamp < timestamp '2020-10-15 10:03:00'
ORDER BY difference DESC
Metrics AWS VULTR Perf. AWS/VULTR
Total records 19076 19076 The same
Max lag 1914ms or 1.59s 39 ms 49x
Min lag 3ms 0 ms 0.1x
AVG lag 8ms 35 ms 4.3x

Screenshot_6

cryptorevizor avatar Oct 15 '20 11:10 cryptorevizor

Any answers?

cryptorevizor avatar Nov 01 '20 22:11 cryptorevizor

yes - its possible, but you'd need to add in some state/storage mechanism in the callback to handle the caching/batching, or you'd need to rely on a 2nd process to receive and handle the updates (or handle writing from a cache, like redis)

bmoscon avatar Nov 28 '20 14:11 bmoscon

@cryptorevizor also take a look at this PR - https://github.com/bmoscon/cryptofeed/pull/339 it will likely fix the issues you are seeing

bmoscon avatar Nov 28 '20 19:11 bmoscon

@cryptorevizor let me know if you have any questions, comments, etc. If I don't hear back I'll consider this resolved and close it

bmoscon avatar Dec 06 '20 22:12 bmoscon