Sharing data between sessions?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

    Some data are common to all user sessions, and to improve
performance/save resources, I'd like to share those data between all
sessions, so that each user doesn't have to hit MySQL for the same
data. I'd rather avoid writing those in a flat file, and keep stuff in
RAM instead.

Someone told me about cache servers like MemCacheD. I was also given
the hints of writing in OO (public class variables) or using

For those of you who had to do the same thing, what would you
recommend, and why?

Thank you.

Re: Sharing data between sessions?

Gilles Ganault wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Zend_Cache will do the trick. It is a generic way to cache data using
different back ends like files, SQLite, memached, APC, etc. I would use

You can also use shared memory with shm_get_var. Any stored data will be
cached across a host. You also have MySQL's query cache available, which
will speed things up without having to change your code much.

Re: Sharing data between sessions?

Gilles Ganault wrote:
Quoted text here. Click to load it

First of all: does your script/server hit a limit, and is the
performance a problem? If no, don't worry about optimization right now.
It needlessly complicates things, and more often then not makes things
terribly inflexible.

As the script ends after the request, using public class variables
(unless hardcoded in the PHP source) with a longer lifetime on its own
cannot be done in PHP, the next request will bring the class into a new,
clean state again. The same goes for superglobals.

Also, realize that unless specified otherwise, the default for the built
in session handler is exactly what you wanted to avoid: flat files
(serialized data).

Memcache is one solution that could work, but is for most sites
unnecessary complication. To a limited extent, you could also use shmop.

However, if the database is stressed, I'd begin optimisation there.
Analyze EXPLAIN outputs, set proper indexes, possibly make views of
often accessed resultsets. If in need, ask in comp.databases.mysql what
you can do to optimize your slowest and/or most often performed queries.

If really stressed, I tend to do exactly what you tried to avoid: saving
data in flat files. The bulk of any typical site is not dynamic, save
for a few areas. Keeping a result as a flat file HTML fragment is quite
easy, and updating it on alterations of the date underneath will mean
you only have to generate it once for an alteration instead of on every
request. Also a possibility, which I wouldn't advise unless alterations
will be done without using your application/bypassing PHP, is using
filemtime() and a query to check wether anything has been altered since
then, and only then recreate it.

Personnally, I've never had the pleasure of working on a site hit
enough, or the horror of a server weak enough, to let memcache make an
important difference.
Rik Wasmus
Now looking for some smaller projects to work on to fund a bigger one
with delayed pay. If interested, mail rik at

Re: Sharing data between sessions?

On Thu, 15 May 2008 14:52:18 +0200, Rik Wasmus
Quoted text here. Click to load it

Yes. Since most data fetched from MySQL is common to all logged-on
users, each session is using a lot of RAM and fetching the same data
from MySQL needlessly. AFAICT, MySQL is as optimized as it could be at
this point. I need to find how to share common query output with every

Quoted text here. Click to load it

If it's possible to have session data live in RAM instead, should I
expect significant performance?

So, possible solutions are:
- ZendCache + MemCacheD/APC
- shared memory

I'll check this out and see how it goes.

Re: Sharing data between sessions?


Quoted text here. Click to load it

Make very sure this is so. There are instances were lowering some settings  
for MySQL can acctually speed up things. And running EXPLAINS on just  
about every query that goes in also helps a lot.

Quoted text here. Click to load it

It is possible, however, it will only affect performance noticably if you  
have HUGE sessions (i.e. a terrible amount of data stored in a $_SESSION).

Quoted text here. Click to load it

And don't forget, profile & trace your current code. Xdebug works great,  
even more so with xdebug.show_mem_delta on.
Rik Wasmus
...spamrun finished

Re: Sharing data between sessions?

On Fri, 16 May 2008 00:44:08 +0200, "Rik Wasmus"

I'll go over MySQL fine-tuning again, check what happens when session
data live in RAM instead of in files under /var/tmp/, and play with
Xdebug more.

BTW, for those of you using APC, how do you keep data in sync with
MySQL? Does it work only if all changes to MySQL go through my PHP +
APC scripts, ie. all INSERT/UPDATE/DELETE imply running SELECT right
after and updating the ad hoc variable in APC?

Thank you.

Re: Sharing data between sessions?

Gilles Ganault schreef:
Quoted text here. Click to load it

Personally I would do it simple: Just use a file that contains the
'shared content', as Rik suggested also.
I have used that solution a few times now, and it never failed me.
(increasing the responsetime for certain pages from 30 secs to 0.5 secs)
I did this both in Java (J2EE) and PHP and VB/ASP. The approach always
works (for me).

Understand that using a 'file' doesn't mean the Harddisk is read every
time to get it for each invocation of your PHP script: It is cached in
memory on almost every OS I saw.
Hence it is bloody fast.
Using a file also keeps things simple: You update the file only when
need arrises. I did it in 2 ways untill now:

1) Update such a file immediately if underlying data is changed.
This involves of course a good understanding which actions can possibly
change the underlying data, and then rebuild the file.

2) Create a cronjob that updates the file every 30 minutes or so.
This is only possible if the data doesn't have to be 100% up-to-date.
(I used that for top-10 lists of complex stuff, and stuff like that)

Using a file for shared content is also a very general solution. If you
switch databases later for some reason, this file logic stays the same.
If you finetune your database... Well, you'll have to do that again if
you switch database. But maybe this is not relevant because you won't
switch databases.

just my 2 cent.

Erwin Moller

Re: Sharing data between sessions?

On Fri, 16 May 2008 10:51:37 +0200, Erwin Moller
Quoted text here. Click to load it

Thanks for the idea. Another solution is to keep user-specific data in
sessions, and store data common to all users in a cache:

//Fetch customer-specific data from session file
if(isset($_SESSION['myprivatevalue'])) {
        print $_SESSION['myprivatevalue'] . "<p>\n";
} else {
        $_SESSION['myprivatevalue'] = "verysecret";

//Fetch common data from cache
//apc_add('scooby-doo', 'daphne');
print "Scooby-do=" . apc_fetch('scooby-doo');

Thanks guys.

Re: Sharing data between sessions?

Quoted text here. Click to load it

Your email implies that you already have user session data - in that
case, it would be a lot more sensible to hold the data close to the
user session data and merge when the session is re-instantiated using
a custom session handler. If you go down this route, you do need to
think about how you save the common data - in the case of users
sessions, there is likely to be little contention, but with a common
repository being updated each time a session is saved there could well
be. This is less of a problem with a DBMS than with files because
requests to update can be queued up. Another approach would be to not
update the common repository when the session is saved but implement
updates via another route (typically I'd expect that the common data
has a longer TTL than user session data).

I've not seen benchmarks, but would expect a DBMS session handler
(particularly one using MySQL) to be only slightly slower than a file
based one (and most of the difference being the mysql conection
overhead - which you may already have elsewhere in your code). However
the difference between fetching a single serialized array from the
database, and fetching two using a union then carrying out an
array_merge I'd expect to be negligible.


Re: Sharing data between sessions?

Gilles Ganault wrote:
Quoted text here. Click to load it

Public class variables won't do you any good.  They are still related to
the current script and can't cross scripts.

MemCachD can help.  You can also use the shared memory functions in PHP.

However, I really wonder if you need this.  If the data are that
currently accessed, they are probably already cached by MySQL, and
accessing the data will be very quick.

Are you actually seeing a performance problem?  Or are you falling into
the trap of premature optimization?

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline