lightweight apis in mruby (Михаил Бортник)
TRANSCRIPT
Lightweight APIs in mRuby
1 / 68
Who am I
2 / 68
Who am IName's Mikhail Bortnyk
3 / 68
Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music Ukraine
4 / 68
Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans school
5 / 68
Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addict
6 / 68
Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addictgithub: @vessi
7 / 68
Who am IName's Mikhail BortnykRuby developer and team leader in R&R Music UkraineCo-creator of Kottans schoolGeek, polyglot, software development addictgithub: @vessitwitter: @mikhailbortnyk
8 / 68
What is mRuby
9 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012
10 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your application
11 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecode
12 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecode
13 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumoto
14 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumotogithub: github://mruby/mruby
15 / 68
What is mRubymRuby is lightweight implementation of Ruby complying (partially) to ISO/IEC30170:2012can be linked and embedded into your applicationcan compile your software into bytecodecan generate C code for embedding bytecodeauthor: Yukihiro 'matz' Matsumotogithub: github://mruby/mrubylatest version: 1.1.0
16 / 68
Typical Rails project
17 / 68
Typical Rails project (I am sosorry for it, mom!)
18 / 68
Typical Rails projectit's based on Rack
19 / 68
Typical Rails projectit's based on Rackit contains a lot of business processes
20 / 68
Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routes
21 / 68
Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routesoften too heavy to serve tons of lightweight requests without scaling
22 / 68
Typical Rails projectit's based on Rackit contains a lot of business processesit responds to many routesoften too heavy to serve tons of lightweight requests without scaling
So, what is the solution?
23 / 68
Solutions
24 / 68
SolutionsYou have a problem and decide to use threads. Two now problems have you.
25 / 68
SolutionsYou have a problem and decide to use threads. Two now problems have you.You have a problem and decide to use functional languages. Now your problems areimmutable.
26 / 68
SolutionsYou have a problem and decide to use threads. Two now problems have you.You have a problem and decide to use functional languages. Now your problems areimmutable.You have a problem and decide to use Java. Now you have ProblemsFactory.
27 / 68
SolutionsYou have a problem and decide to use threads. Two now problems have you.You have a problem and decide to use functional languages. Now your problems areimmutable.You have a problem and decide to use Java. Now you have ProblemsFactory.You have a problem and decide...
28 / 68
Kill it with fire!
29 / 68
Kill it with fire!
Microservices
30 / 68
Microservices
31 / 68
Microservices
32 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
33 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
34 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fast
35 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweight
36 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweightbe stable
37 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweightbe stabledon't switch technology stack aggresively
38 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweightbe stabledon't switch technology stack aggresively
So, I decided to use mRuby. What do we have for serving web here?
39 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweightbe stabledon't switch technology stack aggresively
So, I decided to use mRuby. What do we have for serving web here?
mod_mruby. Apache module that executes mRuby code inside Apache process.
40 / 68
MicroservicesThe idea is to split big project in bunch of independent lightweight services.
Requirements:
be fastbe lightweightbe stabledon't switch technology stack aggresively
So, I decided to use mRuby. What do we have for serving web here?
mod_mruby. Apache module that executes mRuby code inside Apache process.ngx_mruby. Perfectly the same but with Nginx flavour.
41 / 68
Little demonstrationMicroservice without any service
42 / 68
Little demonstrationUsed software:
nginx (ALL)docker (ALL)docker-compose (ALL)mongodb (ALL)ab (ALL)unicorn gem (rack)rack gem (rack)rack-routing gem (rack)mongo gem (rack)ngx_mruby (mRuby)mruby-mongo (mRuby)
43 / 68
Rack execution$> cd testlab/rack$> docker-compose up$> ab -n 10000 -c 100 http://192.168.0.100:10080/api/contacts# and kill docker-compose with ^C
44 / 68
Rack stats (page 1)Document Path: /api/contactsDocument Length: 537 bytes
Concurrency Level: 100Time taken for tests: 7.897 secondsComplete requests: 10000Failed requests: 4399 (Connect: 0, Receive: 0, Length: 4399, Exceptions: 0)Non-2xx responses: 5601Total transferred: 5080859 bytesHTML transferred: 3434440 bytesRequests per second: 1266.27 [#/sec] (mean)Time per request: 78.972 [ms] (mean)Time per request: 0.790 [ms] (mean, across all concurrent requests)Transfer rate: 628.29 [Kbytes/sec] received
45 / 68
Rack stats (page 2)Connection Times (ms) min mean[+/-sd] median maxConnect: 0 4 4.1 2 34Processing: 1 74 72.1 23 423Waiting: 1 74 72.0 22 423Total: 2 78 72.2 27 426
Percentage of the requests served within a certain time (ms) 50% 27 66% 135 75% 147 80% 154 90% 174 95% 192 98% 216 99% 232 100% 426 (longest request)
46 / 68
mRuby execution$> cd testlab/rack$> docker-compose up$> ab -n 10000 -c 100 http://192.168.0.100:10080/api/contacts# and kill docker-compose with ^C
47 / 68
mRuby stats (page 1)Document Path: /api/contactsDocument Length: 98 bytes
Concurrency Level: 100Time taken for tests: 6.721 secondsComplete requests: 10000Failed requests: 0Total transferred: 2140000 bytesHTML transferred: 980000 bytesRequests per second: 1487.83 [#/sec] (mean)Time per request: 67.212 [ms] (mean)Time per request: 0.672 [ms] (mean, across all concurrent requests)Transfer rate: 310.93 [Kbytes/sec] received
48 / 68
mRuby stats (page 2)Connection Times (ms) min mean[+/-sd] median maxConnect: 0 3 5.9 0 29Processing: 5 64 27.5 68 132Waiting: 5 64 27.6 68 131Total: 14 67 24.4 69 132
Percentage of the requests served within a certain time (ms) 50% 69 66% 78 75% 83 80% 88 90% 102 95% 106 98% 112 99% 120 100% 132 (longest request)
49 / 68
Problems
50 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
51 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
you have to recompile mRuby each time you add some dependency
52 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
you have to recompile mRuby each time you add some dependencyoftenly gems are written in pure C
53 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
you have to recompile mRuby each time you add some dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you want
54 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
you have to recompile mRuby each time you add some dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you wantstill not so fast as pure C implementation :(
55 / 68
ProblemsEverything has its price. ngx_mruby and Nginx and mRuby are not exceptions.
you have to recompile mRuby each time you add some dependencyoftenly gems are written in pure CmRuby has no require keyword, so you code will not be so DRY as you wantWRONG!still not so fast as pure C implementation :(
56 / 68
mruby-require to the rescue!
57 / 68
mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.
58 / 68
mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.
located at: github://mattn/mruby-require
59 / 68
mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.
located at: github://mattn/mruby-requiregems that are located in build_config.rb before mruby-require - are included in mRubyVM, after - are not
60 / 68
mruby-require to the rescue!mruby-require allows you both to don't hold some gems in your mRuby virtual machine andto require your own code.
located at: github://mattn/mruby-requiregems that are located in build_config.rb before mruby-require - are included in mRubyVM, after - are notrequirements are loaded at each execution
61 / 68
Miss Rack?
62 / 68
Miss Rack?mruby-hibari is the abstraction that is pretty similiar to well-known Rack interface. But justfaster a lot.
63 / 68
Miss Rack?mruby-hibari is the abstraction that is pretty similiar to well-known Rack interface. But justfaster a lot.
Find it at github://kentaro/mruby-hibari
64 / 68
Seeking for opensourcedevelopers!I am looking for support to continue development of mruby-mongo. Any help will beappreciated.
65 / 68
Still actual :(
66 / 68
Questions?
67 / 68
Thank you!
68 / 68