Best way to handle concurrency issues

后端 未结 11 1198
迷失自我
迷失自我 2021-02-07 23:27

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

11条回答
  •  星月不相逢
    2021-02-07 23:41

    Ahhh, i though it was easyer.

    So, lets make the point: i have a generic database (pgsql or mysql doesn't matter), that contains many generic objects.

    I have $x (actually $x = 200, but is growing, hoping will reach 1000 soon) of exact copy of this database, and for each of them up to 20 (avg 10) users for 9 hours at day.

    If one of those users is viewing a record, any record, i must advice him if someone edit the same record.

    Let's say Foo is watching the document 0001, sit up for a coffee, Bar open and edit the same document, when Foo come back he must see an 'Warning, someone else edited this document! click here to refresh tha page.'.

    That'all i need atm, probably i'll extend this situation, adding a way to see the changes and rollback, but this is not the point.

    Some of you suggested to check the 'last update' timestamp only when foo try to save the document; Can be a solution too, but i need something in real-time ( 10 sec deelay ).

    Long polling, bad way, but seem to be the only one.

    So, what i've done:

    1. Installed Lighttp on my machine (and php5 as fastcgi);
    2. Loaded apache2's proxy module (all, or 403 error will hit you);
    3. Changed the lighttpd port from 80 (that is used by apache2) to 81;
    4. Configured apache2 to proxying the request from mydomain.com/polling/* to polling.mydomain.com (served with Lighttp)
    5. Now, i have another sub http-service that i'll use both for polling and load static content (images, etc..), in order to reduce the apache2's load.
    6. Becose i dont want to nuke the database for the timestamp check, i've tryed some caches system (that can be called from php).
      1. APC: quite simple to install and manage, very lightweight and faster, this would be my first choice.. if only the cache would be sharable between two cgi process (i need to store in cache a value from apache2's php process, and read it from lighttpd's php process)
      2. Memcached: around 4-5 times slower than APC, but run as a single process that can be touched everywhere in my environment. I'll go with this one, atm. (even if is slower, the use i'll do of it is relatively simple).

    Now, i just have to try this system loading some test datas to see ho will move 'under pressure' and optimize it.

    I suppost this environment will work for other long-polling situations (chat?)

    Thanks to everyone who gave me hear!

提交回复
热议问题