Hi Kiat,

I think for small applications (few users, small data set) flat files will be an easier, faster and more efficient choice.

It's when you have a lot of people connected to a large amount of data that a database like MySQL will scale well and shine. mod_perl even allows for persistent, reusable database connections which further increase performance. I'm doing a lot of research into this area now and converting a flat file system to a MySQL database. My goal is to try to handle thousands of people at the same time (with a low amount of data, though).

A flat file (large or small) is prone to corruption when two people try to write to it at the exact same time. This is where flock comes in. Simultaneous read access of the same file is not a problem, but consider this scenario:

I open a file. I read a number from the file. I increment the number. I close the file. I open the file for writing with a lock. I write my number. I close the file.

You read the same number at nearly the same time. You increment the number. You go to open the file to write the new number... it's in use by me. You wait. You then write your number.

In the end, the number was only incremented once. You can see how this example would fail as a counter.

I think the only way around this is to open and lock the file for reading and writing simultaneously, which can be tricky...

In a database this is handled automatically.

In my understanding, flock works on a system level and acts as a traffic signal for files. For example, if I'm currently writing to a file and you want to update it too, you must wait until I'm finished. Your process gets stalled until I release the lock, then you go. Normally we are talking about milliseconds, so performance is not a problem.

Closing a file automatically unlocks it, so you dont have to. Also, flock is UNIX based, and I don't think Windows supports it.

Good luck - I hope this helps. Any other questions, just ask!

:DEV2000

In reply to Re: Which is the better option? by dev2000
in thread Which is the better option? by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.