by Garry Lowther
•
22 November 2023
This afternoon, I researched how to construct intrinsic load balancing within our existing production Web API to take advantage of multiple Web API servers on the TriSys Cloud Platform . I decided to use ChatGPT, given the extremely high profile of OpenAI's flagship service after the amazing events over these last few days. The dialogue between myself and ChatGPT was akin to me working with another ASP.Net Web API expert who knew more about specific topics than I did, however it was necessary for me to test the generated code and indeed suggest a fix to an issue which prevented the code working in a live environment. Me: In ASP.NET Web API, how can I route a request to another Web API server? ChatGPT: In ASP.NET Web API, routing a request from one Web API server to another is a task that typically involves creating a proxy or a forwarding mechanism within your API. This is useful in scenarios like load balancing, microservices architecture, or when integrating with external APIs. Here's a basic approach to achieve this: Me: With regards to Handling the Response and your advice: Once you receive the response from the target server, you can either directly return this response to the client or process/modify it as necessary. Can you supply .NET code which demonstrates this? ChatGPT: After numerous exchanges like this over 20 minutes, and much copy and pasting, I had working code running in the Visual Studio debugger which was remoted to allow my Postman session to make Web API requests. Eventually, we arrived at a working solution. Obviously I asked ChatGPT to summarise this work for this blog post: Title: Simplifying Load Balancing in Web Applications When building a robust web application, ensuring that it can handle multiple users smoothly is crucial. This is where 'load balancing' comes in - it's like having several doors to a shop, so customers don’t have to queue up at just one. In technical terms, we're distributing the user requests across multiple servers. In our journey, we encountered a few hurdles and solved them one by one. Here’s how we did it: 1. The Art of Forwarding Requests: Imagine you’re sending letters (user requests) to a friend (the server). But instead of one friend, you have several who can respond. We created a system where these letters are sent to any one of your friends who's available, ensuring no single friend is overwhelmed. 2. Addressing the Envelopes Correctly: We hit a snag - the letters were being sent, but they had the wrong return address, confusing the recipients. By adjusting the 'return address' on the letters (known as the Host header in web terms), we made sure each letter reached its destination correctly. 3. The Right Stamp for the Letter: Initially, we were putting every possible stamp (headers) on the letters, but not all were needed. So, we carefully selected only the necessary stamps, making sure the letters weren’t overloaded with unnecessary information. 4. Dealing with Misdirection: At one point, our letters were being sent correctly, but they seemed to vanish - like sending them to a house that doesn’t exist (HTTP 404 error). After double-checking the address and making sure our friends were ready to receive the letters, we fixed this issue. 5. Open Lines of Communication: Finally, we made sure that the way we send letters (synchronous vs. asynchronous communication) was efficient. This ensured that our system could handle many letters at once without getting bogged down. In essence, we fine-tuned the process of directing traffic to ensure a smooth experience for the users. It’s like ensuring a well-organized event where guests can move freely without any bottlenecks. This analogy should help non-technical readers understand the complexity behind load balancing in a web application in a more relatable way. Feel free to adjust the language or details to better suit your blog's style and audience!