SlideShare a Scribd company logo
Power to the People: Redis Lua Scripts
@itamarhaber
A Redis Geek, Disque Freak... and Lunatic!
Chief Developer Advocatique at
Have you signed for newsletter?
http://bit.ly/RedisWatch
...is a powerful, fast, lightweight,
embeddable scripting language.
...(pronounced LOO-ah) means
"Moon" in Portuguese.
Please do not write it as "LUA", which
is both ugly and confusing...
http://www.lua.org
Lunatic
Noun: a mentally ill person (not in technical use),
extremely foolish, eccentric, or absurd.
Origin: Middle English, from Old French lunatique, from
late Latin lunaticus, from Latin luna ‘moon’ (from the
belief that change of the moon caused intermittent
insanity).
Luaphobia is pure lunacy
Redis does Lua since v2.6 (Oct 23, 2012)
Scripts are atomic, have (practically) local data access
Lua is pretty and/or ugly isn't perfect, but …
… having the option to use it beats not.
(mea culpa: when all you have is a hammer)
Lua Redis language, primer (1/2)
--[[
this is a multi-line comment
]]
local t = { 1, 2, foo = "bar", nil, false }
for i, v in ipairs(KEYS)
if i % 2 ~= 0 then
redis.call('SET', ARGV[1],)
end
end
Lua Redis libraries, primer (2/2)
Core Lua: base, table, string, math & debug
Also included: bitop, struct, cmsgpack & cjson
redis library: call, pcall, log, status_reply,
Error_reply & sha1hex
[v3.2 spoiler]: debug, breakpoint, set_repl &
replicate_commands
Lua crash course...
ceci n'est pas Le Voyage Dans Le Lune
For your future reference:
RTFM - http://www.lua.org/pil
Redis - http://redis.io/commands/eval
15min - https://gist.github.com/tylerneylon/5853042
A poor & not to scale analogy
Redis is the Sun.
Earth is your application.
Imagine that the Moon is stuck in the middle of
the Sun.
You send non-melting rockets (scripts) with robots
(commands) and cargo (data) back and forth…
A helluava use case #1
Save the bandwidth, save the LATENCY
● Cached compiled multi-operations scripts
● Variadic keys & variable arity arguments (i.e. lots)
● Server-side processing for: exploding input, data
transformation & manipulation, JSON/MessagePack
(de)serialization, imploding (aggregating) output...
UC#1 example: GEOPATHLEN
Meet https://github.com/RedisLabs/geo.lua (WIP)
A (metric only) helper library for Redis geospatial indices
Redis API: GEODIST key elem1 elem2
+ GEODIST key elem2 elem3
+ ...
Lua library: GEOPATHLEN key elem1 elem2 elem3...
Winning use case #2
Transact with intelligence
● Scripts are atomic & blocking
● Comments, variables, data types, operators, control
structures & (some) libraries
● Do you really want to WATCH/MULTI/DISCARD/EXEC?
Lua.tx "recipe": 1) assert 2) process 3) check 4) commit
Liberating use case #3
Patent APIs & data structures
Remember geo.lua?
● GEODEL
● xyzsets (geosets w/ attitude)
● GeoJSON - encode/decode
● ...and ME TRY FILTER!
GEORADIUS -> GEOMETRYFILTER
3. Radius search from pbb's center
2. Compute p's bounding box
4. Filter anything out of pbb
5. Use a semi-infinite ray (PNPOLY)
Find the geoset members in polygon p
1. Read p's definition from geomash
Lua ain't only green cheese
● Massive number crunching? Nah: redimension's
benchmark
● Lua's operators (no ternary?), strings (no split?),
patterns (no regex?), tables (no zip?)...
● Only 32-bit bit operations
● Redis' sandboxing means no external packages
● Access to data not totally inexpensive
● Beware of returning doubles, associative arrays, …
Power to the People: Redis Lua Scripts
SCRIPT DEBUG, YES PLEASE!
Integral Redis Lua
scripts debugger
ZeroBrane Studio
Plugin for Redis
Script source is sent to slaves, reduces wire traffic:
for i = 1, 100 do
redis.call('INCR', KEYS[1])
end
Disadvantages: wasteful (1 + n slaves), long recovery
from AOF, no non-determinism, no "transient" keys
Lua replication < v3.2
Effects & target replication 3.2
-- succeeds if called before any write
redis.replicate_commands()
-- optionally, change replication target
redis.set_repl(redis.REPL_NONE)
-- or redis.REPL_AOF, redis.REPL_SLAVE
-- or redis.REPL_ALL (the default)
redis.call('TIME') -- random prize!
Lua versions
● Redis: 5.1.5 2012-02-17
● Current: 5.3.2 2015-11-30
● No major changes to language,
upgrade TBD soon™
Scripts graveyard:
infospect.lua, hitman.lua and redis-
lua-debugger… but I can still add
port 6379 to /etc/services with
Lua though ;)

