Surely you do the web no stranger to the phrase “asynchronous”, right. However, you have fully understood the mechanism and its application yet? Today, let’s find out about this issue with me!
1. The actual problem:
When I was in school, I used to work on a website project to do English tests. When users solve a multiple choice question, if they are correct they will get 3 points, if they are wrong they will be deducted 1 point, a website is quite simple, right. When the user submits, they just need to check that the answer is correct or not, if correct, update their score by 3, wrong then subtract 1. Too simple!
I tested it locally also found it very smooth. Gleefully push the code to the hosting. And results…
Unlike local, when pushed to hosting, the speed of updating points on the website is 1 second slower than reality. That means when the user completes the answer, assuming the correct answer, then 1 second after the score will be updated. This is also understandable because the speed on the website depends on the user’s network speed and the server’s processing speed. But not being able to update the score immediately will cause a frustrating user experience, so I will find the best way possible.
I struggled to find a solution, I increased the bandwidth for hosting, optimized the query, and minimized processing the code as possible. And heaven does not disappoint people, the results improved significantly, reduced by 0.25 seconds, which means still slow compared to reality … 0.75 seconds.
While it seems to be the most deadlock, I have come up with a “pretty cool” way to reduce the delay time to 0s, which means I update the score immediately when the user has submitted the answer. At that time I did not know what its name was, but now when I have a bit of experience, I know, its name is “asynchronous”.
Concept : Asynchronous is multithreading, in which threads run separately, without interfering with each other.
2. Asynchronous Front End
The actual problem above is an example of asynchronous front end. Also, I find the front end asynchronous application a lot, another simple example is the messaging feature of facebook.
But sometimes the experience is not so good!
To test this, you can test in the following way. Open texting dialog for any one person, disconnect from the internet, compose messages and send, the messages will be displayed immediately on the conversation panel while your computer is offline. You reopen the internet and reload the page, the other message is gone.
3. Asynchronous in Back end
Asynchronous in the back end is equally diverse. For example, when you retrieve your password by email, you enter your email address and click submit, almost immediately your action will be announced successful. In fact, it takes a long time for your email to receive the message, but it is done by an asynchronous thread running in the background, in parallel with you submit, when the implicit request is completed, the message the message will be automatically sent to the user’s email. Imagine, if no asynchronous use is used in this case, when you press the submit button, you have to sit and watch the screen for 5 minutes to make this request. Too inconvenient!
Some other examples:
- Statistics of how many orders are closing today: At 12pm every day, the database will automatically make a request to check how many orders the shipper has closed today and update data into the database, this action will running in the background asynchronously, does not affect the operation of the app.
- Upload photos / videos
- Export files etc …