开发者

Unifying Database Storage

First of all, sorry for the question title - I was unable to think about something better.

I have an interesting problem.

There are three web applications:

1. ApplicationA => example.com -> hosted in Germany  
2. ApplicationB => example2.net -> hosted in Australia  
3. ApplicationC => anotherexample.com -> hosted in United States  

All of them are completely free however owner is planning to implement some paid options. The main issue is that applications are hosted on separate servers, in three different locations.

Now, if owner wants to implement any paid options, he needs to created unified invoicing system (as invoices numbering order needs to be correct).

So, we have situation:

1. user buys a premium option on example.com
2. another user buys a premium option on example2.net
3. third and fourth users buy extra options on anotherexample.com

So we have 4 invoices, so they numbering should be as following: 2011/01, 2011/02, 2011/03, 2001/04.

As mentioned above, the main issue is to unify invoicing system as applications use different databases and are hosted on different servers. Of course, invoices should be stored in application-specified database.

Theoretically we have only one issue: invoices numbers. Obviously we need to create a unified system for invoices storage.

There might be few possible issues:

  • there might be a lot of API requests to invoicing system
  • every single invoice needs to be stored in the database
  • 开发者_如何学JAVA
  • while creating every single invoice in every external application, we need to query invoicing system for the latest invoice number.

I'm really interested in your knowing your approaches and suggestions. Any activity in this case is highly appreciated.


First, I would have the independent invoicing systems in example.com, example2.net and anotherexample.com all have their own internal primary keys for the invoices generated from within each of these systems. Each system should have its own independent copy of the invoicing logic because you don't want an outage on one server knocking out invoicing on every server.

Whenever you have a distributed system where local copies are creating records for something that will be amalgamated later, it's a good idea to use a GUID as the local primary key, or if you have a philosophical objection to GUID as PKs, create a GUID as a candidate key. This way, you can bring together invoices from all of your systems (and any future ones) without worrying about key collisions and you'll be able to track the combined records back to the source records, should you ever have to do that.

Next, you'll need an integrated invoice system where all of the invoice details are collected periodically. To facilitate this, you need processes on each local invoicing system pumping their own records up to the centralized system. Keep a flag on the invoices in the local systems to identify which invoices have been successfully uploaded - or if you have very high volumes, use a work-list table containing the invoice keys that still need transmitting instead of a flag right on the local invoice table.

The centralized invoice system will also want to have a source code on the combined invoice table so that you can easily tell which website created the invoice originally.

As to invoice numbers, I'm assuming from your question that the customer is a bit fussy about having proper sequencing of your invoice numbers. You can have the centralized system generate these numbers for you using a web service to pick up the next invoice ID. If the centralized service is down for any reason, you can still give the customer an "order reference" i.e. the GUID and just hold back on the invoice number until it can be generated through the central server. This should satisfy your customer's need for tight sequential invoice numbers while preserving the ability to operate multiple sites on multiple servers.

If your customer actually doesn't care about tight sequencing of the invoice numbers, then another alternative is to have the central system generate blocks of reserved invoice numbers and allot them to each website. When the website is getting low on its allotment, it asks the central server for another block. This gives you breathing room in the sequences in case there are communication difficulties.


In my opinion, it would be better to use an encoded invoice number. This way, you won't need to worry about number order getting mixed up.

For example, invoices can be prefixed with the country domain like de297,de298, etc., for invoices from Germany.

And going one step further, I'd incorporate the year as well. Thus it would reset at the beginning of each year and still maintain no conflicts, while at the same time keep the invoice number within a small length.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