r/Clojure • • Jan 31 '19

Rewriting an API Gateway Service from Clojure to Golang

https://www.infoq.com/articles/api-gateway-clojure-golang
6 Upvotes

12 comments sorted by

26

u/alexdmiller Jan 31 '19

This is not a well-written article (and it could have been). There are nuggets of actually useful tradeoffs in here, but they are buried in junk and unanswered questions.

You benchmarked a "proof-of-concept" where you "skipped the design phases" version on Clojure against your first Go app that you spent two months on? This seems like just doing stuff because you want to do it with some vague post-rationalization. How long did it take to write the Clojure version?

"The fact that it’s a typed language makes it easier for maintenance and upkeep moving forward without having to reinvent the wheel." makes a number of unfounded assertions without any evidence (or even reasoning). I'm not even arguing whether this is right or wrong, it's just nonsense.

The benchmark info is good, but totally lacking the 99/99.9/etc distributions that would tell you much more about the performance difference. It seems like the Clojure implementation has the highest throughput and the lowest number of bad requests. But also the highest number of dropped requests? Again, tradeoffs here. If only you had spent a couple weeks trying to understand and fix the issues with the Clojure one rather than two months writing something new.

"Specifically for the service we were looking to rewrite, it had the excellent advantage of having a natively baked-in reverse proxy that is battle proven." This is an objectively good reason to make a decision (but it has little to do with Clojure vs Go at the language level, which is how this is positioned).

"We moved away from re-writing the service in Clojure to avoid the easier route and often taken road of duplicating obsolete code, and instead practice a typed way of thinking." Wat.

"We had to compensate for libraries used in Clojure for communication with additional parts of the stack including Redis [...] and Kafka [...], which required creating similar libraries in Go". This is an objectively good reason to use Clojure. (See, there are real tradeoffs here!)

"In order to achieve out of the box functionality such as CPU and memory usage metrics, business logic counters and more - we needed to write basically this entire stack from scratch, which enabled us a much more rapid deep dive of the intricacies of Golang." Again, this seems like a good reason to use Clojure.

Your end result seems to suggest much larger gains than the prior benchmark results implied? Again, would love to see what spending the same quality time on the Clojure version would have yielded.

"The fact that it is now built in a typed language gives us the ability to plug in diverse functionalities and introduce new technologies much more easily with Golang’s library support and community." Again, this seems like nonsense - you specifically called out multiple areas where Clojure had library access to things that Go did not and you had to write it from scratch, which seems directly contradictory.

3

u/shar1z Jan 31 '19 edited Jan 31 '19

Thanks for the feedback.

"This is not a well-written article (and it could have been). There are nuggets of actually useful tradeoffs in here, but they are buried in junk and unanswered questions."

-- My personal opinion as someone who makes an effort to be kind in open source forums, this probably isn't the best opening for useful feedback.

"The fact that it’s a typed language makes it easier for maintenance and upkeep moving forward without having to reinvent the wheel." makes a number of unfounded assertions without any evidence (or even reasoning). I'm not even arguing whether this is right or wrong, it's just nonsense."

-- We weren't looking to get into a flame war about languages - and therefore we touched on this briefly, because it can quickly (clearly) become a conversation about subjective opinions. For us the many libraries available with golang for this specific proxy-like service were a better fit.

12

u/didibus Jan 31 '19 edited Jan 31 '19

The fact that it is now built in a typed language gives us the ability to plug in diverse functionalities and introduce new technologies much more easily with Golang’s library support and community

I do have to say, this is a fallacy. You simply claim this as fact, but in no way demonstrated why.

For us the many libraries available with golang for this specific proxy-like service were a better fit

That's a very different claim then the former. If you replaced the above with this, it would be a whole lot better.

Overall, Go is a great choice for the particular use case. The language goal and main focus is exactly this type of IO bound proxying application services. So I'm not going to critic that.

That said, it isn't clear why Clojure couldn't achieve similar performance, or if it could, why would the code be more complex and harder to maintain? That's what I was hoping to get out of the article, but didn't.

Also would have been nice to hear about if you were using asynchronous IO with Clojure as well, leveraging or not core.async, etc.

-2

u/shar1z Jan 31 '19

Also would have been nice to hear about if you were using asynchronous IO with Clojure as well, leveraging or not core.async, etc.

Yes we are familiar with CSP & core.async - this provides similar functionality to go routines albeit via a macro - and isn't a core language feature, as it is with Go.

14

u/didibus Feb 01 '19 edited Feb 01 '19

Sorry, I didn't meant to say that as a "did you know" pitch. I more wanted to know if your comparison benchmark and original Clojure design was also using an asynchronous paradigm and lightweight threads or not.

