Using LuaJIT in mid-load web-projects
Alexander Gladysh
CTO, LogicEditor
On Lua and LuaJIT
Lua:
• powerful,
• fast,
• lightweight,
• extensible,
• embeddable
scrip ng programming language.
Language
On Lua and LuaJIT
• Origins.
• Popularity growth.
• Where is Lua used?
• LuaRocks.
Popular Dialects
On Lua and LuaJIT
• Lua 5.1 vs. Lua 5.2,
• LuaJIT 2.0,
• Metalua.
LuaJIT 2.0
On Lua and LuaJIT
• JIT, FFI, performance.
• Limita ons on x86 64.
• LuaJIT vs. Lua 5.2.
Why Lua?
On Lua and LuaJIT
Historically: We're coming from computer games industry, where Lua "rules the
world".
Pragma cally:
• Works fast!
• Pleasant to code!
• Easy to learn!
Where to get programmers?
On Lua and LuaJIT
Reeduca on.
Main problems learning Lua
On Lua and LuaJIT
• Mindless nkering with the language.
• NIH-syndrome. Lua is to easy to nker with.
• Diverging from the mainstream. Costs and benefits.
• Language idiosyncrasies:
• Global variables by default.
• Arrays are indexed from 1.
• Size of array with nil element is not defined.
• Everything that is not nil or false — true (0 too).
Most important!
On Lua and LuaJIT
When you code in Lua —	code in Lua!
Lua / LuaJIT place in your stack
On Lua and LuaJIT
First and foremost:
• User-configurable business-logic.
• Code that would otherwise be wri en in C/C++/OCaml.
Frameworks to build webservices with Lua
Our stack
Some of the popular ones:
• Kepler/WSAPI
• OpenResty
• Luvit
We have a "bicycle", built on WSAPI.
What web-problems are we solving with Lua?
Our stack
• Browser and social games.
• Ad networks.
• Other web-services, mobile games etc.
Hardware
Our stack
• Linode
• Hetzner EX6
OS
Our stack
• Xen XCP on top of Ubuntu Sever.
• domU on Ubuntu Server:
• HTTP frontends (nginx).
• HTTP backends (32-bit).
• Workers (32-bit).
• DB (Redis, MySQL).
• Aux (Bind, nginx-based config-server, deployment, monitoring etc.).
Backends
Our stack
• nginx
• spawn-fcgi + mul watch
• LuaJIT 2.0
• FCGI/WSAPI
• Applica on code
Workers
Our stack
• runit
• LuaJIT 2.0
• Applica on code
IPC
Our stack
• ØMQ
• Tasks:
• Replacement for broken signals.
• In-process cache reset.
• (Workers get tasks via Redis.)
System tuning
Our stack
• OS
• bit.ly/kernel-magic (for frontends and backends)
• Redis
• I/O Scheduler: noop on guests, deadline on host
• nginx
• worker rlimit nofile
"DevOps"
Our stack
• Deployment
• High Availability
• Monitoring
Main libraries
Our stack
• lua-nucleo, lua-aplicado (be er — Penlight, telescope)
• slnunicode
• luatexts, luajson
• luasocket, luaposix (be er — ljsyscall)
• WSAPI
• lua-zmq
• lua-hiredis (be er — ljffi-hiredis)
• luasql-mysql
DSL and code genera on
Our stack
• HTTP request handlers.
• Code (par ally sta cally validated).
• Docs.
• (Planned) Smoke-tests.
• SQL schema.
• "ORM" wrapper code.
• DB schema patches.
• Auto-backoffice.
• Docs.
• bit.ly/lua-dsl-talk
• Common parts of a project are generated from text templates.
Performance
Our stack
• About 160M synthe c hits per day per EX6-class server in ad networks.
• About 8K simultaneous ac ve users per EX6-class server in online games.
What did we encounter?
Pi alls
In main:
• Couple "mysterious" problems, due to bugs in early LuaJIT2 betas (all fixed by
now).
• Problems, caused by two versions of the same LuaRocks package installed in
the system.
• Exploding Redis.
• Lousy Hetzner HDD reliability.
Diagnos cs, debugging and monitoring
Pi alls
• Par al sta c code valida on.
• Run me valida on.
• Autotests.
• GC tuning and monitoring.
• Monitoring for request mes, memory usage etc.
• Debugging with logs.
Main unsolved problems
Pi alls
• Long polling / Comet.
• OS signal handling.
• More efficient CPU usage with HTTP handlers.
• LuaRocks:
• Can install two versions of the same rock in the system.
• Can't upgrade a package.
Next genera on stack
• Unblocking API with corou nes, no callback. Get inspired by, or adapt
OpenResty.
• Complete transi on to LuaJIT FFI.
• Consider dropping LuaRocks.
• Drop FCGI, move to epoll and lua-h p-parser.
• Simplify the architecture as much as possible. Drop the configura on server.
More code genera on!
• New DSL design.
Want to know more?
Official Site lua.org, luajit.org
Wiki lua-users.org/wiki, wiki.luajit.org
Mailing Lists lua.org/lua-l.html, luajit.org/list.html
StackOverflow stackoverflow.com/ques ons/tagged/Lua
IRC #lua at irc.freenode.net
Ques ons?
@agladysh
ag@logiceditor.com
meetup.com/Lua-in-Moscow

