Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event-Sourcing: Aggregate Roots and Performance

I'm building a StackOverflow clone using event-sourcing. The MVP is simple:

  1. Users can post a question
  2. Users can answer a question
  3. Users can upvote and downvote answers to non-closed questions

I've modeled the question as the aggregate root. A question can have zero or more answers and an answer can have zero or more upvotes and downvotes.

This leads to a massive performance problem, though. To upvote an answer, the question (being the aggregate root) must be loaded which requires loading all of its answers. In non-event-sourced DDD, I would use lazy loading to solve this problem. But lazy loading in event-sourcing is non-trivial (http://docs.geteventstore.com/introduction/event-sourcing-basics/)

Is it correct to model the question as the aggregate root?

like image 660
MattTannahill Avatar asked Nov 17 '25 08:11

MattTannahill


1 Answers

Firstly don't use lazy loading (while using ORM). You may find yourself in even worse situation, because of that, than waiting a little bit longer. If you need to use it, most of the times it means, that your model is just simply wrong.

You probably want to think about things like below:

  1. How many answers to the question you expect.
  2. What happens, if someone posted an answer while you were submiting yours. The same about upvotes.
  3. Does upvote is just simply +1 and you don't care about it anymore or you can find all upvotes for user and for example change them to downvote (upvotes are identified).

You probably want to go for separate aggregates, not because of performance problems, but because of concurrency problems (question 2).

According to performance and the way your upvote behave you may think about modeling it as value object. (question 3)

Go ahead and read it http://dddcommunity.org/library/vernon_2011/

True performance hit you may achieve by using cqrs read/write separation http://udidahan.com/2009/12/09/clarified-cqrs/

like image 200
Dariss Avatar answered Nov 19 '25 02:11

Dariss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!