Introducing the Platformatic Control Module

Introducing the Platformatic Control Module

A Quickguide on Interservice Communication and the Runtime Management API

When dealing with microservices, simplicity, precision, and efficiency are critical. As part of our April ‘24 Launch, we're excited to introduce the integration of enhanced functionality within our Runtime and its Management API.

This update represents a significant advancement in how developers orchestrate communication between microservices. It simplifies our runtime, addressing a longstanding challenge: the complexity of managing and orchestrating communication between microservices, which often leads to inefficiencies and bottlenecks in development.

By streamlining interservice communication and offering a cohesive platform for managing microservices, we know this release will empower teams to focus more of their time and energy on innovation rather than infrastructure.

In this blog, we'll explore how this update simplifies the runtime, enabling seamless communication between microservices and offering a cohesive platform for managing and orchestrating their interactions.

Platformatic Service and Platformatic Runtime

Backend developers primarily rely on Node.js for crafting robust APIs and microservices. Platformatic acknowledges this preference and provides tailored solutions, notably the Platformatic Service, enabling Node.js developers to speed up development without encountering unnecessary overhead.

Platformatic Service provides a supercharged Fastify experience for building services, easing the burden on developers by handling the management of microservices without the need for centralized registries or external infrastructure. It serves as a toolkit for building robust APIs, allowing users to effortlessly create multiple microservices within the Platformatic ecosystem while also leveraging the Fastify framework.

Enter the Platformatic Runtime—an environment designed to facilitate the creation of multiple microservices under a unified framework. With support for modular monolithic architecture, the Platformatic Runtime equips large-scale enterprises with the scalability needed to navigate complex application landscapes effortlessly.

Navigating Interservice Communication

Typically, there's limited interservice communication within the Platformatic Runtime. Each Platformatic Runtime may encompass three or more microservices.

A singular HTTP entry point is designated for accessing and controlling these services. The reasoning for this is as follows:

Firstly, it facilitates the deployment of microservices as a monolithic unit, aligning with the platform's monolithic architecture approach. Secondly, depending on the application's architecture and requirements, interservice communication may not always be necessary. Lastly, maintaining a single entry point enhances security measures by mitigating the risk of inter-service manipulation or attacks.

Despite these considerations, there are instances where interservice communication becomes essential for optimizing efficiency.

Facilitating Interactions with the Management API

Within the Platformatic runtime, a crucial interface bridges users and microservices: the Management API. While microservices operate as worker threads, the Management API assumes the role of the main thread.

Note*: In Node.js terminology, threads represent units executed by the processing unit of the machine. The main thread handles intensive JavaScript execution, while worker threads provide auxiliary support.*

The Management API functions as the main thread in this scenario. Let’s take a look at how it receives information and facilitates communication between microservices.

Information flows into the Management API through the Unix Socket, a fixed location designed for easy discoverability. Any instruction directed to the Management API passes through this Unit Socket as the initial terminal.

Users can access the Unix Socket via two means: Meraki and Platformatic Control.

Facilitating communication among services, the Management API operates through logs and commands. It can initiate service requests, start and stop services, access logs, and retrieve metrics. These functionalities enable services to maintain internal communication seamlessly.

Platformatic Control Commands

The Platformatic Control module opens up new avenues for internal microservice interaction within Platformatic.

As a reminder, a unix socket is exposed to link with the management API, allowing the Platformatic Control module to initiate events that trigger microservice interactions. This versatile module responds to various commands, each designed to perform specific actions:

  • ps command: Lists all running Platformatic runtime applications along with their names.

  • stop command: Terminates all or any running Platformatic runtime applications.

  • reload and restart commands: Refresh Platformatic runtime microservices.

  • env and config commands: Enables developers to expose environment variables and configuration, respectively.

  • log command: Displays runtime logs, useful for retrieving timestamps or logged events.

  • inject command: Crucial for inter-service communications, as it inputs requests to the runtime service.

These commands provide developers with the tools they need to effectively manage and coordinate microservices within the Platformatic ecosystem.

How does it work?

Now, let’s have a look at how carrying out interservice communication with Platformatic works.

Create a Platformatic Application

To test the control module, create a Platformatic application named plt. Create a database. Note that the database is also an HTTP server. Thus, it is more like creating two microservice applications.

More importantly, the DB has a sample movies database, which will be used for this demo.

Start the application by running this command:

npm create platformatic@latest

If there is a conflict of internal dependencies preventing it, you can resolve or quickly force the command as shown below:

npm create platformatic@latest - -force

The result will look something like this:

 Hello, welcome to Platformatic 1.27.0!
? What kind of project do you want to create? Application
? Where would you like to create your project? plt
? Which kind of project do you want to create? DB
? What is the name of the service? decisive-nostril
? What is the connection string? sqlite://./db.sqlite
? Do you want to create default migrations? yes
? Do you want to create another service? yes
? Which kind of project do you want to create? Service
? What is the name of the service? voracious-provision
? Do you want to create another service? no
? Which service should be exposed? decisive-nostril
? Do you want to use TypeScript? no
? What port do you want to use? 3042
? Do you want to init the git repository? no
✔ Installing dependencies...
[16:00:58] INFO: Project created successfully, executing post-install actions...
[16:00:58] INFO: You are all set! Run `npm start` to start your project.

Once this is done, ensure you configure your Platformatic Runtime as specified here.

Switch to Plt and Start the Application

You will need to switch to the plt directory by running cd plt.

Then, start the application with npm start

This should take you to the Open API documentation page which you can find at locally.

Overview and Testing of Various Key Commands

Check the running applications with npx platformatic ps.

If none, you will get No platformatic runtimes found.

You can run all the available commands in the module by running:

npx platformatic ctl

In return, you will obtain the following:

* `ps` - lists all platformatic runtime applications.
* `stop` - stops a platformatic runtime application.
* `restart` - restarts all platformatic runtime services.
* `reload` - reloads all platformatic runtime services.
* `services` - lists the runtime services.
* `config` - prints runtime or runtime service config file.
* `env` - lists the runtime environment variables.
* `logs` - shows the runtime logs.
* `inject` - injects a request to the runtime service.

Run this command to carry out inter service communications:

npx platformatic inject /movies-db/movies

You will get:

[{"id":1, "title": "star war"}, {"id" :2, "title": "Harry Potter"}]

You can also inject into both movies service and upperMovies route with this command:

npx platformatic inject -s movies-service/upperMovies

This will generate the movie detail in caps such as this:

[{"id":1, "title": "STAR WAR"}, {"id" :2, "title": "HARRY PORTER"}]

You can also generate an Open API documentation in JSON format like this:

npx platformatic inject -s movies-service/ documentation/json

Wrapping Up

In this blog, we've explored the pivotal enhancements within Platformatic Runtime and its Management API, witnessing a significant stride in microservice architecture.

By simplifying runtime complexities and fostering seamless communication between microservices, Platformatic equips developers with the tools necessary to improve interservice communications within their organization.