Announcing PhotonIQ: The New AI CDN For Accelerating Apps, APIs, Websites and Services
Tutorial

Build Geo-Distributed Apps With The New Macrometa CLI

Post Image

Macrometa is a geo-distributed streaming database with built-in compute as a service to provide low latency reads and writes to users across the globe.

In this blog, let’s build a geo-distributed, low latency Address Book application in 10 mins using our new Macrometa CLI.

How to get started

1. Make sure you have Nodejs and nvm installed locally. For nvm, execute the following commands on your terminal.

$curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  1. Create Macrometa account and obtain an API key. Please see below video clip on how to sign up and create an API key.

Installing the Macrometa CLI

  1. Install the Macrometa CLI by using the following command
$npm install -g gdnsl
  1. Verify the installation by executing the version command of the CLI
$gdnsl --version
  1. Initialize the client with the init command to create the YAML file in the local directory.

Create backend for AddressBook

Using the Macrometa CLI, a developer can still do everything they did before from the GUI or REST APIs.

  1. Let’s first create a global document collection called addresses and enable streams on that collection. We will be using this collection to store the address documents. The stream will enable us to receive the updates via push mechanism in the GUI of Address Book application. (Note: The @ prefix is used to decide variables that should be used as parameters to the query-worker REST API endpoint.)
$gdnsl collection create addresses --type doc  --stream
  1. Next, let’s create a Query Worker to add information for the user. Query workers enable us to package a query as a function, deploy it globally and make it available as a REST API endpoint. All of this happens transparently. The calls to this REST API endpoint from the browser will get geo-routed to the location closest to the user and get executed there. Typically the 95%tile request latencies are below 50ms from anywhere around the globe.
$gdnsl query-worker create addAddress --value 
	 'INSERT { "firstname" : @firstname, 
	           "lastname" : @lastname, 
	            "email" : @email, 
	            "_key" : @key 
	          }INTO addresses'
  1. Similarly, create a query-worker to get information about the user.
$gdnsl query-worker create getAddressAll --value 
		'FOR doc IN addresses 
			RETURN { _key : doc._key, 
							firstname : doc.firstname, 
							lastname : doc.lastname, email : doc.email 
						}'
  1. Create a query-worker to update information about the user.
$gdnsl query-worker create updateAddresses --value 
	'UPDATE @_key WITH { 
		firstname : @firstname,
		lastname : @lastname,
		email : @email } 
	IN addresses'
  1. Finally create a query-worker to remove the user record.
$gdnsl query-worker create removeAddress --value 
	'REMOVE @_key in addresses'

We're done! You have now a geo-distributed backend for your application with required collection and query-workers aka functions deployed around the globe and available REST API endpoints.

You can verify if your API end points are working correctly or not by either using CURL or the CLI itself. Below is an example on how to use the CLI to verify the created API endpoints.

# Add user records via `addAdress` API end point.
$gdnsl query-worker run addAddress  --param 'firstname=Anurag2'  --param 'lastname=Dwivedi2' --param 'email=anurag_dwivedi2@macrometa.com' --param='key=address1'
$gdnsl query-worker run addAddress  --param 'firstname=Anurag3'  --param 'lastname=Dwivedi3' --param 'email=anurag_dwivedi3@macrometa.com' --param='key=address2'

# Get user records via `getAddressAll` API end point.
$gdnsl query-worker run getAddressAll

# Update user information via `updateAddress` API end point.
$gdnsl query-worker run updateAddress —-param "_key=address1" --param "firstname=John" --param "lastname=Doe" --param "email=john@doe.co"

# Remove user information via `removeAddress` API end point.
$gdnsl query-worker run removeAddress --param '_key=address2'

Create Frontend for Address Book App

The Address Book application uses React to build its interactive user interface where users can provide the GDN tenant details and fabric details and select a particular region to demonstrate the respective user information from the address book as per the input.

You can clone the source code for this GUI from https://github.com/Macrometacorp/tutorial-addressbook-streams

Deploy App on Github Pages

Once you build and run the AddressBook application in local environment, you can deploy the app to Github pages by adding the following commands in your scripts in package.json

 "predeploy": "npm run build",
 "deploy": "gh-pages -d build"

Also add homepage in package.json

"homepage": "https://macrometacorp.github.io/blog-demo-gdnsl"

Notes:

To deploy the app as a Github page, You need to install gh-pages as a dependency in your package.json i.e.,

npm i gh-pages

Also you need to enable ghpages from settings in your Github repo

Image

Finally, run the following command to deploy the app on gh-pages

npm run predeploy   
npm run deploy

You should create an output similar to the one below.

Image

Summary

That’s it. Now you have a low latency geo-distributed AddressBook app available to serve your users globally. The running application will look like below as shown in the video.

Typically, developers spend most a lot of their time building and wiring together various products and services to provide a globally-distributed backend. The downside of this is that they spend less time on the actual application that moves the needle for their business.

With Macrometa, the developer can spend more time on where it matters to their business and let the platform take care all the other details. The Macrometa CLI provides a convenient way to create your backend in few simple calls and integrate it into your CI/CD pipelines.

Join our newsletter and get the latest posts to your inbox
Form loading...





Featured Posts

Related Posts

Recent Posts

Platform

PhotonIQ
Join the Newsletter