Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do transaction processing using node.js and a noSQL db?

I'm not talking about real money transactions

The project I'm working on is a game where players trade stuff between each other. It's basically a transaction process, player A gives player B 10 groats in exchange for thirty cows, you get the idea.

But as it's interactive and there are many players at once, in a chatroom-like environment all trading randomly I wondered if it was possible to do such a thing with node.js but I see problems.

I come from a DB background where processing transactions and the nature of rollback and commit are necessary to maintain the DB state of health. But if we're talking node.js plus mongoDB (or any other noSQL DB for that matter) that surely is a whole different mentality, but I just don't see how it could handle a trade in the sense that only two parties should be involved without resorting to some form of locking, but surely that's not what node is about.

I haven't found anything yet, but that does not surprise me because node.js is so new.

UPDATE I am aware of the mechanisms of a transaction - and in particular banking style transactions, but this is not the same thing. I may not have made it clear, but the issue is that player B is selling something, to a community of buyers.

That means that although player A initiates a buy instruction on the client side it is also possible that around the same time Player C D or E also clicks to buy the same Cow.

Now in a normal transaction it is expected that at least the first person who obtains a record level table lock at least blocks the other parties from proceeding at that point in time.

However the nature of use of node and in particular its speed, concurrent processing and use for displaying real-time updates database mean that I could easily imagine that the slowest person (we're talking milliseconds) wins.

For example Player A initiates the purchase at the same time as player C. Player A transaction completes and the Groats are paid to Player B, and the Cow is assigned to Player A on the database. A millisecond later the Cow is assigned to player C.

I hope that explains the issue better.

like image 289
T9b Avatar asked Jul 25 '12 16:07

T9b


People also ask

Which DB can be used with node js?

“Node. js can only be used with MongoDB (which is the most popular NoSQL database).”

Can MongoDB be used for transactional applications?

MongoDB has always provided transactional guarantees on single-document operations. Atomicity: Single document operations have always been atomic in MongoDB. These operations can write to one or more fields, including subdocuments, elements in an array, and even nested arrays.

Why NoSQL is used with Nodejs?

With Node. js NoSQL databases, your developers can reduce application development time drastically, owing to no fixed schema and fast querying abilities that come with it. In this guide, we'll help you know the advantages of setting up your own Node. js NoSQL embedded database environment.

How use MongoDB transactions in node js?

You can easily setup the required MongoDB Transactions on Nodejs using the following steps: Step 1: Setup the Environment for MongoDB Transactions on Nodejs. Step 2: Create MongoDB Transactions on Nodejs. Step 3: Test the Function Associated with MongoDB Transactions on Nodejs.


1 Answers

This has nothing to do with Node.JS. Node.JS only connects to the database and transactions are done by database itself (unless you want to manually implement transactions in Node.JS, which might be a bit difficult task - but that's the same for any web server written in any language).

You can easily use (for example) MySQL with Node.JS which supports transactions. So the question you are asking is: can I do transactions with MongoDB? The answer is: no and yes.

No, because MongoDB does not support transactions out of the box.

Yes, because you can use some tricks to emulate transactions. See for example this article.

like image 118
freakish Avatar answered Nov 25 '22 14:11

freakish