Configurable UUID generator
slashdotdash opened this issue · comments
Commanded uses the elixir_uuid
library and UUID.uuid4()
to create version 4 randomly generated UUIDs. This uses pseudo-random bytes generated by the crypto
module. These UUIDs are used for dispatched command identity (command_uuid
) and correlation id (correlation_id
) and within the Commanded test suite.
It would be useful to allow the UUID generator to be configurable for a Commanded application and make the elixir_uuid
library an optional dependency. The Commanded Application config could be extended to specify the UUID generator as a function.
For performance reasons it can be preferable to generate UUIDs which are not entirely random. One example would be the event_id
for events stored in the Postgres database by the EventStore library which is the primary key. A database index on random values does not perform well due to writing to random positions in the index. Allowing partially sequential UUIDs to be generated should improve performance. To do this the Commanded.EventStore.EventData
struct needs to be extended to include an event_id
field which would allow Commanded to generate a UUID for each event using the configured UUID generator. The event store adapters then need to be updated to use this new field when persisting events.
Example
defmodule MyApp.Application do
use Commanded.Application,
otp_app: :my_app,
event_store: [
adapter: Commanded.EventStore.Adapters.EventStore,
event_store: MyApp.EventStore
],
uuid_generator: &MyUUID.new/1
router(MyApp.Router)
end