“error”
In case an error occurs, the information on what caused it will be shown here
Let’s try it
Starting up nginx
$ git clone https://github.com/tarantool/nginx_upstream_module.git
$ cd nginx_upstream_module
$ git submodule update -init -recursive
$ git clone https://github.com/nginx/nginx.git
$ cd nginx && git checkout release-1.9.7 && cd -
$ make build-all-debug
“build-all-debug” is a debug-version. We are aiming at less nginx configuration. For those who want to configure from scratch, there is a “build-all”.
$ cat test-root/conf/nginx.conf
http {
# Adds one Tarantool server as a backend
upstream echo {
server 127.0.0.1:10001;
}
server {
listen 8081 default; #goes to *:8081
server_name tnt_test;
location = /echo { # on *:8081/echo we send ‘echo’
tnt_pass echo;
}
}
}
$ ./nginx/obj/nginx # starting up nginx
Starting up Tarantool
Tarantool can be set up with packages or built.
-- hello-world.lua file
-- This is our stored procedure, it’s fairly simple and it doesn’t use Tarantool as a DB.
--All it does — is just returning its first argument.
function echo(a)
return {{a}}
end
box.cfg {
listen = 10001; -- Specifying the location of Tarantool
}
box.schema.user.grant('guest', 'read,write,execute') -- Give access
If you set up Tarantool with packages, you can start it up this way:
$ tarantool hello-world.lua # the first argument is the name of lua-script.
Invoking the stored procedure
Echo stored procedure can be invoked by any HTTP-connector; all you need to do — HTTP POST 127.0.0.1/echo and in the body there will be the following JSON (see Input Data)
{
"method": "echo", // Tarantool method name
"params": [
{"Hello world": "!"} // 1 method’s argument
],
"id": 1
}
I’ll invoke this procedure with wget
$ wget 127.0.0.1:8081/echo — post-data '{"method": "echo","params":[{"Hello world": "!"}],"id":1}'
$ cat echo
{"id":1,"result":[[{"hello world":"!"}]]}
Other examples:
Let’s sum it up
The pros of using Tarantool nginx upstream module
- No “third parties”; as a rule, the code and the data are on the same level
- Relatively simple configuration
- Load balancing on Tarantool nodes
- High performance speed, low latency
- JSON-based protocol instead of binary protocol; no need to search for Tarantool driver, JSON can be found anywhere
- Tarantool Sharing/Replication and nginx = cluster solution. But that’s the topic for another article
- The solution is used in production
The cons
- Overhead JSON instead of more compact and fast MsgPack
- It’s not a packaged solution. You need to configure it, to think how to deploy it
Plans
- OpenResty and nginScript support
- WebSocket and HTTP 2.0 support
The benchmark results - which are actually pretty cool- will be in a different article. Tarantool and Upstream Module is open source and welcoming to new users. If you wish to try it out, use it or share your ideas, go to GitHub, google group.
Links
Tarantool — GitHub, Google group
Nginx — upstream, Tarantool upstream module
On HackerNews