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

[RubyWine #1] Event-Driven Architecture and Messaging Patterns for Ruby Microservices

[RubyWine #1] Event-Driven Architecture and Messaging Patterns for Ruby Microservices

How to choose a communication protocol for services? Synchronous vs Asynchronous approach. HTTP, Brokered Messaging and gRPC. Issues when working with Threads and asynchronous message queues in Ruby and their solutions. Logging and monitoring of transactional integrity. Publish/Subscribe messaging systems and a brief comparison of them.

Video - https://youtu.be/e9AAUy4kkek

Kirill Shevchenko

April 13, 2019
Tweet

More Decks by Kirill Shevchenko

Other Decks in Programming

Transcript

  1. Event-Driven Architecture
    Event-Driven Architecture
    Messaging Patterns for Ruby Microservices
    https://bit.ly/2RSudnT
    Kirill Shevchenko
    Kirill Shevchenko

    View Slide

  2. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at

    View Slide

  3. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at
    Two years with Microservices

    View Slide

  4. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at
    Two years with Microservices
    Maintainer of Ruby/Rails digest on

    View Slide

  5. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at
    Two years with Microservices
    Maintainer of Ruby/Rails digest on
    Live in Dnipro, Ukraine

    View Slide

  6. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at
    Two years with Microservices
    Maintainer of Ruby/Rails digest on
    Live in Dnipro, Ukraine
    kirill_shevch

    View Slide

  7. Who I am
    Who I am
    Ruby/JS developer, Tech Lead at
    Two years with Microservices
    Maintainer of Ruby/Rails digest on
    Live in Dnipro, Ukraine
    kirillshevch
    kirill_shevch

    View Slide

  8. Agenda
    Agenda
    Road from Monolith to Microservices
    Communication Patterns
    Message Brokers
    Publish/Subscribe
    Testing and Monitoring

    View Slide

  9. Microservices Are
    Microservices Are
    Something You Grow Into,
    Something You Grow Into,
    Not Begin With
    Not Begin With

    View Slide

  10. Let The Adventure
    Let The Adventure
    Begin
    Begin

    View Slide

  11. "Hello World" Setup
    "Hello World" Setup
    +

    View Slide

  12. "Hello World" Setup
    "Hello World" Setup

    View Slide

  13. Background Jobs
    Background Jobs
    +

    View Slide

  14. Background Jobs
    Background Jobs

    View Slide

  15. Cache
    Cache

    View Slide

  16. Business logic size
    Business logic size
    Orders?

    View Slide

  17. Business logic size
    Business logic size
    Products?
    Orders?

    View Slide

  18. Business logic size
    Business logic size
    Analytics?
    Products?
    Orders?

    View Slide

  19. Business logic size
    Business logic size
    Analytics?
    Products?
    Orders?
    Users?

    View Slide

  20. Applying DDD to Microservices
    Applying DDD to Microservices

    View Slide

  21. Pattern: Database Per Service
    Pattern: Database Per Service

    View Slide

  22. Pattern: Database Per Service
    Pattern: Database Per Service

    View Slide

  23. Pattern: Database Per Service
    Pattern: Database Per Service

    View Slide

  24. Messaging Patterns
    Messaging Patterns

    View Slide

  25. Sync vs Async Approach
    Sync vs Async Approach
    Two Ways
    Two Ways

    View Slide

  26. Sync vs Async Approach
    Sync vs Async Approach
    Two Ways
    Two Ways
    The publisher writes a message and waits until reader
    confirm the reading

    View Slide

  27. Sync vs Async Approach
    Sync vs Async Approach
    Two Ways
    Two Ways
    The publisher writes a message and waits until reader
    confirm the reading
    The writer doesn't wait for the reader, you can read
    the message at any time

    View Slide

  28. Synchronous Calls
    Synchronous Calls
    Probably, easiest communication pattern to
    Probably, easiest communication pattern to
    implement is simply calling another service
    implement is simply calling another service
    synchronously, usually via REST.
    synchronously, usually via REST.
    Service 1 Service 2
    REST

    View Slide

  29. Synchronous Calls
    Synchronous Calls
    Protocols
    Protocols
    HTTP/RPC
    HTTP/RPC

    View Slide

  30. Synchronous Calls
    Synchronous Calls
    Protocols
    Protocols
    HTTP/RPC
    HTTP/RPC
    except for
    except for
    streaming
    streaming

    View Slide

  31. HTTP/REST IS GREAT
    HTTP/REST IS GREAT
    Easy to understand (text protocol)
    Web infrastructure already built on top of HTTP
    Great tooling for testing, inspection, modification
    Loose coupling between clients/server makes changes easy
    High-quality http implementations in every language

    View Slide

  32. Synchronous Calls
    Synchronous Calls
    Problems?
    Problems?
    You need wait for a response
    You need to handle errors (like timeouts)
    You work with many different APIs
    You you use a lot of different API wrappers

    View Slide

  33. A gem for building API Wrappers in Ruby
    A gem for building API Wrappers in Ruby

    View Slide

  34. ApiStruct: Configuration
    ApiStruct: Configuration
    ApiStruct::Settings.configure do |config|
    config.endpoints = {
    some_api: {
    root: 'https://someapi.dev/v1'
    }
    }
    end

    View Slide

  35. ApiStruct: Client
    ApiStruct: Client
    class OrdersClients < ApiStruct::Client
    some_api :orders
    def index
    get
    end
    def show(id)
    get(id)
    end
    end

    View Slide

  36. ApiStruct: Entity
    ApiStruct: Entity
    class Order < ApiStruct::Entity
    client_service OrdersClient
    attr_entity :id, :state, :total_price
    end

    View Slide

  37. ApiStruct: Entity
    ApiStruct: Entity
    class Order < ApiStruct::Entity
    client_service OrdersClient
    attr_entity :id, :state, :total_price
    end
    order = Order.show(1)
    # => {"id"=>1, "state"=>"shipping",
    "total_price"=>"99"}

    View Slide

  38. Event-Driven Architecture
    Event-Driven Architecture
    (
    (EDA
    EDA)
    )

    View Slide

  39. Сommunication Сoncepts
    Сommunication Сoncepts
    Messages
    Messages
    Events
    Events
    Commands
    Commands

    View Slide

  40. Messages
    Messages
    The basic unit of communication in Message Brokers
    The basic unit of communication in Message Brokers

    View Slide

  41. Messages
    Messages
    The basic unit of communication in Message Brokers
    The basic unit of communication in Message Brokers
    Literally can be anything
    Literally can be anything

    View Slide

  42. Messages
    Messages
    The basic unit of communication in Message Brokers
    The basic unit of communication in Message Brokers
    Literally can be anything
    Literally can be anything
    An ID, a string, an object, a
    An ID, a string, an object, a
    command, an event or whatever.
    command, an event or whatever.

    View Slide

  43. Commands
    Commands
    It's a one-to-one communication between publisher
    It's a one-to-one communication between publisher
    and subscriber
    and subscriber

    View Slide

  44. Commands
    Commands
    It's a one-to-one communication between publisher
    It's a one-to-one communication between publisher
    and subscriber
    and subscriber
    Event
    Publisher
    Subscriber

    View Slide

  45. Events
    Events
    An event is a message which informs listeners about
    An event is a message which informs listeners about
    something which has happened
    something which has happened

    View Slide

  46. Events
    Events
    An event is a message which informs listeners about
    An event is a message which informs listeners about
    something which has happened
    something which has happened
    Event
    Publisher
    Subscriber 1 Subscriber 2

    View Slide

  47. Topic
    Topic Queue
    Queue
    vs

    View Slide

  48. Topic
    Topic Queue
    Queue
    vs
    All subscribers will receive a
    All subscribers will receive a
    copy of the message
    copy of the message

    View Slide

  49. All subscribers will receive a
    All subscribers will receive a
    copy of the message
    copy of the message
    A single message will be
    A single message will be
    received by exactly one
    received by exactly one
    consumer
    consumer
    Topic
    Topic Queue
    Queue
    vs

    View Slide

  50. Queues
    Queues
    Pros
    Pros
    Simple messaging pattern with a
    transparent communication flow
    Messages can be recovered by
    putting them back on the queue

    View Slide

  51. Cons
    Cons
    Only one consumer can get the
    message
    Implies a coupling between
    producer and consumer as it’s
    an one-to-one relation
    Queues
    Queues

    View Slide

  52. Topics
    Topics
    Pros
    Pros
    Multiple consumers can get a
    message
    Decoupling between producer
    and consumers (publish-and-
    subscribe pattern)

    View Slide

  53. Cons
    Cons
    More complicated
    communication flow
    A message cannot be recovered
    for a single listener
    Topics
    Topics

    View Slide

  54. Virtual Topics
    Virtual Topics
    Pros
    Pros
    Multiple consumers can get a message
    Decoupling between producer and
    consumers (publish-and-subscribe
    pattern)
    Messages can be recovered by putting
    them back on the queue

    View Slide

  55. Virtual Topics
    Virtual Topics
    Cons
    Cons
    Might require additional
    configuration in the broker

    View Slide

  56. FIFO (First-In-First-Out)
    FIFO (First-In-First-Out)
    Queues
    Queues
    The order in which messages are sent
    and received is strictly preserved.

    View Slide

  57. Message Brokers
    Message Brokers

    View Slide

  58. Checklist
    Checklist
    Services discovery
    Messaging
    Load balancing
    Logging
    Queuing

    View Slide

  59. ACTIVE
    Apache
    MQ

    View Slide

  60. Amazon SQS
    Amazon SQS
    Fully managed message queue
    Fully managed message queue

    View Slide

  61. Amazon SQS
    Amazon SQS
    Fully managed message queue
    Fully managed message queue
    Pros
    Pros Cons
    Cons

    View Slide

  62. Amazon SQS
    Amazon SQS
    Fully managed message queue
    Fully managed message queue
    Pros
    Pros
    Available as a cloud service
    Cons
    Cons

    View Slide

  63. Amazon SQS
    Amazon SQS
    Fully managed message queue
    Fully managed message queue
    Pros
    Pros
    Available as a cloud service
    Cons
    Cons
    FIFO queues support only 300
    messages per second

    View Slide

  64. Amazon SQS
    Amazon SQS
    Fully managed message queue
    Fully managed message queue
    Pros
    Pros
    Available as a cloud service
    Cons
    Cons
    FIFO queues support only 300
    messages per second
    No ability to use FIFO with
    Pub/Sub (SNS)

    View Slide

  65. ACTIVE
    Apache
    MQ

    View Slide

  66. ActiveMQ
    ActiveMQ
    Pros
    Pros
    It has two main concepts: topics
    and queues

    View Slide

  67. ActiveMQ
    ActiveMQ
    Pros
    Pros
    It has two main concepts: topics
    and queues
    Load Balancing: replication,
    horizontal scaling

    View Slide

  68. ActiveMQ
    ActiveMQ
    Pros
    Pros
    It has two main concepts: topics
    and queues
    Load Balancing: replication,
    horizontal scaling
    Support for most protocols

    View Slide

  69. ActiveMQ
    ActiveMQ
    Pros
    Pros
    It has two main concepts: topics
    and queues
    Load Balancing: replication,
    horizontal scaling
    Support for most protocols
    Cons
    Cons
    Need backups

    View Slide

  70. ActiveMQ
    ActiveMQ
    Pros
    Pros
    It has two main concepts: topics
    and queues
    Load Balancing: replication,
    horizontal scaling
    Support for most protocols
    Cons
    Cons
    Need backups
    Might require additional
    configuration(FIFO, Virtual Topics)

    View Slide

  71. Protocols
    Protocols

    View Slide

  72. Protocols
    Protocols
    OpenWire
    OpenWire
    STOMP
    STOMP
    AMQP
    AMQP
    MQTT
    MQTT
    What to сhoose?
    What to сhoose?
    WebSocket
    WebSocket
    XMPP
    XMPP

    View Slide

  73. ActiveMQ
    ActiveMQ
    Instance can provide
    Instance can provide
    all protocols at the
    all protocols at the
    same time
    same time

    View Slide

  74. ActiveMQ
    ActiveMQ
    Endpoints
    Endpoints

    View Slide

  75. Publish/Subscribe
    Publish/Subscribe
    pattern and Ruby
    pattern and Ruby

    View Slide

  76. Publishing is simply
    Publishing is simply
    options = {
    command: 'update_order',
    data: { order_id: 1, state: :paid }
    }
    redis = Redis.new
    redis.publish(:orders, options.to_json)

    View Slide

  77. But we need handle
    But we need handle
    pubishing result
    pubishing result
    options = {
    command: 'update_order',
    data: { order_id: 1, state: :paid }
    }
    redis = Redis.new
    redis.publish(:orders, options.to_json).tap do |r|
    Raven.capture_message('Publish failed', extra: options) if r.zero?
    end

    View Slide

  78. Pub/Sub pattern
    Pub/Sub pattern
    redis = Redis.new
    redis.subscribe(:orders) do |on|
    on.message do |channel, message|
    # call the command
    end
    end

    View Slide

  79. Pub/Sub pattern
    Pub/Sub pattern
    redis = Redis.new
    Thread.new do
    redis.subscribe(:orders) do |on|
    on.message do |channel, message|
    begin
    # call the command
    rescue StandardError => e
    Raven.capture_exception(e)
    end
    end
    end
    end

    View Slide

  80. Testing
    Testing

    View Slide

  81. View Slide

  82. Real life problems
    Real life problems
    No one really knows how to test
    microservices

    View Slide

  83. Real life problems
    Real life problems
    No one really knows how to test
    microservices
    No one really knows how to test
    infrastructure for microservices

    View Slide

  84. Solutions?
    Solutions?
    Unit testing for each service with hard
    mocking

    View Slide

  85. Solutions?
    Solutions?
    Unit testing for each service with hard
    mocking
    Up the server with all services for
    running integration testing

    View Slide

  86. Monitoring
    Monitoring
    No more single system, no more centralized
    monitoring

    View Slide

  87. Monitoring
    Monitoring
    No more single system, no more centralized
    monitoring
    Logs and metrics collecting (Beats, Statsd,
    Elasticsearch)

    View Slide

  88. Monitoring
    Monitoring
    No more single system, no more centralized
    monitoring
    Logs and metrics collecting (Beats, Statsd,
    Elasticsearch)
    Aggregation (Logstash)

    View Slide

  89. Monitoring
    Monitoring
    No more single system, no more centralized
    monitoring
    Logs and metrics collecting (Beats, Statsd,
    Elasticsearch)
    Aggregation (Logstash)
    Visualization (Kibana, Grafana)

    View Slide

  90. Monitoring
    Monitoring
    No more single system, no more centralized
    monitoring
    Logs and metrics collecting (Beats, Statsd,
    Elasticsearch)
    Aggregation (Logstash)
    Visualization (Kibana, Grafana)
    Store and maintain all this data and systems

    View Slide

  91. Thanks!
    Thanks!

    View Slide