Implementation of high concurrency processing queue in PHP memcached

Source: Internet
Author: User
Just saw some code in the queue of memcached dealing with snapping, I understand that there are two ways of thinking

1. Use the $mem->set (lock_key,1) to lock, $mem->delete (lock_key) unlocked after the queue ends

2. Use memcached's increment (key,1) to get the queue position

My question is:
1. Is the first method feasible?
2. $memcached->increment (key,1) does this function handle concurrent access? That is, multiple processes call the increment function at the same time, will there be concurrent overwrite problems?

I am a senior year, in the constantly looking for work, due to the lack of systematic php before learning, by YY and 4399 are the time of the second-round brush.
Heart is very cold, find work difficult, and the line and cherish, I want to good refueling, thank you Daniel pull me a


Reply to discussion (solution)

Conflict will occur
You can test it yourself.

Only with memcached you can conflict, for high concurrency conflicts, you can use Memcacheq to solve.
Memcacheq is a middleware developed specifically for the door to solve high concurrency problems, accessing data in a queue.
Reference: http://blog.csdn.net/fdipzone/article/details/17933673

1. The first method is possible, but the lock efficiency is too low, multiple client access is waiting to unlock, super slow (according to my test, 800 clients, each client Access 10 times, filling a 200 queue, it takes 32 seconds).
How this queue is implemented there's already a big God sticker on GitHub.
https://github.com/meetrajesh/php-memqueue/blob/master/memqueue.php
Only the 95th row while ($this->memc->add ($this->lock_key, ' 1 ', 0, 2)) is added while inside! To properly lock.

All operations of 2.memcached are atomic (get/set/increment, see Http://blog.csdn.net/jarfield/article/details/4336035#what's The big Benefit for any this), that is to say, no matter how many operations you throw into the memcached server, are executed sequentially, so method 2 is feasible. I also use 800 clients, each client Access 10 times, and finally view the value of increment, exactly 8000, so will not overwrite.

Coincidentally, I was also this year's senior seniors have been 4399,yy and other companies brush off.
Learn from the experience, slowly re-study it

Forget to say, queue service, recommended with Redis bar, he has a queue of various operations, performance than memcached on the server lock is much better.

http://redis.io/

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.