3

First of all, I must state that this is more of a "strategy" or "algorithm" kind of question, and I'm not sure if it fits the rules here. Please forgive me if this is at the wrong place, and simply let me know where should I ask the question if so. No need to get hostile.

I have also done my research (read questions like Node.js - Monitoring a database for changes or Invoking a PHP script from a MySQL trigger) but couldn't find a really satisfied answer so...

My scenario: I'm working a reserve auction site. The product(s) is set at a price says 100usd, and during the auction period (5min-10min) the price will drop (multiple times) to a RANDOM amount at a RANDOM time. These random amount and time are actually already pre-set at the beginning by the seller.

There will be many buyers/watchers, and we need to update them with real time price when the price drop happens. I have been considering different options: 1. I can use node.js to watch certain files (each product will have a file which simply contains the current price of that product, and we will simply use a cron to update these files?) 2. Or, since we already knew before hand when the price changes will happen, we can have, say a "timer" for each product that will wake up at the specific time and notify all users watching it?

I think there could be some better ways as well, which is why I want to post it here and hopefully someone already did something similar can shed some light on me _

Thank you very much in advance.

Community
  • 1
  • 1
mr1031011
  • 3,574
  • 5
  • 42
  • 59

2 Answers2

1

What kind of database is this ? If it's CouchDB, you can watch for the _changes feed and know exactly when something changed. If those buyers would be in a web browser, it would be easier for them to synchronize with this feed.

If it's another kind of database in which you don't have a changes feed, you should use Pub/Sub and publish to a channel as soon as you update a price. You then listen on another end, and if the buyers are on a web page, you update their views with a WebSocket. Trello's infrastructure is more or less like this (Redis for Pub/Sub, then they hook web sockets in the subscriptions to take the updates to the web page).

I think those are the only ways of doing it in real time. Listening on events. Checking every x ms if something has changed (a.k.a. polling) is not real time.

João Pinto Jerónimo
  • 9,586
  • 15
  • 62
  • 86
  • I'm using mysql so pub/sub with redis sounds like the way to go. I hope you don't mind me asking an extra question: if you know ahead the time you need to update the price in db (i.e at 7:01pm and 9secs, at 7:03 pm and 5secs, at 7:06 pm), which method would you using to schedule this? A PHP scheduler like this one perhaps? https://github.com/chrisboulton/php-resque-scheduler. Or maybe you will ask the nodejs server to do the scheduler itself? (we can update the db either via php or nodejs, both options will work here). PHP can simply let the nodejs knows the time the changes should occur – mr1031011 Aug 19 '12 at 13:24
  • If you are already using MySQL I see no point in deploying another data store (even though they're extremely different), I think you would only need something for pubsub like ZeroMQ... About scheduling, I wouldn't schedule it at all. If you design your application to be event-oriented (and it's the way to go with NodeJS), you don't need to know when the next update will be... Just publish the price update to the channel (and also store it in your database for consistency). – João Pinto Jerónimo Aug 19 '12 at 20:36
  • Thank you, was wondering why I needed Redis which seems to be just another data storage. I asked for a scheduler because even though the updates are random, they are already pre-calculated. A script will have to somehow update the db and also notifies the queue system at the pre-calculated time anyhow. So I was looking at doing this by either php or node.js. Or perhaps there is another way which I don't know of... – mr1031011 Aug 20 '12 at 00:25
  • All you have to do is each time you update the db, you `publish` something to a channel. Let's suppose theres 10 people interested in a pair of shoes. When your script notices that the price of shoes is higher (new bid ?), it does two things: saves the new price to the database, and publishes the new price to the channel `shoes`. Those 10 people should be subscribed to the channel `shoes` if they were interested in seeing the price changes, so they will get the new price right away. IMO you have no gain in having a queue system on this. – João Pinto Jerónimo Aug 20 '12 at 01:47
0

The easiest way would be to fetch those dates in advance and sending them to the client on page load, then set up the timers on client. If you are strict about security, you can send only the times, and let the client fetch the prices only when it is about to display them. If neither is the option, you could poll the server in regular intervals or, if you can push data to the clients, I would go with the timer on server approach.

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • We do not want the users to know of the time and amount of the price drop, which is why we opt for node.js option to be able to push data to them. A timer on the server side looks like the best option for now, though I will need to think of the correct implementation for it. – mr1031011 Aug 19 '12 at 08:12
  • Only the users that know how to debug (the right) javascript memory or (in the case of unencrypted data) read (and search in) the source code will get to that data. That might be a risk depending on the user base. – John Dvorak Aug 19 '12 at 08:35