CQRS Event Sourcing: Validate UserName uniqueness

前端 未结 8 1242
醉话见心
醉话见心 2021-01-29 18:08

Let\'s take a simple \"Account Registration\" example, here is the flow:

  • User visit website
  • Click \"Register\" button and fill form, click \"Save\" button
8条回答
  •  情歌与酒
    2021-01-29 18:40

    I think for such cases, we can use a mechanism like "advisory lock with expiration".

    Sample execution:

    • Check username exists or not in eventually consistent read model
    • If not exists; by using a redis-couchbase like keyvalue storage or cache; try to push the username as key field with some expiration.
    • If successful; then raise userRegisteredEvent.
    • If either username exists in read model or cache storage, inform visitor that username has taken.

    Even you can use an sql database; insert username as a primary key of some lock table; and then a scheduled job can handle expirations.

提交回复
热议问题