BerntB has asked for the wisdom of the Perl Monks concerning the following question:

I am planning to use mod_perl with a hobby program I'm writing.

Common data can easily be shared between httpd processes by using "PerlModule" in the Apache config (right?). Even if all processes needed to load data, it's just at process start anyway.

There is still the "usual" problem -- complex user state. I've seen "Sharing data structures in mod_perl" (and similar ones). (Update: To make it clearer, the problem is that the user state is potentially so complex that recreating it will be prohibitive for all web accesses.)

Q1: There is no "normal" way to make a mod_perl program to keep state for a given user, since requests will go to random processes, right?

I wrote my program with an interface "split" in the middle. I could use SOAP or some other interface, with the not-so-stateful UI running on the web server. (Or even let the users have a Tk client for the UI.)

Q2: What modules on CPAN are recommended for stateful servers for (some kind of) RPC calls? (From local machine's mod_perl or over the internet.)

Background (can safely be ignored):

I started out writing a data editor for an old favorite game. This ended up as potentially useful for other people's needs. (In hindsight, you could probably have mapped this quite well onto XML and let a Schema spec do a good part of the work. :-( )

I have no idea if five people or five thousands will have a use for this.

(-: I have a neat plan for the license. If I copyleft the program, all data used has to be released (since code is generated from data and linked in). This is my evil plan to lighten the documentation needs... "don't complain, look at all the example data!" For those not willing to follow the GPL, I'll grudgingly send a non-GPL version for a non-modest fee. :-)

Replies are listed 'Best First'.
Re: Shared between mod_perl processes?
by perrin (Chancellor) on Aug 19, 2005 at 19:21 UTC
    Q1: Correct.

    Q2: Usually people store the state in a database or share it between processes with something like Cache::FastMmap. You mentioned that the data would take a while to load, but I think you'll find that it's not necessary to load the entire data into memory all the time.

      You mentioned that the data would take a while to load, but I think you'll find that it's not necessary to load the entire data into memory all the time.
      Yes and no. :-)

      It is a a small design system; a small change somewhere will propagate to other places. This is according to a design which is loaded, so the program can't understand what to do (-: probably mappable to the halting problem, or something. :-)

      My application probably won't have a problem with (de)serializing. The standard approach will work splendidly.

      But it could be used for other applications and if you go a factor of ten in complexity against what I've done -- I think you really don't want to tear it down and rebuild the data model for every small mod.

      So I'm afraid that in my case, the standard solution won't scale. (-: But I'll give up solving the problems of 2006 now. :-)

      Thanks for the help, everyone! :-D

      Have to find a job with Perl. Monks and the language is wonderful fun.

Re: Shared between mod_perl processes?
by johnnywang (Priest) on Aug 19, 2005 at 18:33 UTC
    One common way is to use CGI::Session to keep track of user sessions, and you can put anything you want in it. It has several drivers, a standard one is to save the session in files in the /tmp directory.

    If you really want to share data between different processes in memory on the same machine , you can use IPC::Shareable. It's not faster than database storage, I think it internally uses Storable.

      IPC::Shareable is quite slow. If you want to share data fast, use something like Cache::FastMmap or BerkeleyDB instead.
Re: Shared between mod_perl processes?
by themage (Friar) on Aug 19, 2005 at 20:06 UTC
    Hi,

    I like to use HTML::Mason for almost everything I do for web, and I use a Mason Component to cache all data I need shared between processes.

    It didn't let me down so far.

      OK, thanks

      Mason looks good, but seems like overkill for my modest web/html needs. (Hopefully, when/if I reach version 0.3 my ui won't look like 1997-retro. :-)

      My problem is the size of the user state and the cost to instantiate it at every request. It'll be tolerable now, but if my modest application framework is used for a larger problem, it'll hit the roof.

      I'll use one of the existing variants (Cache::*, BerkeleyDB, etc) for now. This potential problem can wait until later.

      Again, thanks everyone, for the answers. I'll try to write my questions better next time so it won't be so confusing. :-)

Re: Shared between mod_perl processes?
by samizdat (Vicar) on Aug 19, 2005 at 17:06 UTC
    This has nothing to do with mod_perl vs cgi. Your user-specific data can be saved on the server with a simple database or on clients with cookies, and it's up to you to determine what format you save it in. If other users or their 'agent programs' need access to a user's data and you're only on one server, then the database approach or shared memory data structures are the way to go. SHM access is raw Perl and it's quite fast and easy if only the user's web access can change his shared data. If you wish to access this all from any client, a Perl program on any machine on the net can access a MySQL or PostgreSQL machine using the appropriate module, as long as that access is allowed by the database and its host's deny/allow rules.
      This has nothing to do with mod_perl vs cgi
      Uhhh... why do you think I asked about that?
      Your user-specific data can be saved on the server with a simple database or on clients with cookies
      I must have been very unclear. My problems was the size and complexity of my data. It would take time to load it.
      SHM access is raw Perl and it's quite fast and easy
      SHM works well? Thanks! I've always thought it was generally a bit of a problem (I've only experience with the old sysV). What CPAN-modules do SHM well?

      I can accept the SHM costs of (de)serializing, for now. It won't scale to many users or complex data, so I don't know if it is the long time solution.

      (Update: Fixed grammar.)

        I was wondering why you made such a point of mentioning mod_perl. The way Perl is executed has nothing to do with the rest of your question, AFAIK.

        SHM -- which is still SYSV :) -- works perfectly, and you can access it with no fear as long as it's Write Once, Read Many blocks. There's no need to use a module at all in this case. Even access for others to write is not difficult; you just build a common lock-block of bits (or bytes, if you can afford to be wasteful), which acts like a set of semaphores. No more tricky than file locking.

        If you intend Net-wide access, think again about databases. Your CPU + IO time will still be far less than the transmission time.