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

Resilient Microservices in Vapor

Resilient Microservices in Vapor

How to build Microservice applications in a resilient and abstract fashion in the Swift Vapor framework.

Caleb Kleveter

October 31, 2019
Tweet

Other Decks in Programming

Transcript

  1. ABOUT ME ▸ Contract Software Developer for Skelpo Inc. ▸

    github.com/skelpo ▸ github.com/SwiftCommerce
  2. ABOUT ME ▸ Contract Software Developer for Skelpo Inc. ▸

    github.com/skelpo ▸ github.com/SwiftCommerce ▸ Vapor Fanatic
  3. ABOUT ME ▸ Contract Software Developer for Skelpo Inc. ▸

    github.com/skelpo ▸ github.com/SwiftCommerce ▸ Vapor Fanatic ▸ Maintainer for Several Core Vapor Repositories
  4. ABOUT ME ▸ @calebkleveter ▸ @calebkleveter ▸ The first time

    I tried sushi I ate all the wasabi at once.
  5. WHY MICROSERVICES: MAINTENANCE ▸ Isolation of Bugs ▸ Easier to

    Understand ▸ Increased Ease of Evolution
  6. SO WHAT ARE RESILIENT MICROSERVICES? ▸ Services with code that

    is written to last (i.e. not made up of ‘quick fixes’).
  7. SO WHAT ARE RESILIENT MICROSERVICES? ▸ Services with code that

    is written to last (i.e. not made up of ‘quick fixes’). ▸ Services with code that can easily be added too or removed from.
  8. SO WHAT ARE RESILIENT MICROSERVICES? ▸ Services with code that

    is written to last (i.e. not made up of ‘quick fixes’). ▸ Services with code that can easily be added too or removed from. ▸ Services with APIs that age well and are predictable.
  9. ABSTRACT LIBRARIES IN THE WILD ‣Defines file I/O for any

    backend. skelpo/Storage by yours truly
  10. ABSTRACT LIBRARIES IN THE WILD ‣Defines file I/O for any

    backend. ‣Comes with a default LocalStorage Implementation. skelpo/Storage by yours truly
  11. ABSTRACT LIBRARIES IN THE WILD ‣Defines file I/O for any

    backend. ‣Comes with a default LocalStorage Implementation. ‣Separate implementations for Amazon S3 and Google Cloud Storage. skelpo/Storage by yours truly
  12. ABSTRACT LIBRARIES IN THE WILD ‣Defines file I/O for any

    backend. ‣Comes with a default LocalStorage Implementation. ‣Separate implementations for Amazon S3 and Google Cloud Storage. ‣*Also has a File type for handling file data representation. skelpo/Storage by yours truly
  13. RECAP: MICROSERVICES AT 50,000” ▸ They can increase reliability and

    save user frustration. ▸ They can provide a better development experience.
  14. RECAP: MICROSERVICES AT 50,000” ▸ They can increase reliability and

    save user frustration. ▸ They can provide a better development experience. ▸ They are a good thing, but they need to be built correctly.
  15. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it
  16. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it ▸ REST, GraphQL, SOAP, gRPC, etc.
  17. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it ▸ REST, GraphQL, SOAP, gRPC, etc.
  18. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it ▸ REST, GraphQL, SOAP, gRPC, etc. ▸ Makes your API predictable.
  19. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it ▸ REST, GraphQL, SOAP, gRPC, etc. ▸ Makes your API predictable. ▸ The client knows what a route should look like.
  20. USE A STANDARDIZED API FORMAT ▸ Pick the API architecture

    you are going to use and stick with it ▸ REST, GraphQL, SOAP, gRPC, etc. ▸ Makes your API predictable. ▸ The client knows what a route should look like. ▸ You know how new routes should look.
  21. USE A STANDARDIZED API FORMAT ▸ Easily abstract the implementation

    for different models. ▸ Resources: ▸ restapitutorial.com ▸ graphql.org ▸ grpc.io
  22. KEEP A CHANGEGLOG ▸ A single source for all changes

    between versions. ▸ What is the good of knowing something changed if you don’t know what ‘something’ is?
  23. KEEP A CHANGEGLOG ▸ A single source for all changes

    between versions. ▸ What is the good of knowing something changed if you don’t know what ‘something’ is? ▸ Resource: keepachangelog.com
  24. CREATE MATCHING DATA STRUCTURES ▸ Use matching data structures for

    matching data sets across your microservices.
  25. CREATE MATCHING DATA STRUCTURES ▸ Use matching data structures for

    matching data sets across your microservices. ▸ A Tale of Two Data Structures: {}.currency == {}.currency_code
  26. USE YOUR REQUEST HEADERS ▸ Use request headers to get

    the client the data they want. ▸ Headers that define the expected response include Accept, Accept-*, and If-*.
  27. USE YOUR REQUEST HEADERS ▸ Use request headers to get

    the client the data they want. ▸ Headers that define the expected response include Accept, Accept-*, and If-*. ▸ Resource: developer.mozilla.org/docs/Web/HTTP/ Headers
  28. RECAP: EXTERNAL RESILIENCE ▸ Use a standardized API format. ▸

    Follow SemVer. ▸ Keep a changelog. ▸ Use consistent data structures.
  29. RECAP: EXTERNAL RESILIENCE ▸ Use a standardized API format. ▸

    Follow SemVer. ▸ Keep a changelog. ▸ Use consistent data structures. ▸ Make use of the request headers.
  30. INTERNAL ABSTRACTION ‣ Databases ‣ Web APIs Use abstractions for

    interfacing with external data sources, such as:
  31. INTERNAL ABSTRACTION ‣ Databases ‣ Web APIs ‣ Files Use

    abstractions for interfacing with external data sources, such as:
  32. USE ROUTE COLLECTIONS ▸ Conform your controller types to RouteCollection.

    ▸ Register the collection to your router. ▸ Registers your route handlers where they are defined.
  33. USE ROUTE COLLECTIONS ▸ Conform your controller types to RouteCollection.

    ▸ Register the collection to your router. ▸ Registers your route handlers where they are defined. ▸ Reduces complexity in the routes function.
  34. PUTTING THE APPLICATION INTO A PROVIDER ▸ Abstracts the application

    from the database. ▸ Plug the app into any other app.
  35. PUTTING THE APPLICATION INTO A PROVIDER ▸ Abstracts the application

    from the database. ▸ Plug the app into any other app. ▸ Custom boot sequences for different app instances.
  36. PUTTING THE APPLICATION INTO A PROVIDER ▸ Abstracts the application

    from the database. ▸ Plug the app into any other app. ▸ Custom boot sequences for different app instances. ▸ Dependency injection for provider instances.
  37. PUTTING THE APPLICATION INTO A PROVIDER ▸ Abstracts the application

    from the database. ▸ Plug the app into any other app. ▸ Custom boot sequences for different app instances. ▸ Dependency injection for provider instances. ▸ Default services (using services.extend).
  38. RECAP: INTERNAL ABSTRACTIONS ▸ Use interfaces (the repository pattern). ▸

    Register your Vapor services under their protocol type.
  39. RECAP: INTERNAL ABSTRACTIONS ▸ Use interfaces (the repository pattern). ▸

    Register your Vapor services under their protocol type. ▸ Use route collections to build your controllers.
  40. RECAP: INTERNAL ABSTRACTIONS ▸ Use interfaces (the repository pattern). ▸

    Register your Vapor services under their protocol type. ▸ Use route collections to build your controllers. ▸ Maybe try putting your app in a provider.
  41. RECAP: INTERNAL ABSTRACTIONS ▸ Use interfaces (the repository pattern). ▸

    Register your Vapor services under their protocol type. ▸ Use route collections to build your controllers. ▸ Maybe try putting your app in a provider. ▸ Check out the Vapor Style Guide. docs.vapor.codes/3.0/extras/style-guide