0

I have websocket price data streaming in to my rails api app which I want to keep updated so any api requests get an updated response. It would be too expensive to save each update to the database. How can I do this? In Ember I can modify the model and it persists. It doesn't seem to happen in rails.

Channel controller:

def receive(message)
  #ActionCable.server.broadcast('channel', message)

  platform = Platform.find(params[:id]);
  market = platform.markets.find_by market_name: message["market_name"]
  market.attributes = {
    market.price = message.values["price"],
    etc......
  }
  #market.save [this is too expensive every time]
end

Am I going about this in the right way? It also seems inefficient to use find every time I want to update which could be multiple times per second. In Ember I created a record Id lookup array so I could quickly match the market_name, I don't see how to do this in rails.

Joshua Jenkins
  • 315
  • 1
  • 15

1 Answers1

0

Persistence to some store is the only way you can have other threads respond with latest value.

Instead of 3 queries( 2 selects and 1 update) you can do it with just 1 update

Market.where(platform_id: params[:id], market_name: message["market_name"]).
       update_all(price: message.values["price"])

With proper index, you might have a sub-ms performance for each update.

Depending on your business need:

If you are getting tons of updates for a market every second(making all prior stale and useless), you can choose to ignore few and not fire update at all.

Vijay Agrawal
  • 1,643
  • 12
  • 17
  • Thanks for the reduced query statement. If there was another couple of platforms added then things could easily get over 1000 updates per second. I want to be able to use this data with a background jobs queue to do things like alert the Ember frontend on price levels reached or more complicated tasks. Saving updates to the database isn't going to work. Perhaps I should be using Redis and save to database less frequently. – Joshua Jenkins Oct 27 '17 at 02:39
  • `1000 per second` is way below what most DBs can handle. – Vijay Agrawal Oct 27 '17 at 03:01
  • If you go down the Redis path, probably you can think of not saving in SQL DB at all. – Vijay Agrawal Oct 27 '17 at 03:02