Backend Learning
Today’s progress → On my working project WaitLess – implement Queue Lifecycle
→ From joining a queue → to viewing it → to calling the next person
#The_Problem
→ Multiple users can hit the same endpoint simultaneously
→ Database does not automatically prevent conflicts
→ Without proper handling:Duplicate ticket numbers will be assigned,the same user might be called multiple times and it will lead to inconsistent and broken queue state.
→ challenge: How to maintain correctness under concurrency
#Transactions
→ Used Prisma — $transaction — to wrap critical operations to ensure all steps execute as a single unit and If any step fails the entire operation is rolled back automatically.
→ Prevents:Half-created records ,Inconsistent data
→ Guarantees strong data integrity
#Row_Level_Locking
→ Used — SELECT ... FOR UPDATE – to lock specific queue rows . It ensures only one request can modify queue at a time
→ Other concurrent requests must wait
→ Prevents race conditions at database level
#Problem_Facing
the problem i am facing now is that when i create a new queue
→ when i send request 1 by 1 from postman , its working properly--and after that when i stress test it ------------ faces an error -→ PrismaClientKnownRequestError:
Invalid `prisma.token.create()` invocation:
Unique constraint failed on the fields: (`"queueId"`, `"ticketCode"`)
and then when i send a request again from postman--> then i encounter the above error this time.
Similarly when i create another new queue and run a stress test --> the output is perfect as expected
- TEST RESULTS ---
Request 1: Assigned Ticket #1
Request 2: Assigned Ticket #5
Request 3: Assigned Ticket #2
Request 4: Assigned Ticket #6
Request 5: Assigned Ticket #7
Request 6: Assigned Ticket #4
Request 7: Assigned Ticket #3
Request 8: Assigned Ticket #10
Request 9: Assigned Ticket #8
Request 10: Assigned Ticket #9
after that when i again run a stress test --> faces again the same error PrismaClientKnownRequestError:
Invalid `prisma.token.create()` invocation:
Unique constraint failed on the fields: (`"queueId"`,
and also the request from postman i encounter the same error .
EndPoints Implemented
Join_Queue
→ Executed inside transaction
→ Lock queue row before processing
→ Read last ticketCode in descending order
→ Generate next ticket number safely
→ Check for existing WAITING token for same user
→ Prevent duplicate queue entries
→ Create new token with:
→ queueId, businessId, userId, ticketCode, state
→ Result:
→ No duplicate tokens
→ Strict sequential order maintained
Get_Queue
→ Return tokens with states: WAITING and CALLED
→ Order by ticketCode ascending
→ Include minimal user info
Call_Next
→ Executed inside transaction
→ Lock queue row again
→ Prevent multiple “call next” executions
→ Find first WAITING token
→ Update state to CALLED
→ Add calledAt timestamp
→ If no token found → throw error
→ Result:
→ Only one token processed
→ No duplication
→ Consistent queue progression
# We_Can_Say_That
→ Transaction ensures atomic execution
→ Lock ensures exclusive access
→ Without transactions: partial failures possible
→ Without locks: race conditions occur
→ With both: predictable and reliable behavior
#Point_to_be_Considered
→ Backend is about managing concurrency and ensuring consistency not just CRUD operations
→ Systems must handle simultaneous actions correctly
#BackendEngineering #PostgreSQL #NodeJS #Concurrency #BuildInPublic #SaaS
0
0 comments
Shoaib Khan
4
Backend Learning
powered by
Make $1k-$10k in 30 days
skool.com/make-1k-5k-in-30-days-8449
Media Valley School is the fastest way to hit $1K/month with freelancing or agency work. Guaranteed.
Land your first $1K month in 30 days
Build your own community
Bring people together around your passion and get paid.
Powered by