3

does anyone know of best practices or common strategies in backend design for serving dynamic images and videos to client applications?

Background: I'm currently building an application that allows users to upload their own images and videos. I'm not really sure about how to serve these media files back to the client in the most efficient way. Do I store the files on the same VPS that my application server is running on? Do I need to save the files in different qualities / densities to better adjust for the clients' screen resolution? (I'll have mostly mobile clients)

I tried googling these questions but apparently I'm asking the wrong questions :-)

I would really appreciate maybe a reference or professional vocabulary on these topics.

Thanks in advance.

Wecherowski
  • 818
  • 11
  • 24

1 Answers1

4

1) You need to split web server and application server.

First of all do not try to stream media files from your backend unless you can offload low-level stuff to OS - most likely you will do it wrong. Use proxy server as an web server to serve such files. nginx will do.

Also you need to have backup of your media files the same way as you do backup of your database. Storing static huge media files along with application server is wrong move - it will not scale at all. You can add cron task to move files to some CDN server - when your move is complete you replace URL in database to match new location.

So by using nginx you will save precious CPU and RAM while file is getting moved to external server. And CDN will help you to dedicate bandwidth and CPU/RAM resources to application server.

2) Regarding image resolution and downsampling:

Screens of modern handsets have the same or even better resolution compared to typical office workstation. Link speeds have much bigger impact on UX. If client has smartphone with huge screen but with slow link you still have to deliver image or video as fast as possible even if quality of media will not be match the resolution of handset.

It makes sense to downsample images on demand and store result on disk for nginx/CDN to serve it again. In case of videos it makes sense to make "bad" version with big compression(quality loss) for the cases of slow link - device will downsample it itself during playback. And you can keep client statistics (screen sizes/downlink speeds) and generate optimized versions of such video file later when you see that it is "popular".

FYI: Several years ago some social meda giant dropped idea to prepare all possible versions of the same media file in favour of FPGA on-the-fly resampler. I do not remember the name of the company and URL to the article. It was probably instagram. Some cloud providers have offers with FPGA or CUDA on board to do heavy lifting. So in some cases you could exchange storage for heave horsepower to do conversion on the fly.

Maxim Sagaydachny
  • 2,098
  • 3
  • 11
  • 22
  • thanks, was going over this answer again. Can you give me a bit more infos on how the upload flow would work? Do I buffer the uploaded files on my app server first so that the user can continue with using the application? (and once everything was moved to the web server I notify the user). Or should I directly send the files to NGINX? – Wecherowski May 14 '20 at 11:16
  • 1
    app server should place new file into folder which is shared between app server and web server so it becomes serviceable at once - there absolutely should be no delay - user should not be affected while your file gets moved wherever it needs to end up finally. I have no idea what your requirements are so can't give you an advice which fits all cases. If access to the file should be restricted (i.e. download is allowed for logged-in user) then have a look at this [trick on how to service file in resource-load friendly](https://stackoverflow.com/a/59458463/12396017) manner – Maxim Sagaydachny May 14 '20 at 11:36
  • Thanks Maxim, one last question: can you give me some professional vocabulary / terminology on these topics so that I can do my own further research? "Separation of concerns in server architecture"? – Wecherowski May 14 '20 at 12:40
  • 1
    Accepted your answer, I've come to understand more and more of your answer after I did some reading :-) ... So the idea is to separate all my media files and file serving to another server (e.g. my own nginx-based server or some cloud storage / hosting solution like S3). And then on top of that I could distribute the stored files to some CDN network, correct? And regarding the downsampling: I'm not preparing different file versions instantly after the upload but rather generate and store on-demand, correct? – Wecherowski Jun 04 '20 at 09:59