SlideShare a Scribd company logo
Designing scalable application:
from umbrella project to
distributed system
Elixir Club 8, Kyiv, October 21, 2017
HELLO!
I am Anton Mishchuk
- Ruby/Elixir developer at Matic Insurance Services
we are hiring!
- A big fan of Elixir programming language
- Author and maintainer of: ESpec and Flowex libraries
github: antonmi
2
BEAM Applications
BEAM VM
Application
GS S GS
GS GS
Application
GS S GS
GS GS
Application
GS S GS
GS GS
3
Intro
○ Monolith or micro-services
○ BEAM approach
○ Umbrella project
○ Inter-service communication
4
Contents
○ “ML Tools” demo project
○ Three levels of code organization
○ Interfaces modules
○ Scaling to distributed system
○ Limiting concurrency
5
ML Tools
○ Stands for “Machine Learning Tools”
○ One can find it here:
□ https://github.com/antonmi/ml_tools
○ Umbrella project with 4 applications:
□ Datasets
□ Models
□ Utils
□ Main
6
Umbrella project
Monolith and microservices at the same time:
○ All applications are in one place
○ Easy to develop and test
○ Service-oriented architecture
○ Ready to scale
7
Code organization
○ Service level
○ Context level
○ Implementation level
8
Service level
Main
Datasets Models Utils
9
Context and
implementation
(datasets)
datasets/
lib/
datasets/
fetchers/
fetchers.ex
aws.ex
kaggle.ex
collections/
○ Datasets.Fetchers
○ Datasets.Fetchers.Aws
○ Datasets.Fetchers.Kaggle
10
Context and
implementation
(models)
models/
lib/
models/
lm/
lm.ex
engine.ex
rf/
rf.ex
engine.ex
○ Models.Lm
○ Models.Lm.Engine
○ Models.Rf
○ Models.Rf.Engine
11
Context and
implementation
(utils)
utils/
lib/
utils/
pre_processing/
pre_processing.ex
filter.ex
normalizer.ex
visualization/
...
○ Utils.PreProcessing
○ Utils.PreProcessing.Filter
○ Utils.PreProcessing.Normalizer
12
Main application
defp deps do
[
{:datasets, in_umbrella: true},
{:models, in_umbrella: true},
{:utils, in_umbrella: true}
]
end
13
Main application
defmodule Main.Zillow do
def rf_fit do
Datasets.Fetchers.zillow_data
|> Utils.PrePorcessing.normalize_data
|> Models.Rf.fit_model
end
end
14
What’s wrong with
this code?
defmodule Main.Zillow do
def rf_fit do
Datasets.Fetchers.zillow_data
|> Utils.PreProcessing.normalize_data
|> Models.Rf.fit_model
end
end
15
Encapsulation
problem
○ Modules, functions and that’s it!
○ Main application has access to all public
functions in all the applications
○ Micro-services -> tightly coupled monolith
16
Interfaces Modules
○ Specific modules in the application that
implements public interface
○ Grouped by context
○ Other application must use only functions
from this modules
17
Just delegate
defmodule Datasets.Interfaces.Fetchers do
alias Datasets.Fetchers
defdelegate zillow_data, to: Fetchers
defdelegate landsat_data, to: Fetchers
end
18
Use Interfaces only!
def rf_fit do
Datasets.Interfaces.Fetchers.zillow_data
|> Utils.Interfaces.PreProcessing.normalize_data
|> Models.Interfaces.Rf.fit_model
end
19
Interfaces Modules
○ It’s just a convention
○ But very important convention
○ It creates a good basis for future scaling
20
Let’s scale!
○ We decide to run each of the applications
on a different node
○ Modules from one application will be not
accessible in another one
○ But we still want keep all the things in one
place
21
Interface applications
Main
Datasets Models Utils
DatasetsInterface
ModelsInterface
UtilsInterface
22
Interface application
models_interface/
config/
lib/
models_interface/
models_interface.ex
lm.ex
rf.ex
mix.ex
23
Inter-Application
Communication
○ RPC (for ‘models’ app)
○ Distributed tasks (for ‘datasets’ app)
○ HTTP (for ‘utils’ app)
24
RPC
○ Remote Procedure Call
○ :rpc.call(node, module, fun, args)
○ BEAM handles all the serialization and
deserialization stuff
25
ModelsInterface.Rf
defmodule ModelsInterface.Rf do
def fit_model(data) do
ModelsInterface.remote_call(
Models.Interfaces.Rf,
:fit_model,
[data]
)
end
end
26
ModelsInterface
defmodule ModelsInterface do
def remote_call(module, fun, args, env  Mix.env) do
do_remote_call({module, fun, args}, env)
end
defp do_remote_call({module, fun, args}, :test) do
apply(module, fun, args)
end
defp do_remote_call({module, fun, args}, _) do
:rpc.call(remote_node(), module, fun, args)
end
end 27
Quick refactoring
○ Replace Models.Interfaces with
ModelsInterface and that’s it
○ You continue develop with the same speed
○ Nothing to change in tests
28
What about tests
defp do_remote_call({module, fun, args}, :test) do
apply(module, fun, args)
end
defp do_remote_call({module, fun, args}, _) do
:rpc.call(remote_node(), module, fun, args)
end
29
What about tests
defp deps do
[
...
{:models, in_umbrella: true, only: [:test]},
{:models_interface, in_umbrella: true},
...
]
end
30
RPC cons
○ Synchronous calls from “local” process
○ Needs additional logic for
asynchronous execution
31
Distributed tasks
○ Build on top of Elixir Task
○ Task are spawned on remote node
○ Supervisor controls the tasks
32
Start Supervisor
defmodule Datasets.Application do
...
def start(_type, _args) do
children = [
supervisor(Task.Supervisor,
[[name: Datasets.Task.Supervisor]],
[restart: :temporary, shutdown: 10000])
]
...
end
end 33
DatasetsInterface
defmodule DatasetsInterface do
...
defp do_spawn_task({module, fun, args}, _) do
Task.Supervisor.async(remote_supervisor(),
module, fun, args)
|> Task.await
end
defp remote_supervisor, do: #config data
end
34
Erlang Distribution
Protocol
Pros:
○ Battle-tested over decades
○ Free serialization / deserialization
Cons:
○ Not very secure
○ No very fast
○ BEAM specific
35
HTTP
UtilsInterface
defmodule UtilsInterface do
...
defp do_remote_call({module, fun, args}, _) do
{:ok, resp} = HTTPoison.post(remote_url(),
serialize({module, fun, args}))
deserialize(resp.body)
end
...
end
36
Plug in Utils app
defmodule Utils.Interfaces.Plug do
use Plug.Router
...
post "/remote" do
{:ok, body, conn} = Plug.Conn.read_body(conn)
{module, fun, args} = deserialize(body)
result = apply(module, fun, args)
send_resp(conn, 200, serialize(result))
end
end
37
Limiting
concurrency
When do you need this?
○ Third-party API rate limits
○ Heavy calculations per request
38
def start(_type, _args) do
pool_opts = [
name: {:local, Models.Interface},
worker_module: Models.Interfaces.Worker,
size: 5, max_overflow: 5]
children = [
:poolboy.child_spec(Models.Interface, pool_opts, []),
]
end
poolboy in
Models.Application
39
defmodule Models.Interfaces.Worker do
use GenServer
...
def handle_call({module, fun, args}, _from, state) do
result = apply(module, fun, args)
{:reply, result, state}
end
end
Models.Interfaces.Worker
40
defmodule Models.Interfaces.Rf do
def fit_model(data) do
with_poolboy({Models.Rf, :fit_model, [data]})
end
def with_poolboy(args) do
worker = :poolboy.checkout(Models.Interface)
result = GenServer.call(worker, args, :infinity)
:poolboy.checkin(Models.Interface, worker)
result
end
end
Models.Interfaces.Rf
41
Conclusion
○ Start with micro-services from
the very beginning
○ Think carefully about app interface
○ Use EDP for communication when scale
○ Keep “communication” code in separate app
42
THANKS!
Ask me questions!
43

