Using nginx-clojure to build custom load balancing algorithm

348 views
Skip to first unread message

Manny Rodriguez

unread,
Nov 4, 2015, 7:33:36 AM11/4/15
to Nginx-Clojure
I am designing a fairly sophisticated fault tolerance model for my app that requires very customized load balancing algorithms. The TL;DR version is that nodes in the balancer pool will flicker between different types of health statuses (Stable, Unstable, Degraded, Offline, etc.) and the balancer algorithm needs to know how to route traffic to nodes based on their health status.

I am interested in using nginx for the balancer implementation, and since I am a JVM developer, I would greatly prefer to write this complicated load balancing algorithm in Groovy/Java rather than in C, Perl, Ruby, etc. I found the nginx-clojure module after doing some basic Google searches for Java-based nginx modules/plugins.

All I'm wondering is this:
  1. Does this nginx-clojure module allow me to write a Java/Groovy class (say MyComplexBalancerAlgorithm.groovy) and then configure nginx to use that algorithm for routing traffic?
  2. If so, what is the API/contract between my custom balancer algorithm class and what the module expects? Where is this contract documented/defined? In other words, I assume nginx-clojure would pass MyComplexBalancerAlgorithm something like a J2EE servlet request (javax.servlet.http.HttpServletRequest), and would expect back a descriptor of which node in the balancer pool to forward the request off to (?). So what is nginx-clojure passing my algorithm, what method would I need to implement (say, MyComplexBalancerAlgorithm#onRequest, etc.), and what is the return type expected?
  3. Are there any known performance caveats/pitfalls when working with this module?
Thanks in advance!

Best,
Manny

Yuexiang Zhang

unread,
Nov 4, 2015, 9:53:36 AM11/4/15
to Manny Rodriguez, Nginx-Clojure
Hi,

  1. Does this nginx-clojure module allow me to write a Java/Groovy class (say MyComplexBalancerAlgorithm.groovy) and then configure nginx to use that algorithm for routing traffic?

Yes. We can write a nginx rewrite handler

  1. If so, what is the API/contract between my custom balancer algorithm class and what the module expects? Where is this contract documented/defined? In other words, I assume nginx-clojure would pass MyComplexBalancerAlgorithm something like a J2EE servlet request (javax.servlet.http.HttpServletRequest), and would expect back a descriptor of which node in the balancer pool to forward the request off to (?). So what is nginx-clojure passing my algorithm, what method would I need to implement (say, MyComplexBalancerAlgorithm#onRequest, etc.), and what is the return type expected?
Please see 2.5.2 Simple Dynamic Balancer By Nginx rewrite handler in the previous link. 
Furthermore this link explains what the request map contains.
  1. Are there any known performance caveats/pitfalls when working with this module?
Because in this case most of work will be done by nginx build-in http proxy module  and nginx-clojure is also carefully designed for performance so I don't think there 's any performance issue.
Furthermore if our rewrite handler want to access external services such as tcp/http services please also take a look at this doc about selecting Coroutine based Socket, Asynchronous Socket/Channel Or Thread Pool for slow I/O operations .

Regards.
Xfeep
 

--
You received this message because you are subscribed to the Google Groups "Nginx-Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nginx-clojur...@googlegroups.com.
To post to this group, send email to nginx-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nginx-clojure/d16a2e24-b2ed-40ca-b73a-399024749920%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manny Rodriguez

unread,
Nov 4, 2015, 9:59:08 AM11/4/15
to Nginx-Clojure, mannyro...@gmail.com
Awesome, thanks @Xfeep!

Quick followup question: in that link there is the Java source file 'MyRewriteProxyPassHandler.java'. I assume that I would compile and package this class into a JAR (say, 'complex-loadbalancer.jar') and then deploy the JAR to all the nodes where nginx is running, and that I would then instruct nginx or nginx-clojure to look for this JAR somewhere on the local file system, yes?

Thanks again so much!

Yuexiang Zhang

unread,
Nov 4, 2015, 10:30:33 AM11/4/15
to Manny Rodriguez, Nginx-Clojure
Hi, you're welcome.

Please take a look at this link 2.1 JVM Path , Class Path & Other JVM Options 

Regards.
Xfeep

Yuexiang Zhang

unread,
Nov 4, 2015, 10:34:53 AM11/4/15
to Manny Rodriguez, Nginx-Clojure
BTW, for easy dev/debug with nginx-clojure, please try nginx-clojure-embed in dev enviroments, so you can test/debug your handler just like the plain java program, e.g. set breakpoints in your favor IDE or do some unit tests. 

Regards.
Xfeep
Reply all
Reply to author
Forward
0 new messages