Sequential numbers in a distributed word

We are building an order processing system. We have a cluster of processing servers. We need to assign readable numbers to orders (for example, ORD-000001, ORD-000002).

The main problem is that it is difficult for us to implement a system lock. I am thinking about expiration lockout schemes. But everything means still bottlenecks.

We are approaching DDD, so direct access to the database is difficult. We use NHibernate. And we use UnitOfWork.

Pls help with some ideas. Every idea will be valuable. Any links to read something on the topic?

UPDATE: I want to emphasize that I need sequential numbers. And therefore, hi / low algorithms cannot be used. At this time, I am considering a scenario where

  • I assign "probably a good number";
  • Click it in the database;
  • If it fails, try assigning another "probably a good number";
  • If success, fixation,

But I can not find goot technology for this.

+3
source share
2 answers

Are you trying to make sure that all orders have unique numbers, but without a central location that coordinates the distribution of numbers?

Here are two options for you.

  • Suppose you expect that in fact ten servers will be in any realistic time period. Ask each server to pass the sequence numbers of the form ORD-XXXXXN. N is the server number. Thus, server 0 transmits ORD-000000, ORD-000010, ORD-000020, etc. Server 6 issues ORD-000006, ORD-000016, ORD-000026, etc.

  • , 10000 . , , -. , , . 0-9999, - 10000-19999, - 20000-29999 ..

+2

, ,

CREATE TABLE OrderNumbers (ID INT IDENTITY(1,1), Dummy VARCHAR(1))
INSERT INTO OrderNumbers (Dummy) SELECT ''
SELECT 'ORDER_N' + CONVERT(VARCHAR(50), @@IDENTITY) AS NewOrderNumber

: ( ), , +1 - 1, 4, 10, 1, 2, 3.

UPDATE: , jprete, - :

CREATE TABLE CustomerOrderNumber (ID INT, CustomerID INT)
CREATE FUNCTION GetMaxId ( @mycustomerid INT ) RETURNS INT
AS BEGIN
 DECLARE @maxid INT
 SET @maxid = SELECT ID FROM CustomerOrderNumber WHERE CustomerID = @mycustomerid
 SET @maxid = @maxid + 1
 UPDATE CustomerOrderNumber SET ID = @maxid
 RETURN @maxid
END
+1

Source: https://habr.com/ru/post/1718225/


All Articles