B
B
Bur Ov2021-12-18 12:09:27
PHP
Bur Ov, 2021-12-18 12:09:27

How to deal with issuing the same information to different REST API clients?

Good afternoon. There is a REST API, with the help of a request, the user receives the necessary information from the database, if the requests go in such a way that the script on the server has time to process it and this information is noted in the database that it has already been given, then it is no longer issued to another user, but if several requests from different users at the same time, then the server can issue the same account, i.e. the first request does not have time to cancel the information as already issued and it is issued to both users. It even happens that if 1 user makes 2 requests in 1 second to give him information, then it happens that he gives out the same information, although it must always be unique.
How to deal with such a problem? Maybe you need to somehow configure the server so that it sequentially executes requests and gives answers? But then, if there are about 100 requests in the same 1-2 seconds, then users simply will not wait for a response from the server.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Gordinskiy, 2021-12-18
@DmitriyGordinskiy

SELECT ... FOR UPDATE SKIP LOCKED

N
nApoBo3, 2021-12-18
@nApoBo3

Depends on the development language and subd.
You need to implement a synchronous method in one form or another.
In postgres, as already mentioned above, for these purposes, you can use select for update, it allows you to block the record.
Another option is to first update the relevant record with a specific key, and then select on that key.
It is also possible to make an additional lock table with a unique key and try to write an entry to it if it is not possible to re-execute the action either on the server, or re-execute the request from the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question