SlideShare a Scribd company logo
1 of 46
Download to read offline
Lua as a business logic 
language in high load 
application 
Ilya Martynov 
ilya@iponweb.net 
CTO at IPONWEB
Company background 
●Ad industry 
●Custom development 
●Technical platform with 
multiple components
Custom web server 
●One of the components of 
our technology stack 
●Written in C++ 
●Uses Lua as an embeded 
scripting language
Adserving requirements 
● High load 
●Complex logic about what ads 
to show and how to track them 
●Hardware is not always cheaper 
than developer's time
What is high load
What is high load
How do we come to use 
Lua? 
● First version of adserver is 
pure C++ 
●Runs fast 
●Development is slow
How do we come to use 
Lua? 
●Developers who can write low 
level code and can write business 
logic code are rare animals 
●Operational costs: there is a 
better balance between cost to 
run and cost to develop
How do we come to use 
Lua? 
●Separation of church and state 
●C++ for low level and 
performance critical bits 
● Scripting language for 
business logic
So what do we use as a 
scripting language?
Why Lua? 
●So that I can attend Lua 
workshop as a speaker!
Why Lua?
Game developers like Lua 
for good reasons 
●Fastest scripting language 
●Easiest to embed scripting 
language 
● Simple but expressive 
●Can be sandboxed
Why NOT Lua 
●Poor libraries (compared to 
competition) 
●But this is NOT as big deal 
for development in special 
domain (advertising)
Architecture
Multithreaded C++ server 
●Worker thread per CPU 
core 
●One Lua interpreter state 
per worker
Multithreaded C++ server 
● Multiple coroutines in each 
Lua interpreter state 
●New HTTP request → new 
coroutine in idle Lua 
interpreter state
Sandbox environment 
●Only safe subset of Lua 
standard library available 
●Special high level IO APIs to 
access external world 
●Only allow what is really 
required
Why coroutines 
●Networking IO APIs mean Lua 
code may wait for responses 
●Coroutines can be paused 
until response so that we can 
process other requests 
meanwhile in worker thread
API design 
●Hide as much complexity 
from Lua developers as 
possible
API design example 
●Networking APIs: Allow 
parallel requests without 
async or multithreading 
programming model 
●Separate operations to create 
requests and to wait for results
API design example
HTTP client API example
Business logic 
●Select ad creative (banner) 
to show from all ad 
campaigns 
●Track important evens for 
ad creative like clicks
Selecting ad creative 
●Complex targeting rules 
●Ad campaign delivery 
optimization 
●Money calculations
Selecting ad creative
Business data as native 
Lua data 
● Most of data our business logic 
works with is read-only 
● Amount of data required in real-time 
is relatively low 
● Solution: use Lua data 
structures as in-memory storage
Business data as native 
Lua data 
●Very natural Lua code – it is 
all just iterations over Lua 
data structures 
●Very fast – you cannot beat 
in-memory data
Problem with data
Out of memory 
●You cannot share Lua data 
between Lua interpreter states 
●More CPU cores → higher 
memory usage 
● Projects became bigger too → 
more data
Out of memory: LuaJIT 
32 bit 
32 bit 
3 GB of RAM 
Linux 
application 
64 bit 
Linux 
32 bit 
application 
4 GB of RAM 
64 bit 
Linux 
64 bit 
application 
all RAM available 
but LuaJIT can 
use only 1GB
Memory problem solution 
●Switch to stock Lua? 
● Not as fast as LuaJIT 
●Fixes immediate problem 
but with higher memory 
usage breaks due to GC
Memory problem solution 
●Move business data out of 
Lua 
●But we need backward 
compatibility with existing 
Lua codebases
Attempt #1: userdata 
●userdata + metatables to 
expose C++ managed data 
storage as “fake” Lua tables 
●Each field access via userdata 
is C function call → slow 
compared to native Lua data
Attempt #2: FFI cdata 
● FFI – alternative interface to C code 
from Lua available in LuaJIT 
● FFI is designed to be LuaJIT friendly 
●cdata is sort of like userdata for FFI 
– also can use metatables to “fake” 
Lua tables
How does LuaJIT work? 
●Runs parts of your code as interpreted 
and parts of it as JIT compiled 
● As long as hot spots are covered you 
are good 
● If code not written with LuaJIT in mind 
then most of it will not be compiled
FFI: leap of faith
FFI: leap of faith 
FFI Lua 1 Lua 2 
jit 1.57 1.87 2.00 
nojit 55.1 5.05 5.95 
3rd party benchmark – source at 
https://github.com/client9/ipcat/tree/master/lua
FFI: leap of faith 
● If you introduce FFI in your 
application it will run slower 
● Until you manage to get 
LuaJIT to JIT compile 
enough parts of it
How to make LuaJIT 
happy 
●Use compilation traces to 
find why code doesn't 
compile 
●Unfortunately for uninitiated 
they look like gibberish
Compilation trace
LuaJIT challenge 
●Requires special low level 
knowledge to make code 
run fast 
●Sometimes leads to non-intuitive 
Lua code
LuaJIT quiz
LuaJIT challenge 
●Breaks our abstractions – 
Lua developers forced to 
work on lower level than 
normally needed
Wraping up 
●Lua: unique challenges 
●Lua: despite everything very 
powerful and successful 
technology
Lua as a business logic language in high load application

