blog-image

Challenge - Building a LoadBalancer assisted by DeepSeek AI

  • dHENRY
  • 11/05/2025
  • (Reading time : 6 mn)

(**) Translated with www.DeepL.com/Translator

As a subscriber to Hacker News and overwhelmed by articles on artificial intelligence, particularly generative AI, I’m exploring the possibility of learning a new programming language, Rust, by developing a concrete application.

The project, a LoadBalancer :

  • Yaml configuration: frontend ⇨ acl ⇨ backend ⇨ servers
  • Frontends :
    • Listeners HTTP/HTTPS
    • websocket upgrade
  • Backends :
    • HTTP et HTTPS
  • Roundrobin distribution , only
  • Insertion of “X-Forwarded-For” header
  • API Rest for configuration changes
  • Configuration hot reload
  • Automatic certificate loading (format pem) for TLS resolution
  • Integrate a basic anti-bot system. The aim is not to develop a complete system, but to attempt an implementation at the heart of the LoadBalancer.

** Checked items are functional in the first commit.

That sounds very ambitious. I should point out that I’m no beginner in software development and that I have a good knowledge of infrastructures. This will have a major impact on what happens next.

Phase 1 : Learning Rust

To each his own, over a weekend I enrolled online at MIT and took a course on “Machine Learning”. This type of course offers many algorithms, with examples in Python, which I don’t know any more than Rust, but the most important thing is the examples with results. The first step is to reimplement the algorithms in Rust using a test-driven development approach. (testing with Rust is pure pleasure).

Phase 2: Choosing an AI

It will be DeepSeek. This AI has caused quite a stir and set the record straight on the stock market valuation of the AI ecosystem :)

On paper, as powerful as ChatGPT, free and doesn’t ask for your phone number. Regarding privacy, the company that owns DeepSeek, sees the VPN’s outgoing IP address and my “prompts” don’t include anything confidential. To avoid unwanted “copy and paste”, use constants in the code, so that the values are never “captured” by the AI.

Phase 3: Building the LoadBalancer

I start by asking for an explanation of how a LoadBalancer works. I usually work with Haproxy, so I’m sure to find similarities with this software in my reasoning. The answers are correct, with lots of diagrams (mermaid). The DeepSeek AI is very talkative.

Naively, I start with the presented code, since I don’t know Rust and more particularly its ecosystem of dependencies. I start with a first forwarder: HTTP listener to HTTP backend. Without too much effort, the result is amazing. I continue with an HTTP to HTTPS listener, again quite easy.

It gets more complicated, a listener HTTPS to HTTP or HTTPS, the Rust client brick (hyper) works for both. As for the listener, you have to implement the concept of TLS acceptance, everything goes haywire, the AI mixes code with version levels of non-compatible components. I get by with the component manufacturer documentation (not with the AI). Finding the syntax of implementing a REST service with “hyper” complex, the AI suggests I integrate the “rocket” framework. I always follow its advice and the cohabitation seems to work.

After about 36 hours of work, I’ve got a working system, with websocket upgrade, and a configuration that can be modified without rebooting. For the websocket tests, I asked the AI for a Node.js websocket mini-server, and the code is in the git project.

It’s amazing: as a Rust novice who obviously doesn’t understand all aspects of the language, the LoadBalancer works. Let’s move on to performance: deplorable, as you’d expect.

The code provided by the AI is based on version 0.14 of the hyper component, most certainly with a poor implementation of the “hyper” concept.

But from this point on, I have a perfect breakdown of the modules needed to build a LoadBalancer that meets my initial specifications.

Phase 4: Optimization

Optimization in this case consisted in removing all dependencies and choosing the fastest framework for parsing the HTTP protocol. Forgetting AI for a moment, I scoured the web for benchmarks, and found some. My choice remains “hyper”.

So I’m back with the latest version 1.6.0. The “Gap” is very large since 0.14. The DeepSeek AI apparently stopped at version “1.0.0”, even though it claims to know “1.6.0” by presenting me with methods that existed in “1.0.0” but were abandoned in “1.6.0”. In short, the migration took 4 man-days (I’m still a Rust beginner).

Performance

The results obtained are to be put into perspective when compared with Haproxy, a monster of the reverse proxy world in existence for over 20 years, which has in-house parsers and optimization parameters that have required a great deal of research.

I haven’t taken the time to set up test conditions similar to what can be found in this field, but with tools like “ab” and “wrk”, it’s about 20% less than with Haproxy, very suitable for my personal use and my beginnings with the Rust language.

Environment :

  • Haproxy installed on a “Raspberry Pi 4” type unit, with two network cards (incoming ⇨ backend)
  • My proxy installed on a “Rock64” type unit, with two network cards (incoming ⇨ backend)
  • Backend: Matrix messaging service (without websocket) installed in a Kubernetes cluster exposed NodePort.
  • TLS resolution performed by LoadBalancer (HTTPS ⇨ HTTP)

Conclusion

The aim of this exercise was to evaluate self-training in an AI-assisted programming language.

The “DeepSeek” AI is very chatty and very often gives good advice, but you should never stop at its first level of response. In other words, read the full answer.

When you’re as mediocre in UI design as I am, the AI excels at answering questions like: “I would like to get a simple HTML template for the error proxy response, such as Service not available, Gateway error, etc.”, I copied the HTML code as it was generated. It’s clean, responsive…

The drawbacks, in this case: AI constantly mixes up the code of different versions of the components to be manipulated, but gives a lead much more quickly than a traditional search engine.

My technique after a few days of using AI :

  • Search for the manufacturer’s documentation for all the components involved, open for each one in a different tab.
  • Ask the AI as specific questions as possible.
  • Draw an analogy between your answer and the examples in the source code of the Rust component you’re using.

Overall, it’s been an incredible experience, learning a new language, new concepts, in just a few days, and I definitely don’t consider myself an expert in Rust programming yet. There are still a lot of design errors in the code.

The use of AI reinforces these fundamental principles :

  • I design the desired architecture.
  • I read (a lot).
  • And I code using AI (with all its drawbacks), my editor is not connected to an AI (copy and paste from DeepSeek).

Confirmation : AI, lacking reasoning skills, is not ready to replace programmers.

Time spent : Spread over 2 weeks including 2 weekends (intense ~10-12h/day) + May 1 and 8 (~10-12h/day) + 2~3h after work over 7 days.

Start April 29, 2025 ⇨ May 11, 2025, I’ll let you do the math…

The code can be found here, I don’t have enough experience to start optimizing Rust code, but I’m going to persevere and finish implementing the initial specifications.

+++

(**) Translated with www.DeepL.com/Translator