Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Choreography vs Orchestration in services

Choreography vs Orchestration in services

Mete Atamel

May 02, 2022
Tweet

More Decks by Mete Atamel

Other Decks in Programming

Transcript

  1. Choreography vs Orchestration in serverless microservices Mete Atamel Developer Advocate

    at Google @meteatamel atamel.dev speakerdeck.com/meteatamel
  2. Imagine a simple e-commerce transaction Services calling each other directly

    Frontend App Engine Order request Payment Processor Cloud Run Authorize & charge CC Shipper Cloud Functions Prepare & ship items Notifier Cloud Run Notify user
  3. Simple REST: Pros and Cons Pros ➕ Better than a

    single monolith ➕ Easy to implement: Services simply call each other Cons ➖ Too much coupling ➖ Each service can be a single point of failure ➖ Each service needs its own error / retry / timeout logic ➖ Who ensures the whole transaction is successful? (hint: saga pattern)
  4. Choreography (event-driven) Event-driven services Frontend App Engine Order request Payment

    Processor Cloud Run Authorize & charge CC Shipper Cloud Functions Prepare & ship items Notifier Cloud Run Notify user Message Broker
  5. Choreography: Pros and Cons Pros ➕ Services are loosely coupled

    ➕ Services can be changed/scaled independently ➕ No single point of failure ➕ Events are useful to extend the system Cons ➖ Difficult to monitor ➖ Errors / retries / timeouts are hard ➖ The business flow is not captured explicitly ➖ Who ensures the whole transaction is successful?
  6. Orchestration Orchestrated services Frontend App Engine Order request Payment Processor

    Cloud Run Authorize & charge CC Shipper Cloud Functions Prepare & ship items Notifier Cloud Run Notify user Orchestrator
  7. Orchestration: Pros and Cons Pros ➕ Business flow captured centrally

    and source controlled ➕ Each step can be monitored ➕ Errors / retries / timeouts are centralized ➕ Services are still independent Cons ➖ A new orchestrator service to learn and maintain ➖ Orchestrator could be a single point of failure ➖ Loss of eventing flexibility ➖ How do you compensate for failed steps? (part of saga pattern)
  8. It depends... Choreography Services are not closely related or can

    exist in different bounded contexts Orchestration Are services closely related? Can you describe the business logic in a flow chart?
  9. Hybrid approach Orchestrated bounded contexts communicating via events Orchestrated Bounded

    Context Message Broker Orchestrated Bounded Context Orchestrated Bounded Context
  10. Choreography (event-driven) AWS: SQS, SNS, EventBridge Azure: Event Grid, Event

    Hubs, Service Bus Google Cloud: Pub/Sub, Eventarc Other: Kafka, Pulsar, Solace PubSub+, RabbitMQ, NATS...
  11. AWS Step Functions Azure Logic Apps Workflows Organization AWS Microsoft

    Google Data serialization JSON JSON JSON and YAML Workflow metadata structure Yes? Yes? No? Named steps Yes Yes Yes Basic jumps Yes Yes Yes Step execution states Yes? Yes? No? Workflow execution state Yes Yes Yes Conditional jumps Yes Yes Yes HTTP Requests No Yes Yes Runtime parameters Yes Yes Yes Static variables definitions Yes Yes Yes Pass variable between steps Yes Yes Yes Data types supported JSON type String,Int,Double,Bool,Array,Ob ject,SecureString,SecureObject String, Int, Double, Bool, Array, Map/Object,Null Data type definition Implicit (JSON) Implicit Implicit JSON -> Dictionary Yes Yes Yes Arrays Yes Yes Yes Dictionaries Yes Yes Yes Array iterations Yes Yes Yes Parallel Array iteration Yes Yes No Built-in for(i =0;i<10;i++) Yes Yes Preview Parallel step execution Yes Yes No
  12. Subworkflows No Yes Yes Expressions - math operators Yes Yes

    Yes Expressions - string functions Yes Yes Yes Expressions - other functions (SHA..) No Yes No Embedded JS No Yes No Environment Variables No Yes Limited Callbacks Yes Yes Preview Secrets No Yes Yes Bash commands No No No Delay Yes Yes Yes OnError Yes Yes Yes Retry Yes Yes Yes Return output Yes Yes Yes Input / output from a local file No No No echo to stdout No No No Build and run docker container steps No No No Open source runtime No No No User Interface - visualization Yes Yes Yes User Interface - visual editing Yes Yes No Connectors library Some Yes Yes AWS Step Functions Azure Logic Apps Workflows
  13. CNCF Serverless Workflow serverlessworkflow.io Defines a vendor-neutral, open-source, and fully

    community-driven ecosystem for defining and running DSL-based workflows that target the Serverless technology domain. • Specification for defining DSL-based workflows • Developer SDKs for different programming languages • Workflow runtimes supporting the specification • Developer tooling support for writing DSL-based workflows
  14. Serverless Compute External API’s Google API’s etc... Workflows - orchestrate

    & integrate SaaS API’s Private API’s Other Clouds github.com/GoogleCloudPlatform/workflows-demos/tree/master/service-chaining
  15. - processPayment: params: [paymentDetails] call: http.post args: url: https://payment-processor.run.app/... body:

    input: ${paymentDetails} result: processResult - shipItems: call: http.post args: url: https://.../cloudfunctions.net/ship body: input: ${processResult.body} result: shipResult - notifyUser: call: http.post ... Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items YAML or JSON syntax
  16. Payment Processor Cloud Run Authorize & charge CC Notifier Cloud

    Run Notify user Shipper Cloud Functions Prepare & ship items Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items WAIT Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items shipmentDetails userDetails Step Sequencing Serverless Pause Variable passing JSON Parsing Steps
  17. Errors and retries Payment Processor Cloud Run Authorize & charge

    CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items MAX: 5 times BACKOFF Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items Pager Cloud Run Escalate to support SUCCESS ERROR Configurable retries Configurable exception handling
  18. Conditionals and 3rd party calls Notifier Cloud Run Notify user

    Shipper Cloud Functions Prepare & ship items Pager Cloud Run Escalate to support SUCCESS ERROR Out of Stock? No Request from the supplier Yes Read inventory Inventory DB Update inventory Inventory DB Supplier API
  19. Other useful features Subworkflows to encapsulate common reusable flows Connectors

    to connect to other Google Cloud services & APIs More iterations, callbacks
  20. Deploy, execute, manage workflows # Deploy a workflow gcloud workflows

    deploy my-workflow --source=workflow.yaml # Execute a workflow gcloud workflows execute my-workflow # See the result gcloud workflows executions describe <your-execution-id> --workflow my-workflow
  21. Lessons Learned ➕ Simple REST was refreshing (vs. 3 eventing

    formats) ➕ Less code (eg. no event parsing, no Image Analysis & Garbage Collector functions) ➕ Less setup (eg. no Pub/Sub, no Scheduler, no Eventarc) ➕ Easier error handling (eg. the whole chain stops on error)
  22. Lessons Learned ➖ New service to learn with its quirks

    and limitations ➖ Code vs. YAML, a single YAML file ➖ Debugging / testing / logging, no IDE support ➖ Loss of parallelism & eventing flexibility
  23. Serverless + Serverful You can combine Serverless orchestration and VMs

    for best of both worlds 1. Containerize the long-running task, so it can run anywhere. 2. Plan to run the container on a Compute Engine VM with no time limitations. 3. Automate the creation of the VM, running of the container on the VM, and deletion of the VM with Workflows. github.com/GoogleCloudPlatform/workflows-demos/tree/master/long-running-container