Home > Software engineering >  Need suggestions: Send multiple images to backend, perform upload operation in backend, send respons
Need suggestions: Send multiple images to backend, perform upload operation in backend, send respons

Time:04-27

I need some best practice guidelines for a backend service in a scenario like this one:

  1. UI sends multiple images for uploading to the backend service
  2. Backend service receives all of the images and processes upload to storage one by one
  3. There can be failure in 1 or multiple image upload

My question is how do I send the response towards UI if my backend service is unable to upload 1 or more file(s).

One way can be to send failed and successful image link together in a JSON response body. So the UI knows about the failure and handles it in its own way. Another way can be to send only the successfully uploaded images' link which is the best case scenario.

Any suggestions will be welcomed with some reference links.

CodePudding user response:

This isn't really just a question of best practice, as there are multiple ways you could implement it, more than one of which could be valid. This is actually an architecture and design question, with more than one valid answer, hence I don't think it fits as a Stack Overflow question and you will not get references to any one correct approach.

That said, by way of an answer I will outline what I think you need. At a very high level, and not necessarily in this order but taking these factors into account, I would:

  1. Design the UI process flow. For example, you may decide that the user process will have several stages:
  • User selects first image for upload;
  • User selects each subsequent image for upload;
  • User presses some kind of "Go" button after selecting all images;
  • System now uploads the batch, and user receives a response confirming success or otherwise;
  • User has option to click through to detailed success/error details.
  1. Design the required success/error reports

  2. Design the data needed to support the overall functionality

  3. Provide one or more APIs giving the upload function and the report function(s) the CRUD access they need to this data

If you hit any specific technical issues at any stage, then please post a new questions accordingly as you go.

As to the point you mentioned, how to send the UI response, there is more than one valid way but I would return a basic success/falure response initially, containing only minimal details such as number of successes, and return more details in further messages in response to user actions (such as clicking through to detailed success/error details), at which point I would retrieve the requested error details from the database.

As I said at the start of my answer, I don't think your question can be answered just in terms of best practices, as it's a whole architecture and design question, but I hope my answer helps you along this path.

CodePudding user response:

Use an Orchestrator - something specific that can coordinate multiple actions and provide a meaningful result back to the caller.

This might be as simple as a component sitting in the UI that orchestrates calls to the backend. The UI component and the backend service might be designed as parts of a cohesive solution, or the UI component might simply act as a type of client/proxy/facade to some random backend service.

  1. UI calls the orchestrator with references to all the images it needs uploading.
  2. The orchestrator works through the items, uploading each as you prefer (sequentially or in parallel, etc). For each file, handle errors however you prefer - e.g. try once and die gracefully on failure; put errors into a queue or some other mechanism for retry (how many times is up to you); etc.
  3. Based on rules internal to the orchestrator, return status to the caller.

For potentially long-running processes (like file uploads) make sure the call to the orchestrator is asynchronous.

Rather than only returning "complete" result at the end, the orchestrator might provide a simple status back, allowing callers to get some idea of where processing is at. For example, you might have a call-back (from the orchestrator to it's caller) that simply emits very simple statuses like: processing, failed and complete. A more complex solution would be for the orchestrator to return more specific info like %complete and detailed error info.

Have a look at how the big cloud providers do complex file uploads by reading their documentation and studying their API's.

I need some best practice guidelines for a backend service

In no particular order:

  1. Keep it as simple as possible - generally, the fewer moving parts the better. E.g. pay attention to the Single Responsibility Principle (SRP).
  2. Clean up after yourself. If the upload service generates any data - make sure you have a clean-up process so you don't end up with mountains of un-needed data lying around, especially stuff like image files. If you design an upload solution that maintains state (which is independent of what happens to the images once they are uploaded) then you'll be storing data which probably won't be needed once the images are all processed.
  3. Think about support - not just developer debugging but also operational support. Getting your solution into production is not the end result, it's just the beginning.
  4. If designing this solution across teams (e.g. frontend and backend teams) make sure both teams are involved in the design. If the backend team can't provide a solution that works for the frontend team then it's not going to end well.
  5. Think about the likely error scenarios and how can you handle them.
  • Related