Search code examples
apiresponsequarkus

Can I send an API response before successful persistence of data?


I am currently developing a Microservice that is interacting with other microservices.

The problem now is that those interactions are really time-consuming. I already implemented concurrent calls via Uni and uses caching where useful. Now I still have some calls that still need some seconds in order to respond and now I thought of another thing, which I could do, in order to improve the performance:

Is it possible to send a response before the sucessfull persistence of data? I send requests to the other microservices where they have to persist the results of my methods. Can I already send the user the result in a first response and make a second response if the persistence process was sucessfull? With that, the front-end could already begin working even though my API is not 100% finished.

I saw that there is a possible status-code 207 but it's rather used with streams where someone wants to split large files. Is there another possibility? Thanks in advance.


Solution

  • "Is it possible to send a response before the sucessfull persistence of data? Can I already send the user the result in a first response and make a second response if the persistence process was sucessfull? With that, the front-end could already begin working even though my API is not 100% finished."

    You can and should, but it is a philosophy change in your API and possibly you have to consider some edge cases and techniques to deal with them.

    In case of a long running API call, you can issue an "ack" response, a traditional 200 one, only the answer would just mean the operation is asynchronous and will complete in the future, something like { id:49584958, apicall:"create", status:"queued", result:true }

    Then you can

    • poll your API with the returned ID to see if the operation that is still ongoing, has succeeded or failed.

    • have a SSE channel (realtime server side events) where your server can issue status messages as pending operations finish

    • maybe using persistent connections and keepalives, or flushing the response in the middle, you can achieve what you point out, ie. like a segmented response. I am not familiar with that approach as I normally go for the suggesions above.

    But in any case, edge cases apply exactly the same: For example, what happens if then through your API a user issues calls dependent on the success of an ongoing or not even started previous command? like for example, get information about something still being persisted?

    You will have to deal with these situations with mechanisms like:

    • Reject related operations until pending call is resolved "server side": Api could return ie. a BUSY error informing that operations are still ongoing when you want to, for example, delete something that still is being created.

    • Queue all operations so the server executes all them sequentially.

    • Allow some simulatenous operations if you find they will not collide (ie. create 2 unrelated items)