Architecture design: Crypto Exchange example
Tags: systemdesign, softwareengineering, webdev, learning
Published: 2024-01-07 07:31:06

Designing systems is fun. That is why I decided to design a system for an imaginary crypto exchange called BitEx.
Let's take a look at the architecture. For that, we start with explaining the microservice. My language of choice is C# and Typescirpt but it does not matter much.

1. Microservices Architecture:
User Service:
Implementation: Develop using a framework like Django(Python), .Net(C#) or Spring (Java).
Functionality:
User registration, authentication, and authorization.
User profile management.
Order Service:
Implementation: Utilize a technology stack such as Node.js with Express or Django (Python).
Functionality:
Order creation, modification, and cancellation.
Validation of order parameters.
Market Service:
Implementation: Use a language like Go, C#, or Java with Spring Boot.
Functionality:
Real-time market data retrieval.
Trading pair information.
Execution Service:
Implementation: Develop using a language suitable for low-latency processing (e.g., Go).
Functionality:
Execution of matched orders.
Account balance updates.
Notification Service:
Implementation: Choose a lightweight framework like Django (Python) or Express (Node.js).
Functionality:
Publishing order status changes.
Real-time notifications to users.

2. Message Queue (Kafka):
Implementation:
Set up Kafka clusters using Apache Kafka or use a managed service like Azure Event Hubs.
Create Kafka topics for User Events, Order Events, and Market Events.
Use Kafka producers and consumers in microservices to publish and subscribe to events.

3. Database Management:
MySQL Database:
Implementation: Set up MySQL instances or use Azure Database for MySQL.
Functionality:
Store user account information.
Persist order details.
Support transactions for atomic operations.
Redis:
Implementation: Deploy Redis instances or use Azure Cache for Redis.
Functionality:
Caching user sessions for quick authentication.
Pub/Sub for real-time communication.

4. Docker Containers and Kubernetes:
Implementation:
Containerize microservices using Docker.
Use Kubernetes for container orchestration, managing deployments, and scaling.
Implement Kubernetes ConfigMaps and Secrets for configuration management.
Functionality:
Efficient scaling with Kubernetes Horizontal Pod Autoscaler.
Rolling updates and rollback capabilities.
Load balancing across microservice instances.

5. Load Balancing:
Implementation:
Utilize Azure Load Balancer to distribute traffic.
Configure load balancing rules for each microservice.
Functionality:
Evenly distribute incoming requests to maintain high availability.
Handle automatic failover in case of node failures.

6. Security (Azure):
Azure Active Directory (AAD):
Implementation: Integrate microservices with AAD for user authentication.
Functionality:
Secure user authentication and authorization.
Azure Key Vault:
Implementation: Use Azure Key Vault for managing secrets and sensitive information.
Functionality:
Secure storage of API keys, passwords, and encryption keys.
Azure Security Center:
Implementation: Enable Azure Security Center for threat detection.
Functionality:
Continuous monitoring and threat detection.
Automated responses to security incidents.
Azure Network Security Groups (NSG):
Implementation: Configure NSGs to control traffic between microservices.
Functionality:
Control inbound and outbound traffic to enhance security.

7. Monitoring and Logging:
Azure Monitor:
Implementation: Integrate microservices with Azure Monitor.
Functionality:
Real-time monitoring of application performance and availability.
Set up alerts for critical events.
Application Insights:
Implementation: Add Application Insights for detailed performance monitoring.
Functionality:
Code-level insights and diagnostics.
Tracing requests across microservices.
Azure Log Analytics:
Implementation: Configure microservices to send logs to Azure Log Analytics.
Functionality:
Centralized logging for easy troubleshooting.
Custom queries and analysis of log data.

8. Scalability:
Azure Kubernetes Service (AKS):
Implementation: Use AKS for managed Kubernetes clusters.
Functionality:
Automatic scaling based on resource usage.
Efficient resource utilization and cost management.
Azure Container Registry (ACR):
Implementation: Store Docker container images in ACR(Azure Container Registry).
Functionality:
Secure and managed storage of container images.

9. Fault Tolerance:
Azure Availability Zones:
Implementation: Deploy microservices across Azure Availability Zones.
Functionality:
High availability and redundancy.
Minimize downtime in case of failures.
Azure Backup:
Implementation: Set up Azure Backup for regular backups of databases.
Functionality:
Data recovery in case of accidental deletions or data corruption.
10. Testing:
Implementation:
Utilize testing frameworks for unit testing (e.g., JUnit, pytest).
Set up automated integration tests and stress tests.
Functionality:
Ensure the reliability and correctness of microservices.
Identify and fix performance bottlenecks.

11. Compliance:
Implementation:
Regularly audit and update security configurations to comply with industry standards.
Collaborate with legal and compliance teams to address regulatory requirements.
Functionality:
Periodic security and compliance audits are a must. Especially having regular security checks using bounty programs.

Now let's estimate the system capacity.

Estimating User Handling Capacity:
User Service:
Suppose the User Service can handle 500 user registrations/authentication requests per second.
Therefore, it can handle 500×60=30,000 user-related transactions per minute.
Order Service:

Assume the Order Service can process 1,000 order-related transactions per second.
This results in 
1,000×60=60,000 order-related transactions per minute.
Market Service:

Assume the Market Service can provide market data for 800 requests per second.
Thus, it can handle 800×60=48,000 market-related transactions per minute.
Execution Service:

Suppose the Execution Service can execute 700 matched orders per second.
This translates to 700×60=42,000 matched order transactions per minute.

Notification Service:
Assume the Notification Service can handle 600 notification events per second.
Consequently, it can handle 600×60=36,000 notification events per minute.
Total User Handling Capacity:
The overall user handling capacity of the system per minute is the minimum of the capacities of the individual services since the system is only as fast as its slowest component.

Finally the total capacity would be the minimum ofcapacities of the system which is 30,000

Total Capacity=min(30,000,60,000,48,000,42,000,36,000)

In this example, the total user handling capacity per minute is 30,000 transactions. This estimation helps understand the system's potential load and can guide decisions related to scaling, optimization, and capacity planning. For example we can see that our registration and authorization service capacity is 30,000. So we need to plan if we are going to have a heavy marketing campaign.