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

Function Composition in a Serverless World (OSC...

Function Composition in a Serverless World (OSCON 2018)

Avatar for Soam Vasani

Soam Vasani

July 18, 2018
Tweet

More Decks by Soam Vasani

Other Decks in Programming

Transcript

  1. First, what’s FaaS? Function-as-a-Service enable developers to deploy parts of

    an application on an “as needed” basis using short-lived functions. Benefits of FaaS: • Complete abstraction of servers away from the developer • Billing based on consumption and executions, not server instance sizes • Scaling services is simplified !2
  2. !5 How should we combine both functions into one? Recognize

    + Translate Gato “Composed” Function
  3. Many approaches !6 •Manual compilation •Direct function calls or “chaining”

    •Coordinator pattern •Event-driven •Workflows In increasing order of complexity:
  4. Manual Compilation Merge functions on a source code level. •

    One big function that calls all other individual functions. • One big task from FaaS framework’s point-of-view. !7
  5. !8 func recognizeImage(image) { // A: Call API for image

    tagging. } func translate() { // B: Translate text } func combo() { recognizeImage(…) translate(…) }
  6. Pros: • Very simple, no framework needed at all •

    No serialization overhead Cons: • Function gets bigger; more expensive and may load slowly • Cannot scale independently !9
  7. Merged Function Scaling Function A Function B vs. Instance 1

    Instance 2 Instance 1 Instance 2 !10 Function B Function B Function A Function A Function B Function A Function A
  8. Direct Function Calls (chaining) Form a chain, calling each other

    over HTTP. • Each task is a separate FaaS function. • Each function knows what comes after it and calls it. !11
  9. func recognizeImage(image) { // A: Recognize image // HTTP call

    to translation function } func translate() { // B: Translate text } !12
  10. Pros: Cons: • Each function waits for the next function,

    wasting $ • Messaging complexity: handling failures, and thinking about fallbacks, retries. • Function updates are error-prone • Cold starts add up • No external components needed • Independent scaling, unlike manual compilation !13
  11. Coordinator Pattern Functions that manage the execution of other functions.

    • One “omniscient” function calls each function (via remote HTTP); manages the execution flow. • Similar to direct functions, except each function is unaware of the other functions. !14
  12. !15

  13. Pros: • No need to modify the primitive functions •

    Very flexible; user can manipulate the control flow how they like. (Separation of concerns) Cons: • Overhead of an extra function • Even more cold starts • Coordinator is a long running function (it starts first, and ends last). !16
  14. Event-Driven Function Composition !18 Focus on the data flow instead

    of the control flow: computation as reactions to events
  15. !19

  16. !20

  17. • Get all the luxury of message queues (e.g. message

    retries, error handling, persistence). • Loosely coupled functions • Commonly used and well understood architecture. Pros • Web of implicit dependencies. • Difficult to version or upgrade functions. • Supports limited control flow constructs. (e.g. conditional and on-error constructs) Cons !21
  18. !23

  19. • Centralization of composition logic, logging, and visualization • Upgrade

    a workflow + set of functions “consistently” • Loosely coupled functions • Handles communication complexity (latency, retries, failures, etc.) • Improved performance (better/anticipating scheduling of functions) Pros • More infrastructure complexity • Need to learn workflow-specific language or DSL Cons !27
  20. Which approach should you use? !34 • It Depends! •

    Function size • Function resource usage • Your app’s performance and cost requirements • Tolerance for complexity and operational effort
  21. • A workflow is just another function! • No magic/privileged

    functions or constructs • Dynamic tasks: use conditional branches. • (JavaScript) expressions in workflows for simple data selections and transformations • Tools for debugging and inspecting workflows Advanced Concepts !38
  22. YAML Workflow RecognizeImage Translate tasks: RecognizeImage: … Translate: … requires:

    - RecognizeImage This YAML file specifies the sequence of the function logic. !39
  23. Pros: • Workflow Framework sees the whole picture • “Consistent”

    Upgrades (in theory at least…) • Loosely coupled functions, unaware that they are being used in a workflow • Messaging abstracted away: latency, retries, failures, queueing… • Can be reliable/fault-tolerant. • Engine predicts invocations to improve the performance (pre-boot functions). Cons: • Adds additional component = complexity • Workflow-specific language to learn… usually YAML (some sort of learning curve), Usually not a general purpose language • Hard to unit test and debug, depending on maturity of framework !40