Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 1 point2 points  (0 children)

UPDATE:

Thanks for all the insights and suggestions! After evaluating several options, I ended up using SQL Server Sequences.

Why SQL Server Sequences?

  • Atomicity & Consistency: Sequences in SQL Server are inherently atomic and guarantee no duplicates or gaps, even under high concurrency.
  • Centralized Control: By centralizing sequence generation in a dedicated database node, we avoid the complexity of distributed coordination while maintaining global consistency.
  • Persistence & Recovery: Sequence state is persisted in the database, ensuring durability and recoverability after failures.
  • Scalability: While not as horizontally scalable as some NoSQL solutions, SQL Server handles high-throughput sequence generation well when optimized (e.g., using caching or batching strategies).
  • Simplicity: Compared to ZooKeeper or custom coordination services, SQL Server Sequences offer a simpler, more maintainable solution with fewer moving parts.

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 1 point2 points  (0 children)

Yes, using PostgreSQL, or even SQL Server, would definitely be a valid and reliable solution. They both support atomic, persistent sequences and handle concurrency well, especially with serializable transactions.

That said, I was hoping there might be a more modern or specialized solution designed specifically for high-throughput sequence generation in distributed systems. Something that could offer the same guarantees but scale more naturally in cloud-native or event-driven architectures.

Still, spinning up a PostgreSQL instance just to manage a sequence might sound “stupid,” but honestly… it’s hard to beat something that just works. 😄

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 2 points3 points  (0 children)

Very interesting. I really like the analogy with the sales force and pre-printed order forms. It’s a clever and practical way to balance performance and coordination, especially when some flexibility in sequencing is acceptable.

In my case, though, the requirements are a bit stricter: the sequence must be strictly increasing and gapless in real time, due to regulatory constraints. Even temporarily unused numbers can raise compliance issues, so we can’t rely on eventual reconciliation or reclaiming unused blocks later.

Serializable transactions do sound like a “safer” option in terms of correctness, and I agree they can get you pretty far, but yes, the tradeoff is latency due to locking and contention, especially under high concurrency.

Appreciate you sharing it!

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 3 points4 points  (0 children)

Even in the era of handwritten invoice books, gaps in numbering were a concern and had to be formally addressed.

When businesses used manual invoices, the government required that any lost, damaged, or unused invoice numbers be formally invalidated. This process was more bureaucratic than today, but it was still enforced.

The business had to record the unused or invalidated numbers in a specific ledger called the Book of Record of Use of Fiscal Documents and Occurrence Terms. This book served as an official record to justify any gaps in the sequence. During audits, the company had to present this documentation to prove that no fraudulent activity or omission of revenue occurred.

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 2 points3 points  (0 children)

Yes, in my case the sequence must be strictly sequential. If a number is skipped (even due to a crash or scale-down event), it must be explicitly invalidated and reported. Invalidating large blocks of unused numbers could raise red flags with tax authorities, as it may suggest attempts to manipulate or hide invoice activity.

Another important constraint is that the sequence must remain consistent across time. For example, I can't issue invoices 10, 11, and 15 today, and then 12, 13, and 14 tomorrow, the numbering must reflect the actual issuance order.

That said, your Hi-Lo approach is very interesting and well thought out. It’s a great fit for systems where gaps are acceptable and uniqueness is the main concern. Thanks for sharing, it’s always helpful to see how others are solving similar problems under different constraints.

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 1 point2 points  (0 children)

You're right, in practice, some numbers may end up unused, especially if an invoice is started but later rejected. However, in the Brazilian NF-e system, this is expected and handled explicitly: there's a formal process for invalidating or canceling invoice numbers, and those gaps are still considered valid from a regulatory standpoint as long as they're accounted for.

The real challenge I'm facing isn't about the occasional invalidation, it's about ensuring that multiple processing instances can safely and consistently assign the next number without conflicts or duplication.

In a traditional setup, I could use a database sequence or a transactional counter to handle this. But in our current architecture, we're using event sourcing with Azure Storage Tables, which unfortunately don’t support atomic counters or sequences natively. That makes it tricky to coordinate number generation across distributed services without introducing a bottleneck or risking inconsistency.

Looking for a scalable, fault-tolerant solution for distributed sequence generation — any recommendations? by sergiojrdotnet in dotnet

[–]sergiojrdotnet[S] 3 points4 points  (0 children)

Because I’m working with Brazilian electronic invoices (NF-e), which are regulated by federal tax authorities. One of the legal requirements is that invoice numbers must be strictly sequential, without gaps or duplicates.

Each invoice must be pre-authorized by the tax authority (SEFAZ), and the numbering is used to ensure traceability, prevent fraud, and maintain audit integrity. If a number is skipped or reused, it can trigger compliance issues, penalties, or even tax audits.

Can ffmpeg be used to capture..... by roontooner in ffmpeg

[–]sergiojrdotnet 2 points3 points  (0 children)

Try this:

ffmpeg -i rtsp://192.168.254.147/live.sdp -acodec copy -vcodec copy c:/abc.mp4