Sobes.tech
Middle — Middle+

Explain the concept of event-driven architecture (EDA).

sobes.tech AI

Answer from AI

Event-Driven Architecture (EDA) is an approach to designing software systems where components interact through events.

Main ideas:

  • Event is a message indicating that something has happened (e.g., a change of state, a user request, a system notification).
  • System components respond to events by subscribing to or listening for them.
  • The architecture allows for creating more flexible and scalable systems, where parts of the application are loosely coupled.

Example: in an order processing system, an "New Order" event can trigger a chain of actions — checking stock availability, notifying the warehouse, updating statistics.

In Python, this can be implemented using the "observer" pattern or specialized libraries like pyee or asyncio with events.

import asyncio

class EventEmitter:
    def __init__(self):
        self._events = {}

    def on(self, event, callback):
        self._events.setdefault(event, []).append(callback)

    async def emit(self, event, *args):
        for callback in self._events.get(event, []):
            await callback(*args)

async def on_new_order(order_id):
    print(f"Processing new order {order_id}")

emitter = EventEmitter()
emitter.on('new_order', on_new_order)

asyncio.run(emitter.emit('new_order', 123))

Thus, EDA promotes asynchrony, modularity, and improves application scalability.