Two days ago I posted this question, I don't think I explained the problem right, so I will try to explain it better here.
Let's say I want to create a service, which returns a unique number when a request comes in.
This number should:
- start at
1 - be incremented by
1each time the service gets called - the same number should not be given to two users who request the service at the same time
- when the number reaches a certain threshold, the number gets reset back to
1 - I can manually reset it to
1even if it didn't reach the threshold
Now I need to build that service in an ASP.NET Core Web API, and Entity Framework Core, and Postgres as the database.
Here is an example python code that implement what I need:
class Numbering:
def __init__(self):
self.number = 0
def get_number(self):
self.number += 1
return self.number
def reset(self):
self.number = 0
num = Numbering()
for i in range(10):
print(num.get_number())
num.reset()
print("number has been reset")
for i in range(10):
print(num.get_number())
I want to create the same thing, but at a scale with multiple users requesting this service, no same numbers are given to two unique requests.
GENERATED ALWAYS AS IDENTITYwhich is backed by asequence. You may be better served just creating theseq_idwith aDEFAULTof a predefinedsequence. Then you can use theCYCLEparameter with themaxvalueparameter of thesequenceto automatically wrap the numbering back to 1. Basically you are looking for a throw away number that can be passed out without concurrency issues. A potential downside is a sequence can have gaps.