What is a Microservice?
In this guide we'll walk through what a microservice is, how they are used, and the pros/cons for using them in production Ruby on Rails applications.
Guide Tasks
  • Read Tutorial

It would be hard to know how to properly build a microservice application without first knowing what a microservice is. Even though the term microservice may be relatively new in application development circles, the concept has been around for a very long time. Let's go back in time to 1913 and look at a real world example of a microservice in the manufacturing industry.

large

Before Henry Ford revolutionized the manufacturing industry with the assembly line, warehouse workers would be involved in many different aspects of building a car. A welder would work on a car door and then move to a part of the engine, and then move to whatever was ready to be worked on. This process made manufacturing a car a very slow process. It was also very expensive because workers had to be trained on a wide range of skills. Ford implemented the assembly line process, where each worker had a single task, for example our same welder would only work on welding a passenger door on. This streamlined the entire manufacturing process, allowed for many more cars to be produced compared with the old processes, and also was less error prone since workers could specialize in a single task instead of having to master a dozen different skills.

Coming back to the present, there are many similarities between the assembly line and microservices:

  • A microservice should only perform a single task

  • Since microservices can focus on a single application feature they should theoretically be less error prone

  • A microservice architecture can compartmentalize an application's features, allowing the app to scale since it is by its nature already distributed

Many developers, especially new devs, are intimidated by microservices because, on the surface, they appear to be very different types of applications. However the more that I work on microservice based applications I've discovered that they're not any more difficult to work with compared with monolithic (single) apps. There is more setup involved since each application needs to be built and deployed separately, however there are some key benefits that should be considered:

  • A system built up of microservice based applications don't need to all be the same programming language/framework. I'm currently working on a large application that is made up of over 20 microservice applications and the apps use: Rails, Sinatra, AngularJS, Lua, and Django. In addition to using different languages and frameworks, the system uses Postgres, Dynamo DB and Redis for databases across the different applications. Since the applications all communicate with each other using JSON based APIs it doesn't matter what language they're built in. This comes in very handy when you have large teams that specialize in different languages.

  • Microservices work well with client-side JavaScript MVC frameworks such as Angular and Ember. There's quite a bit of debate on whether or not front end frameworks should be integrated into Rails, however the fact is that they are incredibly popular and when you use a microservice based system it gives you the flexibility to use any frontend that you prefer.

  • Microservices are built for scalability. Since each application in a microservice based system can be hosted on its own server instance, it makes it very straightforward and affordable to scale applications.

  • Microservices, if built properly, should be straightforward to test. Of course this should be the case for all application types, however there is something to be said for keeping applications small from a testability perspective.

Microservice Examples

So what are some good examples of microservices? Below are a few that I've worked on:

  • An authentication system for a large enterprise. Imagine if you had a company with thousands of employees that use a dozen different applications. If you had a single auth microservice all of the apps would be able to use it so if a user signs into it they can securely access the other company apps.

  • A frontend that functions as the view for the application. No complex logic resides in the front end, it simply calls the other microservice applications to know what data to render to users.

  • A media encoding engine that is tasked with encoding video files and uploading them to the server.

Resources