Sobes.tech
Senior

[name] asked: tell about event-driven architecture.

sobes.tech AI

Answer from AI

Event-driven architecture is an approach to building applications where components interact with each other through events. Instead of direct method calls, the system responds to events that can be generated by users, the system, or external sources.

Main features:

  • Asynchronous: event processing occurs independently, improving scalability and responsiveness.
  • Decoupling of components: the event sender does not know who or how it will be processed.
  • Flexibility and extensibility: easy to add new event handlers without changing existing code.

Example in Go using channels for event transmission:

package main

import (
    "fmt"
    "time"
)

type Event struct {
    Name string
    Data interface{}
}

func eventProducer(events chan<- Event) {
    for i := 0; i < 5; i++ {
        events <- Event{Name: "tick", Data: i}
        time.Sleep(time.Second)
    }
    close(events)
}

func eventConsumer(events <-chan Event) {
    for event := range events {
        fmt.Printf("Received event: %s with data %v\n", event.Name, event.Data)
    }
}

func main() {
    events := make(chan Event)
    go eventProducer(events)
    eventConsumer(events)
}

In this example, eventProducer generates events, and eventConsumer processes them, interacting through a channel, illustrating the event-driven approach.

[name] asked: tell about event-driven architecture… - sobes.tech