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

[RubyWine #1] Event-Driven Architecture and Mes...

[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. Who I am Who I am Ruby/JS developer, Tech Lead

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

    at Two years with Microservices Maintainer of Ruby/Rails digest on
  3. 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
  4. 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
  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 kirillshevch kirill_shevch
  6. Sync vs Async Approach Sync vs Async Approach Two Ways

    Two Ways The publisher writes a message and waits until reader confirm the reading
  7. 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
  8. 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
  9. 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
  10. 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
  11. A gem for building API Wrappers in Ruby A gem

    for building API Wrappers in Ruby
  12. 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"}
  13. Messages Messages The basic unit of communication in Message Brokers

    The basic unit of communication in Message Brokers
  14. 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
  15. 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.
  16. Commands Commands It's a one-to-one communication between publisher It's a

    one-to-one communication between publisher and subscriber and subscriber
  17. 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
  18. 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
  19. 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
  20. Topic Topic Queue Queue vs All subscribers will receive a

    All subscribers will receive a copy of the message copy of the message
  21. 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
  22. Queues Queues Pros Pros Simple messaging pattern with a transparent

    communication flow Messages can be recovered by putting them back on the queue
  23. 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
  24. Topics Topics Pros Pros Multiple consumers can get a message

    Decoupling between producer and consumers (publish-and- subscribe pattern)
  25. Cons Cons More complicated communication flow A message cannot be

    recovered for a single listener Topics Topics
  26. 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
  27. FIFO (First-In-First-Out) FIFO (First-In-First-Out) Queues Queues The order in which

    messages are sent and received is strictly preserved.
  28. Amazon SQS Amazon SQS Fully managed message queue Fully managed

    message queue Pros Pros Available as a cloud service Cons Cons
  29. 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
  30. 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)
  31. ActiveMQ ActiveMQ Pros Pros It has two main concepts: topics

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

    and queues Load Balancing: replication, horizontal scaling Support for most protocols
  33. 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
  34. 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)
  35. Protocols Protocols OpenWire OpenWire STOMP STOMP AMQP AMQP MQTT MQTT

    What to сhoose? What to сhoose? WebSocket WebSocket XMPP XMPP
  36. ActiveMQ ActiveMQ Instance can provide Instance can provide all protocols

    at the all protocols at the same time same time
  37. 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)
  38. 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
  39. Pub/Sub pattern Pub/Sub pattern redis = Redis.new redis.subscribe(:orders) do |on|

    on.message do |channel, message| # call the command end end
  40. 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
  41. Real life problems Real life problems No one really knows

    how to test microservices No one really knows how to test infrastructure for microservices
  42. Solutions? Solutions? Unit testing for each service with hard mocking

    Up the server with all services for running integration testing
  43. Monitoring Monitoring No more single system, no more centralized monitoring

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

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

    Logs and metrics collecting (Beats, Statsd, Elasticsearch) Aggregation (Logstash) Visualization (Kibana, Grafana)
  46. 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