Scripting libdrizzle with Lua inside Nginx Scripting libdrizzle with - - PowerPoint PPT Presentation

scripting libdrizzle with lua inside nginx scripting
SMART_READER_LITE
LIVE PREVIEW

Scripting libdrizzle with Lua inside Nginx Scripting libdrizzle with - - PowerPoint PPT Presentation

Scripting libdrizzle with Lua inside Nginx Scripting libdrizzle with Lua inside Nginx (agentzh) agentzh@gmail.com 2012.4 "MySQL is always the bottleneck !" "Really?!" Some benchmarks on Amazon EC2 Small


slide-1
SLIDE 1

Scripting libdrizzle with Lua inside Nginx

slide-2
SLIDE 2

Scripting libdrizzle with Lua inside Nginx

章亦春 (agentzh)

agentzh@gmail.com

2012.4

slide-3
SLIDE 3

"MySQL is always the bottleneck!" "Really?!"

slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

Some benchmarks on Amazon EC2 Small instances

slide-7
SLIDE 7
slide-8
SLIDE 8

A Slow MySQL Query select sleep(1)

slide-9
SLIDE 9

♡ Amazon Linux AMI 2011.09 ♡ nginx 1.0.14 ♡ php-fpm 5.3.10

slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14

A Fast MySQL Query with a Small Resultset select * from world.City

  • rder by ID

limit 1

slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17

A Fast MySQL Query with a Big Resultset (100 KBytes) select * from world.City

  • rder by ID

limit 1000

slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21

We integrated libdrizzle directly into Nginx! http://wiki.nginx.org/HttpDrizzleModule

slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27

Let's just mud with nginx.conf, the Nginx configuration file

slide-28
SLIDE 28

