Quick revision
The short answer
Instead of one database call per id, each id service node reserves a block of ids with one statement, such as UPDATE id_segments SET max_id = max_id + 10000 WHERE name = 'payment' RETURNING max_id, and hands ids out from memory, prefetching the next block in the background when the current one is about 10 percent from exhausted, while clients can take small blocks locally to avoid most network calls. What it gives up: ids are unique and increasing within a node but not globally in order, a crash loses the rest of a block and leaves gaps, so ids must never be used where gaplessness is required, and the segment database remains a single logical dependency.
What to remember
In this answer, we replaced one database call per id with segment allocation: each node reserves a block with one UPDATE ... RETURNING, hands ids out from memory, and prefetches the next block when 10 percent remains, while clients can take small blocks locally. The design gives up global ordering and gaplessness, since ids increase only within a node and crashes leave gaps, and it keeps the segment database as a single logical dependency, which we soften with adaptive block sizes and high availability.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Instead of one database call per id, each id service node reserves a block of ids with one statement, such as UPDATE id_segments SET max_id = max_id + 10000 WHERE name = 'payment' RETURNING max_id, and hands ids out from memory, prefetching the next block in the background when the current one is about 10 percent from exhausted, while clients can take small blocks locally to avoid most network calls. What it gives up: ids are unique and increasing within a node but not globally in order, a crash loses the rest of a block and leaves gaps, so ids must never be used where gaplessness is required, and the segment database remains a single logical dependency.
Why this question is asked
A single database sequence is the simplest way to get unique, increasing ids, and it works until traffic grows. At 50,000 ids per second, every id is a network round trip and a write to one hot row, and the sequence becomes the bottleneck for every payment. Interviewers ask this to see whether we can trade a property we do not need, strict global order, for throughput we do need. In this answer, we will see why the sequence hits a wall, design segment allocation with double buffering and client-side blocks, list what the design gives up, and make it resilient.
1Why One Sequence Hits a Wall
With one id per call, the id service asks the database for nextval on every request. At 50,000 ids per second that is 50,000 round trips, each adding perhaps half a millisecond to a millisecond to the caller's latency, and all of them serialised on one sequence in one database. The database can only go so fast, the network adds latency to every payment, and if the database blips, payments stop immediately.
Notice that almost all of that work is wasted. The database only needs to guarantee that no two ids are the same; it does not need to be involved in every single id. That observation is the whole design.
2Segment Allocation
Each node of the id service reserves a block, also called a segment, of ids in one statement, then hands them out from memory. The database row records the highest id reserved so far.
The UPDATE locks the row for an instant, so two nodes can never receive the same range. A node now makes one database call per 10,000 ids. At 50,000 ids per second across the fleet, that is about 5 calls per second instead of 50,000, and each id costs only an increment in memory.
3Double Buffering So Callers Never Wait
If a node waited until its block ran out before fetching the next one, a caller would occasionally pay for a database round trip, and during a database hiccup every caller would wait. So each node holds two buffers: the current segment and the next one. When the current segment is, say, 10 percent from exhausted, a background thread fetches the next segment. When the current one runs out, the node switches to the next instantly.
- Serve id from current segmentan in-memory increment
- Less than 10 percent left and no next segment?YesFetch next segment in backgroundNoKeep serving
- Current segment exhausted?YesSwitch to next segmentNoReturn the id
- Database call only per segmentabout 5 per second fleet-wide
4 steps in order. Play them, or step through them one at a time.
The switch itself takes a lock briefly so only one thread swaps the buffers. With prefetching, callers wait on the database only if the next segment could not be loaded in the time it took to use the last 10 percent of the current one, which gives a useful buffer against database problems.
4Taking Blocks on the Client
The same idea can move one level further out. Instead of calling the id service for each id, a payment service instance can ask for a small block, say 100 ids, and use them locally. That removes most network calls from the payment path entirely. The cost is larger gaps, since a client that restarts loses its unused ids, and ids from different clients interleave even less in order. For a service that creates many payments per second, it is usually worth it.
5What the Design Gives Up
Three properties change. First, ordering: ids increase within a node, but node A may be handing out 10,500 while node B hands out 20,100, so an id issued later can be smaller than one issued earlier. Ids are only roughly increasing over time, which still keeps index inserts mostly near the end. Second, gaps: when a node crashes or is redeployed, the rest of its segment is lost, so ids jump. That is fine for a primary key and wrong for anything that must be gapless, such as invoice numbers in jurisdictions that require consecutive numbering; those need a separate, transactional counter. Third, the dependency: the segment database is still one logical dependency, now only for new segments.
| Property | Single sequence | Segments |
|---|---|---|
| Database calls | One per id (50,000/s) | One per segment (about 5/s) |
| Latency per id | A network round trip | An in-memory increment |
| Global order | Increasing | Increasing per node only |
| Gaps | Small (sequence cache, rollbacks) | Up to a segment per crash |
| Database outage | Stops ids at once | Covered by buffered segments |
To keep that dependency healthy, the step size adapts to traffic: a node that used its last segment in 20 seconds doubles the step, aiming for a segment to last several minutes, and shrinks it when traffic falls, so gaps stay small during quiet hours. With segments lasting minutes, the database can be down briefly without callers noticing. The database itself runs with high availability, a synchronous replica and automatic failover, so a primary failure does not hand out a range twice.
6Follow-up questions
Compare this with Snowflake-style generation for a system spread over three regions.
Segment allocation needs a database that hands out ranges, and a single database in one region would make the other two depend on cross-region calls for every segment. We would give each region its own segment database with disjoint ranges, for example by putting a region number in the high bits, which works but means ids are not ordered across regions at all. Snowflake-style ids need no shared database: each generator only needs a unique worker id, which can include a region field, and ids remain roughly time-ordered across regions, limited by clock differences. The price is dependence on well-behaved clocks and careful worker id assignment. For three regions, I would lean towards Snowflake-style ids unless something requires ids from a database-controlled range.
Why not cache sequence values in the database instead?
PostgreSQL sequences can cache values per session, which reduces sequence contention but not the network round trip per id, and the cache lives in each database session rather than in our service. Segment allocation solves both by moving the block to the application.
What happens if two nodes fetch a segment at the same moment?
The UPDATE takes a row lock, so the second statement waits for the first to commit and then adds the step to the new max_id. Each node receives a different range.
7Conclusion
In this answer, we replaced one database call per id with segment allocation: each node reserves a block with one UPDATE ... RETURNING, hands ids out from memory, and prefetches the next block when 10 percent remains, while clients can take small blocks locally. The design gives up global ordering and gaplessness, since ids increase only within a node and crashes leave gaps, and it keeps the segment database as a single logical dependency, which we soften with adaptive block sizes and high availability.
Open with
I would switch from one database call per id to segment allocation. Each id service node reserves a block of ids with a single statement, UPDATE id_segments SET max_id = max_id + 10000 WHERE name = 'payment' RETURNING max_id, which gives it the range below that value, and then it hands out ids from memory with an atomic increment.
Then, if they want more
At 50,000 ids a second that is about five database calls a second instead of 50,000. To make sure callers never wait, each node double buffers: when the current segment is about 10 percent from exhausted, a background thread fetches the next one, and the switch is instant.
Clients can also take small blocks locally, which removes most network calls from the payment path. What we give up: ids are unique and increasing within a node, but not globally in order, because different nodes hand out different ranges at the same time.
A crash loses the rest of a segment, so there are gaps, and these ids must never be used where gapless numbering is required, like legal invoice numbers.
I would make the block size adapt to traffic so a segment lasts a few minutes, and run the segment database with high availability, because it is still a single logical dependency for new segments.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design an id service that scales and keeps ids roughly increasing, and explain what it...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/cad7ed3e23/