More Related Content

What's hot

PHP is the king, nodejs is the prince and Lua is the fool
PHP is the king, nodejs is the prince and Lua is the foolPHP is the king, nodejs is the prince and Lua is the fool
PHP is the king, nodejs is the prince and Lua is the fool
Alessandro Cinelli (cirpo)
 
RubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on RailsRubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on Rails
elliando dias
 

What's hot (20)

Go & multi platform GUI Trials and Errors
Go & multi platform GUI Trials and ErrorsGo & multi platform GUI Trials and Errors
Go & multi platform GUI Trials and Errors
 
CubeJS: eBay’s Node.js Adoption Journey
CubeJS: eBay’s Node.js Adoption JourneyCubeJS: eBay’s Node.js Adoption Journey
CubeJS: eBay’s Node.js Adoption Journey
 
Programming iOS in Lua - A bridge story
Programming iOS in Lua - A bridge storyProgramming iOS in Lua - A bridge story
Programming iOS in Lua - A bridge story
 
Dot Net Core
Dot Net CoreDot Net Core
Dot Net Core
 
IBM Think Session 8598 Domino and JavaScript Development MasterClass
IBM Think Session 8598 Domino and JavaScript Development MasterClassIBM Think Session 8598 Domino and JavaScript Development MasterClass
IBM Think Session 8598 Domino and JavaScript Development MasterClass
 
Zend con 2016 bdd with behat for beginners
Zend con 2016   bdd with behat for beginnersZend con 2016   bdd with behat for beginners
Zend con 2016 bdd with behat for beginners
 
PHP is the King, nodejs the prince and python the fool
PHP is the King, nodejs the prince and python the foolPHP is the King, nodejs the prince and python the fool
PHP is the King, nodejs the prince and python the fool
 
PHP is the king, nodejs is the prince and Lua is the fool
PHP is the king, nodejs is the prince and Lua is the foolPHP is the king, nodejs is the prince and Lua is the fool
PHP is the king, nodejs is the prince and Lua is the fool
 
Infrastructure as Data with Ansible
Infrastructure as Data with AnsibleInfrastructure as Data with Ansible
Infrastructure as Data with Ansible
 
Python for IoT, A return of experience
Python for IoT, A return of experiencePython for IoT, A return of experience
Python for IoT, A return of experience
 
RubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on RailsRubyStack: the easiest way to deploy Ruby on Rails
RubyStack: the easiest way to deploy Ruby on Rails
 
History of Java 1/2
History of Java 1/2History of Java 1/2
History of Java 1/2
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 
Node.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniquesNode.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniques
 
Engage 2019: The good, the bad and the ugly: a not so objective view on front...
Engage 2019: The good, the bad and the ugly: a not so objective view on front...Engage 2019: The good, the bad and the ugly: a not so objective view on front...
Engage 2019: The good, the bad and the ugly: a not so objective view on front...
 
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
Bootstrapping a simple enterprise application with Java EE successor, Jakarta...
 
Intro to Perfect - LA presentation
Intro to Perfect - LA presentationIntro to Perfect - LA presentation
Intro to Perfect - LA presentation
 
2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
 

Viewers also liked

Las16 309 - lua jit arm64 port - status
Las16 309 - lua jit arm64 port - statusLas16 309 - lua jit arm64 port - status
Las16 309 - lua jit arm64 port - status
Linaro
 
LAS16-400K2: TianoCore – Open Source UEFI Community Update
LAS16-400K2: TianoCore – Open Source UEFI Community UpdateLAS16-400K2: TianoCore – Open Source UEFI Community Update
LAS16-400K2: TianoCore – Open Source UEFI Community Update
Linaro
 

Viewers also liked (14)

What's wrong with web
What's wrong with webWhat's wrong with web
What's wrong with web
 
Lua first steps
Lua first stepsLua first steps
Lua first steps
 
Lua and its Ecosystem
Lua and its EcosystemLua and its Ecosystem
Lua and its Ecosystem
 
Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015
 
Get started with Lua - Hackference 2016
Get started with Lua - Hackference 2016Get started with Lua - Hackference 2016
Get started with Lua - Hackference 2016
 
