Credit and resource management for microservices

Introducing Quots, a tool for handling user credits and resources for your microservices.

Pantelis Karatzas
euclia
5 min readOct 26, 2020

--

Landing page of quots

Let’s explain the problem

In the microservice landscape one hard problem is to manage the cost for a request that may have a route between multiple microservices. In modern applications, different services are needed and communicate with each other via interface calls. Each microservice handles different aspects of an application’s needs and may have a different cost of usage in terms of resources and eventually for the end user.

Since an application will probably use more than one cloud services, this easily gets hard to monitor: Imagine an application that utilises a compute engine or a kubernetes-managed cluster, a database and some machine learning APIs. These are three different cloud services with three different costs that utilised at different levels in order to satisfy the needs of the application. As an additional complicating factor, costs may vary over time and more services may be added or removed.

So how does a team working with microservices handle costing? How can an application be loosely coupled due to the use of microservices and maintain their advantages but at the same time effectively handle usage costs in centralised way?

Introducing Quots

Quots is a service that offers a solution to that problem. It is a Dockerised application that can run on any private or public cloud and can be applied on a service mesh or a basic microservice architecture.

The main thing that Quots tries to handle is microservices. This is handled through the applications page of Quots.

For each microservice a new application should be created. The first thing that the user should give is the application’s id.

For the sake of the demonstration we created an application under the id medium.

The applications main interface

Each application has the id and is secret (token) that is required by the microservices that will consume the Quots api. The application secret can be regenerated. Right now the application does not have a cost. This is something that is added through the applications page.

Usage types and costs

When adding an application characteristics, the user will be asked to enter the usage type and then the usage cost. Here for example we have added a GB cost that will handle a storage cost and a generic task usage type. For example the actual cost is 10 credits perGB and 5 credits for the task usage.

How can credits be consumed

Each user created on Quots through the offered Quots client’s has been assigned some credits by default.

For example here we have a random user under the id idrand, an email (not valid) and a username. By default upon the application deployment each user has 30 credits. This is something configurable and can be changed due to the needs. The user has used 0.2 credits on GB (amount of data saved on the main application ) and 5 on a task. Also credits can be added for the user if needed.

Clients

The easiest way to interact with the Quots app and API is through the interaction of the application with the clients offered for Python, Java, Go and C#.

The Java artifact can be found at https://search.maven.org/artifact/xyz.euclia/jquots/0.0.4/jar

When installed the client should be initialized:

public static void initQuots() {                                             
client =JQuotsClientFactory.createNewClient(“http://localhost:8002", “medium”, “d6LeSteKxezkZgNnCN”, new JQuotsSerializer(new ObjectMapper()));
serializer = new JQuotsSerializer(new ObjectMapper());
}

Then it can consume credits for each user whenever required:

public CanProceed canUserProceed() {
Future<Response> proceed = client.canProceed(“idrand”, “GB”, “10”);
if (proceed.get().getStatusCode() == 200) {
CanProceed cp = serializer.parse(proceed.get().getResponseBody(), CanProceed.class);
else{ ErrorReport er = serializer.parse(proceed.get().getResponseBody(),ErrorReport.class);
}
}

The credits are consumed by each application/service and that information can then be retrieved and is passed through the user entity:

Future<Response> user = client.getUserById("idrand");
QuotsUser qu = serializer.parse(user.get().getResponseBody(), QuotsUser.class);

Similarly it can be used from python, Go and c# services:

C# nuget package

Install-Package netquots -Version 0.1.0

Go package

go get github.com/euclia/goquots

Python pip module

pip install pyquots

Install Quots on your cluster

A docker-compose file that deploys the quots application. By now it runs on mongo db.

version: '3'

services:
mongo:
image: mongo
container_name: "quots-db"
ports:
- 27017:27017
networks:
- quots-network
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
volumes:
- ./data/db:/data/db
deploy:
replicas: 1
restart_policy:
condition: unless-stopped
quots:
image: euclia/quots
ports:
- 8000:8000
environment:
- MONGO_URL=mongo:27017
- CREDITS=20
- DATABASE=quotsdb
- QUOTS_USER=QUOTS
- QUOTS_PASSWORD=QUOTS
depends_on:
- mongo
deploy:
replicas: 1
restart_policy:
condition: unless-stopped

networks:
quots-network:
external: true

An example deployment on a kubernetes cluster

kind: Service
apiVersion: v1
metadata:
name: quots-app
namespace: quots
spec:
selector:
app: quots-app
ports
- protocol: TCP
port: 8000
targetPort: 8000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: quots-app
namespace: quots
spec:
selector:
matchLabels:
app: quots-app
replicas: 1
template:
metadata:
name: quots-app
labels:
app: quots-app
spec:
containers:
- name: quots-app
image: euclia/quots
ports:
- containerPort: 8000
env:
- name: MONGO_URL
value: <Your mongo db url>
- name: CREDITS
value: <Your user default credits>
- name: DATABASE
value: <Your database name>
- name: QUOTS_USER
value: <Username for quots>
- name: QUOTS_PASSWORD
value: <Password for quots>

For further support you can contact us through euclia@euclia.xyz

--

--