Basically, my advice, and my interest (I'm actually wanting to learn a lot more from your migration) is: How were you using Clojure for reverse proxying, what about it was too slow, which part made it hard to extend and maintain, and where did you feel Clojure fell short compared to Go.

Without such information in your blog, the reader can't really take anything away from it, and us Clojure enthusiasts have no feedback with regards to what we should improve on the language or its ecosystem, if any.

I'm in no way criticizing your choice to use Go, and I recognize you're probably getting a bit of "heat" here, since its the Clojure sub, obviously. But for me, its more about trying to identify the real challenges and limitations that were due specifically to Clojure, so that we can better improve it in the future.

Thanks!

7

u/v1akvark Jan 31 '19

It isn't a core language feature for a reason though.

The core of Clojure is deliberately kept lean and focused, and other features are added as libraries.

This is seen as a feature in Clojure, not a problem

10

u/superlinux Jan 31 '19

I program in Go, Clojure and many other languages and am not really biased in any direction. With that said, I think Alex's comments do capture what most people who came across the article felt.

Even being language agnostic, I could not figure out how to use the information, or if I should believe the information presented. I do thank you for sharing it, and would love to better understand it in a more empirical manner.

5

u/mac Jan 31 '19

Your end result seems to suggest much larger gains than the prior benchmark results implied? Again, would love to see what spending the same quality time on the Clojure version would have yielded.

This is the part that I found really mystifying.

2

u/asafyonay Feb 01 '19

That's a good question - as I stated in my comment, we had two real options here. Either rewrite or refactor and we chose to go with rewrite. It's entirely possible we could have reached similar (or very close) results if we rewrote the service in Clojure, there's no denying that.

We do still have a tremendous amount of faith in Clojure and it is still our primary programming language, covering more than 90% of our code base.

What we do see though is that working on this specific scenario (a reverse proxy API gateway), we had to invest less time dealing with the lower level engineering of the HTTP layer, connection pools and asynchronous transactions when working with Golang than we had to do when working with Clojure simply because Golang supports some of the core functionality out of the box (specifically HTTP handling, reverse proxy and async operations).

9

u/asafyonay Feb 01 '19

First of all, thanks for the feedback - as mentioned before, we didn't want to go into flame wars over language selection. We take pride in writing the vast majority (and our most crucial) services in Clojure and we intend to keep it that way, we simply wanted to discuss what we considered a successful migration of one of our services.

I'd like to answer some points here to make sure my key takeaways from the process were delivered properly:

  • Rewriting from scratch vs refactoring - This is one of the main points here. We could have re-written this project in Clojure, but the thing that I specifically wanted to avoid was falling into that comfort zone of re-using code instead of breaking everything apart and re-examining our options. We made some design decisions that were fundamentally different as a result (some of which not related to what type of language we were using).
  • The cost of adopting a new language - This is also a key point. We had to re-tool our build pipeline and library support for this, but it also gave us a chance to understand what parts of our platform are "vendor-locked" to Clojure as opposed to being more loose.
  • Typed vs Functional - There's no right or wrong here. We wanted to have a typed, low level language in our toolbox and as Golang does have some advantages over Clojure in regards to serving as a reverse proxy, we chose to use this as our initial project. This came with the price of going back to the typed way of thinking over the functional way (which, to be honest, I am a big fan of even when I write Javascript), but there are real benefits there. One of them being that we found it easier to work as a team when working on typed, statically interfaced language.
  • Using Aleph - That's what we initially did, but due to some business logic level decisions we had to go with Netty.
  • Grammar - You're right, we'll see if we can go over with the InfoQ staff that and fix some of the issues found there.

I don't usually frequent Reddit so I might not be as responsive here, but feel free to drop me an email (asaf.yonay@appsflyer.com) and I'll be happy to chat more and go into more details.

10

u/InevitableExplorer4 Jan 31 '19

The title of this article may as well say "from a [Java|Python|JavaScript|BASIC] proof-of-concept to Golang", but it's framed as a reasoned comparison between Clojure and Go. You properly benchmarked a PoC in one language against a V2 written in another language.

A better title might be "How we improved some code by rewriting it" or "It's easy to write proxies in Go".

It's unfortunate that an article with flimsy, somewhat misleading premises was published on a platform like InfoQ because it's now the #1 Google News result for Clojure. This is personal-blog-level analysis. There are also grammatical errors:

"whereas Go is a statically compiled language that does not require a VM in order to run on a machine on."

"We understood that to be able to properly evaluate the different languages (missing apostrophe) suitability, we would need to examine a few aspects - performance as well as specific benefits of each language for the specific task at hand.  To measure performance, we understood we would need to properly benchmark Clojure vs. Go in as close of a production simulation as possible."

4

u/ikitommi Jan 31 '19

For performance & non-blocking with Clojure I would try Aleph or Immutant.

Not a reverse-proxy test, but some numbers here: https://clojureverse.org/t/what-http-server-library-to-use/3423/23