Hands on lua
Hands on luaHands on lua
Hands on lua
 
Lua: the world's most infuriating language
Lua: the world's most infuriating languageLua: the world's most infuriating language
Lua: the world's most infuriating language
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua table
 
Las16 309 - lua jit arm64 port - status
Las16 309 - lua jit arm64 port - statusLas16 309 - lua jit arm64 port - status
Las16 309 - lua jit arm64 port - status
 
LAS16-400K2: TianoCore – Open Source UEFI Community Update
LAS16-400K2: TianoCore – Open Source UEFI Community UpdateLAS16-400K2: TianoCore – Open Source UEFI Community Update
LAS16-400K2: TianoCore – Open Source UEFI Community Update
 
BUD17-DF15 - Optimized Android N MR1 + 4.9 Kernel
BUD17-DF15 - Optimized Android N MR1 + 4.9 KernelBUD17-DF15 - Optimized Android N MR1 + 4.9 Kernel
BUD17-DF15 - Optimized Android N MR1 + 4.9 Kernel
 
Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析Lua/LuaJIT 字节码浅析
Lua/LuaJIT 字节码浅析
 
LuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super HeroLuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super Hero
 

Similar to Lua as a business logic language in high load application

What's New in OpenLDAP
What's New in OpenLDAPWhat's New in OpenLDAP
What's New in OpenLDAP
LDAPCon
 
mRuby - Powerful Software for Embedded System Development
mRuby - Powerful Software for Embedded System DevelopmentmRuby - Powerful Software for Embedded System Development
mRuby - Powerful Software for Embedded System Development
Kazuhiro Koga 古賀一博
 

Similar to Lua as a business logic language in high load application (20)

What's New in OpenLDAP
What's New in OpenLDAPWhat's New in OpenLDAP
What's New in OpenLDAP
 
Instant developer onboarding with self contained repositories
Instant developer onboarding with self contained repositoriesInstant developer onboarding with self contained repositories
Instant developer onboarding with self contained repositories
 
Serverless Boston @ Oracle Meetup
Serverless Boston @ Oracle MeetupServerless Boston @ Oracle Meetup
Serverless Boston @ Oracle Meetup
 
The Fn Project by Jesse Butler
 The Fn Project by Jesse Butler The Fn Project by Jesse Butler
The Fn Project by Jesse Butler
 
Go at uber
Go at uberGo at uber
Go at uber
 
mRuby - Powerful Software for Embedded System Development
mRuby - Powerful Software for Embedded System DevelopmentmRuby - Powerful Software for Embedded System Development
mRuby - Powerful Software for Embedded System Development
 
Fluentd: Unified Logging Layer at CWT2014
Fluentd: Unified Logging Layer at CWT2014Fluentd: Unified Logging Layer at CWT2014
Fluentd: Unified Logging Layer at CWT2014
 
AirBNB's ML platform - BigHead
AirBNB's ML platform - BigHeadAirBNB's ML platform - BigHead
AirBNB's ML platform - BigHead
 
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
 Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa... Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
Bighead: Airbnb’s End-to-End Machine Learning Platform with Krishna Puttaswa...
 
Not my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructureNot my problem - Delegating responsibility to infrastructure
Not my problem - Delegating responsibility to infrastructure
 
Why Go Lang?
Why Go Lang?Why Go Lang?
Why Go Lang?
 
Monkey Server
Monkey ServerMonkey Server
Monkey Server
 
Introduction to Python Programming Basics
Introduction  to  Python  Programming BasicsIntroduction  to  Python  Programming Basics
Introduction to Python Programming Basics
 
Adopting language server for apache camel feedback from a java/Eclipse plugi...
Adopting language server for apache camel  feedback from a java/Eclipse plugi...Adopting language server for apache camel  feedback from a java/Eclipse plugi...
Adopting language server for apache camel feedback from a java/Eclipse plugi...
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Go at Skroutz
Go at SkroutzGo at Skroutz
Go at Skroutz
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
PUG ROMAGNA - Alternative a Magento2
PUG ROMAGNA - Alternative a Magento2PUG ROMAGNA - Alternative a Magento2
PUG ROMAGNA - Alternative a Magento2
 
Dynatech presentation for TSI Career Day
Dynatech presentation for TSI Career DayDynatech presentation for TSI Career Day
Dynatech presentation for TSI Career Day
 
Programming Languages used in AI
Programming Languages used in AIProgramming Languages used in AI
Programming Languages used in AI
 

Recently uploaded

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Recently uploaded (20)

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

