When you think about PHP, “concurrency” probably isn’t among the first things that come to mind. Fortunately (unfortunately?) we live in a world where distributed applications are the de-facto standard which means that not even humble, single-threaded PHP is immune to the woes of concurrent programming. Standing on the shoulders of giants, and inspired by the work of many before me, I’ve built a very simple redis-backed mutex:
Suppose in your application that you are interacting with an API that doesn’t allow partial updates. You would use the mutex as follows:
This implementation is far from unique, but it is very functional and hopefully simple enough to be dropped-in to most projects with only minor customizations.
Discussion on hacker news.