Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 465325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:21:53+00:00 2026-05-12T23:21:53+00:00

i have a LAPP (linux, apache, postgresql and php) environment, but the question is

  • 0

i have a LAPP (linux, apache, postgresql and php) environment, but the question is pretty the same both on Postgres or Mysql.

I have an cms app i developed, that handle clients, documents (estimates, invoices, etc..) and other data, structured in 1 postgres DB with many schemas (one for each our customer using the app); let’s assume around 200 schemas, each of them used concurrently by 15 people (avg).

EDIT: I do have an timestamp field named last_update on every table, and a trigger that update the timestamp every time the row is update.

The situation is:

  1. People Foo and Bar are editing the document 0001, using a form with every document details.
  2. Foo change the shipment details, for example.
  3. Bar change the phone numbers, and some items in the document.
  4. Foo press the ‘Save’ button, the app update the db.
  5. Bar press the ‘Save’ button after bar, resending the form with the old shipment details.
  6. In the database, the Foo changes have been lost.

The situation i want to have:

  1. People Foo, Bar, John, Mary, Paoul are editing the document 0001, using a form with every document details.
  2. Foo change the shipment details, for example.
  3. Bar and the others change something else.
  4. Foo press the ‘Save’ button, the app update the db.
  5. Bar and the others get an alert ‘Warning! this document has been changet by someone else. Click here to load the actuals data’.

I’ve wondered to use ajax to do this; simply using an hidden field with the id of the document and the last-updated timestamp, every 5 seconds check if the last-updated time is the same and do nothing, else, show the alert dialog box.

So, the page check-last-update.php should look something like:

<?php
//[connect to db, postgres or mysql]
$documentId = isset($_POST['document-id']) ? $_POST['document-id'] : 0;
$lastUpdateTime = isset($_POST['last-update-time']) ? $_POST['last-update-time'] : 0;
//in the real life i sanitize the data and use prepared statements;
$qr = pg_query("
    SELECT
        last_update_time
    FROM
        documents
    WHERE
        id = '$documentId'
");
$ray = pg_fetch_assoc($qr);
if($ray['last_update_time'] > $lastUpdateTime){
    //someone else updated the document since i opened it!
    echo 'reload';
}else{
    echo 'ok';
}
?>

But i dont like to stress the db every 5 seconds for every user that have one (or more…) documents opened.

So, what can be another efficent solution without nuking the db?

I thought to use files, creating for example an empty txt file for each document, and everytime the document is updated, i ‘touch’ the file updating the ‘last modified time’ as well… but i guess that this would be slower than db and give problems when i have much users editing the same document.

If someone else have a better idea or any suggestion, please describe it in details!

* – – – – – UPDATE – – – – – *

I definitely choosen to NOT hit the db for check the ‘last update timestamp’, dont mind if the query will be pretty fast, the (main) database server has other tasks to fullfill, dont like the idea to increase his overload for that thing.

So, im taking this way:

  1. Every time a document is updated by someone, i must do something to sign the new timestamp outside the db environment, e.g. without asking the db. My ideas are:
    1. File-system: for each document i create an empry txt files named as the id of the document, everytime the document is update, i ‘touch’ the file. Im expecting to have thousands of those empty files.
    2. APC, php cache: this will be probably a more flexible way than the first one, but im wondering if keeping thousands and thousands of data permanently in the apc wont slow down the php execution itself, or consume the server memory. Im little bit afraid to choose this way.
    3. Another db, sqlite or mysql (that are faster and lighter with simple db structures) used to store just the documents ID and timestamps.
  2. Whatever way i choose (files, apc, sub-db) im seriously thinking to use another web-server (lighttp?) on a sub-domain, to handle all those.. long-polling requests.

YET ANOTHER EDIT:

The file’s way wouldnt work.

APC can be the solution.

Hitting the DB can be the solution too, creating a table just to handle the timestamps (with only two column, document_id and last_update_timestamp) that need to be as fast and light as possible.

Long polling: that’s the way i’ll choose, using lighttpd under apache to load static files (images, css, js, etc..), and just for this type of long-polling; This will lighten the apache2 load, specially for the polling.

Apache will proxy-up all those request to lighttpd.

Now, i only have to decide between db solution and APC solution..

p.s: thanks to all whom already answered me, you have been really usefull!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:21:54+00:00Added an answer on May 12, 2026 at 11:21 pm

    I agree that I probably wouldn’t hit the database for this. I suppose I would use APC cache (or some other in-memory cache) to maintain this information. What you are describing is clearly optimistic locking at the detailed record level. The higher the level in the database structure the less you need to deal with. It sounds like you want to check with multiple tables within a structure.

    I would maintain a cache (in APC) of the IDs and the timestamps of the last updated time keyed by the table name. So for example I might have an array of table names where each entry is keyed by ID and the actual value is the last updated timestamp. There are probably many ways to set this up with arrays or other structures but you get the idea. I would probably add a timeout to the cache so that entries in the cache are removed after a certain period of time – i.e., I wouldn’t want the cache to grow and assume that 1 day old entries aren’t useful anymore).

    With this architecture you would need to do the following (in addition to setting up APC):

    • on any update to any (applicable) table, update the APC cache entry with the new timestamp.

    • within ajax just go as far “back” as php (to obtain the APC cache to check the entry) rather than all of the way “back” to the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Set the TextBox.ReadOnly property to true and then set the… May 14, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer Actually, @Mark's approach will work too. I just figured out… May 14, 2026 at 8:43 pm
  • Editorial Team
    Editorial Team added an answer You are not able to filter on a collection in… May 14, 2026 at 8:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.