9 ms·
Pushing Nginx to its limit with Lua
- zrail 14y agoI used OpenResty as the core for an experimental HTTP routing service at my previous job. The idea was to maintain host:port pairs for HTTP services in Redis and use a small bit of Lua code to look up the right place to proxy a virtual host to. It worked well enough, but never got deployed because we were concerned about the Redis SPOF. Now-a-days it wouldn't be that big of a deal. That said, the nginx/lua combo is wonderful to work with. I got the core of that thing working in just a few hours.
- moonboots 14y agoI'm using nginx+lua as the backend to http://typing.io http://typing.io, and I've found the combination to be a fast and robust alternative to more full featured web stacks like Rails.
- smegel 14y agoBut what happens when people put big numbers in my Fibonacci calculator?
- cheald 14y agoLua implements TCO, so it'll do just fine. :P
- dbaupp 14y agoIf we are being pedantic, the naive Fibonacci implementation isn't tail recursive. (And any tail recursive implementation is unlikely to need TCO before the numbers get too large.)
- InclinedPlane 14y agoSorry friend, it's time to be SUPER pedantic. Silly Computer Science nerds spend a lot of time trying to figure out the most efficient implementation of a recursive Fibonacci number function. "Ooo, let's make sure to take advantage of tail-call optimization, oh, and memoization, aren't we fancy?" Meanwhile, mathematicians look at the problem and then cock their heads to the side and say "uh, guys, that's way too much trouble, just use the closed form solution and calculate any value in constant time using a tiny number of floating point operations". Because Fib(n) = (phi^n - (1-phi)^n)/sqrt(5), where phi is the golden ratio.
- ufo 14y agoAnd then the paranoid Computer Science nerd gets freaks out because of is immortal fear of floating point errors and uses the alternate fast matrix exponentiation implementation that takes just in log(n) time.
- dbaupp 14y agoNo, if there's any computations happening, that's engineers and physicists, especially if there are approximations happening. A mathematician looks at the recurrence formula and generalises it (e.g. f(n) = a f(n-1) + b f(n-2) or f(n) = f(n-1)*f(n-2) (solve this one, hint: xkcd) or ...) and investigates its properties. The actual values of the numbers are not of much interest. And a^n can only be computed in constant time if one uses exp(n log(a)), and (I think) that the scale of the numbers can have a large impact on the accuracy of the result, and so for large n, one needs more operations within both exp and log to give the same (relative) accuracy.
- stcredzero 14y agoIt would seem that this approach gives all the advantages of async with none of the drawbacks involved with writing callbacks. If someone added a type annotation extension to the language and parser, this would be a killer combination for large code bases. Lua is also a good match for "The Good Parts" of Javascript, so it should be possible to have all the code in one language.
- kyrra 14y agoLua was never designed to be used for really large code basis. It's a lightweight scripting language on top of C. The entire runtime is written in 20k lines of C. It also is designed to be easily incorporated in C programs. Nginx is a popular server case, while World Of Warcraft is another popular us. It's really a pretty amazing language, when you embed it you can choose exact what functionality is exposed to the scripts you run in it, so it's pretty good for sandboxes type code (though, imposing memory restrictions is much harder).
- stcredzero 14y agoLua was never designed to be used for really large code basis. Bases. That doesn't mean that it wouldn't be good for large code bases. Many languages end up getting used for purposes they weren't designed for.
- raylu 14y agoJavascript was never really designed, but look how that ended up.
- _dps 14y ago> Lua was never designed to be used for really large code basis I'd certainly agree that "maintainable for large code bases" was probably never on Ierusalimschy & Co's language design requirements document, but you could say the same of same of Perl, Python, Ruby, Javascript, and most Lisps. None of those languages have any additional mechanisms traditionally associated with enforcing uniformity and consistency across large projects. This hasn't stopped people from building large successful projects using these languages. As for your comment on the implementation size: I'm not sure which way you meant it (complimentary or pejorative) but I often find people react to this in exactly the opposite fashion I would. I see "self-ish/JS-ish/python-ish semantics in 20kloc of ANSI C? Sign me up!". I find some people see it and assume it to be a toy. It reminds me of the Bill Gates line about (paraphrasing) "measuring software's success by lines of code is like measuring an aircraft's success by its weight".
- qnk 14y agoI'm really glad to see a Cloudflare post at the top of Hacker News, I've been following them and they have lots of interesting things to say. I switched from Amazon Cloudfront to Cloudflare two months ago and can't be happier. My hosting bill was reduced a 95% and my website speed decreased only a 13%, a figure my weekend project can afford to make it profitable.
- eastdakota 14y agoDon't like that one bit. No reason your speed should decrease at all. That's quite different from other people's experience comparing CloudFlare to CloudFront. If you have a second, submit a ticket and we'll make sure you've got everything setup for maximum performance.
- qnk 14y agoThanks for your concern Matthew, I'm not very worried about this. That 13% of difference in speed is most probably due to the fact that Amazon has a Cloudfront edge location in Madrid, the city most of my traffic comes from. I haven't really measured the speed from other locations but I will do it and file the ticket to let you know about it.
- eastdakota 14y agoThanks for the follow up. Madrid likely to be one of the next cities we expand to (although latency to Paris is <30ms). Stay tuned.
- saurik 14y agoWhat caused the reduction in your hosting bill? Was it Amazon's supposedly-poor cache hit ratio, or a CloudFlare-specific feature? Have you tried negotiating with a larger CDN?
- eastdakota 14y agoCloudFlare typically reduces bandwidth usage and load by about 70%. That can translate into cost savings if your host (e.g., AWS) charges you for bandwidth.
- jumby 14y agoWhy doesn't everyone try Apache Traffic Server with its LUA plugin which has way better performance and many more capabilities?
- tsewlliw 14y agoThat sounds interesting. Want to post your writeup of your experience with it?
- piotrSikora 14y ago> has way better performance Do you have any performance comparison? > many more capabilities As...? Care to compare?
- jumby 14y agoHow about honoring the RFC's as a first comparison as to capabilities. Example: nginx doesn't even honor Vary headers: http://trac.nginx.org/nginx/ticket/118 http://trac.nginx.org/nginx/ticket/118
- justincormack 14y ago(a) no evidence of better performance. Lua/Nginx is very fast too. (b) Its just a proxy, while Lua/Nginx/openresty is much more of a full web development environment. You can use it as a proxy, but it is not the main use case. (c) I don't think Traffic Server has more capabilities, as far as I can see, I think it has fewer as it is much more focus on being a proxy product, while Nginx is a full web server.
- hypnotist 14y agoI think Lua/nginx and ATS are quite different beasts, solving different problems. Nice presentation on ATS - "Apache Traffic Server: More Than Just a Proxy" https://www.usenix.org/conference/lisa11/apache-traffic-server-more-just-proxy https://www.usenix.org/conference/lisa11/apache-traffic-serv...
- mattdeboard 14y agoI've written an authorization server for CloudFront using Python/Flask and have wondered whether just writing the same functionality via Lua could be a better/faster replacement. I'm just not clear on how to use Lua to make the calls to our API. This is a good pointer in the right direction though.
- mtourne 14y agoNginx exposes multiple phase to process an incoming HTTP Request. Mainly rewrite, access, content, header_filter, and log You can embed Lua at the access phase, and query your API from there. Some of the Lua libraries of the OpenResty package [1] might help you do that. And here [2] you can find an example of OAuth support. [1] http://openresty.org/#Components http://openresty.org/#Components [2] http://seatgeek.com/blog/dev/oauth-support-for-nginx-with-lua http://seatgeek.com/blog/dev/oauth-support-for-nginx-with-lu...
- justincormack 14y agoHere is some example auth code for Amazon services in Lua for Nginx I wrote ages ago, which might help get an idea of what the code might look like https://gist.github.com/948423 https://gist.github.com/948423 It is very fast doing that type of stuff in Lua, and it all gets done in the same request context. There is some new functionality in openresty since that example, so there are probably other choices of ways to implement it.
- tferris 14y agoSounds amazing and would love to step in immediately. Have some basic questions after reading briefly the post and the comments here. 1) What's the difference between OpenResty and the Nginx+Lua module (is the module the core of OpenResty)? 2) How does it compare to Node regarding ecosystem, performance and every day usage/maintainability (if it's comparable)?
- jhancock 14y agoI've only been playing with openresty for a couple of months. Here's what I've learned: 1 - openresty is nginx and many official and unofficial modules. From what I've asked the experts, there is supposed to be no difference in the source; openresty is just a single package of many modules with an easy to use ./config and make process. 2 - The nginx module and lua lib ecosystem seems to have most of the basics in order to roll a high level app framework, but only a half baked one exists thus far: https://github.com/appwilldev/moochine https://github.com/appwilldev/moochine Also: https://github.com/antono/valum https://github.com/antono/valum https://github.com/pintsized/lua-resty-rack https://github.com/pintsized/lua-resty-rack My impression is if you have the time to roll your own sinatra-like framework, it should be pretty straightforward.
- tferris 14y agoThanks!
- decktech 14y agoI've been using Openresty (with Lua and Redis support) to collect a large amount of data from GET requests for a video sharing site. Having nginx push data directly into Redis is super fast, lends easily to real-time metrics, and makes it easy to batch everything up and push it into MySQL at the end of the day. Now that Redis supports internal Lua scripts, you can also do custom atomic functions and other neat things. If you can get around the Redis SPOF, OpenResty + Redis is great for large-volume data collection. Thousands of requests/sec on an EC2 Small at < 10% load.