Transactions in NoSQL?

2019-01-29 21:38发布

I'm looking into NoSQL for scaling alternatives to a database. What do I do if I want transaction-based things that are sensitive to these kind of things?

11条回答
够拽才男人
2楼-- · 2019-01-29 22:10

Depends on your DB, but ... I would say in general, you can use 'Optimistic transactions' to achieve this but I imagine one should make sure to understand the database implementation's atomicity guarantees (e.g. what kind of write and read operations are atomic).

There seems to be some discussions on the net about HBase transactions, if thats any help.

查看更多
Lonely孤独者°
3楼-- · 2019-01-29 22:11

The problem with one transaction and two operations (for example one pay $5,000, second receive $5,000) - is that you have two accounts with same priority. You cannot use one account to confirm second (or in reverse order). In this case you can guaranty only one account will be correct (that is confirmed), second (that confirm) may have fails. Lets look why it can fails (using message aproatch, sender is confirmed by receiver):

  1. Write +$5,000 to receiver account
  2. If success - write -$5,000 to sender account
  3. If fails - try againt or cancel or show message

It will guaranty save for #1. But who guaranty if #2 fails? Same for reverse order.

But this is possible to implements to be safe without transactions and with NoSQL. You are always allowed use third entity that will be confirmed from sender and receiver side and guaranty your operation was performed:

  1. Generating unique transaction id and creating transaction entity
  2. Write +$5,000 to receiver account (with reference to transaction id)
  3. If success - set state of transaction to send
  4. Write -$5,000 to sedned account account (with reference to transaction id)
  5. If success - set state of transaction to receive

This transaction record will guaranty that is was ok for send/receive massages. Now you can check every message by transaction id and if it has state received or completed - you take it in account for user balance.

查看更多
不美不萌又怎样
4楼-- · 2019-01-29 22:11

surely there are others

查看更多
你好瞎i
5楼-- · 2019-01-29 22:13

Just wanted to comment to money transaction advice on this thread. Transactions are something you really want to use with money transfers.

The example given how do que the transfers is very nice and tidy.

But in real life transferring money may include fees or payments to other accounts. People get bonuses for using certain cards that come from another account or they may get fees taken from their account to another account in same system. The fees or payments can vary by financial transaction and you may need to keep up bookkeeping system that shows credit and debit of each transaction as it comes.

This means you want to update more than one row same time since credit on one account can be debit on one or more accounts. First you lock the rows so nothing can change before update then you make sure data written is consistent with the transaction.

That's why you really want to use transactions. If anything goes wrong writing to one row you can rollback whole bunch of updates without the financial transaction data ending inconsistent.

查看更多
Emotional °昔
6楼-- · 2019-01-29 22:14

That's why I'm creating a NoSQL Document store solution to be able to use "real" transactions on Enterprise applications with the power of unstructured data approach. Take a look at http://djondb.com and feel free to add any feature you think could be useful.

查看更多
祖国的老花朵
7楼-- · 2019-01-29 22:22

Generally speaking, NoSQL solutions have lighter weight transactional semantics than relational databases, but still have facilities for atomic operations at some level.

Generally, the ones which do master-master replication provide less in the way of consistency, and more availability. So one should choose the right tool for the right problem.

Many offer transactions at the single document (or row etc.) level. For example with MongoDB there is atomicity at the single document - but documents can be fairly rich so this usually works pretty well -- more info here.

查看更多
登录 后发表回答