Hisham Mubarak
Hisham Mubarak

Reputation: 1609

Managing constantly changing data in Database

I need some advice on how to architect my data in monogoDB. I have this app, where users can view, add, edit and remove credit and debit transactions. Below is how the data looks. Transactions Table

The balance column here is dynamic. For example if someone adds a transaction dates 10-09-2017, all the amount in the balance field thereafter needs to change in that moment to reflect the new transaction. Right now, I am not saving this balance field at all in the database and is calculating it every time when the user loads the page, reloads it, and also when editing, deleting, adding a transaction. Now it is fast, but I assume, in the future, when the user has a lot of transactions, they will become slow as these calculations needs to be done before the user is displayed the data table. Is there a more efficient way to do this?

Also I am doing the calculations on the client side, so the load is on the client's device and not on server. I think if it is on server side, and a lot of users start using it, the API requests will become much slower and not unusable at all after a while. Is this the right way?

PS : Also it was hard making sure the reader understand my questions but I have tried my best. Please let me know if I should explain this in more details or if I should add any more details.

Upvotes: 2

Views: 913

Answers (1)

mbnx
mbnx

Reputation: 942

It is not a question about mongodb, it is a question about user interface. Will you really display the whole history of transactions at once? You should either utilize pagination (simplest) or reload on scroll to load your data.

Before you get problems because of the balance cell calculation, it is more likely that you experience problems because of:

  • Slow loading from network (almost certainly)
  • Slow page interaction because of DOM size (maybe)

Show the first 100 to 500 transactions and provider the user with some way to load earlier entries.

Update - Regarding server-side balance calculation:

You could calculate balance on server-side and store it into a second collection which serves as a cache. If a transaction insertion happens in the past, you recalculate the cache. To speed this up, you can utilize snapshots: Within a third collection, you could store the current balance in certain intervals, e.g. with the following data structure:

{ Balance: 150000, Date: 2017-02-03, LastTransactionId: 546 } 

When a transaction is inserted in the past, take the most recent snapshot before that past moment and recalculate the cache based on that. This way, you. can keep the number of recalculated transactions pretty small.

Upvotes: 2

Related Questions