Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paweloczadly/c3dd445cbc062c802e1d to your computer and use it in GitHub Desktop.
Save paweloczadly/c3dd445cbc062c802e1d to your computer and use it in GitHub Desktop.
Microservice in Scala - Warsjawa 2014

About

Luzne notatki z warsztatow "Microservices in Scala" na Warsjawa 2014

Microservices

  • Wyzszy poziom abstrakcji niz MVC.
  • Multiple small apps with REST/HTTP interfac. Porozumiewanie za pomoca REST/HTTP
  • Multiple small apps with deferred messafe processing. Aplikacje ktore przetwarzaja male wiadomosci (np. z RabbitMQ)
  • Duzo aplikacji dla kazdego kontrolera
  • Multiple single-file apps with less than 100 lines of code
  • Multiple small apps possibly written in different languages communicating with each other
  • Small single-purpose apps with bounded domain enabling polyglot programming
    • add your favourite ingredients
    • differences from monolithic architecture

Architektura

  • separation of concerns
  • sharing data in threads
  • komunikacja miedzy serwisami
  • kontrakty miedzy modulami
  • deployment strategies
  • non-functional guarantees
  • cache, cluster baz danych, sla
  • it is allowed to duplicate domain. Having common library is not effective. In case of a change in common redeployment of all dependend services is required.
  • if some modules will not be changed, they can be used as dependencies.

Roznice

Monolitic

  • Caly kod w jednym miejscu
  • Wszystkie moduly umieszczane obok siebie
  • Dluga kompilacja, tworzymy jedna duza paczke, testy jedn, int, ...
  • Migracja DB
  • Deployment: kazda z paczek jest identyczna, korzysta z tej samej DB, latwo skonfigurowac
  • Zaleznosci: global variables, messages, function calls, data structures, shared memory, multiple levels of indirection
  • Managed with: SOLID, encapsulation, dependency injection
  • Interfaces vs Contracts: biblioteki wystawiaja swoje API, mozna testowac przez testy i kompilatory, type validations, api calls defined by the language, encapsulation, SRP
  • Problems: meta-programming, monkey patching, global state and workaround

Microservices

  • Pojedyncze programy bardzo krotkie
  • Obrazy VM ze skonfigurowanymi appkami
  • Deployment: kazdy serwis zajmuje sie jedna rzecza, to jest ich duzo, nie ma porzadku, configuration management, service discovery (zookeeper, consul.io), monitoring
  • Zaleznosci: different protocols (np. serwisy sie komunikuja po HTTP), shared DB's, shared message queues
  • Managed with: contracts
  • Interfaces vs Contracts: maja kontrakt miedzy soba, spoken / unspoken, should be defined on 'paper', defined by protocols and technologies used like: HTTP, websockets, RPC, RabbitMQ, shared MongoDB
  • Problems: changing contract or having no contract requires changing multiple services, multiple API versions without easy solution for validating consistency

Zalety mikroserwisow

Small single purpose code

Serwisy musza wykonywac jedno zadanie. Zalety:

  • Mala zlozonosc (np. mniej niz 100 linii)
  • Nie wiazemy kodu z kodem innych modulow
  • 'this big' or one hundred lines of code
  • Easy to rewrite
  • Rewrite instead of refactor
  • Easy to spot inputs and outputs
  • Still requires readable logic

Metryki

  • business performance using relevant metrics (np. wystawilem 50000 faktur)
  • visaulization (ex. kamon, custom charts)
  • continuous deployment metrics

Polyglot programming

  • Mozemy pisac w roznych jezykach ale miec jeden kontrakt (np. HTTP)

Problemy

  • Synchronuous and asynchronous processing
  • SLAs and guarantees
  • Shared and private databases
  • Making layers of micro services
  • bare-metal vs platforms
  • data-drie systems, eventsourcinf and real-time messaging

Przetwarzanie sync vs async

Sync

  • natychmiastowa odp
  • fast and hard failure
  • asking
  • request timeouts
  • problematic to debug series of request

ex. chain of HTTP request and getting 200

Async

  • post jobs to be processed
  • tell don't ask
  • fire and forget
  • need of pooling and waiting for response
  • longer jobs
  • failures can be recovered from!

ex. uploading a video file to be processed and getting 'video uploaded successfully'

Shared vs private DBs

Shared

  • Preety strong contract
  • Convenient for simple domain problems
  • Easier to maintain
  • Problems:
    • coupling too many services, too deeply
    • can become performance bottle-neck

Private

  • useful for decoupling and reuse
  • useful for performance
  • useful for embedding in microservices that abstract complicated domain models
  • difficult to maintain, although can many DBs can work on single cluster/instance
  • useful for giving microservice authority over some data
  • problems
    • data sharding

Layers of microservices

  • frontend
    • CORS (cross origin resource sharing)
    • rendering of HTML: server-side or in js
    • moving caches to CDN
    • keeping modularity and reusability also on the frontend side
    • one backend microservice failure shouldn't break whole frontened
  • dns/routing/loadbalancing (wiedza gdzie sa microservicey)
  • user facing, services caches (np. autentykacja)
    • CORS
    • Versioning of API
    • Authentication
    • Caching
    • Loadbalancing
    • Security
  • internal services workers
    • Security
    • Scalability
    • Monitoring for failures
    • Keeping the data consistent

Platform abstraction layers

  • bare metal servers vs platforms
  • high level framework = more difficult polyglot programming + easier maintenance + possible code sharing + more tight coupling opportunities

Bare-metal

Unix processes

  • finagle
  • akka
  • jvm

Platforms

Multiple interfaces

  • only rest
  • only rabbitmq
  • only atom

Stateful databases vs eventsourcing

Stateful

  • state stored in the database
    • ex. John has 3122$ on his account
  • transactions and locks that enable us to mutate the state

Eventsourcing

  • no state just events
    • ex. John got spent 12$
  • state is inferred from past events
  • every situation can be replayed
  • events are immutable = no problem with locking

Spray

  • Client and server side REST/HTTP libs
  • Multiple layers
  • Really fast
  • Aquired by Typesafe
  • Spray => akka-http
    • reactive streams
    • new building blocks for Play
  • HttpRequest => HttpResponse
  • Different levels of abstraction, focus: DSL
  • Key koncepts: routing, marshalling
  • Routing:
    • directives:
      • transform, extract, filter, side-effecting
      • composable
      • concatanable (~) - jesli poprzednia dyrektywa odrzuci to nastepna jest wykonywana
    • useful directives:
      • path, pathprefix
      • get, post, put, delete, patch
      • parameter
      • formFields
      • entity
      • logRequestResponse
      • complete (!) - to co chcemy by wykonalo sie w zapytaniu. Caly kod w complete

SBT

~re-start po zmianie w kodzie restartuje serwer

Play

  • fully blown web framework
  • not very well-suited for microservices
  • Rails like (M)VC
  • routes => controllers => views
@michalgorny
Copy link

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment