Microservices Architecture
1.1 Overview
1.2 Structure of Microservices
The CapsureLabs microservices architecture is divided into several key services, each organized by function:
Manages user data, authentication, and access control.
Each of these services is deployed independently and communicates with other services using secure, well-defined interfaces. The microservices structure is designed to support horizontal scaling, meaning each service can scale independently based on demand.
1.3 Service Communication
Communication between services is handled through two primary mechanisms:
REST is used for standard HTTP communication, providing simple and scalable communication between internal services.
REST API Communication
A common scenario involves the User Service interacting with the NFT Service to fetch a user’s NFT assets. Here’s an example configuration for a REST API-based communication:
User Service Code for Fetching NFTs
// Import dependencies
const axios = require('axios');
// Function to fetch user NFTs from the NFT service
async function fetchUserNFTs(userId) {
try {
const response = await axios.get(`http://nft-service.internal/api/nfts/user/${userId}`);
return response.data;
} catch (error) {
console.error('Error fetching NFTs:', error);
throw new Error('Unable to fetch user NFTs');
}
}
gRPC Communication
For high-performance communication, CapsureLabs uses gRPC. In this example, the Trading Service sends real-time market data to the Analytics Service using gRPC.
Trading Service Code for Sending Market Data via gRPC
Define gRPC Protocol (market.proto
)
market.proto
)syntax = "proto3";
package market;
service MarketDataService {
rpc SendMarketData (MarketDataRequest) returns (MarketDataResponse);
}
message MarketDataRequest {
string assetSymbol = 1;
double price = 2;
int64 timestamp = 3;
}
message MarketDataResponse {
bool success = 1;
}
Trading Service Client Implementation
const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');
// Load protocol definition
const PROTO_PATH = './market.proto';
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {});
const marketProto = grpc.loadPackageDefinition(packageDefinition).market;
// Define client
const client = new marketProto.MarketDataService('analytics-service:50051', grpc.credentials.createInsecure());
// Function to send market data
function sendMarketData(assetSymbol, price, timestamp) {
const request = { assetSymbol, price, timestamp };
client.SendMarketData(request, (error, response) => {
if (error) {
console.error('Error sending market data:', error);
return;
}
console.log('Market data sent successfully:', response.success);
});
}
In this example, Trading Service sends market data to the Analytics Service via gRPC, providing a low-latency communication path for real-time updates.
1.4 Service Discovery and Load Balancing
To ensure that services can dynamically locate and communicate with each other, CapsureLabs employs a service discovery mechanism. This mechanism uses a registry where services register themselves upon startup, and other services can query this registry to locate the necessary endpoints. This registry enables load balancing and supports scaling up instances of high-demand services.
For service discovery and health checks.
1.5 Data Management and Persistence
Each microservice is responsible for managing its own data and database. This design follows the database-per-service pattern, which isolates each service’s data to reduce dependencies. For shared data, each service either directly queries or requests access from the data-owning service.
1.6 Error Handling and Resilience
To ensure that the system remains stable, CapsureLabs implements
Circuit breaker patterns prevent cascading failures by monitoring service health and disabling calls to failing services.
Last updated