Lua as a business logic language in high load application

  • 1. Lua as a business logic language in high load application Ilya Martynov ilya@iponweb.net CTO at IPONWEB
  • 2. Company background ●Ad industry ●Custom development ●Technical platform with multiple components
  • 3. Custom web server ●One of the components of our technology stack ●Written in C++ ●Uses Lua as an embeded scripting language
  • 4. Adserving requirements ● High load ●Complex logic about what ads to show and how to track them ●Hardware is not always cheaper than developer's time
  • 7. How do we come to use Lua? ● First version of adserver is pure C++ ●Runs fast ●Development is slow
  • 8. How do we come to use Lua? ●Developers who can write low level code and can write business logic code are rare animals ●Operational costs: there is a better balance between cost to run and cost to develop
  • 9. How do we come to use Lua? ●Separation of church and state ●C++ for low level and performance critical bits ● Scripting language for business logic
  • 10. So what do we use as a scripting language?
  • 11. Why Lua? ●So that I can attend Lua workshop as a speaker!
  • 13. Game developers like Lua for good reasons ●Fastest scripting language ●Easiest to embed scripting language ● Simple but expressive ●Can be sandboxed
  • 14. Why NOT Lua ●Poor libraries (compared to competition) ●But this is NOT as big deal for development in special domain (advertising)
  • 16. Multithreaded C++ server ●Worker thread per CPU core ●One Lua interpreter state per worker
  • 17. Multithreaded C++ server ● Multiple coroutines in each Lua interpreter state ●New HTTP request → new coroutine in idle Lua interpreter state
  • 18. Sandbox environment ●Only safe subset of Lua standard library available ●Special high level IO APIs to access external world ●Only allow what is really required
  • 19. Why coroutines ●Networking IO APIs mean Lua code may wait for responses ●Coroutines can be paused until response so that we can process other requests meanwhile in worker thread
  • 20. API design ●Hide as much complexity from Lua developers as possible
  • 21. API design example ●Networking APIs: Allow parallel requests without async or multithreading programming model ●Separate operations to create requests and to wait for results
  • 23. HTTP client API example
  • 24. Business logic ●Select ad creative (banner) to show from all ad campaigns ●Track important evens for ad creative like clicks
  • 25. Selecting ad creative ●Complex targeting rules ●Ad campaign delivery optimization ●Money calculations
  • 27. Business data as native Lua data ● Most of data our business logic works with is read-only ● Amount of data required in real-time is relatively low ● Solution: use Lua data structures as in-memory storage
  • 28. Business data as native Lua data ●Very natural Lua code – it is all just iterations over Lua data structures ●Very fast – you cannot beat in-memory data
  • 30. Out of memory ●You cannot share Lua data between Lua interpreter states ●More CPU cores → higher memory usage ● Projects became bigger too → more data
  • 31. Out of memory: LuaJIT 32 bit 32 bit 3 GB of RAM Linux application 64 bit Linux 32 bit application 4 GB of RAM 64 bit Linux 64 bit application all RAM available but LuaJIT can use only 1GB
  • 32. Memory problem solution ●Switch to stock Lua? ● Not as fast as LuaJIT ●Fixes immediate problem but with higher memory usage breaks due to GC
  • 33. Memory problem solution ●Move business data out of Lua ●But we need backward compatibility with existing Lua codebases
  • 34. Attempt #1: userdata ●userdata + metatables to expose C++ managed data storage as “fake” Lua tables ●Each field access via userdata is C function call → slow compared to native Lua data
  • 35. Attempt #2: FFI cdata ● FFI – alternative interface to C code from Lua available in LuaJIT ● FFI is designed to be LuaJIT friendly ●cdata is sort of like userdata for FFI – also can use metatables to “fake” Lua tables
  • 36. How does LuaJIT work? ●Runs parts of your code as interpreted and parts of it as JIT compiled ● As long as hot spots are covered you are good ● If code not written with LuaJIT in mind then most of it will not be compiled
  • 37. FFI: leap of faith
  • 38. FFI: leap of faith FFI Lua 1 Lua 2 jit 1.57 1.87 2.00 nojit 55.1 5.05 5.95 3rd party benchmark – source at https://github.com/client9/ipcat/tree/master/lua
  • 39. FFI: leap of faith ● If you introduce FFI in your application it will run slower ● Until you manage to get LuaJIT to JIT compile enough parts of it
  • 40. How to make LuaJIT happy ●Use compilation traces to find why code doesn't compile ●Unfortunately for uninitiated they look like gibberish
  • 42. LuaJIT challenge ●Requires special low level knowledge to make code run fast ●Sometimes leads to non-intuitive Lua code
  • 44. LuaJIT challenge ●Breaks our abstractions – Lua developers forced to work on lower level than normally needed
  • 45. Wraping up ●Lua: unique challenges ●Lua: despite everything very powerful and successful technology