package Myriad;
# ABSTRACT: async microservice framework

use Myriad::Class;

our $VERSION = '0.007';

=encoding utf8

=head1 NAME

Myriad - microservice coördination

=begin markdown

[![Coverage status](](
[![Test status](](

=end markdown


 use Myriad;


Myriad provides a framework for dealing with asynchronous, microservice-based code.
It is intended for use in an environment such as Kubernetes to support horizontal
scaling for larger systems.

Overall this framework encourages - but does not enforce - single-responsibility
in each microservice: each service should integrate with at most one external system,
and integration should be kept in separate services from business logic or aggregation.
This is at odds with common microservice frameworks, so perhaps it would be more accurate
to say that this framework is aimed at developing "nanoservices" instead.

=head2 Do you need this?

If you expect to be dealing with more traffic than a single server can handle,
or you have a development team larger than 30-50 or so, this might be of interest.

For a smaller system with a handful of users, it's I<probably> overkill!

=head1 Modules and code layout

=over 4

=item * L<Myriad::Service> - load this in your own code to turn it into a microservice

=item * L<Myriad::RPC> - the RPC abstraction layer, in C<< $self->rpc >>

=item * L<Myriad::Storage> - abstraction layer for storage, available as C<< $self->storage >> within services

=item * L<Myriad::Subscription> - the subscription handling layer, in C<< $self->subscription >>


Each of the three abstractions has various implementations. You'd set one on startup
and that would provide functionality through the top-level abstraction layer. Service code
generally shouldn't need to care which implementation is applied. There may however be cases
where transactional behaviour differs between implementations, so there is some basic
functionality planned for checking whether RPC/storage/subscription use the same underlying
mechanism for transactional safety.

=head2 Storage

The L<Myriad::Storage> abstract API is a good starting point here.

For storage implementations, we have:

=over 4

=item * L<Myriad::Storage::Redis>

=item * L<Myriad::Storage::PostgreSQL>

=item * L<Myriad::Storage::Memory>


Additional transport mechanisms may be available, see CPAN for details.

=head2 RPC

Simple request/response patterns are handled with the L<Myriad::RPC> layer ("remote procedure call").

Details on the request are in L<Myriad::RPC::Request> and the response to be sent back is in L<Myriad::RPC::Response>.

=over 4

=item * L<Myriad::RPC::Redis>

=item * L<Myriad::RPC::PostgreSQL>

=item * L<Myriad::RPC::Memory>


Additional transport mechanisms may be available, see CPAN for details.

=head2 Subscriptions

The L<Myriad::Subscription> abstraction layer defines the available API here.

Subscription implementations include:

=over 4

=item * L<Myriad::Subscription::Redis>

=item * L<Myriad::Subscription::PostgreSQL>

=item * L<Myriad::Subscription::Memory>


Additional transport mechanisms may be available, see CPAN for details.

=head2 Transports

Note that I<some layers don't have implementations for all transports> - MQ for example does not really provide a concept of "storage".

Each of these implementations is supposed to separate out the logic from the actual transport calls, so there's a separate ::Transport set of classes here:

=over 4

=item * L<Myriad::Transport::Redis>

=item * L<Myriad::Transport::PostgreSQL>

=item * L<Myriad::Transport::Memory>


which deal with the lower-level interaction with the protocol, connection management and so on. More details on that
can be found in L<Myriad::Transport> - but it's typically only useful for people working on the L<Myriad> implementation itself.

=head2 Other classes

Documentation for these classes may also be of use:

=over 4

=item * L<Myriad::Exception> - generic errors, provides L<Myriad::Exception/throw> and we recommend that all service errors implement this rôle

=item * L<Myriad::Plugin> - adds specific functionality to services

=item * L<Myriad::Bootstrap> - startup used in C<> for providing autorestart and other functionality

=item * L<Myriad::Service> - base class for a service

=item * L<Myriad::Registry> - support for registering services and methods within the current process

=item * L<Myriad::Config> - general config support, commandline/file/storage


=head1 METHODS


use curry;
use Future;

use Myriad::Commands;
use Myriad::Config;
use Myriad::Exception;
use Myriad::Exception::InternalError;
use Myriad::Registry;
use Myriad::RPC;
use Myriad::RPC::Client;
use Myriad::Storage;
use Myriad::Subscription;
use Myriad::Transport::HTTP;
use Myriad::Transport::Memory;
use Myriad::Transport::Redis;

use Log::Any::Adapter;

use Net::Async::OpenTracing;
use Metrics::Any::Adapter qw(DogStatsd);

    $REGISTRY = Myriad::Registry->new;

# Enable Future time trace
$Future::TIMES = 1;


# The IO::Async::Loop instance
has $loop;
# Any coderefs to call when the framework starts
has $startup_tasks;
# Any coderefs to call when shutdown is requested
has $shutdown_tasks;
# The Myriad::Config instance
has $config;
# Registered commands for the management interface
has $commands;
# Our temporary Net::Async::Redis instance that should
# really be abstracted away by the ::Transport and
# storage/rpc/subscription abstractions
has $redis;
# The in-memory "transport" instance
has $memory_transport;
# The Myriad::RPC instance to serve RPC requests for
# the services in this process
has $rpc;
# The Myriad::RPC::Client instance to send requests
# to other services.
has $rpc_client;
# The Net::Async::HTTP::Server instance for endpoint
# requests
has $http;
# The Myriad::Subscription instance to emit
# and listen for events
has $subscription;
# The Myriad::Storage instance to manage data
# stored by the service or access other services data.
has $storage;
# Future representing shutdown
has $shutdown;
# Future for passing to things that want to react to
# shutdown, pretty much everything outside this file
# should only be able to access this one
has $shutdown_without_cancel;
# The Net::Async::OpenTracing instance
has $tracing;
# Any service definitions which is added by registry
has $services;
# Ryu::Source that can be used to recieve commands events
has $ryu;

# Note that we don't use Object::Pad as heavily within the core framework as we
# would expect in microservices - this is mainly due to complications regarding
# rôle/inheritance behaviour, and at some future point we expect to refactor code
# to move more of these classes over to Object::Pad.

    $startup_tasks = [ ];
    $shutdown_tasks = [ ];

=head2 loop

Returns the main L<IO::Async::Loop> instance for this process.


method loop { $loop //= IO::Async::Loop->new }

=head2 services

Hashref of services that have been added to this instance,
as C<name> => C<Myriad::Service> pairs.


method services { $services //= {} }

=head2 configure_from_argv

Applies configuration from commandline parameters.

Expects a list of parameters and applies the following logic for each one:

=over 4

=item * if it contains C<::> and a wildcard C<*>, it's treated as a service module base name, and all
modules under that immediate namespace will be loaded

=item * if it contains C<::>, it's treated as a comma-separated list of service module names to load

=item * a C<-> prefix is a standard getopt parameter



async method configure_from_argv (@args) {
    # Allow config parsing to extract the information
    $config = Myriad::Config->new(
        commandline => \@args


    $commands = Myriad::Commands->new(
        myriad => $self

    # At this point, we expect `@args` to contain only the plain
    # parameters such as the service name or a request to run an RPC
    # method.
    my $method = 'service';
    while(@args) {
        my $arg = shift @args;
        if($commands->can($arg)) {
            $method = $arg;
            await $commands->$method(shift @args, @args);
        } else {
            await $commands->$method($arg, @args);

    $self->on_start(async sub {
        await $config->listen_for_updates;

method config () { $config }

=head2 redis

The L<Net::Async::Redis> (or compatible) instance used for service coördination.


method redis () {
    unless($redis) {
            $redis = Myriad::Transport::Redis->new(
                $config ? (
                    redis_uri              => $config->transport_redis->as_string,
                    cluster                => ($config->transport_cluster->as_string ? 1 : 0),
                    client_side_cache_size => $config->transport_redis_cache->as_number,
                ) : ()

        $self->on_start(async method {
            await $self->redis->start;

=head2 memory_transport

The L<Myriad::Transport::Memory> instance.


method memory_transport () {
    unless ($memory_transport) {
            $memory_transport = Myriad::Transport::Memory->new


=head2 rpc

The L<Myriad::RPC> instance to serve RPC requests.


method rpc () {
    unless($rpc) {
            $rpc = Myriad::RPC->new(
                transport => $config ? $config->rpc_transport->as_string : '',
                myriad    => $self,

        $self->on_start(async method {
            $rpc->start->retain->on_fail($self->$curry::weak(sub {
                my ($self, $reason) = @_;
                    unless $self->shutdown_future->is_ready;

        $self->on_shutdown(async method {
            await $rpc->stop;

=head2 rpc_client

The L<Myriad::RPC::Client> instance to request other services RPC.


method rpc_client () {
    unless($rpc_client) {
            $rpc_client = Myriad::RPC::Client->new(
                # We should use same transport as $rpc.
                transport => $config ? $config->rpc_transport->as_string : '',
                myriad => $self,

        $rpc_client->start->retain->on_fail(sub {

        $self->on_shutdown(async method {
            await $rpc_client->stop;

=head2 http

The L<Net::Async::HTTP::Server> (or compatible) instance used for health checks
and metrics.


method http () {
    unless($http) {
            $http = Myriad::Transport::HTTP->new

=head2 subscription

The L<Myriad::Subscription> instance to manage events.


method subscription () {
    unless ($subscription) {
            $subscription = Myriad::Subscription->new(
                transport => $config ? $config->subscription_transport->as_string : '' ,
                myriad    => $self,

        $self->on_start(async method {
            $subscription->start->retain->on_fail(sub {

        $self->on_shutdown(async method {
            await $subscription->stop;

=head2 storage

The L<Myriad::Storage> instance to manage data.


method storage () {
    unless($storage) {
        $storage = Myriad::Storage->new(
            transport => $config ? $config->storage_transport->as_string : '',
            myriad => $self,

=head2 registry

Returns the common L<Myriad::Registry> representing the current service state.


method registry () { $REGISTRY }

=head2 add_service

Instantiates and adds a new service to the L</loop>.

Returns the service instance.


async method add_service ($srv, %args) {
    return await $self->registry->add_service(
        service      => $srv,
        myriad       => $self,

=head2 service_by_name

Looks up the given service, returning the instance if it exists.

Will throw an exception if the service cannot be found.


method service_by_name ($srv) {
    return $self->registry->service_by_name(

=head2 ryu

a source to corresponde to any high level events.


method ryu () {
    unless($ryu) {
            $ryu = Ryu::Async->new

=head2 shutdown

Requests shutdown.


async method shutdown () {
    my $f = $shutdown
        or die 'attempting to shut down before we have started, this will not end well';

    try {
        # Each service may have its own shutdown or cleanup operations
        my @shutdown_operations = map {
        } keys $services->%*;

        # We also have generic tasks, such as transport or RPC/subscription
        push @shutdown_operations, map {
        } splice $shutdown_tasks->@*;

        await Future->wait_any(
            $self->loop->timeout_future(after => 5)

        $f->done unless $f->is_ready;
    } catch ($e) {
        $f->fail($e) unless $f->is_ready;
    return $f->without_cancel;

=head2 on_start

Registers a coderef to be called during startup.
The coderef is expected to return a L<Future>.


method on_start ($code) {
    push $startup_tasks->@*, $code;

=head2 on_shutdown

Registers a coderef to be called during shutdown.

The coderef is expected to return a L<Future> indicating completion.


method on_shutdown ($code) {
    push $shutdown_tasks->@*, $code;

=head2 shutdown_future

Returns a copy of the shutdown L<Future>.

This would resolve once the process is about to shut down,
triggered by a fault or a Unix signal.


method shutdown_future () {
    return $shutdown_without_cancel //= (
        $shutdown //= $self->loop->new_future->set_label('shutdown')

=head2 setup_logging

Prepare for logging.


method setup_logging () {
    my $level = $config->log_level;
    $level->subscribe(my $code = sub {
            log_level => $level->as_string,

=head2 setup_tracing

Prepare L<OpenTracing> collection.


method setup_tracing () {
        $tracing = Net::Async::OpenTracing->new(
            host => $config->opentracing_host,
            port => $config->opentracing_port,
            protocol => 'jaeger',
    $self->on_shutdown(async method {
        await $tracing->sync

=head2 run

Starts the main loop.

Applies signal handlers for TERM and QUIT, then starts the loop.


async method run () {
    for my $signal (qw(TERM INT QUIT)) {
        $self->loop->attach_signal($signal => $self->$curry::weak(method {
            $log->infof("%s received, exit", $signal);

    try {
        # Run the startup tasks, order is imporatant
        for my $task ($startup_tasks->@*) {
            await $self->$task;
    } catch ($e) {
        die "Startup tasks failed - $e";

    # Set shutdown future before starting commands.
    $shutdown //= $self->loop->new_future->set_label('shutdown');

    $commands->run_cmd->retain()->on_fail(sub {

    await $self->shutdown_future;



=head1 SEE ALSO

Microservices are hardly a new concept, and there's a lot of prior art out there.

Key features that we attempt to provide:

=over 4

=item * B<reliable handling> - requests and actions should be reliable by default

=item * B<atomic storage> - being able to record something in storage as part of the same transaction as acknowledging a message

=item * B<flexible backends> - support for various storage, RPC and subscription implementations, allowing for mix+match

=item * B<zero transport option> - for testing and smaller deployments, you might want to run everything in a single process

=item * B<language-agnostic> - implementations should be possible in languages other than Perl

=item * B<first-class Kubernetes support> - k8s is not required, but when available we should play to its strengths

=item * B<minimal boilerplate> - with an emphasis on rapid prototyping


These points tend to be incompatible with typical HTTP-based microservices frameworks, although this is
offered as one of the transport mechanisms (with some limitations).

=head2 Perl

Here are a list of the Perl microservice implementations that we're aware of:

=over 4

=item * L<> - MQ-based (via STOMP), using L<AnyEvent>

=item * L<> - more of a web framework, but a popular one

=item * L<Async::Microservice> - L<AnyEvent>-based, using HTTP as a protocol, currently a minimal wrapper intended to be used with OpenAPI services


=head2 Java

Although this is the textbook "enterprise-scale platform", Java naturally fits a microservice theme.

=over 4

=item * L<Spring Boot|> - One of the frameworks that integrates well
with the traditional Java ecosystem, depends on HTTP as a transport. Although there is no unified storage layer,
database access is available through connectors.

=item * L<Micronaut|> - This framework has many integrations with industry-standard
solutions - SQL, MongoDB, Kafka, Redis, gRPC - and they have integration guides for cloud-native solutions
such as AWS or GCP.

=item * L<DropWizard|> - A minimal framework that provides a RESTful
interface and storage layer using Hibernate.

=item * L<Helidon|> - Oracle's open source attempt, provides support for two types of
transport and SQL access layer using standard Java's packages, built with cloud-native deployment in mind.


=head2 Python

Most of Python's frameworks provide tools to facilitate building logic blocks behind APIs (Flask, Django ..etc).

For work distribution, L<Celery|> is commonly used as a task queue abstraction.

=head2 Rust

=over 4

=item * L<> - although this is a web framework, rather than a complete microservice system,
it's reasonably popular for the request/response part of the equation

=item * L<> - another web framework, this time with a focus on the actor pattern


=head2 JS

JS has many frameworks that help to implement the microservice architecture, some are:

=over 4

=item * L<Moleculer|> - generally a full-featured, well-designed microservices framework, highly recommended

=item * L<Seneca|>


=head2 PHP

=over 4

=item * L<Swoft|> - async support via Swoole's coroutines, HTTP/websockets based with additional support for Redis/database connection pooling and ORM


=head2 Cloud providers

Microservice support at the provider level:

=over 4

=item * L<AWS Lambda|> - trigger small containers based on logic, typically combined
with other AWS services for data storage, message sending and other actions

=item * L<Google App Engine> - Google's own attempt

=item * L<Heroku|> - Allow developers to build a microservices architecture based on the services they provide
like the example they mentioned in this L<blog|>


=head1 AUTHOR

Deriv Group Services Ltd. C<< >>


=over 4

=item * Tom Molesworth C<< >>

=item * Paul Evans C<< >>

=item * Eyad Arnabeh

=item * Nael Alolwani


=head1 LICENSE

Copyright Deriv Group Services Ltd 2020-2021. Licensed under the same terms as Perl itself.