0

So this is my DataStructure:

Project
  - Name
  - ID
  - Image
  - History
       - User
       - Comment

When my application first starts it is pulling all projects with all details.

obviously very slow -high RAM (mostly because of images)

For now it is working but as projects add up performance is getting more worse every day.

I've done some research but cannot figure out what would be the best.

The options I think I have:

  1. Lazy-Loading on ProjectDetails (load details when user clicks on it)
  2. Using Thumbnails for Images (showing the real image only in detail-mode)

Questions for Option 1:

  1. Wouldn't this increase the query-count drasticly ?
  2. What is better after all keeping things in RAM or query more often ?
  3. What do I do with the Data I've loaded lazy ? Do I dispose everything when the details collapse or should I store them for some time in case the user opens it again ?

I'm planning on implementing both but I'm afraid of the unanswered questions mentioned above.

Felix D.
  • 105
  • 4
  • 2
    There is not a one-size-fits-all answer for this question. The trade off between loading more up front versus loading as needed is a factor in nearly all software projects that pull in data, and only you can know how to balance that. – Graham Feb 25 '19 at 14:12
  • Performance testing seems to be needed. How much would it actually increase the query count vs can the database handle it? Also, you could look into things like how frequently users click the images and how responsive it needs to be to provide a good user experience. – bitsoflogic Feb 25 '19 at 14:44
  • If the bad performance is due to front-loading everything, you could load only the essentials, then eagerly (non-blocking) load the pictures (knowing that it is likely the user will be clicking it soon). As @Graham said though, all these trade-offs really require a deep understanding of your business needs, which is in your court. – bitsoflogic Feb 25 '19 at 14:48
  • @all thanks alot for your comments. I will try to think about all the points you mentioned ! I will eventually search some logs and collect some data for a deep analysis on what is requested in what timespan. – Felix D. Feb 25 '19 at 15:21

3 Answers3

3

Opting to load everything into memory on start up is a premature optimization. You haven't collected statistics on how long it takes to query, determined if some caching is going to speed things up more than your indexes etc.

To answer your specific questions:

Wouldn't this increase the query-count drastically?

That depends on your definition of drastic. If you never pulled from the database after startup, then you will be accessing the database more regularly.

However, that activity is likely to be well within what your database can handle. You will have to do your own analysis to ensure that.

What is better after all keeping things in RAM or query more often?

That depends entirely on how much memory we are talking about, and whether you can purge items from memory that just aren't needed. While most servers can have a lot of RAM, it is still one of the most expensive places to store data. Keeping everything in memory at once minimizes the ways you can scale your system.

When profiling your system, you will find that some information is just taking up space in memory and is used very sparingly. However other items are used much more often.

Caching systems allow you to keep often used data resident in memory, while allowing the less frequently needed items to be reclaimed by the virtual memory system. With intelligent caching, you will use more memory than blindly hitting the database all the time, but the often requested data is available to speed things up.

By keeping things in memory, you also lose the ability to do complex queries on your data and take advantage of the indexing that the database can do. You will find that your database has a very intelligent caching system built in, so the more often you run the same query, the faster it will run (to a point).

What do I do with the Data I've loaded lazy ? Do I dispose everything when the details collapse or should I store them for some time in case the user opens it again?

If your language supports it, I would use a weak reference to the data set. Weak references are a way to hold on to data in case it is used frequently, but still allows the garbage collector to dispose of the data if loading new data causes additional memory pressure.

Designing around weak references requires you to check if it is still populated, and re-query if it isn't. It's one of the key mechanisms for caching.

Berin Loritsch
  • 45,784
  • 7
  • 87
  • 160
0

Lazy option seems better in your case, what's slow in a database request is the stream opening. A nice way to fix this issue is to let the stream open all the time. When a user will request something, you'll just need to perform the query, not the stream opening.

You can't store all your database in memory.

0

If the images are what's eating up your RAM, then maybe don't cache them or don't cache as many of them.

When a user goes to a page that has all of the data cached except the image, you can show a loading icon instead of an image while you fetch it.

Michael
  • 101
  • 2