You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Chris Ochs <ch...@paymentonline.com> on 2003/12/20 03:42:58 UTC

Storing variables in memory

I have an application where known users connect, and normally I do a db
query to get their configuration information.  What I would like to do is
preload all of this information into memory, say into a global hash where
all the mod perl processes can access it.

I could have each process load the data the first time it runs and stick it
into a global hash.

And since the primary goal is making the program faster, any alternatives
that would take longer than a database select wouldnt' do me any good.

Any ideas?



-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Storing variables in memory

Posted by harm <ha...@tty.nl>.
On Sat, Dec 20, 2003 at 11:16:53AM -0500, Perrin Harkins wrote:
> Eric Sammer wrote:
> >I've had fantastic luck with MLDBM::Sync thus far, not that it's the all 
> >in one wonder tool.
> 
> I think it's a great tool, especially when you need easy access to 
> complex data structures, but I wanted to point the non-intuitive fact 
> that a local MySQL can be a great cache for a remote database, and is 
> actually faster than all of the other shared memory or file-based 
> solutions except IPC::MM and BerkeleyDB.  I will have a full article 
> about this soon.

Another approach to the caching problem is memcached:
http://www.danga.com/memcached/

it is a C daemon which you access over a socket. It is key=>value based
storage and if you use it to store strings it is _really_ fast. (You
can put arbitrary perl objects in there, but it uses Storable.pm which makes
it a little slower).  This is great to store pieces of HTML. 
Since it is a seperate process it stores every value
once and there is no copy for every mod_perl process. If the parserd deamon dies the perl
client will just ignore the caching and go on uncached. It will try every now and then to
see if the daemon got restarted.


Little bechmark (source attached):

Benchmark: timing 40000 iterations of cache_filecache, cache_memcached, cache_memorycache, notcached...
cache_filecache: 20 wallclock secs (15.25 usr +  2.65 sys = 17.90 CPU) @ 2234.64/s (n=40000)
cache_memcached:  5 wallclock secs ( 2.62 usr +  1.08 sys =  3.70 CPU) @ 10810.81/s (n=40000)
cache_memorycache:  7 wallclock secs ( 5.70 usr +  0.04 sys =  5.74 CPU) @ 6968.64/s (n=40000)
 notcached:  6 wallclock secs ( 5.56 usr +  0.01 sys =  5.57 CPU) @ 7181.33/s (n=40000)


The 'wallclock secs' are the ones to watch because usr/sys are incomplete
since mamcached uses a seperate process.
The cached routine is this very interesting (yeah, right) routine:

    foreach (sort 1..100) {
         $ret .= qq'&gt; $_<br>';
    }
    return $ret;

anything more fancy then this will only make the difference between memcached
and notcached bigger :)





Harmen
(Ok, this it not suited for complex structures which this thread is about but maybe 
someone can use it nevertheless.)



-- 
                               The Moon is Waning Crescent (10% of Full)
                        tty.nl - muziek-en-artiesten.2dehands.nl: 107625

Re: Storing variables in memory

Posted by Perrin Harkins <pe...@elem.com>.
Eric Sammer wrote:
> I've had fantastic luck with MLDBM::Sync thus far, not that it's the all 
> in one wonder tool.

I think it's a great tool, especially when you need easy access to 
complex data structures, but I wanted to point the non-intuitive fact 
that a local MySQL can be a great cache for a remote database, and is 
actually faster than all of the other shared memory or file-based 
solutions except IPC::MM and BerkeleyDB.  I will have a full article 
about this soon.

- Perrin



-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Storing variables in memory

Posted by Eric Sammer <er...@ineoconcepts.com>.
Perrin Harkins wrote:
> Eric Sammer wrote:
> 
>> Any DBM file or shared memory caching will be infinitely faster than 
>> making a DB round trip.
> 
> Actually, it turns out that this is no longer true.  MySQL is really 
> fast these days.  A simple query on a local MySQL is faster than just 
> about anything except IPC::MM or BerkeleyDB (using built-in locking).

I suppose that's true. I was also taking into account (potential) 
network lag if the rdbms is running on another box. Of course, I was 
also trying to be rdbms agnostic, to be fair. ;)

I've been running with postgres for most of our recent projects, but 
most of them have to run with just about anything that DBI uses so 
having a caching layer helps. I've noticed a sizable increase in 
performance when testing with ab and jmeter. Also, according to 
Apache::DProf, things are running smoother.

> MLDBM::Sync is still fast enough for most uses, and faster than a 
> complex query (joins or multiple items in the WHERE clause) on most 
> databases.

I've had fantastic luck with MLDBM::Sync thus far, not that it's the all 
in one wonder tool.

-- 
Eric Sammer
eric@ineoconcepts.com
http://www.ineoconcepts.com

-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Storing variables in memory

Posted by Perrin Harkins <pe...@elem.com>.
Eric Sammer wrote:
> Any DBM file or shared memory caching will be infinitely faster than 
> making a DB round trip.

Actually, it turns out that this is no longer true.  MySQL is really 
fast these days.  A simple query on a local MySQL is faster than just 
about anything except IPC::MM or BerkeleyDB (using built-in locking).

MLDBM::Sync is still fast enough for most uses, and faster than a 
complex query (joins or multiple items in the WHERE clause) on most 
databases.

If the data in question is small enough to fit in memory and doesn't 
need to be updated while the server is running, there are instructions 
in the Guide as well as the books about how to preload the data during 
startup.

- Perrin


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Storing variables in memory

Posted by Eric Sammer <er...@ineoconcepts.com>.
Chris Ochs wrote:
> I have an application where known users connect, and normally I do a db
> query to get their configuration information.  What I would like to do is
> preload all of this information into memory, say into a global hash where
> all the mod perl processes can access it.

You want to use one of the many caching options. You should take a look 
at Cache::Cache or MLDBM::Sync. There are others as well. Personally, I 
use MLDBM::Sync (stores multi-level data structures in dbm files and 
allows for locking). MLDBM::Sync is implemented (as most of them are, I 
believe) as a tied hash so it works just about how you think it would.

If you wanted to truly preload it (i.e. at server startup time) you 
could write a bit of code in your mod_perl startup.pl file.

Perrin Harkins / Bill Hilf wrote a bit about this here:
http://perl.apache.org/docs/tutorials/apps/scale_etoys/etoys.html

It has also be discussed on this list many time. Search the archives.

See also:

http://search.cpan.org/~chamas/MLDBM-Sync-0.30/Sync.pm
http://search.cpan.org/~dclinton/Cache-Cache-1.02/lib/Cache/Cache.pm

(There are other modules that work equally well.)

> And since the primary goal is making the program faster, any alternatives
> that would take longer than a database select wouldnt' do me any good.

Any DBM file or shared memory caching will be infinitely faster than 
making a DB round trip. There is usually some initial overhead during 
the initial fetch because it has to fetch the info from the rdbms and 
then write it to the cache, but it's trivial when you consider the benefits.

HTH.
-- 
Eric Sammer
eric@ineoconcepts.com
http://www.ineoconcepts.com

-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html