Wonders of multithreading

Hello, so I have this question, maybe it’s more coding logic related overall than bubble itself, but I’m sure there’s many smart people on the forum, so there we go-

The problem is with multithreading, let’s say we have 1 entry in our database like so :
ID = 123 , state = not busy

Then we would use bubbles API to get the entries that only have state = not busy like so

https://website.com/api/1.1/obj/things?api_token=abc&constraints=[{"key":"state","constraint_type":"equals","value":"not busy"}]

and once we get the the response back, we can then check the uniqueID returned and update it’s state to state = busy

And it works great, right?
Right?

It certainly does until we start to make more requests to bubble
Let’s say 5 people make the same API request at similar time, -
All 5 people will receive the same object as ‘not busy’

Then all of them will do the update to: state = busy
and all 5 of them would think that they are the only ones that are using that thing

In MySQL there’s at least a couple of methods to do this, like locking the table from others being able to access it.
So the question is - How do you make sure different sessions don’t get the same object?
Thank you.

1 Like

you could just add a new key that specifies a user (either a unique slug or user ID) and make the corresponding changes in your bubble app to add a userID when the object is generated.

So the JSON object would look like :
[{ “key”: “state”,
“contraint_type”: “equals”,
“value”: “not busy”,
“user_id” : “uniqueID” }]

such that 5 different users accessing the same thing will be interacting with similar JSON objects, with a different user uniqueID.

I could also be misunderstanding this – if I’m completely off, send some screenshots with more specific details :slight_smile:

1 Like

Thank you for your answer I can see it working in some use cases, but in this example it’s a unique object (think of it like an apartment for rent, but in digital space) , and it would have 2 states
available/rented
So if 2 users ask(make a request) to get available rooms - they get the same room, and they both ‘rent’ it (make a request to set state rented)

It is only 1 object, and it’s creator is administrator.

Now that I’m writing this your idea might actually just work, only need to update the buyers ID on the request, then we wait let’s say 5 seconds and check if the owner ID hasn’t changed ( other person hasn’t rented it)

But then again, what is the safe time to wait for other person to ‘buy’ it.

Let’s say I give you this objects uniqueID from my database-
1648922581808x909931021104673720

Both, me and you make a request now to my bubble database, and get the response that this item has the state ‘available’

Then after some time, you update this items state to ‘rented’ and owner = yourUserID
You start living in it
At the exact same time , I update this items state to ‘rented’ and owner = MyUserId
I start moving in, and it’s a funny situation.

The more I think about it the less I seem to understand :roll_eyes:

This will be an interesting thread :slight_smile:

I see this plugin below and the author mentions “ongoing costs” so perhaps it is requesting out to a third-party service/DB to hold the semaphore for the lock. eg

https://bubble.io/plugin/database-lock-1626966832899x409936294626197500

But fundamentally I don’t see how we can achieve this lock in the Bubble DB with API calls inherently multi-threaded and the Bubble DB not having locks/transactions (as we would understand and implement in a traditional DB).

I’m noodling on some idea how you could authenticate the requests as Bubble users and use the test is “logged in” as a semaphore.

ah… if multiple users can interact with the same object then you can just store a list of userIDs as an object, inside your original object:

[{ “key”: “state”,
“contraint_type”: “equals”,
“value”: “not busy”,
“user_ids” : [{ 
        "user1" : "user1ID"
        "user2":  "user2ID"
         etc. }]
 }]

and then remove the users’ ID when their rent is finished.

You can check if a user is currently a “buyer” or “renter” of the object by checking to see if their ID is in “user_ids”. In JS , objectName.user_ids returns the list, while objectName.user_ids.user1 returns the first user.

Although I am now slightly confused as to why you’d want to make requests to a bubble database :grin:

Yes, looks like you missed the point a little, maybe I expressed myself incorrectly.
There should never be 2 renters of the same apartment, only 1 person can be.

@lindsay_knowcode 's answer is on point, and the plugin should solve this exact issue, I am going to give it a try!
Got to say I’m a little skeptical because of the fact that it has 0 installs, and it doesn’t really explain how the functionality is achieved in the plugin page :roll_eyes: