Page 1 of 1

Shared memory locking

Posted: Tue Dec 28, 2004 12:51 pm
by tomtomtom
Hey,

I'm implementing shared memory into my PHP application for performance reasons, but I'm stuck.

Since the memory will be receiving up to 10-15 reads per second and 1 or 2 writes per second, I want to have a locking mechanism, using semaphores.

I'm not sure what to lock though. The obvious case is where the script reads data, manipulates it, then writes it, which I will enclose in a lock.

But I'm guessing it'd be a good idea to lock all write operations. But then what about reads?

Is it theoretically possible for a read to happen at the same instant as a write, so that the read operation picks up a corrupt mixture of new and old data?

So, should I lock all reads? Can I do this using a read-lock, so that more than one read can occur simultaneously?


Hope this makes some kind of sense. I'd appreciate any help / ideas!

Thomas

Posted: Tue Dec 28, 2004 1:38 pm
by magicrobotmonkey
i think when the mem's locked it cant be read either so the read(s) will have to wait until the write's done anyways.

Posted: Wed Dec 29, 2004 5:55 am
by carlmcdade
Coming from ASP and VB I can see how shared memory would be used for a large app where a single instance of an object would be used to communicate to other processes and dlls thus saving resources. But how can this increase performance in PHP? I thought shared memory was just sort of communication scheme. Locking and unlocking memory spaces might improve stability but how can it increase throughput when everything has to wait in line?

Posted: Wed Dec 29, 2004 7:24 am
by Weirdan
carlmcdade wrote:I thought shared memory was just sort of communication scheme. Locking and unlocking memory spaces might improve stability but how can it increase throughput when everything has to wait in line?
Shared memory could be used as a cache storage for example. And, as with any shared resources, it should be properly locked. Otherwise you would get sometimes complete garbage, especially under high load. Read lock does not prevent concurrent reads, it only forces write operations to wait.

Posted: Wed Dec 29, 2004 12:22 pm
by carlmcdade
Weirdan wrote: Shared memory could be used as a cache storage for example.
That's interesting ! How is this with shared hosting environment and is it something that is easier to clean and maintenance than what I use now? Which is MySQL tables or flat files.

Posted: Thu Dec 30, 2004 3:14 pm
by tomtomtom
SHM-based caching is great. I think it doesn't come into its own though until load is so high that reading from the HDD is the bottleneck (up to that point a file-based cache is easier - jpcache is good). It should be used in addition to a backing database, because it's volatile.

Back the locking... locking every read would be incredibly inefficient... isn't there a read-lock in PHP's implementation of semaphores, as there is in the flock() function???? If not, is it possible to simulate this behaviour?

Posted: Mon Jan 03, 2005 12:50 pm
by carlmcdade
It sucks that this does not work in windows and is too volatile for me to be experimenting with on a hosted site. I guess I will have to install Xandros again.

Posted: Mon Jan 03, 2005 1:01 pm
by feyd
carlmcdade wrote:It sucks that this does not work in windows and is too volatile for me to be experimenting with on a hosted site. I guess I will have to install Xandros again.
It's odd to me why they haven't implemented it. Those services exist on the bulk of windows machines used as servers, although sometimes hidden from the user.

Posted: Mon Jan 03, 2005 3:39 pm
by ianlandsman
Not sure if this is an option for you, but I've used the system memory functions of mmcache very successfully. They abstract all the tough stuff.