A high-performance, production-ready microservice for securely compiling and executing user-submitted code against test cases. Built for LeetCode-style competitive programming platforms.
CXE is a standalone microservice that handles the complete lifecycle of code execution:
- Receives code submissions via REST API
- Generates test harness code dynamically
- Compiles user code securely
- Executes in isolated Docker containers
- Returns execution results with runtime/memory metrics
- π Secure Sandbox Execution - Docker-based isolation with resource limits
- β‘ Async Queue Processing - Redis-backed job queue with worker pool
- π§ Memory Tracking - Real-time memory usage measurement
- π― Multi-Language Support - Java & Python (extensible)
- π Per-Test-Case Metrics - Individual timing and output for each test
- π LeetCode-Compatible - Automatic imports, custom data structures (ListNode, TreeNode, Node)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Submission Service β
β (External Consumer) β
βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β HTTP POST /submit
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Code Execution Engine β
β βββββββββββββββββ βββββββββββββββββ βββββββββββββββ β
β β Controller βββββΆβ Redis Queue βββββΆβ Workers β β
β β (REST API) β β (Job Queue) β β (Pool) β β
β βββββββββββββββββ βββββββββββββββββ ββββββββ¬βββββββ β
β β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β File Generators ββ β
β β βββββββββββββββ βββββββββββββββββββββββββββ ββ β
β β β Main.java β β Solution.java β ββ β
β β β (Harness) β β (User Code + DS) β ββ β
β β βββββββββββββββ βββββββββββββββββββββββββββ ββ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Docker Container Execution ββ
β β β’ Compile (javac / python syntax check) ββ
β β β’ Run with timeout and memory limits ββ
β β β’ Parse TEST_CASE_RESULT output ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Java 17+
- Docker (with
dockercommand accessible) - Redis (or use docker-compose)
docker pull hrishabhjoshi/my-java-runtime:17docker-compose up -d redis./gradlew bootRunThe service starts on http://localhost:8081
POST /submit
Content-Type: application/jsonRequest Body:
{
"submissionId": "run-12345",
"userId": 1,
"questionId": 42,
"language": "JAVA",
"code": "class Solution {\n public int[] twoSum(int[] nums, int target) {\n // solution\n }\n}",
"metadata": {
"functionName": "twoSum",
"returnType": "int[]",
"parameters": [
{ "name": "nums", "type": "int[]" },
{ "name": "target", "type": "int" }
]
},
"testCases": [
{ "input": [[2,7,11,15], 9] },
{ "input": [[3,2,4], 6] }
]
}Response (202 Accepted):
{
"submissionId": "run-12345",
"status": "QUEUED",
"message": "Submission queued for execution"
}GET /status/{submissionId}Response:
{
"submissionId": "run-12345",
"status": "COMPLETED",
"runtimeMs": 15,
"memoryKb": 21340,
"testCaseResults": [
{
"index": 0,
"passed": null,
"actualOutput": "[0,1]",
"executionTimeMs": 3
}
]
}GET /result/{submissionId}Key settings in application.yml:
cxe:
worker:
count: 4 # Number of parallel workers
execution:
timeout-seconds: 10 # Max execution time per submission
memory-limit-mb: 256 # Container memory limit
docker:
image: hrishabhjoshi/my-java-runtime:17src/main/java/xyz/hrishabhjoshi/codeexecutionengine/
βββ controller/
β βββ ExecutionController.java # REST API endpoints
βββ service/
β βββ helperservice/
β β βββ ExecutionQueueService.java # Redis queue operations
β β βββ ExecutionWorkerService.java # Worker thread pool
β βββ filehandlingservice/
β βββ java/
β β βββ JavaFileGenerator.java # Main + Solution file gen
β β βββ JavaMainClassGenerator.java # Test harness generator
β β βββ JavaSolutionClassGenerator.java # User code wrapper
β β βββ InputVariableGenerator.java # Input parsing
β β βββ ValueDeclarationGenerator.java # Type-specific codegen
β βββ python/
β βββ PythonFileGenerator.java
βββ dto/
β βββ ExecutionRequest.java # Incoming request DTO
β βββ ExecutionStatus.java # Status response DTO
βββ CodeExecutionManager.java # Core execution orchestrator
| Type | Example Input | Generated Code |
|---|---|---|
int |
42 |
int x = 42; |
int[] |
[1,2,3] |
int[] x = new int[]{1, 2, 3}; |
int[][] |
[[1,2],[3,4]] |
int[][] x = new int[][]{{1,2},{3,4}}; |
String |
"hello" |
String x = "hello"; |
String[] |
["a","b"] |
String[] x = new String[]{"a", "b"}; |
char[][] |
[["a","b"]] |
char[][] x = new char[][]{{'a','b'}}; |
ListNode |
[1,2,3] |
Auto-generates linked list construction |
TreeNode |
[1,2,3,null,4] |
Auto-generates binary tree construction |
Node |
[[2,4],[1,3]] |
Auto-generates graph construction |
| Type | Example Input |
|---|---|
int |
42 |
List[int] |
[1,2,3] |
List[List[int]] |
[[1,2],[3,4]] |
str |
"hello" |
ListNode |
[1,2,3] |
TreeNode |
[1,2,3,null,4] |
- Docker Isolation: Each execution runs in a disposable container
- Resource Limits: Memory and CPU constraints prevent abuse
- Timeout Enforcement: Hard kill after timeout to prevent infinite loops
- No Network Access: Containers run without network by default
- Temp Cleanup: All generated files are deleted after execution
./gradlew test./gradlew clean builddocker-compose up -d
./gradlew bootRun- Redis Commander:
http://localhost:8085(enable with--profile dev-tools) - Application Logs: See
application.ymlfor log level configuration
CXE is designed to be consumed by a Submission Service which handles:
- User authentication
- Oracle (reference solution) execution
- Result comparison and verdict determination
- Database persistence
See context/frontend-integration-guide.md for full integration details.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Part of the AlgoCrack Platform - A modern competitive programming platform.