0

I'm working on a small project on Google App Engine, trying to implement a site that allows participants to buy and sell fake goods, similarly to a stock market where the system will show real time-ish BID/ASK spreads.

As a quick example:

  • A Seller places and order to sell 10 Boxes for 8.00 (Order 1)
  • A Buyer places then an order to purchase 5 boxes for up to 9.00 (Order 2)

When the second order is placed, the system will need to do multiple tasks, all contingent on all of the tasks completing successfully.

  • Take the funds (8.00 x 5) to pay for the boxes from the Buyer and give them to the Seller
  • Take the boxes (5) from the Seller and give them to the Buyer
  • Update the orders as complete (OID 2 ) or update as partially filled (OID 1) so that they cannot be double matched
  • Take a fee from each of the participants and add it to a system account

If all that I needed was to move funds from one participant to another, I can do that safely even if the system were to fail in the middle. But to ensure that all of the tasks above complete correctly, and roll-back if any of them fail seems overly complex in App Engine.

Additionally, my "Order Book" and order matching engine are single threaded right now (using Mutexes for locking.) This seems to go against the whole point of using App Engine, but I'm not sure I see a way around it.

So (finally) - My questions are:

  • Is there a best practice when using App Engine where there are multiple steps that all depend on every step completing correctly?
  • Does anyone have any suggestions as how to either, allow the order book to be multi-threaded, or if it remains single threaded - is there a best practice to not have this core piece block the use of the site as it scales? I've thought about using tasks to queue the order adds/updates/cancels to keep the book separate from direct participant input.

Thank you, I look forward to your help!

4

2 回答 2

1

I think if you can sequence the orders on a commodity, then you can clear orders offline. By "offline", I mean you can come to me at the end of the day, tell me the sequence of the orders, and I can tell you which trades happened. The one snag is, what if a buyer does not have the funds when a transaction should have cleared? You can address this in two ways:

  1. Put the funds in an escrow so that any orders that can clear do.
  2. Drop buy orders when you try to clear them if the funds are not available.

As you suggested, you'll probably need cross-entity group transactions in order to make sure the fund transfers are correct (i.e. funds are neither created nor destroyed).

You can sequence orders by time (e.g. paced_at = db.DateTimeProperty(auto_now_add=True)). If two orders have the same time, then use something (preferably something fair and deterministic) to break the tie. Hash (for fairness) of the numeric id (for determinism) might not be a bad choice here.

App Engine is inherently multithreaded in the sense that an app can have many concurrent instances (instances are not necessarily threads within the same process though). Instances are created automatically, and there is currently no way to cap the number of instances at 1. If the state of your app is in Datastore (as opposed to local memory, memecache, or somewhere else), and your transactions are correct, then your app will be multithreaded "for free". Of course, your transactions being correct is not trivial.

Another tool to keep in mind is that tasks can be transactional. This may come in handy if you want to do offline book clearing using tasks.

于 2013-04-13T12:07:57.843 回答
0

For others that stumble upon this -

It seems that Cross-Group Transactions are now possible in Google App Engine:

https://developers.google.com/appengine/docs/python/datastore/transactions#Using_Cross_Group_Transactions

This means that you can, within a single transaction concurrently read, perform logic, and then write to multiple entities at once.

I believe that this solves the first issue that I proposed in my question.

The second question - is there was way to have an order book that is not single threaded, is still open.

Thank you!

于 2013-04-12T15:29:36.410 回答