Phpmemcached's high concurrency queue implementation problem I just read some code of memcached's queue for handling the flash sales problem. I understand two ideas:
1. use $ mem-> set (LOCK_key, 1) to lock. after the queue is completed, $ mem-> delete (LOCK_key) is unlocked.
2. use memcached's increment (key, 1) to get the queue location
My problem is:
1. is the first method feasible?
2. $ memcached-> increment (key, 1) Can this function handle concurrent access? That is, if multiple processes call the increment function at the same time, will the concurrency overwrite problem occur?
I am a senior this year. I am constantly looking for a job. due to the lack of systematic PHP, I have been reviewed by both YY and 4399.
It's cool, it's hard to find a job, and you can cherish it. I want to cheer up. thank you for pulling me!
Reply to discussion (solution)
Conflicts may occur.
You can test it on your own.
Only memcached will cause conflicts. for high concurrency conflicts, you can use memcacheQ to solve them.
Memcacheq is a middleware designed specifically for solving high concurrency issues and stores data in queues.
Reference: http://blog.csdn.net/fdipzone/article/details/17933673
1. the first method is acceptable, but the lock efficiency is too low. access from multiple clients is waiting for unlocking and the speed is extremely slow (according to my test, 800 clients, each client accesses 10 times and fills up a 200 queue, which takes 32 seconds ).
How to implement this queue: github has already posted code
Https://github.com/meetrajesh/php-memqueue/blob/master/memqueue.php
Only 95th rows of while ($ this-> memc-> add ($ this-> lock_key, '1', 0, 2) are added in while! In order to properly lock.
2. all memcached operations are atomic (get/set/increment, see setting the big benefit for all this). That is to say, no matter how many operations you perform, you are dropped to the memcached server, all are executed sequentially, so Method 2 is feasible. I also used 800 clients, each of which Accessed 10 times and finally checked the value of increment, which is exactly 8000, so it won't be overwritten.
Coincidentally, I also suffered 4399 million yuan in my senior year this year, and YY and other companies swept away.
Think Twice. learn from your work.
I forgot to mention it. for the queue service, we suggest using redis. its own queue operations provide much better performance than memcached's locking on the server.
Http://redis.io/