booster
booster copied to clipboard
[Question] - How to handle atomic increments?
Example: a human-readable reference is needed next to a UUID. On create the BE needs to use the latest existing reference + 1. On update, a user can pass a custom reference, as long as the reference is not in use yet.
Possible solution:
- Do not set the reference together with the other values.
- Create an event handler which catches the
DossierCreatedevent - Store a counter state outside of Booster (Redis/non-event DB?)
- Create a new event from the event handler
DossierReferenceCreatedbased on the counter value
The previous question was asked by our contributor @thomas-advantitge.
Here's the answer:
Given the highly concurrent and eventful nature of Booster, counters are hard, so generally, we tend to avoid them. But thankfully, entities are transactional in some way. Perhaps a possibility here is to have a DossierCount entity with a unique ID so command handlers can query it with Booster.entity(). The first thing that comes to mind is that the count could be updated while the command handler is executing. A possible solution would be introducing a retry strategy with an event handler to ensure the counter hasn’t changed. If you could use non-sequential codes, that would be ideal though. UUID is only an alias for the String type so if you could ensure unicity by other means, for instance, using a barcode number or a real reference that is used in the real world, everything should work as well as using UUIDs.