About Microservices.io

Microservices.io is brought to you by Chris Richardson. Experienced software architect, author of POJOs in Action and the creator of the original CloudFoundry.com. His latest startup is eventuate.io, a microservices application platform.

Learn more about microservices

Chris offers a comprehensive set of resources for learning about microservices including articles, an O'Reilly training video, and example code. Learn more

Microservices consulting and training

Chris offers a comprehensive consulting services, workshops and hands on training classes to help you use microservices effectively. Get advice

Need training in London?

I'll be there in May!

Contact me

Example microservices applications

Want to see an example? Check out Chris Richardson's example applications. See code

Get the book: Microservice patternsnew


Signup for the newsletter

A new microservices application platform that solves distributed data management problems.

Join the microservices google group

Pattern: Messaging

Context

You have applied the Microservice architecture pattern. Services must handle requests from the application’s clients. Furthermore, services must sometimes collaborate to handle those requests. They must use an inter-process communication protocol.

Forces

Solution

Use asynchronous messaging for inter-service communication. Services communicating by exchanging messages over messaging channels.

Examples

There are numerous examples of asynchronous messaging technologies

UserRegistrationController from the Microservices Example application is an example of a component, which is written in Scala, that sends a message via RabbitMQ using the Spring Framework’s RabbitTemplate:

@RestController
class UserRegistrationController @Autowired()(registeredUserRepository: RegisteredUserRepository, rabbitTemplate: RabbitTemplate) {

  import MessagingNames._

  @RequestMapping(value = Array("/user"), method = Array(RequestMethod.POST))
  def registerUser(@Validated @RequestBody request: RegistrationRequest) = {
    val registeredUser = new RegisteredUser(null, request.emailAddress, request.password)
    registeredUserRepository.save(registeredUser)
    rabbitTemplate.convertAndSend(exchangeName, routingKey, NewRegistrationNotification(registeredUser.id, request.emailAddress, request.password))
    RegistrationResponse(registeredUser.id, request.emailAddress)
  }

  @ResponseStatus(value = HttpStatus.CONFLICT, reason = "duplicate email address")
  @ExceptionHandler(Array(classOf[DuplicateKeyException]))
  def duplicateEmailAddress() {}


}

Resulting context

This pattern has the following benefits:

  • Loose coupling since it decouples client from services
  • Improved availability since the message broker buffers messages until the consumer is able to process them
  • Supports a variety of communication patterns including request/reply, notifications, request/async response, publish/subscribe, publish/async response etc

This pattern has the following drawbacks:

  • Additional complexity of message broker, which must be highly available

This pattern has the following issues:

  • Request/reply-style communication is more complex
  • Client needs to discover location of message broker

See also


Copyright © 2017 Chris Richardson • All rights reserved • Supported by Kong.