Distributed Services with Malla

Copy Markdown View Source

Introduction

Services declared with global: true are announced to the other nodes of the cluster, and a node that knows where a service runs can call it with Malla.remote/4, or with the call macro.

In this tutorial, you'll:

  1. Set up two Livebook sessions as separate nodes
  2. Create services on one node and call them from the other
  3. See service discovery in action: a service that starts later is found by the other node
  4. Call a service through a virtual module, a local proxy that Malla generates for it

The tutorial does not show what happens when a node fails or when several nodes run the same service: see Service Discovery and Remote Calls for how nodes are chosen and when a call goes to the next node.

Setup

Malla uses :pg (process groups) for service discovery, which only works between visible nodes. Since Livebook starts its runtimes as hidden nodes, you need to start IEx sessions manually and attach Livebook to them.

Step 1: Start two IEx nodes

Open two terminals from the project directory:

# Terminal 1 (Storage Node)
iex --name storage@127.0.0.1 --cookie malla -S mix

# Terminal 2 (Client Node)
iex --name client@127.0.0.1 --cookie malla -S mix

Step 2: Attach Livebook to each node

Open each notebook in its own Livebook tab. For each tab, go to the Runtime panel, choose Attached node, and enter:

  • Storage notebook: Name storage@127.0.0.1, Cookie malla
  • Client notebook: Name client@127.0.0.1, Cookie malla

Step 3: Open the notebooks

  1. Storage Node — start here, creates the StorageService
  2. Client Node — connects to the storage node and makes remote calls

Run the Storage notebook first, then switch to the Client notebook.

This tutorial only scratches the surface of what Malla offers. Check out the guides and API documentation for the full picture: plugin lifecycle, configuration, reconfiguration, request handling, tracing, and more.