4 ms·
Is there any reason in principle why this has to be done with Lua and not with other interpreted languages? Once you are writing interpreted code which reads r
by slurgfest 14y ago
Is there any reason in principle why this has to be done with Lua and not with other interpreted languages?
Once you are writing interpreted code which reads request data and interacts with Redis you are not that far from what a normal Ruby or Python web app does anyway.
- FraaJad 14y agoTwo reasons: 1. Lua is very easy to embed into C code. Python and Ruby, not even in the same ball park ... 2. This nginx+lua config is often implemented using LuaJIT. Luajit is the fastest dynamic language. it's speed is very close to Java's in many cases. So, very little performance penalty..
- slurgfest 14y agoAre there real numbers showing that the Python interpreter's performance would be unsuitable for this purpose? (How about pypy's, since JIT seems to be one of the keywords here?) Or has it been decided (as if a priori) that Lua is the only workable solution?
- ansible 14y agoIs Python unsuitable? No, it can probably be made to work. It is just a question of performance, and effort needed to integrate it. Will a Python implementation run slower and use more memory than a Lua one? Probably. what other requirements were you thinking of that would be helped with a Python implementation? You don't need to be afraid of Lua. It is a nice little language, kind of like a Scheme wrapped in a Python syntax.
- justincormack 14y ago1. Lua is designed to be embedded, it is much easier than trying to embed Ruby or Python into a web server. 2. It is a way off an app as it runs in the same process as the web server. 3. Lua is very small and very fast, so it is fine to use it just for doing tiny things like reading cookies, there is no real overhead (most people use LuaJIT so it is compiled anyway). Remember that the point of many of these examples is to offload from Ruby as it is too slow... Also, Lua is a very nice language, easy to learn (there is not much of it), and most of the functionality exposed is simply the built in functionality of nginx (async http), plus some libraries, so having the libraries that come with other languages is not that useful as you can't use anything blocking (like with Node) inside Nginx.
- slurgfest 14y agoI don't question that Lua is a nice language, but it seems to be repeatedly implied in this thread that it is not possible to do this with other languages, although there doesn't appear to be any evidence for that. Embedding Python in a web server is quite doable and has nothing to do with having libraries available. I'm really not seeing why everyone should switch to Lua for these tasks. That is quite different from saying that people who like Lua shouldn't use it.
- justincormack 14y agoThe libraries thing is just you dont need the libraries, which are the main advantage. Python is a 3MB binary, while Lua is 175k, for a start. This is for Nginx which is 960k. Embedding something three times the size of the host is not really an embed... Lua is embedded in all sorts of stuff, Nginx, Apache, Redis in web space for example. Python is a great language, but just not a good choice for that. But feel free to give a Python embed a go.
- transmit101 14y agoOur main goal is to avoid passing requests upstream to Rails, which has massive performance penalties (TCP/IP, probably multiple database hits, web servers written in Ruby). By keeping everything non-blocking and inside the Nginx event loop, and cutting the upstream out entirely, we are making a massive saving on each request. This definitely outweighs a small performance penalty incurred for using LuaJIT.
- slurgfest 14y agoI'm no Ruby expert, but I think the massive performance penalties of Rails are mostly not in the Ruby interpreter itself. Rails is a huge project and it does a lot of work. It is possible that Ruby is impracticably slow in itself, I guess. Keeping things non-blocking and inside the Nginx event loop doesn't specifically mean you have to use Lua, does it?
- transmit101 14y agoI'm not really sure what point you're trying to make. Are you saying that it's not desirable to reduce the load on your backend web servers? I'm neither a Lua expert nor an evangelist, but my clear impression is that Lua is much more suited to embedding in a web server than Ruby or Python. On the Lua website it is described as "a powerful, fast, lightweight, embeddable scripting language" - that's not a description I would ascribe to either Ruby or Python. I also know that Lua has a long history of being embedded in games, embedded systems and other servers like Redis. Perhaps somebody more experienced in Lua can help me out here though. In this case there seems to be good evidence that Lua is the right tool for the job. One minor point- in your first comment you talk about interpreted Lua code, but this is not accurate because Lua can be compiled down to byte code before being loaded into Nginx. Finally - "Embedding Python in a web server is quite doable". I would be interested to read a blog post about your experiments in this area ;)
- vidarh 14y agoI love Ruby. It is my favourite language by far. And I strongly believe that it is possible to do a Ruby implementation that is fast enough for most uses. But current implementations are slow. And there are a lot of things in the language that makes a fast implementation hard. For example the fact that class definitions are executable and can be re-opened at any time, including by passing a string to eval(). They combine to make it extremely hard to make method calls fast. And since pretty much everything is method calls in Ruby, that's not good. As an example, a Ruby implementation has to be able to deal with code that might overload the + method for integers, so even basic stuff like integer additions can't be statically inlined without precautions to handle cases like that. I'd say for what they're doing, with tiny little rewrites in Nginx, Lua might be better suited - the Lua implementation is known for being extremely fast.