Using LuaJIT in mid-load web-projects

  • 1.
    Using LuaJIT inmid-load web-projects Alexander Gladysh CTO, LogicEditor
  • 3.
    On Lua andLuaJIT Lua: • powerful, • fast, • lightweight, • extensible, • embeddable scrip ng programming language.
  • 4.
    Language On Lua andLuaJIT • Origins. • Popularity growth. • Where is Lua used? • LuaRocks.
  • 5.
    Popular Dialects On Luaand LuaJIT • Lua 5.1 vs. Lua 5.2, • LuaJIT 2.0, • Metalua.
  • 6.
    LuaJIT 2.0 On Luaand LuaJIT • JIT, FFI, performance. • Limita ons on x86 64. • LuaJIT vs. Lua 5.2.
  • 7.
    Why Lua? On Luaand LuaJIT Historically: We're coming from computer games industry, where Lua "rules the world". Pragma cally: • Works fast! • Pleasant to code! • Easy to learn!
  • 8.
    Where to getprogrammers? On Lua and LuaJIT Reeduca on.
  • 9.
    Main problems learningLua On Lua and LuaJIT • Mindless nkering with the language. • NIH-syndrome. Lua is to easy to nker with. • Diverging from the mainstream. Costs and benefits. • Language idiosyncrasies: • Global variables by default. • Arrays are indexed from 1. • Size of array with nil element is not defined. • Everything that is not nil or false — true (0 too).
  • 10.
    Most important! On Luaand LuaJIT When you code in Lua — code in Lua!
  • 11.
    Lua / LuaJITplace in your stack On Lua and LuaJIT First and foremost: • User-configurable business-logic. • Code that would otherwise be wri en in C/C++/OCaml.
  • 12.
    Frameworks to buildwebservices with Lua Our stack Some of the popular ones: • Kepler/WSAPI • OpenResty • Luvit We have a "bicycle", built on WSAPI.
  • 13.
    What web-problems arewe solving with Lua? Our stack • Browser and social games. • Ad networks. • Other web-services, mobile games etc.
  • 14.
  • 15.
    OS Our stack • XenXCP on top of Ubuntu Sever. • domU on Ubuntu Server: • HTTP frontends (nginx). • HTTP backends (32-bit). • Workers (32-bit). • DB (Redis, MySQL). • Aux (Bind, nginx-based config-server, deployment, monitoring etc.).
  • 16.
    Backends Our stack • nginx •spawn-fcgi + mul watch • LuaJIT 2.0 • FCGI/WSAPI • Applica on code
  • 17.
    Workers Our stack • runit •LuaJIT 2.0 • Applica on code
  • 18.
    IPC Our stack • ØMQ •Tasks: • Replacement for broken signals. • In-process cache reset. • (Workers get tasks via Redis.)
  • 19.
    System tuning Our stack •OS • bit.ly/kernel-magic (for frontends and backends) • Redis • I/O Scheduler: noop on guests, deadline on host • nginx • worker rlimit nofile
  • 20.
    "DevOps" Our stack • Deployment •High Availability • Monitoring
  • 21.
    Main libraries Our stack •lua-nucleo, lua-aplicado (be er — Penlight, telescope) • slnunicode • luatexts, luajson • luasocket, luaposix (be er — ljsyscall) • WSAPI • lua-zmq • lua-hiredis (be er — ljffi-hiredis) • luasql-mysql
  • 22.
    DSL and codegenera on Our stack • HTTP request handlers. • Code (par ally sta cally validated). • Docs. • (Planned) Smoke-tests. • SQL schema. • "ORM" wrapper code. • DB schema patches. • Auto-backoffice. • Docs. • bit.ly/lua-dsl-talk • Common parts of a project are generated from text templates.
  • 23.
    Performance Our stack • About160M synthe c hits per day per EX6-class server in ad networks. • About 8K simultaneous ac ve users per EX6-class server in online games.
  • 24.
    What did weencounter? Pi alls In main: • Couple "mysterious" problems, due to bugs in early LuaJIT2 betas (all fixed by now). • Problems, caused by two versions of the same LuaRocks package installed in the system. • Exploding Redis. • Lousy Hetzner HDD reliability.
  • 25.
    Diagnos cs, debuggingand monitoring Pi alls • Par al sta c code valida on. • Run me valida on. • Autotests. • GC tuning and monitoring. • Monitoring for request mes, memory usage etc. • Debugging with logs.
  • 26.
    Main unsolved problems Pialls • Long polling / Comet. • OS signal handling. • More efficient CPU usage with HTTP handlers. • LuaRocks: • Can install two versions of the same rock in the system. • Can't upgrade a package.
  • 27.
    Next genera onstack • Unblocking API with corou nes, no callback. Get inspired by, or adapt OpenResty. • Complete transi on to LuaJIT FFI. • Consider dropping LuaRocks. • Drop FCGI, move to epoll and lua-h p-parser. • Simplify the architecture as much as possible. Drop the configura on server. More code genera on! • New DSL design.
  • 28.
    Want to knowmore? Official Site lua.org, luajit.org Wiki lua-users.org/wiki, wiki.luajit.org Mailing Lists lua.org/lua-l.html, luajit.org/list.html StackOverflow stackoverflow.com/ques ons/tagged/Lua IRC #lua at irc.freenode.net
  • 29.