absmach / magistrala

Industrial IoT Messaging and Device Management Platform
https://www.abstractmachines.fr/magistrala.html
Apache License 2.0
2.47k stars 671 forks source link

Events/History service #1957

Closed dborovcanin closed 3 months ago

dborovcanin commented 9 months ago

Events/History service will track Thing and Channel (and Users?) change/update history. There are two potential approaches and the goal of this issue is to explore the drawbacks and benefits of them. 1) Use events from Things service, process them, and store them in the internal DB. 2) Use DB triggers to automatically update history table(s).

rodneyosodo commented 9 months ago

Proposed solution

Events from Things service, process them and store them in the internal DB

This approach is based on the idea that the Things service will publish events to the event store and the events processing service will subscribe to them. The service will process the events and store them in the internal DB. The service will expose REST API to query the history.

Pros

Cons

DB triggers to automatically update history table(s)

This approach is based on the idea that we can use DB triggers to automatically update history table(s) when a record is inserted, updated, or deleted in the Things table.

Pros

Cons

Unconsidered approach

Since we already have an event store that stores our events, there is no need to store them in the internal DB. We can use the event store to query the history.

For example, if I want to get the history of a thing, I can query the event store for all events related to that thing. The events endpoint will return a list of events, and I can use the different filters to get the events I need, e.g.: ?thingId=123, ?thingId=123&operation=update, ?thingId=123&channelId=456, etc.

Events can be easily sent to other services or systems for further analysis or action. For example, we can send events to a reporting engine to generate reports. We can send events to a dashboard application to display the history of a thing or channel. We can send events to a monitoring system to monitor the health of our system.

Conclusion

I would lean on using DB triggers to automatically update history table(s). This approach is simple and requires minimal development effort. You don't need to implement a custom event processing system, and you can rely on the database to handle the event processing logic. Since the updates are triggered at the database level, there is a higher level of consistency. Eliminates the need for separate event processing infrastructure. Based on this reference triggers can be used to audit data modifications. The downside of this approach is that it is tightly coupled to the service database which means that if the database schema changes, the triggers will need to be updated. Also, the changes/history will be stored in the same database as the service data which means that the database will grow faster and the performance will be affected. This negates the use of event-driven architecture. By definition, event-driven architecture is a software architecture pattern promoting the production, detection, consumption of, and reaction to events. An event can be defined as "a significant change in state". Event sourcing is an architectural pattern in which entities do not track their internal state utilizing direct serialization or object-relational mapping, but by reading and committing events to an event store, a type of database optimized for storage of events.

Therefore, I would propose a different approach. We already have an event store that stores all events. We would use the event store to query the history. For example, if I want to get the history of a thing, I can query the event store for all events related to that thing. The events endpoint will return a list of events, and I can use the different filters to get the events I need, e.g.: ?thingId=123, ?thingId=123&operation=update, ?thingId=123&channelId=456, etc.

arvindh123 commented 9 months ago

@rodneyosodo In current Event Store, It possible to store data without any retention policy for infinite time and storage size?

rodneyosodo commented 9 months ago

Infinite time I don't think so, but you can set the MaxAge you want the data to live.

Based on our discussion with @dborovcanin , the broker i.e. message and events should temporarily store that data. This is because the brokers help us be distributed.

For persistent storage, we can use the consumers to store the data in our different supported databases. This is will something similar to how messages are stored.

image

rodneyosodo commented 3 months ago

@dborovcanin This is closed by https://github.com/absmach/magistrala/pull/2057