upstream my_mysql_backend { drizzle_server 127.0.0.1:3306 dbname=test password=some_pass user=monty protocol=mysql; # a connection pool that can cache up to # 200 mysql TCP connections drizzle_keepalive max=200 overflow=reject; }

slide-29
SLIDE 29

location ~ '^/cat/(.*)' { set $name $1; set_quote_sql_str $quoted_name $name; drizzle_query "select * from cats where name=$quoted_name"; drizzle_pass my_mysql_backend; rds_json on; }

slide-30
SLIDE 30

$ curl 'http://localhost/cat/Jerry' [{"name":"Jerry","age":1}]

slide-31
SLIDE 31

The dynamic SQL Query for This Request select * from cats where name='Jerry'

slide-32
SLIDE 32
slide-33
SLIDE 33

The Slow MySQL Query again! select sleep(1)

slide-34
SLIDE 34
slide-35
SLIDE 35
slide-36
SLIDE 36
slide-37
SLIDE 37

The Fast MySQL Query with a Small Resultset Again! select * from world.City

  • rder by ID

limit 1

slide-38
SLIDE 38
slide-39
SLIDE 39
slide-40
SLIDE 40

The Fast MySQL Query with a Big Resultset (100 KBytes) Again! select * from world.City

  • rder by ID

limit 1000

slide-41
SLIDE 41
slide-42
SLIDE 42
slide-43
SLIDE 43

We also embedded Lua and LuaJIT directly into Nginx! http://wiki.nginx.org/HttpLuaModule

slide-44
SLIDE 44
slide-45
SLIDE 45

Use the Lua language to access the ngx_drizzle module!

slide-46
SLIDE 46
slide-47
SLIDE 47

location = /api { content_by_lua ' local rds_parser = require "rds.parser" local cjson = require "cjson" local resp = ngx.location.capture("/cat/Jerry") local data, err = rds_parser.parse(res.body) ngx.print(cjson.encode(data.resultset)) '; }

slide-48
SLIDE 48

$ curl 'http://localhost/api' [{"name":"Jerry","age":1}]

slide-49
SLIDE 49

The Fast MySQL Query with a Small Resultset Revisited! select * from world.City

  • rder by ID

limit 1

slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52

The Fast MySQL Query with a Big Resultset (100 KBytes) Again! select * from world.City

  • rder by ID

limit 1000

slide-53
SLIDE 53
slide-54
SLIDE 54
slide-55
SLIDE 55

I just implemented the Lua cosocket API! http://wiki.nginx.org/HttpLuaModule#ngx.socket.tcp

slide-56
SLIDE 56

! ! a socket API based on Lua coroutines ! ! a socket API that is synchronous ! ! a socket API that is nonblocking

slide-57
SLIDE 57
slide-58
SLIDE 58

I wrote the lua-resty-mysql library based on the cosocket API. http://github.com/agentzh/lua-resty-mysql

slide-59
SLIDE 59

It is a pure Lua MySQL driver written from scratch!

slide-60
SLIDE 60

local resty_mysql = require "resty.mysql" local mysql = resty_mysql:new() local ok, err = mysql:connect{ host = "127.0.0.1", port = 3306, database = "world", user = "monty", password = "some_pass" }

slide-61
SLIDE 61

local query = "select * from cats" local rows, err, errno, sqlstate = mysql:query(query) for i, row in ipairs(rows) do

  • - process the row table

end

slide-62
SLIDE 62
  • - add the current MySQL connection
  • - into the per-worker connection pool,
  • - with total capacity of 1024 connections and
  • - 60 seconds maximal connection idle time

local ok, err = mysql:set_keepalive(60000, 1024)

slide-63
SLIDE 63

The Slow MySQL Query Revisited! select sleep(1)

slide-64
SLIDE 64
slide-65
SLIDE 65
slide-66
SLIDE 66
slide-67
SLIDE 67

The Fast MySQL Query with a Small Resultset Revisited! select * from world.City

  • rder by ID

limit 1

slide-68
SLIDE 68
slide-69
SLIDE 69
slide-70
SLIDE 70

The Fast MySQL Query with a Big Resultset (100 KBytes) Revisited! select * from world.City

  • rder by ID

limit 1000

slide-71
SLIDE 71
slide-72
SLIDE 72
slide-73
SLIDE 73

How about comparing with the NodeJS world?

slide-74
SLIDE 74

♡ node 0.6.14 ♡ node mysql 0.9.5 ♡ node generic pool 1.0.9

slide-75
SLIDE 75
slide-76
SLIDE 76
slide-77
SLIDE 77
slide-78
SLIDE 78
slide-79
SLIDE 79

Caching responses with ngx_srcache + ngx_memc http://wiki.nginx.org/HttpSRCacheModule http://wiki.nginx.org/HttpMemcModule

slide-80
SLIDE 80
slide-81
SLIDE 81

# configure the cache storage location location /memc { internal; set $memc_key $query_string; set $memc_exptime 300; memc_pass 127.0.0.1:11211; }

slide-82
SLIDE 82

location = /api { set $key "$uri?$args"; srcache_fetch GET /memc $key; srcache_store PUT /memc $key; # drizzle_pass/fastcgi_pass/content_by_lua/... }

slide-83
SLIDE 83
slide-84
SLIDE 84
slide-85
SLIDE 85
slide-86
SLIDE 86

Find the source for all the benchmarks given here: http://github.com/agentzh/mysql-driver-benchmark

slide-87
SLIDE 87

Any questions? http://openresty.org https://groups.google.com/group/openresty

slide-88
SLIDE 88 S c r
slide-89
SLIDE 89
slide-90
SLIDE 90
slide-91
SLIDE 91
slide-92
SLIDE 92
slide-93
SLIDE 93
slide-94
SLIDE 94
slide-95
SLIDE 95
slide-96
SLIDE 96
slide-97
SLIDE 97
slide-98
SLIDE 98
slide-99
SLIDE 99
slide-100
SLIDE 100
slide-101
SLIDE 101
slide-102
SLIDE 102
slide-103
SLIDE 103
slide-104
SLIDE 104
slide-105
SLIDE 105
slide-106
SLIDE 106
slide-107
SLIDE 107
slide-108
SLIDE 108
slide-109
SLIDE 109
slide-110
SLIDE 110
slide-111
SLIDE 111
slide-112
SLIDE 112
slide-113
SLIDE 113
slide-114
SLIDE 114
slide-115
SLIDE 115
slide-116
SLIDE 116
slide-117
SLIDE 117
slide-118
SLIDE 118
slide-119
SLIDE 119
slide-120
SLIDE 120
slide-121
SLIDE 121
slide-122
SLIDE 122
slide-123
SLIDE 123
slide-124
SLIDE 124
slide-125
SLIDE 125
slide-126
SLIDE 126
slide-127
SLIDE 127
slide-128
SLIDE 128
slide-129
SLIDE 129
slide-130
SLIDE 130
slide-131
SLIDE 131
slide-132
SLIDE 132
slide-133
SLIDE 133
slide-134
SLIDE 134
slide-135
SLIDE 135
slide-136
SLIDE 136
slide-137
SLIDE 137
slide-138
SLIDE 138
slide-139
SLIDE 139
slide-140
SLIDE 140