2

Hi I have order creation functionality in my project and I am giving a order_id to client which is a auto-increment ID

order = serializer.save(user=user,created_by=user,platform=platform)

Now how should I handle the race condition on order table if two instance of my code are running on kubernetes pods?

1
  • Can you add more details about your system and order transactions? Commented May 27, 2024 at 0:37

2 Answers 2

0

race condition on order table if two instances of my code are running on two kubernetes pods?

There shouldn't be one, as they should be hitting a central RDBMS server such as Postgres or MariaDB. Some cloud vendors have offerings with attractive failover properties; you might consider an AWS Postgres RDS cluster.

So on the one hand you're paying the ACID tax, but on the other hand those central locks make race worries simply disappear.


Your question didn't specify, but let's assume we've chosen to go the NoSQL route. Then GUIDs are the way to go.

Alternatively you could assign a unique ID to each pod, and make each pod responsible for persisting the serials it has given out, so that even across reboots we'll never see a duplicate (pod_num, serial) pair. It's possible to combine those into a 64-bit integer if you give each pod ID a range of, say, a million serials it is allowed to hand out before needing to request a fresh pod_num.

A related design would spin up a SerialService which hands out blocks of a hundred or more unique serials to pods which request them. Run that service on a single pod, and make it responsible for persisting what it hands out, so we survive reboots. Make each block big enough so clients won't block during typical recovery operations for the service, such as reboot. Clients will want to request "next block please!" long before exhausting the one they're currently working through.

Or just stick with GUIDs.

Sign up to request clarification or add additional context in comments.

2 Comments

We are using AWS Postgres RDS cluster does it provide the race condition tolerance ?
Yes, it does by default. Put another way, you’d have to go out of your way, perhaps by changing Isolation Level and discarding Primary Key or similar Unique index, to write code that is accidentally susceptible to races. Just take care to BEGIN a transaction, do stuff, COMMIT the transaction, and pay attention to the success status. Failed commit means you lost the race and must retry. Make sure your automated test suite deliberately provokes such failures and verifies the retry did the right thing.
0

Using a central order processing system could be a solution. Here, for all incoming order requests, apply a queue lock based on the parameters that would cause a race condition.

order:
// Request handler

lock(product_id):
//continue
//Release

Additionally, if your table is in normal form, it might be beneficial to implement row-level locking. However, you need to carefully plan what actions to take if the operation fails.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.