Using shared memory in mod_perl environment

520 views Asked by At

I have a requirement wherein

  1. I have to place a data structure (Perl hash) in memory, so that each HTTP process (running a Perl script) will use that hash.

  2. The hash structure is around 300 MB.

  3. The environment is mod_perl

I thought of creating a module to load at Apache start that creates a hash in a shared region and returns a reference to it.

Can you please comment on the behaviour, or suggest alternative solutions. Also please point to some good resources to check the examples.

2

There are 2 answers

0
Sobrique On

I would be thinking in terms of handing it around via Storable store it to a file, retrieve it at start.

If it needs to change, you'd need to use flock to arbitrate IO, and potentially some sort of mechanism for checking when it changed last (e.g. check mtime).

1
OGATA Tetsuji On

If you place huge hash data on mod_perl memory, then mod_perl parent process reads it at server startup phase.

In first, you create Your/HugeData.pm on @INC directory.

package Your::HugeData;

our %dictionary = (
    ....
);

Next, apache process reads this on startup.

# In apache.conf (or anywhere apache config file)
PerlModule Your::HugeData

Then your script can use %Your::HugeData::dictionary as package variable.

# In mod_perl handler script or ModPerl::Registry (CGI emulate) script.
use Your::HugeData;
...
my $tokyo = $Your::HugeData::dictionary{tokyo};

When you use prefork MPM on Linux Apache, OS prefers "Copy on Write" mechanism. So forked child processes see parent proces'es data if you only read the data. In other words, there may be not waste memory using.