I have a little bit like the logic implementation of the function, performance optimization, high concurrency problems to ask

Source: Internet
Author: User
Business scenario: Suppose there are 1000 people browsing the same microblog at the same time, and these 1000 people will be like, in order to simplify, we assume that the 1000 people are like in chronological order.

The likes control cannot let the user send the AJAX request once every click, 1000 people simultaneously online, we cannot guarantee that these 1000 users are not some bored person. If they play the control at the same time (if not at the same time), the server will definitely be down to the dogs! So the problem is, the front-end side of the original points like the number must be added 1 or minus 1. Backend aspects of the user ID and likes to praise the target users of the database stored in the likes of the user (usually we will see who likes me) to match, if there is a minus one, does not exist then add one. But this time we can not emphasize data consistency, unless the user refreshes, if the emphasis on data consistency, I would like to add 125, this experience is not very good. So the return is the original user to see the point like the number plus one or minus one, then the inevitable every click to send AJAX requests, server downtime is just about ...

There is no good solution

Reply content:

Business scenario: Suppose there are 1000 people browsing the same microblog at the same time, and these 1000 people will be like, in order to simplify, we assume that the 1000 people are like in chronological order.

The likes control cannot let the user send the AJAX request once every click, 1000 people simultaneously online, we cannot guarantee that these 1000 users are not some bored person. If they play the control at the same time (if not at the same time), the server will definitely be down to the dogs! So the problem is, the front-end side of the original points like the number must be added 1 or minus 1. Backend aspects of the user ID and likes to praise the target users of the database stored in the likes of the user (usually we will see who likes me) to match, if there is a minus one, does not exist then add one. But this time we can not emphasize data consistency, unless the user refreshes, if the emphasis on data consistency, I would like to add 125, this experience is not very good. So the return is the original user to see the point like the number plus one or minus one, then the inevitable every click to send AJAX requests, server downtime is just about ...

There is no good solution

You can go to Redis, and every day like to save to Redis, users like and cancel the likes of the operation is the content of the cache, not the database. Then write the daily final point of praise to the database at Low peak times (for example, 0:00), so that you only need to write data to the database once a day to solve the problem.

There is no better way to store in Redis is a more conventional approach, Redis operations are atomic, so dirty data is not generated.
Then the next day is the data persistence problem, set a cycle, timing data from Redis to MySQL run over.

1, like the request to store the queue (Redis RabbitMQ ZeroMQ etc.)
2, the queue consumption will be based on business system pressure to determine whether to put the likes of the brush into MySQL
3, of course, whether to praise also need two sets of logical judgment

If the volume is really large, you can redis+ the message queue update, first write the data to Redis, and then use the queue method to slowly deposit the database

I thought of a crooked idea and didn't know useful not:
Late in the web to send AJAX requests, for example, the user clicked a bit like, then JS will be like the success of the style display (like the number of +1, show likes avatar, etc.), but do not immediately send an AJAX request, waiting for 10s (or when the user leaves the page, refresh the page and other operations), If the halfway did not like and cancel the likes operation, and then send the AJAX request, if halfway through the likes and cancel the likes operation, then re-timer delay! For those who like to click to refresh a bit and then cancel the likes of the user, can only kneel to call dad, haha ha!

Like, isn't live = live + 1 it? What do you mean, "add 125 to your liking"?

  • 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.