Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby on rails

How to use hexagonal architecture in a Rails application

Educative Team

Hexagonal architecture

The hexagon in a hexagonal architecture represents the application, where each side of the hexagon represents the external source with which the application interacts. The external sources interact with the application using ports and adapters.

Learn more about hexagonal architecture in this shot.

Hexagonal architecture in Rails

In Rails, we can use the following rails components to play the role of port, adapter, and external devices in hexagonal architecture:

  • Ports: HTTP protocol and message queue clients
  • Adapters: The Rails controllers, HTTP and message queue clients
  • External devices: The user interface, databases and infrastructure

Rails controllers

The following core responsibilities make the controllers work like the adapters of the hexagonal architecture:

  1. Play the role of the application’s interface to the HTTP protocol.
  2. Respond to HTTP messages by triggering actions on a domain objects.
  3. Check and query the model objects to find out if they performed their tasks.

How to use hexagonal architecture in rails application

We can use different techniques to implement the hexagonal architecture in a Rails application.

  1. As discussed above, in Rails, controllers play the role of adapters, so business logic is not contained in the controllers. This makes it easier for controllers to coordinate with external sources. Here, use case service components (service objects) can be used with controllers to separate each business logic and allow controllers to coordinate with external sources.

Use case services component
  • Service objects are designed to execute one service at a time, perform it well, and provide a single point of failure. One controller can have multiple services based on the number of business logic actions it wants to perform.
Service object
  1. Using passive controllers is another way we can implement the hexagonal architecture in Rails. Passive controllers are the controllers that do not make any decision on the use case service failure or success. They only invoke or get invoked on requests or callbacks from the service objects.

Passive controllers
  1. To not clutter the business logic and controllers with the codebase, calls to database are handled separately. Repositories can be used as a descriptive and easily testable interface for querying the database to perform a single responsibility at a time. Repositories can manage all interactions with the database so interactions with external sources become relatively easy.


The hexagon in the architecture contains all the business logic, which can be implemented and tested without worrying about the technology or loading the Rails or databases.

The hexagonal architecture helps Rails to separate the application and the business logic. In Rails, the application helps to notify the external sources about the results of any request that external source made. The application does not know anything about Rails or the external source, for example, the database. To interact with the database, the application uses repositories.


ruby on rails
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring