Redux for the Server: Developing a (opens in new tab)
Traditional CRUD-based architectures often struggle to meet complex backend requirements such as audit logging, version history, and state rollbacks. To address these challenges, Daangn’s Frontend Core team developed Ventyd, an open-source TypeScript library that implements event sourcing on the server using patterns familiar to Redux users. By shifting the focus from storing "current state" to storing a "history of events," developers can build more traceable and resilient systems.
Limitations of Traditional CRUD
- Standard CRUD (Create, Read, Update, Delete) patterns only record the final state of data, losing the context of "why" or "how" a change occurred.
- Implementing complex features like approval workflows or history tracking usually requires manual table management, such as adding
statuscolumns or creating separate history tables. - Rollback logic in CRUD is often fragile and requires complex custom code to revert data to a previous specific state.
The Event Sourcing Philosophy
- Instead of overwriting rows in a database, event sourcing records every discrete action (e.g., "Post Created," "Post Approved," "Profile Updated") as an immutable sequence.
- The system provides a built-in audit log, ensuring every change is attributed to a specific user, time, and reason.
- State can be reconstructed for any point in time by "replaying" events, enabling seamless "time travel" and easier debugging.
- It allows for deeper business insights by providing a full narrative of data changes rather than just a snapshot.
Redux as a Server-Side Blueprint
- The library leverages the familiarity of Redux to bridge the gap between frontend and backend engineering.
- Just as Redux uses Actions and Reducers to manage state in the browser, event sourcing uses Events and Reducers to manage state in the database.
- The primary difference is persistence: Redux manages state in memory, while Ventyd persists the event stream to a database for permanent storage.
Technical Implementation with Ventyd
- Type-Safe Schemas: Developers use
defineSchemato define the shape of both the events and the resulting state, ensuring strict TypeScript validation. - Validation Library Support: Ventyd is flexible, supporting various validation libraries including Valibot, Zod, TypeBox, and ArkType.
- Reducer Logic: The
defineReducerfunction centralizes how the state evolves based on incoming events, making state transitions predictable and easy to test. - Database Agnostic: The library is designed to be flexible regarding the underlying storage, allowing it to integrate with different database systems.
Ventyd offers a robust path for teams needing more than what basic CRUD can provide, particularly for internal tools requiring high accountability. By adopting this event-driven approach, developers can simplify the implementation of complex business logic while maintaining a clear, type-safe history of every action within their system.