AY
ayushcode12/backend-intern-assignment
Spring Boot backend system for ingesting machine events with deduplication, concurrent-safe updates, and statistical analytics over time windows.
Backend Intern Assignment – Machine Events System
Overview
This project implements a backend system that ingests machine events, deduplicates and updates them safely under concurrent load, and provides statistical insights over time windows.
Architecture
- Spring Boot REST application
- Layered design: Controller → Service → Repository
- H2 in-memory database for local execution
Data Model
- EventEntity represents a machine event
- eventId is the primary key to support deduplication
- Indexed fields: machineId, factoryId, eventTime
Deduplication & Update Logic
- Same eventId + identical payload → ignored (deduped)
- Same eventId + different payload:
- Newer receivedTime → update
- Older receivedTime → ignored
- receivedTime is set by backend to ensure consistency
Validation Rules
- durationMs must be between 0 and 6 hours
- eventTime cannot be more than 15 minutes in the future
- defectCount = -1 is stored but ignored in defect calculations
Thread Safety
- eventId as primary key ensures uniqueness
- Batch ingestion runs inside a transactional boundary
- Database-level constraints prevent race conditions during concurrent ingestion
Performance Strategy
- Batch processing using JPA
- Indexed queries for stats endpoints
- Single transaction per batch
- Verified via benchmark (see BENCHMARK.md)
API Endpoints
POST /events/batch
Ingests a batch of machine events and returns counts for accepted, deduped, updated, and rejected events.
GET /stats
Returns event count, defect count, average defect rate, and health status for a machine in a given time window.
GET /stats/top-defect-lines
Returns top production lines by defect count for a factory within a time window.
Tests
- Deduplication and update logic
- Validation failures
- Defect exclusion logic
- Time window boundary correctness
- Concurrent ingestion thread-safety test
Setup & Run
- Java 17 required
- Run using:
mvn clean spring-boot:run - APIs available at http://localhost:8080
Improvements with More Time
- Pagination for stats endpoints
- Persistent database (PostgreSQL)
- Async ingestion with message queues
- Caching for frequently queried stats