views:

456

answers:

3

I find myself doing some relatively advanced stuff with memcached in PHP. It's becoming a mental struggle to think about and resolve race conditions and concurrency issues caused by the lock-free nature of the cache.

PHP seems pretty poor in tools when it comes to concurrency (threads, anyone?), so I wonder if there are any solutions out there to test/debug this properly.

I don't want to wait until two users request two scripts that will run as parallel processes at the same time and cause a concurrency issue that will leave me scratching my head, or that I might not ever notice until it snowballs into a clusterfsck.

Any magic PHP concurrency wand I should know of?

A: 

Not specifically for this issue but: FirePHP?

George Mauer
For that particular matter I prefer my current file-based logging system with precise timestamps that can help with determining what was running when.
Gilles
+2  A: 

PHP is not a language designed for multi-threading, and I don't think it ever will be.

If you need mutex functionality, PHP has a Semaphore functions you can compile in.

Memcache has no mutex capability, but it can be emulated using the Memcache::add() method.

If you are using a MySQL database, and are trying to prevent some kind of race condition corruption, you can use the lock tables statement, or use transactions.

JMack
Thanks for the pointers! I'm still looking for something in the testing department rather than adding safety nets to the implementation without actually testing it in a real concurrent situation. For example it's cool to be able do semaphores, but what's the point if it can't be tested properly?
Gilles
I've started using Memcache:add thanks to your advice and I'm a happy camper.
Gilles
+1  A: 

You could try pounding on your code with a load test tool that can make multiple requests at the same time. Jmeter comes to mind.

Ryan Doherty