|
2 | 2 | title: Events |
3 | 3 | --- |
4 | 4 |
|
5 | | -Events in Krescent are immutable facts that represent something that has happened in the system. They are the primary building blocks of Krescent's event-driven architecture. |
| 5 | +Events in Krescent are immutable facts that represent something that has happened in the system. They are the primary |
| 6 | +building blocks of Krescent's event-driven architecture. |
6 | 7 |
|
7 | | -## Base `Event` Class |
| 8 | +## Physical vs Virtual Events |
8 | 9 |
|
9 | | -All events in Krescent inherit from a base `Event` class. This class provides common functionality and a consistent structure for all events. |
| 10 | +In Krescent, events can be categorized into three types: |
10 | 11 |
|
11 | | -## `EventMetadata` |
| 12 | +- **Physical Events**: These are events that are sourced from an event stream and have a position, id and timestamp. |
| 13 | + All events in this category must be strictly serializable and are typically defined as Kotlin data classes. |
| 14 | +- **Virtual Events**: These are events created by the framework, a virtual event stream or model extensions and |
| 15 | + are either derived from physical events or related to the event processing pipelines state. |
| 16 | +- **System Events**: These are just a special case of virtual events which are only emitted by the framework and usually |
| 17 | + represent internal state changes, notifications or stream state changes. |
12 | 18 |
|
13 | | -Every event carries metadata, which is encapsulated in the `EventMetadata` class. This metadata includes: |
| 19 | +#### Example Event Definition |
14 | 20 |
|
15 | | -- **id**: A unique identifier for the event. |
16 | | -- **type**: The type of the event. |
17 | | -- **timestamp**: The time at which the event occurred. |
18 | | -- **position**: The position of the event in the event stream. |
| 21 | +```kotlin |
| 22 | +@Serializable |
| 23 | +data class BookAddedEvent( |
| 24 | + override val bookId: String, |
| 25 | + val title: String, |
| 26 | + val author: String, |
| 27 | + val price: Double, |
| 28 | + val copies: Int, |
| 29 | +) : Event(), BookEvent |
19 | 30 |
|
20 | | -## Physical vs. Virtual Events |
| 31 | +@Serializable |
| 32 | +data class BookPriceChangedEvent( |
| 33 | + override val bookId: String, |
| 34 | + val price: Double, |
| 35 | +) : Event(), BookEvent |
21 | 36 |
|
22 | | -Krescent distinguishes between two main types of events: |
| 37 | +interface BookEvent { |
| 38 | + val bookId: String |
| 39 | +} |
| 40 | +``` |
23 | 41 |
|
24 | | -- **Physical Events**: These represent actual occurrences in the system, such as a user action or a sensor reading. |
25 | | -- **`VirtualEvent`**: These are events that are derived or generated by the system itself, often as a result of processing physical events. |
| 42 | +## Event Catalog |
26 | 43 |
|
27 | | -## `SystemEvent` |
| 44 | +Physical events must be registered in one or more `EventCatalog` instances. The `EventCatalog` is responsible for |
| 45 | +defining and maneging known event types, their serialization and deserialization for the event processing pipeline. |
28 | 46 |
|
29 | | -`SystemEvent`s are a special category of events used by Krescent for internal purposes. Some examples include: |
| 47 | +The catalog also defines the type identifier for each event. Naming usually follows domain-based `domain.event` |
| 48 | +naming conventions where the type starts with the namespace of the domain and then ends with the event name. |
30 | 49 |
|
31 | | -- **`SystemStreamHeadEvent`**: Indicates the beginning of an event stream. |
32 | | -- **`SystemStreamTailEvent`**: Indicates the end of an event stream. |
33 | | -- **`SystemHintCommitTransactionEvent`**: Provides a hint to commit a transaction, often used in scenarios involving transactional event processing. |
| 50 | +```kotlin |
| 51 | +val bookstoreEventCatalog = buildEventCatalog(1) { |
| 52 | + event<BookAddedEvent>("book.added") |
| 53 | + event<BookPriceChangedEvent>("book.price_changed") |
| 54 | + event<BookRemovedEvent>("book.removed") |
| 55 | + event<BookLentEvent>("book.lent") |
| 56 | + event<BookReturnedEvent>("book.returned") |
| 57 | + event<BookCopyAddedEvent>("book.copy_added") |
| 58 | + event<BookCopyRemovedEvent>("book.copy_removed") |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +All event catalogs are **versioned** using a revision number. You should increment the revision number **whenever |
| 63 | +breaking changes** are made to the events in the catalog that would **affect serialization or deserialization** or |
| 64 | +**change the meaning** of the events. |
| 65 | + |
| 66 | +> [!IMPORTANT] |
| 67 | +> A change in this revision number will cause all models that use this catalog to discard their checkpoints the next |
| 68 | +> time they are loaded. |
34 | 69 |
|
35 | | -These system events help manage and coordinate the flow of events within Krescent. |
|
0 commit comments