More Related Content

PPTX
Developing a Redis Module - Hackathon Kickoff
PDF
Extend Redis with Modules
PDF
What's new in Redis v3.2
PDF
Play with FILE Structure - Yet Another Binary Exploit Technique
PDF
Introduction to redis - version 2
PDF
Triton and symbolic execution on gdb
PDF
Klee and angr
PDF
Pwning in c++ (basic)
Developing a Redis Module - Hackathon Kickoff
Extend Redis with Modules
What's new in Redis v3.2
Play with FILE Structure - Yet Another Binary Exploit Technique
Introduction to redis - version 2
Triton and symbolic execution on gdb
Klee and angr
Pwning in c++ (basic)

What's hot (20)

PDF
Linux Binary Exploitation - Return-oritend Programing
PDF
Elephants and Raft
PDF
Windows 10 Nt Heap Exploitation (English version)
PDF
Csp scala wixmeetup2016
PDF
PDF
台科逆向簡報
PDF
Redis — The AK-47 of Post-relational Databases
PDF
Redis - for duplicate detection on real time stream
PDF
Redis SoCraTes 2014
PDF
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
PDF
Finding Xori: Malware Analysis Triage with Automated Disassembly
KEY
JavaOne 2012 - JVM JIT for Dummies
PDF
Asynchronous single page applications without a line of HTML or Javascript, o...
PDF
Apache Hadoop Shell Rewrite
PDF
DConf 2016: Keynote by Walter Bright
PPT
NOSQL and Cassandra
PDF
Everything as Code with Terraform
PDF
Configuration management II - Terraform
PDF
Introduction to PostgreSQL
PDF
Upgrading to MongoDB 4.0 from older versions
Linux Binary Exploitation - Return-oritend Programing
Elephants and Raft
Windows 10 Nt Heap Exploitation (English version)
Csp scala wixmeetup2016
台科逆向簡報
Redis — The AK-47 of Post-relational Databases
Redis - for duplicate detection on real time stream
Redis SoCraTes 2014
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
Finding Xori: Malware Analysis Triage with Automated Disassembly
JavaOne 2012 - JVM JIT for Dummies
Asynchronous single page applications without a line of HTML or Javascript, o...
Apache Hadoop Shell Rewrite
DConf 2016: Keynote by Walter Bright
NOSQL and Cassandra
Everything as Code with Terraform
Configuration management II - Terraform
Introduction to PostgreSQL
Upgrading to MongoDB 4.0 from older versions
Ad

Viewers also liked (7)

