I have an app that has an assets
table like the following:
user_id | code | current_price |
---|---|---|
1 | ALUP11 | 12.5 |
2 | ALUP11 | 11.9 |
user_id
and code
are unique together and current_price
is updated if the user triggers a task that fetches the values from another service. I'm also using CQRS so when this task finishes it updates the read model asynchronously through messaging communication. So if an user has N
assets
, N + 1
messages will be dispatched to the broker when he/she triggers the task that updates current_prices
.
I'm starting to think that this was bad design because the current_price
of an asset
isn't something that should be bound to an user
but rather something global, shared across all users
.
My initial idea is to move this data to Redis and query from it to construct the aggregations that needs this data when updating the read model. If I go for it, I see a trade-off:
- On one hand it'll be expensive to update the read model because when updating the
current_price
of theassets
of anuser
I need to perform the aggregation on a small subset of entities, while movingcurrent_price
to a global DB forces me to update allassets
of all eligibleusers
.- Thus when this global tasks runs, it will publish
M * N
- whereM
is the number of users andN
is the number ofassets
for eachuser
- messages to the broker, which doesn't seem scalable at all to me; - I can think in a way to publish only
M
messages to the queue, i.e., instead of having a task that will update only oneasset
of a givenuser
, I could have a task that updates allassets
of a givenuser
.
- Thus when this global tasks runs, it will publish
- On the other hand, the load on that other service will reduce significantly because the number of task invocations will reduce dramatically. This services fetches the data from an external and paid API so some costs reduction will also happen here.
I'm inclined towards 2. because I think it'll be simpler and more effective to scale (considering that I can change the task logic as described in the second point of 1.).
Real-time accuracy is NOT crucial and it's "OK" to lose the granularity of updates for individual assets
.
NOTE: this is not a perceptive problem YET, but I think this will hurt myself in the future.
Have you experienced something similar? Is my train of though in the right direction?