Why I Switched from Python to Go Lang for AI Deployment

Code In a Jiffy · Intermediate ·📊 Data Analytics & Business Intelligence ·2y ago

Key Takeaways

The video discusses the author's experience deploying an AI project with both Python and Go, highlighting the challenges and performance differences between the two languages, with a focus on response times, scalability, and resource management.

Full Transcript

I deployed the same AI project with go and python this meant developing the same project twice once in each language why you ask because bugs will only scare you once and we like to keep our coding CER stories bilingual the project was a recommendation Engine with multi-arm Bandits that was responsible for personalizing the whole Discovery screen for hundreds of thousands of users the implementation needed to be fast scalable and efficient in resources so deploying in a language that can achieve that is key with though go is famous for its speed and efficiency it wasn't as straightforward as it seemed you see I had this recommendation engine working in python as a chrone job for some time now and it resulted in a noticeable increase in Revenue however we wanted to make this recommendation engine more Dynamic responsive and have them update every time the user refreshed their screens I went with using the fast API library to create my serving API in Python most of the code was written in pandas and numpy two highly efficient libraries written in C the last function was the only one without pandas and numpy using a for Loop to D duplicate elements rearrange items and handle promotional requests despite trying to make it faster it took up to 60% of the response time but that wasn't the big issue as the response was around 400 milliseconds locally the issue came and we tried to deploy on 5% of users the response time was not as expected and it kept increasing till it reached 5 Seconds we had to skate the number of pods in kubernetes up to 46 just for the small percentage of users this small percentage had a lot of concurrent requests per second but that meant that to fully deploy to all users we needed over 920 pods when I brought this up with the devops team they made me feel like this $1 100 billion the next day was the start of the weekend and I decided to take a break psych I didn't I knew needed to find a way to handle this fast as this project might be postponed till further notice because it's not impactful enough and I really wanted to dump my spaghetti code for this CR jop aside since Python's implementation had little room for improvement I decided to switch to goang I had seen a team successfully make this transition at my old company and our backend team already uses go daily my goal was to learn go as quickly as possible and then rework some parts of the recommendation engine this would allow me to report back on Monday with the performance difference between go and Python and get everyone on board using go on this project I started by getting a taste of what goang can do so I created a function to sum up the first n numbers in Python and go and found that go was a whopping 100 times faster this was reassuring so I started learning the basics of goang from a fast YouTube video as quickly as possible then I jumped straightforward to developing the recommendation engine in the process I relied on chat GPT to help me write some code translate code from python to go and even explain a couple of Concepts to me it wasn't always producing the correct code but it sure helped me move faster along the way I used trucks and go interfaces to structure my code and it came naturally since I used design by contract in a lot of my data science code I also found a numpy alternative called go num and a panda's alternative called go and go data frame I was happy to find all theun functions I needed were available in those Cod packages I used group bu merge and aggregate functions every function I searched for was supported I kept working on the project until our Monday meeting but I hadn't timed or tested the code yet during our meeting I suggested trying out go Lang but there were concerns about the time it would take to redo it in go and the availability of all python data science libraries in goang a senior backend engineer also mentioned that most of the Python code is already optimized in pandas and numpy and that we won't see much difference between the two languages he was partly right but more on that later in the end I convinced him to see the run times of the go code I already have and decide from there after the meeting I compared Python's runtime with go and found that go was 30 times slower wait what the backend engineer was right pandas was highly optimized and its go counter part was so much slower surprisingly what took Panda's 9 milliseconds to process to go data frame more than 330 milliseconds but what if I ditched data frames and merged and aggregated data by using stroks maps and Loops I mean the experimental summation function was 100 times faster than python so it might reach Panda's performance I restructured the data into a map of structs using the merging column s key I also implemented the merging and aggregation functions by hand but it was worth it at the end when I saw the code that I've written was three times faster than pandas and go overall runtime was 10 times faster than pythons we kicked off G version for 5% of users using the same 46 pods and it ran smoothly scaling up to 10% then 50% then eventually 100% of users on the same 46 pods showed it would handle the load faithlessly go not only showed Superior response times but also exhibited better control over resource management and concurrency crucial elements for the project scalability however it's essential to note that while go excelled in these aspects it doesn't Encompass all the data science libraries we might need and some libraries might exist in go like go data frame but are not efficient as Python's implementations moreover working with gold demanded on more code and in development I would move away from python only if it fails to handle the amount of traffic being thrown at it I hope you enjoyed this video and have a wonderful day

Original Description

In this video, I share my journey of deploying the same AI project with both Go and Python, highlighting the challenges, successes, and performance differences. 🤔 Takeaways: * Go excels in response times, scalability, and resource management. * Some data science libraries might be less efficient in Go compared to Python. * Consider switching to Go for performance gains when Python hits traffic limits. 🚨 Important Note: While Go may not cover all data science libraries, its strengths shine in specific scenarios. Explore the performance gains and make a strategic decision based on your project's requirements.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

The video teaches the importance of considering performance and scalability when deploying AI projects, and how Go can be a viable alternative to Python for certain use cases. The author shares their experience deploying a recommendation engine with both languages, highlighting the challenges and successes.

Key Takeaways
  1. Develop a recommendation engine with multi-arm bandits
  2. Deploy the engine with Python and measure response times
  3. Switch to Go and rework the engine
  4. Compare performance between Python and Go
  5. Optimize the Go implementation for better performance
💡 Go can offer superior response times and better control over resource management and concurrency, but may require more code and development effort, and not all data science libraries are available or efficient in Go.

Related Reads

📰
Govern a Country, Govern Your Data: The Same Failures
Learn how data governance can be understood through the lens of governing a country, highlighting the importance of effective management and decision-making
Medium · Data Science
📰
Gara-Gara Satu Rumus Excel, Cara Saya Kerja di Kantor Berubah Total
Learn how one Excel formula changed the author's work life and discover the power of data science in transforming workflows
Medium · Data Science
📰
Epistract v3
Learn to tackle a lesser-known knowledge graph problem with Epistract v3 and improve your data science skills
Medium · Data Science
📰
Direct Lake on OneLake Just Hit GA. Here’s When It Replaces Import Mode, and When It Doesn’t.
Learn when to use Direct Lake on OneLake, a new GA feature that combines DirectQuery's freshness with Import's performance, and how it impacts data science workflows
Medium · Data Science
Up next
90% of Excel Users Don't know This Trick Even Exists
Data Sensei
Watch →