Convert Event-Driven to API-based System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-11

Problem Statement

Design a system that translates an existing event-driven workflow into an API-based architecture. The goal is to allow services that consume events (from message brokers like Kafka, RabbitMQ, or internal queues) to instead interact via request/response APIs while preserving the original functionality.


✅ Functional Requirements


1.Event Ingestion
•Capture events from existing event sources (e.g., UserCreated, OrderPlaced).
•Maintain event metadata: eventId, type, payload, timestamp.
2.API Interface
•Expose REST or gRPC endpoints for each type of event.
•Support synchronous request/response semantics replacing asynchronous event consumption.
3.Mapping & Transformation
•Map incoming API requests to the equivalent event payloads internally.
•Optionally, transform payload structure if API schema differs from event schema.
4.State Management (Optional)
•Maintain current state of entities if API calls require it.
•Ensure idempotency when handling repeated API requests.
5.Error Handling & Retries
•Provide meaningful error messages for failed requests.
•Retry or queue requests if internal processing fails.
6.Monitoring & Logging
•Track API calls and corresponding internal events.
•Provide metrics for request rate, success/failure rate, and latency.


🔐 Constraints & Assumptions


•Event-driven system is already in production.
•API layer should not break existing workflows.
•Assume JSON or protobuf payloads for events and APIs.
•Focus is on LLD, modular components, and mapping logic, not network or security configuration.


🧱 Key Components & Classes


1.Event Class
1 2class Event { 3private: 4 string eventId; 5 string eventType; 6 string payload; 7 DateTime timestamp; 8public: 9 string getEventType(); 10 string getPayload(); 11};
1.EventConsumer Class (Optional)
1 2class EventConsumer { 3public: 4 Event fetchEvent(); 5 void acknowledgeEvent(string eventId); 6};
1.APIController Class
1 2class APIController { 3private: 4 EventMapper* mapper; 5 EventProcessor* processor; 6public: 7 Response handleRequest(Request req); 8};
1.EventMapper Class
1 2class EventMapper { 3public: 4 Event mapRequestToEvent(Request req); 5 Response mapEventToResponse(Event event); 6};
1.EventProcessor Class
1 2class EventProcessor { 3public: 4 void processEvent(Event event); 5 bool validateEvent(Event event); 6};
1.Logging & Monitoring Class (Optional)
•Logs API calls, event processing, and failures.


🔄 Example Workflow


1.API request received → APIController.handleRequest(req).
2.Request mapped to internal event → EventMapper.mapRequestToEvent(req).
3.Event validated and processed → EventProcessor.processEvent(event).
4.Response generated → EventMapper.mapEventToResponse(event) and returned to client.
5.Logging captures request, event processing, and outcome.


🧪 Expected Deliverables


•Class diagram showing APIController, EventMapper, EventProcessor, and Event.
•Implementation of request-to-event mapping, processing, and response generation.
•Test cases:
•Successful API-to-event processing
•Invalid payload handling
•Idempotency checks
•Logging verification


💡 Discussion Points


•Maintaining event ordering and consistency when converting to synchronous APIs.
•Handling duplicate requests and idempotency.
•Optional: supporting batch API calls to replicate event streams.
•Extensibility to bi-directional communication (API → event or event → API).


Solution

Comments