More Related Content

Similar to Designing scalable application: from umbrella project to distributed system - Anton Mishchuk (20)

KEY
Polyglot parallelism
Phillip Toland
 
PPTX
The Right Kind of API – How To Choose Appropriate API Protocols and Data Form...
Nordic APIs
 
PDF
Cloud Native API Design and Management
AllBits BVBA (freelancer)
 
PDF
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Greg Vaughn
 
PDF
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Elixir Club
 
PDF
Introducing Elixir and OTP at the Erlang BASH
devbash
 
KEY
Messaging, interoperability and log aggregation - a new framework
Tomas Doran
 
PPTX
Tech talk microservices debugging
Andrey Kolodnitsky
 
PPTX
Debugging Microservices - key challenges and techniques - Microservices Odesa...
Lohika_Odessa_TechTalks
 
PDF
Building the ideal betting stack | London Erlang User Group presentation
Erlang Solutions
 
PPTX
Elixir introduction
Al Sayed Gamal
 
PDF
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
South Tyrol Free Software Conference
 
PPTX
General Introduction to technologies that will be seen in the school
ISSGC Summer School
 
PDF
Dataflow: Declarative concurrency in Ruby
Larry Diehl
 
PDF
Distributed Systems in Data Engineering
Oluwasegun Matthew
 
PPT
Suman's PhD Candidacy Talk
Suman Srinivasan
 
PDF
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
PDF
10 fn s03
Scott Foster
 
PDF
10 fn s03
Scott Foster
 
PDF
Writing a REST Interconnection Library in Swift
Pablo Villar
 
Polyglot parallelism
Phillip Toland
 
The Right Kind of API – How To Choose Appropriate API Protocols and Data Form...
Nordic APIs
 
Cloud Native API Design and Management
AllBits BVBA (freelancer)
 
Elixir Elevated: The Ups and Downs of OTP at ElixirConf2014
Greg Vaughn
 
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Elixir Club
 
