![Page 1: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/1.jpg)
![Page 2: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/2.jpg)
LUA AS A SECOND LANGUAGE
![Page 3: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/3.jpg)
LUKA MUŽINIĆ
@lmuzinic
![Page 4: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/4.jpg)
![Page 5: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/5.jpg)
LIGHTROOMMYSQL WORKBENCHWIRESHARKASTERISKWORLD OF WARCRAFT
WHERE CAN WE FIND LUA?
![Page 6: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/6.jpg)
LUA IS LIGHT IN FEATURESDYNAMICALLY TYPED FEW ATOMIC STRUCTURES EVEN LESS COMPOUND STRUCTURES FUNCTIONS AS FIRST CLASS CITIZENS
![Page 7: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/7.jpg)
DATA TYPES
BOOLEANINTEGERFLOATSTRING
BOOLEANNUMBERSTRING
SCALAR
![Page 8: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/8.jpg)
DATA TYPESCOMPOUND
ARRAYOBJECTCALLABLEITERABLE
TABLEFUNCTION
![Page 9: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/9.jpg)
DATA TYPESSPECIAL
NULL RESOURCE
NIL USERDATATHREAD
![Page 10: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/10.jpg)
LUA IS LIGHT IN FEATURESCOERCIONCLOSURES COROUTINES GARBAGE COLLECTION
![Page 11: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/11.jpg)
class Player{ public $name = ''; public function __construct($name) { $this->name = $name; }}
Player = { name = ''} function Player:new(object) object = object or {}; setmetatable(object, self); self.__index = self; return object; end
OBJECTS?
![Page 12: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/12.jpg)
NAMESPACES?namespace Player\Npc; Player = {}
Player.Npc = {}
![Page 13: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/13.jpg)
BASEPACKAGECOROUTINESTRINGUTF8TABLEMATH IOOSDEBUG
LUA STANDARD LIBRARY
![Page 14: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/14.jpg)
array_change_key_case, array_chunk, array_column, array_combine, array_count_values,
array_diff_assoc, array_diff_key, array_diff_uassoc, array_diff_ukey, array_diff, array_
fill_keys, array_fill, array_filter, array_flip, array_intersect_assoc, array_intersect_
key, array_intersect_uassoc, array_intersect_ukey, array_intersect, array_key_exists,
array_key_first, array_key_last, array_keys, array_map, array_merge_recursive, array_
merge, array_multisort, array_pad, array_pop, array_product, array_push, array_rand,
array_reduce, array_replace_recursive, array_replace, array_reverse, array_search,
array_shift, array_slice, array_splice, array_sum, array_udiff_assoc, array_udiff_
uassoc, array_udiff, array_uintersect_assoc, array_uintersect_uassoc, array_uintersect,
array_unique, array_unshift, array_values, array_walk_recursive, array_walk, array,
arsort, asort, compact, count, current, end, extract, in_array, key_exists, key, krsort,
ksort, list, natcasesort, natsort, next, pos, prev, range, reset, rsort, shuffle,
sizeof, sort, uasort, uksort, usort,
ARRAY
![Page 15: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/15.jpg)
concat, insert, maxn, remove, sort
TABLE
![Page 16: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/16.jpg)
ECOSYSTEM
![Page 17: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/17.jpg)
ECOSYSTEMTIOBE INDEX
#9 #33
![Page 18: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/18.jpg)
ECOSYSTEMGITHUB REPOSITORIES
1M+ 62K+
![Page 19: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/19.jpg)
ECOSYSTEMSTACK OVERFLOW QUESTIONS
1.2M+ 15K+
![Page 20: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/20.jpg)
ECOSYSTEMIDE
INTELLIJ ECLIPSEZEROBRANE STUDIO VISUAL STUDIO EMACSATOMSUBLIME
![Page 21: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/21.jpg)
ECOSYSTEMPACKAGE MANAGEMENT
LUAROCKS LUADIST
![Page 22: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/22.jpg)
ECOSYSTEMPACKAGE MANAGEMENT
BUSTED SPECL LUASOCKET REDIS-LUA LUA-CJSON UUID LUASQL-MYSQL LUA-GEOIP
![Page 23: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/23.jpg)
![Page 24: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/24.jpg)
REDISMOST LOVED DB
THIRD YEAR IN A ROW (via STACK OVERFLOW INSIGHTS)
![Page 25: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/25.jpg)
REDISDATA STRUCTURE STORE
CACHE DATABASE MESSAGE BROKER ATOMIC LUA SCRIPTING
![Page 26: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/26.jpg)
REDISLUA SCRIPTING
PROMISE OF ATOMICITY ACCESS REDIS ITSELF WITH redis.call() OR redis.log()
LEARN CONVERSION BETWEEN DATATYPES NO GLOBAL VALUES BASE LIBRARIES + STRUCT, CJSON & CMSGPACK
![Page 27: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/27.jpg)
![Page 28: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/28.jpg)
REDISHOW TO RUN LUA IN REDIS
redis.call('zadd', KEYS[1], ARGV[1], ARGV[2])
local rank = redis.call('zrevrank', KEYS[1], ARGV[2]) local position = tostring(rank + 1)
return position
![Page 29: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/29.jpg)
REDISHOW TO RUN LUA IN REDIS
DON’T BE SCARED :)
![Page 30: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/30.jpg)
REDISHOW TO RUN LUA IN REDIS
EVAL
![Page 31: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/31.jpg)
REDISHOW TO RUN LUA IN REDIS
$client = new Predis\Client($parameters, $options);
$position = $client->eval($lua, 1, 'highscores', 1234, 'BOB'); $position = $client->evalsha(sha1($lua), 1, 'highscores', 1234, 'BOB');
![Page 32: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/32.jpg)
REDISHOW TO RUN LUA IN REDIS
redis.call('zadd', KEYS[1], ARGV[1], ARGV[2])
local rank = redis.call('zrevrank', KEYS[1], ARGV[2]) local position = tostring(rank + 1)
return position
$client = new Predis\Client($parameters, $options);
$position = $client->eval($lua, 1, 'highscores', 1234, 'BOB'); $position = $client->evalsha(sha1($lua), 1, 'highscores', 1234, 'BOB');
![Page 33: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/33.jpg)
REDISPOSSIBLE USAGES
THINK MORE HELPER SCRIPTS, LESS BUILD A FRAMEWORK IN REDIS SOMETHING + LOGSALL STATISTICS STUFF TRANSACTION LIKE THINGS
![Page 34: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/34.jpg)
![Page 35: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/35.jpg)
NGINXWHERE CAN WE FIND IT?
PROXYBALANCERCACHE WEB SERVER
![Page 36: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/36.jpg)
NGINXWHY DO WE USE IT?
VERY LOW MEMORY FOOTPRINT EVENT DRIVEN POOL OF WORKERS
![Page 37: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/37.jpg)
NGINX+ PHP-FPM
VERY LOW MEMORY FOOTPRINT EVENT DRIVEN POOL OF WORKERS SHARED NOTHING ARCHITECTURE SYNCHRONOUS CODE WITH BLOCKING I/O
![Page 38: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/38.jpg)
NGINX+ LUA
VERY LOW MEMORY FOOTPRINT EVENT DRIVEN POOL OF WORKERS ALLOWS FOR SHARED DATA BETWEEN REQUESTS SYNCHRONOUS CODE WITH NON-BLOCKING I/O
![Page 39: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/39.jpg)
NGINX + LUAGET STARTED WITH OPENRESTY
BUNDLE, NOT A FORK NGINX + LUAJIT + MODULES LEVERAGE EVENT MODEL AND NON-BLOCKING I/O NUMEROUS MODULES
![Page 40: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/40.jpg)
NGINX + LUAPHASES (SIMPLIFIED)
REWRITEACCESSCONTENTLOG
![Page 41: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/41.jpg)
NGINX + LUAPHASES (SIMPLIFIED)
REWRITE_BY_LUA_BLOCKACCESS_BY_LUA_BLOCKCONTENT_BY_LUA_BLOCK LOG_BY_LUA_BLOCK
REWRITE_BY_LUA_FILEACCESS_BY_LUA_FILECONTENT_BY_LUA_FILELOG_BY_LUA_FILE
![Page 42: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/42.jpg)
NGINX + LUAUSAGES
ACCESS CONTROL & SECURITY CHECKS REPONSE HEADER MANIPULATION FETCH BACKEND INFO DIRECTLY FROM MYSQL OR REDIS CACHE FORMAT LOG & ADD MORE INFOMATION
![Page 43: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/43.jpg)
![Page 44: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/44.jpg)
~curl-Gshttp://api:8080/highscores
[{"name":"ALC","score":"1234"},{"name":"BOB","score":"1000"},{"name":"LMZ","score":"999"}]
![Page 45: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/45.jpg)
NGINX + LUACONFIGURATION
location /highscores { default_type application/json; content_by_lua_block {
# ... } }
![Page 46: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/46.jpg)
NGINX + LUACONFIGURATIONlocal cjson = require "cjson" local redis = require "resty.redis" local red = redis:new() local ok, err = red:connect("127.0.0.1", 6379)
local highscores, err = red:zrevrange("highscores", 0, 10, "withscores")
local response = {} for i = 1, #highscores, 2 do local player = { name = highscores[i], score = highscores[i + 1] }
table.insert(response, player) end
ngx.say(cjson.encode(response))
![Page 47: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/47.jpg)
~#ab-n50000-c100-khttp://37.139.24.32:8080/
![Page 48: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/48.jpg)
~#ab-n50000-c100-khttp://37.139.24.32:8080/
ConcurrencyLevel:100Timetakenfortests:21.821secondsCompleterequests:50000Failedrequests:0Keep-Aliverequests:49548Totaltransferred:12197740bytesHTMLtransferred:4550000bytesRequestspersecond:2291.40[#/sec](mean)Timeperrequest:43.641[ms](mean)Timeperrequest:0.436[ms](mean,acrossallconcurrentrequests)Transferrate:545.90[Kbytes/sec]received
![Page 49: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/49.jpg)
ConnectionTimes(ms)minmean[+/-sd]medianmaxConnect:000.103Processing:34414.642727Waiting:34414.642727Total:44414.742728
Percentageoftherequestsservedwithinacertaintime(ms)50%4266%4375%4480%4590%4995%5398%6099%63100%728(longestrequest)
![Page 50: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/50.jpg)
![Page 51: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/51.jpg)
HOMEWORKTOLD YA
STEP OUTSIDE OF YOUR COMFORT ZONEEXPERIMENT
![Page 52: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/52.jpg)
Get GOing with a new language Kat Zień, Stadium @ 2:15pm
![Page 54: LUA AS - luka.muzinic.net · lua is light in features dynamically typed few atomic structures even less compound structures functions as first class citizens](https://reader033.vdocuments.mx/reader033/viewer/2022052014/602b7730adfa1f759207db82/html5/thumbnails/54.jpg)
@lmuzinicLuka Muzinic
THANK YOU
https://joind.in/talk/b8671
Photos by Nicolas Thomas, zhan zhang and Александр Суслов on Unsplash