Hey Guys, a warm welcome to tech-maze, in today's article we will be exploring how an online code runner works.
For the sake of simplicity and to be more precise we will focus on basic flow and necessary component which has used to provide online code submission and running platform to the users, rest of the features such as metrics, average score, pass percentage, caching etc., we will skip as these are add-on's on top of the main system.
Following scenario will be the main focus of this article, this information can give a broad view how a code runner web app works in real time.
1. how a user submit an answer and get the result
2. how code runner platform process each submission internally.
Lets begin with simple design
Design 1 -->
In this design, user submit a programs to server and server process the file, it execute few test cases (provided test cases in a file stored in DB/cache) against the submitted solution and compare the output with one of the file (stored in DB/cache), lets name it as programOutput file
if it matches the output then result saves in DB as passed(accepted) else save as failed (rejected).
When user click on result button on the web page, it fires an api request which goes through microservice to DB internally and fetch the result of that particular user and display on the UI.
This design looks good but there is a problem, since its happening in so real time and in synchronous way, there will be many chances of rejecting the request when application overwhelmed by number of request, web application will start experiencing slowness and later it will be unresponsive.
Following design can be used to overcome from design 1 problem
Design 2 -->
here we have to go in asynchronous way, so when user submit the solution, save his/her file to separate database (better to use cache memory, we can use Redis or Gemfire which is in-memory data grid, fast and fault tolerant) and save an event in message queue, at the same time inform user with a message stating that "your solution has submitted successfully".
save event in message queue through the server using publisher-subscriber messaging service.
server publish the event with details ( Event - userId, linkToFIle, name_of_file etc.)
and at the other side server picks the event from front of the queue, after fetching the event, fetch the file from database based on the link provided by the event object and run the test cases to verify the solution, later compare the result with the programOutput file, if it matches then save the result as passed (accepted) else save as failed (rejected).
This approach seems legitimate and working, but here too user can face a problem, what if a user submit malicious code as a part of solution, and when server run that code, system may crash, may unresponsive or system starts running in infinite loop and end up with out of memory.
This will make application unavailable to rest of the user.
Following design can be used to overcome from design 2 problem
Design 3 -->
Here we can use, container based technology which can provide multiple pods, running in worker nodes, and these pods contain our application which runs the user code, in this case even if it crashes, it wont affect the actual system as its a small chunk or a unit of worker node, all other instances will still be running other users code hence no downtime, or system crash.
If someone doesn't know Kubernetes then, its good to explore this technology as 70 % of the organization currently using this and in future Kubernetes will be coming with new features which can enhance the performance of cloud based application. meanwhile for this article, just think there is a bucket which provides multiple instances of an environment to run users code, if one instance fail, other keeps running.
In this way we can overcome from design 2 concern.
Finally, we have developed a system which is robust and does the thing pretty well.
Please let me know what you guys think in comment section, feel free to share your suggestion/thoughts.
That's all folks in this article, hope this will help at some extent to understand how online code runner platform works and this also gave a glimpse of how to begin with designing a system from scratch.
A Big thank you for checking out my article, this really encourage me to come up with more topics.
Here are some resources you might be interested ------------------------
Comments