Introducing Elixir and OTP at the Erlang BASH
devbash
 
Messaging, interoperability and log aggregation - a new framework
Tomas Doran
 
Tech talk microservices debugging
Andrey Kolodnitsky
 
Debugging Microservices - key challenges and techniques - Microservices Odesa...
Lohika_Odessa_TechTalks
 
Building the ideal betting stack | London Erlang User Group presentation
Erlang Solutions
 
Elixir introduction
Al Sayed Gamal
 
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
South Tyrol Free Software Conference
 
General Introduction to technologies that will be seen in the school
ISSGC Summer School
 
Dataflow: Declarative concurrency in Ruby
Larry Diehl
 
Distributed Systems in Data Engineering
Oluwasegun Matthew
 
Suman's PhD Candidacy Talk
Suman Srinivasan
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
10 fn s03
Scott Foster
 
10 fn s03
Scott Foster
 
Writing a REST Interconnection Library in Swift
Pablo Villar
 

More from Elixir Club (20)

PDF
Kubernetes + Docker + Elixir - Alexei Sholik, Andrew Dryga | Elixir Club Ukraine
Elixir Club
 
PDF
Integrating 3rd parties with Ecto - Eduardo Aguilera | Elixir Club Ukraine
Elixir Club
 
PDF
— An async template - Oleksandr Khokhlov | Elixir Club Ukraine
Elixir Club
 
PDF
BEAM architecture handbook - Andrea Leopardi | Elixir Club Ukraine
Elixir Club
 
PDF
You ain't gonna need write a GenServer - Ulisses Almeida | Elixir Club Ukraine
Elixir Club
 
PDF
— Knock, knock — An async templates — Who’s there? - Alexander Khokhlov | ...
Elixir Club
 
PDF
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Elixir Club
 
PDF
Erlang cluster. How is it? Production experience. — Valerii Vasylkov | Elixi...
Elixir Club
 
PDF
Promo Phx4RailsDevs - Volodya Sveredyuk
Elixir Club
 
PDF
Web of today — Alexander Khokhlov
Elixir Club
 
PDF
ElixirConf Eu 2018, what was it like? – Eugene Pirogov
Elixir Club
 
PDF
Implementing GraphQL API in Elixir – Victor Deryagin
Elixir Club
 
PDF
WebPerformance: Why and How? – Stefan Wintermeyer
Elixir Club
 
PDF
GenServer in Action – Yurii Bodarev
Elixir Club
 
PDF
Russian Doll Paradox: Elixir Web without Phoenix - Alex Rozumii
Elixir Club
 
PDF
Practical Fault Tolerance in Elixir - Alexei Sholik
Elixir Club
 
PDF
Phoenix and beyond: Things we do with Elixir - Alexander Khokhlov
Elixir Club
 
PDF
Monads are just monoids in the category of endofunctors - Ike Kurghinyan
Elixir Club
 
PDF
Craft effective API with GraphQL and Absinthe - Ihor Katkov
Elixir Club
 
PDF
Elixir in a service of government - Alex Troush
Elixir Club
 
Kubernetes + Docker + Elixir - Alexei Sholik, Andrew Dryga | Elixir Club Ukraine
Elixir Club
 
Integrating 3rd parties with Ecto - Eduardo Aguilera | Elixir Club Ukraine
Elixir Club
 
— An async template - Oleksandr Khokhlov | Elixir Club Ukraine
Elixir Club
 
BEAM architecture handbook - Andrea Leopardi | Elixir Club Ukraine
Elixir Club
 
You ain't gonna need write a GenServer - Ulisses Almeida | Elixir Club Ukraine
Elixir Club
 
— Knock, knock — An async templates — Who’s there? - Alexander Khokhlov | ...
Elixir Club
 
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Elixir Club
 
Erlang cluster. How is it? Production experience. — Valerii Vasylkov | Elixi...
Elixir Club
 
Promo Phx4RailsDevs - Volodya Sveredyuk
Elixir Club
 
Web of today — Alexander Khokhlov
Elixir Club
 
ElixirConf Eu 2018, what was it like? – Eugene Pirogov
Elixir Club
 
Implementing GraphQL API in Elixir – Victor Deryagin
Elixir Club
 
WebPerformance: Why and How? – Stefan Wintermeyer
Elixir Club
 
GenServer in Action – Yurii Bodarev
Elixir Club
 
Russian Doll Paradox: Elixir Web without Phoenix - Alex Rozumii
Elixir Club
 
Practical Fault Tolerance in Elixir - Alexei Sholik
Elixir Club
 
Phoenix and beyond: Things we do with Elixir - Alexander Khokhlov
Elixir Club
 
Monads are just monoids in the category of endofunctors - Ike Kurghinyan
Elixir Club
 
Craft effective API with GraphQL and Absinthe - Ihor Katkov
Elixir Club
 
Elixir in a service of government - Alex Troush
Elixir Club
 
Ad

Recently uploaded (20)

PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
July Patch Tuesday
Ivanti
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Ad

Designing scalable application: from umbrella project to distributed system - Anton Mishchuk