ScaleCube-js

Idan Levin
4 min readApr 29, 2021

--

Scalecube is a toolkit for creating microservices/micro-frontends-based systems.

But, what does it mean?

TL;DR

You can create microservices/micro-frontends systems really fast using scalecube-js. Don’t believe me, try it!
- Browser only: https://codesandbox.io/s/bwtl7
- Cluster + Browser: https://codesandbox.io/s/yjjvu

What is ScaleCube?

ScaleCube got its name from scale cube the 3 axes of scaling:

ScaleCube

X-Axis — Horizontal scaling, scale by cloning
Y-Axis — Functional decomposition, scale by splitting different things
Z-Axis — Data Partitioning, scale by splitting similar things

The power of ScaleCube comes from its definition of the microservices model.

ScaleCube Microservices model

Service in scalecube service can be anything, module, class, function in the end you need to provide ScaleCube a reference to the service methods you want to expose.

Service Definition to provision or consume a service we need to know a little bit about the service, this metadata is called Service Definition. In ScaleCube-js service definition is the Service name, what methods it has, and what is the method invocation model. For now, only Request/Response (as Promise) and Request/Stream (as Observable) are implemented in ScaleCube-js. But it also can be extended for example to Publisher/Subscriber.

Microservice: just a logical container that contains services and connects them to other services on different Microservices containers. The interaction is done by plugins for transport, discovery, cluster.

Transport: when to services on different processes we need to transport requests and responses.

Cluster: services on clusters keep changing. Scaling, deployment, and faults making new service instances available or unavailable. Cluster is the module that distributes services metadata and checks which instances are alive and which are dead.

Discovery: responsible for service discovery, to know in real-time what service instances are available and how to reach them.

Router: gets a list of available service instance and select an instance for invocation. Like Round-Robin, Ant Router, or any other routing logic

How it’s look in code?

ScaleCube-js comes with two ready-to-use presets one for browsers and one for Node.

Microservices on Node- this preset comes with RSocket base transport over WS/S or TCP, cluster module based on SWIM membership protocol over UDP, and RSocket based gateway which allows clients (any kind of consumers, browsers, or servers outside the cluster) to easily connect to your cluster.

Microservices on browser- this preset have transport, cluster, and discovery built on top browser PostMessage, which allows it to work even if services are in worker or iframes! Thanks to ScaleCube discovery, you can also create and consume services if you have multiple bundles and frameworks like Angular, Vue, React cross bundle boundaries without Window, hacks, and hard work.

Advanced users can override those preset or even create presets of their own with custom transport, discovery, etc.

This is how to create a microservice in scalecube-js, you need some code for your service, which can be anything, TS, JS, your favorite framework, class, or module… In our case, it’s just a function. We can put multiple services in a single microservice container. When we are calling createMicroservice we need to provide an array of services. Each service has a reference to the service, the reference should be {[key:string]: function} so if we have a class instance or a module we can just put a reference to it. In the field seedAddress we are putting an address or array of addresses that our microservice will search for other microservices. We usually will create dedicated microservice/s in an address known in advance for that, we will refer to this special microservice as “seed”.

The second part of the service we are providing createMicroservice is the service definition. The service definition is really the minimum ScaleCube needs to know about the service, the service name and the service methods names, and the methods asynchronous model.

That’s it! We can use our service!
Here we are creating an empty microservice and use it and greeting service definition to create a proxy. After we got the proxy we can invoke remote services the same as a local module or a class!

Learn more

To learn more, everything you need is here: https://github.com/scalecube/scalecube-js including full documentation.

You can also play with the code-sandboxes
- Browser + server https://codesandbox.io/s/yjjvu
- Browser https://codesandbox.io/s/bwtl7

Give it a try, feedbacks & questions are welcomed
Happy hacking

Bonus everything in one CodeSandbox

https://codesandbox.io/s/scalecube-full-tutorial-yjjvu

--

--