My auction: Multiple people accessing the same text data file at the same time... Ouch!

From: John Smith (someone_at_microsoft.com)
Date: 11/29/03


Date: Sat, 29 Nov 2003 18:19:10 GMT

I created a program for a local on-line auction used in a charity
fundraiser.
In all, there were approximately 150 items, 700 users (people who
registered), and approximately 2000 bids during the one week that it was
live.

I used a tab-delimited text file for keeping track of bids.
Although the file contains 5 fields: (the item number, bid amount, date of
bid, username of bidder, and the number of bids on the item), let's keep
things simple and assume only 3 fields (the item number, bid amount, and the
username of the bidder).

The file would look something like this:
1 5.25 joe
2 6.15 sue
3 2.50 john
4 3.75 bob
Each field is separated by a tab. The actual file had 150 lines (1 line for
each item).

When someone submitted a bid, the program did 2 things:
a) Read the file and close it
b) Re-write the file with the new information and close it.

It worked great, until the last 10 minutes of the auction when the file got
corrupted. It was fortunate that I also kept a log of every bid so I didn't
lose anything.

What happened of course is that everyone waited until the last minute to
make their bid. There were approximately 500 bids in the last 15 minutes.

At first, I thought that if someone submitted a bid from the website, it
would go to the web server and my perl script would do what it had to and
then terminate. If someone else was bidding at the same time, the server
would have to wait until the perl script had finished the first request, and
then the server would call on the perl script again to do the second
request.

What I have to assume now is that many threads of the perl script could be
running at the same time. What I mean is that although the perl script
hadn't finished its first request, a second thread could be started, etc,
etc,... So basically multiple threads of the perl script could be running
simultaneously, and accessing that text data file at the same time.

I don't know if there's a way to limit the number of threads that the perl
script can run simultaneously (to just one). Or perhaps if it was possible
to set the file properties to disallow multiple access.

So now I'm thinking that perhaps this type of programming (using simple text
files as data files) has many disadvantages compared to using real data
files. Unfortunately, I'm not at that stage of linking Microsoft Data Base
files or whatever data files, I haven't graduated to that level yet.

Does anyone know a simple way to tackle this? Any ideas at all?

Thanks for all
G.Doucet

The following are parts of the routines that read, and re-write the data
file.
# Variables
# $item = number of the item
# $bida = bid amount
# $code = username of bidder
# $bidsfil = 'bids.txt' <--- the actual data file described above
# @bidsfile = content of entire file
# $bidsline = individual line (record) of file
# $bidsfield = individual field of record

if(open(FILE,"$bidsfil")) # Open the file for
reading
  { #
  @bidsfile=<FILE>; # Read the entire file
  close(FILE); # Close the file
  } #

if(open(FILE,">$bidsfil")) # Open the file for
writing
  { #
  $i=0; # set counter to zero
  foreach $bidsline (@bidsfile) # Loop through every line
in the file
    { #
    $i++; # increment counter
    if($i==$item) # check if we're at the
correct item
      { #
      @bidsfield=(); # push the contents of the
line into fields
      push(@bidsfield,split(/\t/,$bidsline)); # get the current bid
      $cbid = $bidsfield[1]; #
                                                  #
      if($bida>$cbid) # if the bid is higher...
        { #
        $bidsline="$i\t$bida\t$code\t\n"; # ...re-create the line
with new bid amount
        } #
                                                  #
      } #
    print FILE $bidsline; # print line to the file
    } #
  close(FILE); # Close the file
  } #



Relevant Pages

  • Re: My auction: Multiple people accessing the same text data file at the same time... Ouch!
    ... >bid, username of bidder, and the number of bids on the item), let's keep ... >username of the bidder). ... Large bidding sites actually use a timed transaction model--they get ... >would have to wait until the perl script had finished the first request, ...
    (comp.lang.perl.misc)
  • Re: Exponential Bidding on Projects
    ... Days to complete job * $$$ wanted per day = Bid amount. ... My half-baked thought is to use non-linear pricing. ... If I can't identify a poorly written specification it's 'cause I can't read the spec, and it's my fault. ...
    (sci.electronics.design)
  • Re: Blatant Invalid Bid Retraction and Ebay Claim Not Enough Evidence
    ... The Ebay rules state: ... You made a typographical error and entered the wrong bid amount. ... correct bid amount immediately after you retract your bid. ...
    (alt.marketing.online.ebay)
  • Re: How Do People Bid On So Many Auctions?
    ... Which software enables you to mass select a bid amount across various ... SNIP snipe service will let you set up a 'Group shot'. ...
    (uk.people.consumers.ebay)
  • Re: E-bay questions
    ... place a "bid" days before then cancel it at will because it only goes to ... are free to cancel it at will. ... You are no longer free to cancel your bids "at will" on eBay. ... You made a typographical error and entered the wrong bid amount. ...
    (rec.collecting.coins)