If You Want To Scale Your Application, Learn How to Async
Understanding the importance of async programming
When we start off with creating a web application, we don't care about how performant it is (and we shouldn't, to be honest). Our aim is to get a basic POC done and see if it gets some audience. This approach fits well until your user base grows beyond a number. Let's see what happens when your user base is growing.
Your server receives 5 requests every second. You have a reverse proxy designed to forward those requests to your HTTP server. You have configured your HTTP server to forward these requests to your application, which in turn processes the requests one by one and returns a response back. If you are working with a framework like Django which uses Python which has limitations when it comes to running parallel processes, this approach can cause issues. If you are one of the application developers and hasn't done much to optimise your endpoints, then trust me, your application is entirely dependent on the amazing architecture of your HTTP server and reverse proxy!
When a lot of requests pour into your service, optimising your application code by reducing the number of loops or substituting operations with inbuilt functions can only go so far. Asynchronous programming to the rescue! It's a new paradigm of programming. It's not intuitive. When I learnt it myself, I found it really hard to understand. But it's effective!
Explain like I am a five year old?
Let's say a user is creating a profile on your website. Now, you have to send a mail to their email ID asking for confirmation. But when the server tries to send a mail, it has to communicate with the SMTP server, wait for it to respond, send the message, wait for the acknowledgement.
See how much waiting is done by the server? What if it can execute other tasks during this time instead of being idle? Now, that would decrease the overall time required to complete the process. Hence, in such scenarios, we break from our pattern of executing the code line by line and use asynchronous functions to speed up the execution.
Another way of thinking about async is to understand what are blocking and non-blocking tasks. In the backend, when you are requesting some data from the database, the process running your program is waiting for the response from the database and sitting idle. In this case, it's blocking the execution of further code.
While certain languages like Javascript have AJAX calls by default async, in other languages like Python, you will have to make your requests async deliberately.
Some of the scenarios where you would consider doing async operation:
When a user wants to import a CSV of records and create profiles from the details given in it. Your server receives the file and acknowledges the user that you are processing the request, while in the background you create the records.
When a user wants details from multiple sources and you have to make HTTP requests to each one of them. You can create async operation for each of the source and execute the fetch operation in parallel.
How to do it?
Every language has its own way of doing it. While javascript has async/await keywords to do it, Java has threads and Python has both. What method works for you best in your scenario or language is something you need to explore. For e.g. if you are programming in python, you don't have to start coding async functions from scratch.
If you have to delegate function execution to another process you can take a look at the Celery project. This tool is incredible and everyone who has started working with python on scale should learn how to use this
If you have to make multiple requests asynchronously, you can take a look at grequests
If you want to dive deep into how to write asynchronous programs from scratch you can check out this tutorial by Tech with Tim - Video
I am not getting into the discussion of concurrency and parallelism right now. That's a topic for another issue of this newsletter. Stay tuned!
When it's not a good idea?
Asynchronous programming is not suitable if your tasks are CPU intensive, since async programming leverages the idle time of CPU when it's performing I/O operations. So if you are using a deep learning model to analyse an image, it wouldn't really do much to scale your application.
Takeaway
Get comfortable with code which doesn't execute linearly. It's gonna take some effort but it's also going to help you make insanely scalable applications.
Connect with me on Twitter
Superbly written! Thanks for your inputs. :D
Would you prefer to use Node JS instead?