There is some storage of access codes, each of which has two properties - expiration time and the number of attempts to confirm this code. After any of these parameters have somehow expired, the code can no longer be used, and it must be removed from the database. Here I see two strategies, how to deal with it:
- Delete the code when requesting this code from the database. Pros: no background tasks, minuses: race conditions (someone can get a record with retries = 1 at the moment when someone tries to confirm the same code in parallel) and a bulk code of simply getting a record
- Write a background-worker who will constantly crawl the base to find outdated entries. Pros: recordings take place as usual (checks remain on the validity of the recording, but there is no need to cause its removal), minuses: some lag and the existence of background workers, all the same race conditions.
Both strategies seem foolish to me, and I cannot figure out how to solve the problem correctly (although it seems to me that there is a more obvious solution that can minimize the minimum). The application itself is distributed, but the use of the service of distributed locks is quite acceptable (however, in itself, its use will save only from race conditions).
What is the most appropriate strategy for removing such records?
cron
service. - Vanyamba Electronics