PPTX
Benchmarking Redis by itself and versus other NoSQL databases
PPTX
A Weight Off Your Shoulders: MongoDB Atlas
PPTX
Scalable Streaming Data Pipelines with Redis
PDF
Highly scalable caching service on cloud - Redis
PPTX
Redis Indices (#RedisTLV)
PDF
A New “Sparkitecture” for Modernizing your Data Warehouse: Spark Summit East ...
PPTX
Redis Use Patterns (DevconTLV June 2014)
Benchmarking Redis by itself and versus other NoSQL databases
A Weight Off Your Shoulders: MongoDB Atlas
Scalable Streaming Data Pipelines with Redis
Highly scalable caching service on cloud - Redis
Redis Indices (#RedisTLV)
A New “Sparkitecture” for Modernizing your Data Warehouse: Spark Summit East ...
Redis Use Patterns (DevconTLV June 2014)
Ad

Similar to Power to the People: Redis Lua Scripts (20)

PPTX
Use of Lua in Lab Devices
PDF
Lua+tdd
PDF
Rclex: A Library for Robotics meet Elixir
PDF
Ruby is dying. What languages are cool now?
PDF
Roberto De Ioris - Scriptiamo Unreal Engine con Lua - Codemotion Rome 2019
PDF
Docker and Go: why did we decide to write Docker in Go?
PDF
Lua and its Ecosystem
PDF
Open Source Storage at Scale: Ceph @ GRNET
PPT
A brief history of Lua - Roberto Ierusalimschy (PUC Rio)
PDF
Java(ee) mongo db applications in the cloud
PDF
Tarantool 1.6 talk at SECR 2014 conference
PDF
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
PDF
Getting Started with Hadoop
PPTX
Lua and fable jonathan shaw (lionhead)
ODP
Darktable -- raw converter for linux
PDF
Introduction to Docker, December 2014 "Tour de France" Bordeaux Special Edition
PPTX
MozillaPH Rust Hack & Learn Session 1
PDF
What You Can Learn from Obscure Programming Languages
ODP
Introduction to Lua Luajit Openresty Luvit
PDF
openSUSE Asia 2024: Developing an application for GNOME in Rust
Use of Lua in Lab Devices
Lua+tdd
Rclex: A Library for Robotics meet Elixir
Ruby is dying. What languages are cool now?
Roberto De Ioris - Scriptiamo Unreal Engine con Lua - Codemotion Rome 2019
Docker and Go: why did we decide to write Docker in Go?
Lua and its Ecosystem
Open Source Storage at Scale: Ceph @ GRNET
A brief history of Lua - Roberto Ierusalimschy (PUC Rio)
Java(ee) mongo db applications in the cloud
Tarantool 1.6 talk at SECR 2014 conference
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
Getting Started with Hadoop
Lua and fable jonathan shaw (lionhead)
Darktable -- raw converter for linux
Introduction to Docker, December 2014 "Tour de France" Bordeaux Special Edition
MozillaPH Rust Hack & Learn Session 1
What You Can Learn from Obscure Programming Languages
Introduction to Lua Luajit Openresty Luvit
openSUSE Asia 2024: Developing an application for GNOME in Rust

More from Itamar Haber (11)

PDF
Redis v5 & Streams
PDF
Redis Modules API - an introduction
PDF
Redis Lua Scripts
PDF
Introduction to Redis
PDF
How I Implemented the #1 Requested Feature In Redis In Less than 1 Hour with ...
PDF
Redis Streams - Fiverr Tech5 meetup
PDF
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
PPTX
Redis Developers Day 2015 - Secondary Indexes and State of Lua
PDF
Use Redis in Odd and Unusual Ways
PPTX
Why Your MongoDB Needs Redis
PPTX
Redis & MongoDB: Stop Big Data Indigestion Before It Starts
Redis v5 & Streams
Redis Modules API - an introduction
Redis Lua Scripts
Introduction to Redis
How I Implemented the #1 Requested Feature In Redis In Less than 1 Hour with ...
Redis Streams - Fiverr Tech5 meetup
Leveraging Probabilistic Data Structures for Real Time Analytics with Redis M...
Redis Developers Day 2015 - Secondary Indexes and State of Lua
Use Redis in Odd and Unusual Ways
Why Your MongoDB Needs Redis
Redis & MongoDB: Stop Big Data Indigestion Before It Starts

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Cloud computing and distributed systems.
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
KodekX | Application Modernization Development
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”
Cloud computing and distributed systems.
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Programs and apps: productivity, graphics, security and other tools
KodekX | Application Modernization Development
sap open course for s4hana steps from ECC to s4
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing

Power to the People: Redis Lua Scripts

  • 2. @itamarhaber A Redis Geek, Disque Freak... and Lunatic! Chief Developer Advocatique at Have you signed for newsletter? http://bit.ly/RedisWatch
  • 3. ...is a powerful, fast, lightweight, embeddable scripting language. ...(pronounced LOO-ah) means "Moon" in Portuguese. Please do not write it as "LUA", which is both ugly and confusing... http://www.lua.org
  • 4. Lunatic Noun: a mentally ill person (not in technical use), extremely foolish, eccentric, or absurd. Origin: Middle English, from Old French lunatique, from late Latin lunaticus, from Latin luna ‘moon’ (from the belief that change of the moon caused intermittent insanity).
  • 5. Luaphobia is pure lunacy Redis does Lua since v2.6 (Oct 23, 2012) Scripts are atomic, have (practically) local data access Lua is pretty and/or ugly isn't perfect, but … … having the option to use it beats not. (mea culpa: when all you have is a hammer)
  • 6. Lua Redis language, primer (1/2) --[[ this is a multi-line comment ]] local t = { 1, 2, foo = "bar", nil, false } for i, v in ipairs(KEYS) if i % 2 ~= 0 then redis.call('SET', ARGV[1],) end end
  • 7. Lua Redis libraries, primer (2/2) Core Lua: base, table, string, math & debug Also included: bitop, struct, cmsgpack & cjson redis library: call, pcall, log, status_reply, Error_reply & sha1hex [v3.2 spoiler]: debug, breakpoint, set_repl & replicate_commands
  • 8. Lua crash course... ceci n'est pas Le Voyage Dans Le Lune For your future reference: RTFM - http://www.lua.org/pil Redis - http://redis.io/commands/eval 15min - https://gist.github.com/tylerneylon/5853042
  • 9. A poor & not to scale analogy Redis is the Sun. Earth is your application. Imagine that the Moon is stuck in the middle of the Sun. You send non-melting rockets (scripts) with robots (commands) and cargo (data) back and forth…
  • 10. A helluava use case #1 Save the bandwidth, save the LATENCY ● Cached compiled multi-operations scripts ● Variadic keys & variable arity arguments (i.e. lots) ● Server-side processing for: exploding input, data transformation & manipulation, JSON/MessagePack (de)serialization, imploding (aggregating) output...
  • 11. UC#1 example: GEOPATHLEN Meet https://github.com/RedisLabs/geo.lua (WIP) A (metric only) helper library for Redis geospatial indices Redis API: GEODIST key elem1 elem2 + GEODIST key elem2 elem3 + ... Lua library: GEOPATHLEN key elem1 elem2 elem3...
  • 12. Winning use case #2 Transact with intelligence ● Scripts are atomic & blocking ● Comments, variables, data types, operators, control structures & (some) libraries ● Do you really want to WATCH/MULTI/DISCARD/EXEC? Lua.tx "recipe": 1) assert 2) process 3) check 4) commit
  • 13. Liberating use case #3 Patent APIs & data structures Remember geo.lua? ● GEODEL ● xyzsets (geosets w/ attitude) ● GeoJSON - encode/decode ● ...and ME TRY FILTER!
  • 14. GEORADIUS -> GEOMETRYFILTER 3. Radius search from pbb's center 2. Compute p's bounding box 4. Filter anything out of pbb 5. Use a semi-infinite ray (PNPOLY) Find the geoset members in polygon p 1. Read p's definition from geomash
  • 15. Lua ain't only green cheese ● Massive number crunching? Nah: redimension's benchmark ● Lua's operators (no ternary?), strings (no split?), patterns (no regex?), tables (no zip?)... ● Only 32-bit bit operations ● Redis' sandboxing means no external packages ● Access to data not totally inexpensive ● Beware of returning doubles, associative arrays, …
  • 17. SCRIPT DEBUG, YES PLEASE! Integral Redis Lua scripts debugger ZeroBrane Studio Plugin for Redis
  • 18. Script source is sent to slaves, reduces wire traffic: for i = 1, 100 do redis.call('INCR', KEYS[1]) end Disadvantages: wasteful (1 + n slaves), long recovery from AOF, no non-determinism, no "transient" keys Lua replication < v3.2
  • 19. Effects & target replication 3.2 -- succeeds if called before any write redis.replicate_commands() -- optionally, change replication target redis.set_repl(redis.REPL_NONE) -- or redis.REPL_AOF, redis.REPL_SLAVE -- or redis.REPL_ALL (the default) redis.call('TIME') -- random prize!
  • 20. Lua versions ● Redis: 5.1.5 2012-02-17 ● Current: 5.3.2 2015-11-30 ● No major changes to language, upgrade TBD soon™ Scripts graveyard: infospect.lua, hitman.lua and redis- lua-debugger… but I can still add port 6379 to /etc/services with Lua though ;)