views:

292

answers:

3

Hello,

I have an auction website which let my users place an unlimited amount of autobiddings.

To monitor these autobiddings something has to check the database every second.

My question is if it is better to use mysql trigger events or to user a cronjob every minute that executes a 60 sec looping php script.

If i use the mysql trigger events there will be hundreds of events stacks on eachother, and fired on different times. Is this even possible?? ANd isn't the server load goning to be enourmous. I heard somewhere that the database will be locked while there is a schedueled event. I am using innoDB tables btw.

I hope some one can shed some light on this toppic.

Regards!

A: 

You'd best run a seperate script that runs eternally and whatches your database. That way you won't need cron. Nor a massive amount of triggers.

But you might want to reconsider your entire question. It's not necessary to actually update the bids every second. You only need to fill in the past x minutes/hours when someone actually points his browser to an auction or makes a manual bid. If it's all autobids you can calculate forwards an backwards with ease.

Jauco
Hi, thank you for the reaction. This could be a solution to the problem. But i want to be able to send the user an email the second they have won the action. And if lets see 3 people enable their auto bids, and they leave the website, all the bids have to be done at random during the time there are bids left. When the bids are done, the email has to be send. I really want the whole process done without someone having to be on the website.
Saif Bechan
Then the first part of my answer, i.e. a seperate script that runs eternally (with a while(true) loop and a sleep 1 sec.) would be the best option
Jauco
A: 

The database handles scheduled requests no different from other request. But as many scheduled requests contain database and table maintenance operations that do lock the database it is not uncommon for them to do so.

Having said that: as your systems has to react to actions by a user the technical preferable way of doing this is using triggers. In practice this might lead to performance problems when your site does have high loads - though using a scheduled event might cause the same trouble.

My advise is to put your logic in stored procedures and call these stored procedures from the triggers. When you find that the triggers don't keep up you can always remove the triggers and call the stored procedures from a cron job.

Matijs
Hi, thank you for the answer. I have a follow up question on this. This could seem like a beginner question, but what are triggers exactly, mysql triggers. And what are the stored procedures. I assume the stored procedures are the php scripts i wrote that update the db. If i make a php script that loops forever until eterniry will this consume a lot of cpu power?
Saif Bechan
Triggers are pieces of SQL code that are automatically executed when data in a table changes. Stored procedures are collections of SQL commands bundled together. Just check the documentation on the MySQL site.
Matijs
A: 

I would probably model the solution to your auto-bidding problem a bit differently:

How about an event-based approach? You store the auto-bid requests of your users and if someone actually bids on an object you process the previously queued auto-bids.

This has the following benefits:

  • load on the database is distributed organically
  • you only do lookups that are actually needed at the time.
  • it is real-time and not tick-based
  • it is easier to reason about the business/application logic because it is local instead of global
tosh
Hi, thank you for the answer, but i dont think i can use this model. Let's say 3 people have enabled their autobids, and leave the website. Now there is nobody on the website, or busy on the website. All the bids have to be done before the auction can end.
Saif Bechan
From my understanding it would still work because if nobody visits the websites, no one can bid, so the auto-bids would not have to be processed, right?Of course if you want to send out notifications (email, sms, …) when an auction ends then you need to observe the system. Though I guess it would be good enough to use a cron which runs once every 5 minutes and checks all auctions that have ended. Would this work for you or are there additional requirements for how the auctions are supposed to work?
tosh
Well people pay to place a bid, so it is important that all the autobids are placed.
Saif Bechan