Microservices with Meteor

2016. 9. 29. 17:56Meteor

http://justmeteor.com/blog/microservices-with-meteor/

---------

Microservices with Meteor

In this blog post we’ll talk about the microservices architecture and how you can build such an architecture with Meteor.

At the end of this blog post you should have a solid understanding why microservices are used today and if your project should use them as well.

Let’s start

What are microservices?

Let’s get back in time when the internet was still young and the first scripting languages appeared which were used to build dynamic web applications. Back in those days you would develop your application, spin up a server, upload all your scripts to the server and hope everything works out (even if thousands of people use your web application).
Those were the days back then.

Web applications got more complex over time. Nowadays a web application consists of different types of functionality.

Imagine web shop like Amazon.
There is the marketplace, there are recommendations, categories, reviews, ratings, your wishlist, topseller lists, …

Now imagine that you have one monolithic code base which contains all this functionality.
What if one component has a bug and fails? What if you’ve missed a semicolon? A database migration got wrong? The worst case could be that your whole application goes down.

A solution which was developed to target those issues is the so called “microservices” architecture.
This architecture proposes a separation of all the web applications functionality in different “mini” applications which communicate through standard web protocols (e.g. HTTP or DDP) with each other.
This way you have one core application which just “includes” and talks to the microservices. Your application is less tightly coupled and every microservice acts on it’s own behalf and can be developed, updated and deployed independently (even in it’s own programming language / stack if you want).

A blog implementation with the help of microservices

Let’s implement a blog application with the microservices architecture so that we can get a better understanding how this actually works.
Imagine that we have posts and comments. Our goal is to encapsulate the posts and comments into our own Meteor applications.
So we have one Meteor application which contains all the blog posts and another one which handles all the comments.

We assume that our blog posts application runs on port 4000 and is already fully implemented (we are able to post new blog posts, edit blog posts, remove blog posts, …).

Furthermore we have a comment application which runs on port 5000 and handles all the comments for us.
This application has one Method addComment and one publication getCommentsForPost(postId).

Now we want a way to submit and view all comments for a specific post in our blog post application.
The first thing we need to do is to connect our blog post application with the comments applications.
Thanks to the DDP protocol this is very easy and straightforward.

In the blog post application we add the line:

var commentApplicationConnection = DDP.connect("http://localhost:5000");

Now we can simply call the addComment method of our comment application from within our blog post application:

var comment = { title: 'Good blog post', body: 'This blog post was very helpful!' };
commentApplicationConnection.call('addComment', comment);

Furthermore we can subscribe to all the comments by post id:

commentApplicationConnection.subscribe('getCommentsForPost', 1234);

That’s it.
We have successfully created a mini blogging / commenting application with the help of the microservice architecture.

The great thing about this architecture is that all the Meteor applications are independent (even the databases are separated). You could e.g. use Version 0.6.0 for the comments application and 1.2.0.2 for the blog post application. You can develop and update the applications independently as well.
If you’re in a large team you could even switch from Meteor to another framework / programming language for the comments application (although you may need to switch the protocols for the communication between the applications).

Conclusion

Microservices are a very powerful architecture and thanks to the DDP protocol it’s very convenient to adopt such an architecture with Meteor.
Microservices are widely adopted nowadays. Netflix, SoundCloud, Amazon, Spotify, Groupon, … nearly all of high traffic web applications have switched from a Monolithic to a microservices architecture so that they can iterate and scale their web applications more efficiently.

Additional resources

Here are some useful links if you’re interested in further information about the microservices architecture.

Auditing meteor applications

Things to be aware of when developing large scale Meteor apps
Get the book

About the author

Hi, I'm Philipp Müns, a Meteor and JavaScript enthusiast.
Full stack developer at justmeteor.com and goltfisch.de

  • How do you connect different meteor “microservices” to the same database?

    • You can create a database microservice app with it’s API (basically a set of CRUD methods).

    • You could set the MONGO_URL environment variable and point all the microservices to the same MongoDB (although a losse coupling is recommended when developing microservices)

  • Jørgen Foss Eri

    Wow, is it really that easy?! Holy carp that changes things.

  • Reminds me I need to learn more about DDP.

  • Luis Alvarez

    Thanks for the article, very short and concise. My question is, suppose I have the Collection and it’s model in one of the microservices, is there a way to access them? I mean, what if I want to grab a comment from the posts app and call a method from within a comment instance? (I would like to avoid code duplication)
    Thanks!

    • Hey Luis,
      thank you for your comment. You can call every Method / subscribe to publications from one Meteor app through another one (you only need a DDP connection between the two apps).

  • Jorge Pastorini

    Thanks for the article, You may also find this post useful as an introduction to Microservices development and to understand the history behind it.
    http://www.art2link.com/divide-conquer-microservice-approach/

    • Thank you for sharing the blog post.