A match made in heaven: Nginx, Redis, and Lua – Part 1
Have a need for speed? Want to truly do some crazy stuff with web programming? Give the combination of nginx, lua, and redis a test drive.
So what the heck am I talking about. A couple of years ago I joined a band of motley men build high volume software platform to help out nonprofits on the web. More to come later about that. Doing prelim architecture, we figured out some system needs.
- Crazy fast web server: nginx
- Just as crazy fast nosql server: redis
- Some glue to connect it together: lua
Quick background about these components
Nginx: Started off by Igor Sysoev in 2002, it is one of a few servers addressing C10k problem. I admire about Nginx because how it is based on an asynch event-driven architecture.
Nginx powers several high-visibility sites, such as Netflix, Hulu, Pinterest, CloudFlare, Airbnb, WordPress.com, GitHub, SoundCloud, Zynga, Eventbrite, Zappos, Media Temple, Heroku, RightScale, Engine Yard and NetDNA
Redis: Salvatore Sanfilippo, also known as antirez, created Redis as an in memory advanced key-value store. It is plain simple key based database that can contain strings, hashes, lists, sets and sorted sets. Redis is used by this sites such as: craigslist.org, github.com, guardian.co.uk, Disqus, Pinterest, stackoverflow.com. flickr.com, instagram.com, twitter.com. Lua is supported as a scripting language that can run operations in Redis, and this is how I discovered the wonderful scripting language call Lua.
Lua: The Pontifical Catholic University of Rio de Janeiro in Brazil designs, implements, and maintains this lightweight, super fast scripting language. Lua is actually one of the leading languages used in video games like World of Warcraft and Angry Birds. Xiaozhe Wang and Yichun “agentzh” Zhang maintain a nginx module that allows us to embed Lua scripts inside of Nginx. Since I always want Ferrari performance let’s throw in LuaJit into this stack of toys.
Well enough about what this stuff is, let’s install it.
As Gentoo is my distro of choice here are the additions to the portage files.
NGINX_MODULES_HTTP="autoindex access browser charset empty_gif fastcgi gzip limit_req limit_conn map proxy referer rewrite ssi userid lua ndk stub_status upstream_ip_hash geoip"
dev-db/redis jemalloc test dev-libs/libpcre jit -bzip2 -readline -zlib unicode www-servers/nginx fastcgi ssl xslt pcre-jit luajit aio
=dev-lang/luajit-2.0.0_beta7 ~amd64 =dev-lua/lua-cjson-2.0.0 **
emerge redis dev-libs/libpcre lua dev-lua/lua-cjson dev-lang/lua dev-lang/luajit emerge nginx
Redis client for Nginx
mkdir -p /u01/lua cd /u01/lua git clone https://github.com/agentzh/lua-resty-redis
Redis and Nginx should start without any initial configuration. We will need to tweak the nginx conf in order to run lua, but fire up Nginx and point you browser to localhost and voila. Start redis and you can test with:
./redis-cli redis> ping
should respond with ‘PONG’.
Other Install Notes
- Nginx install notes here outline source install
- Redis can be cloned from https://github.com/antirez/redis and installed from source
- Luarocks is a simple way to install cjson
Part 2 of this series will cover basic Nginx Lua programming.
Part 3 will cover using redis and Nginx.