📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

What is the HiLo pattern in EF Core and what trade-offs does it bring?

Mid-Level Theory Intermediate

🎯 Key points

  • UseHiLo uses a SQL Server sequence; default EntityFrameworkHiLoSequence, increment 10
  • One round trip reserves a block; ids assigned in memory from that block
  • Uniqueness still comes from the database sequence
  • Keys known before SaveChanges; inserts batch without reading back identities
  • Gaps after restarts or unused blocks; not ordered across instances
  • Every writer (including raw SQL) must use the same sequence
  • Block size: larger for high-throughput imports, smaller to limit gaps

⏱ 30-second answer

UseHiLo() makes EF Core get keys from a database sequence (EntityFrameworkHiLoSequence by default, INCREMENT BY 10). One round trip reserves a block of values; EF Core then hands out ids from that block in memory before SaveChanges, so entities have real keys early and inserts batch without an OUTPUT of identity values. The database still guarantees uniqueness, because every instance reserves its own block from the same sequence. Trade-offs: gaps (a restarted process or a recycled context loses the rest of its block), ids are not in insert order across instances, the sequence becomes shared schema every writer must use (raw SQL inserts must call NEXT VALUE FOR), and a block too small means more sequence calls while a block too large means bigger gaps. Size the block to insert throughput: UseHiLo("OrderSeq") with the sequence's increment set, for example, to 100 for bulk imports.

🗣 Full answer (senior)

How I would answer this in an interview

"HiLo is a client-side key allocation scheme backed by the database. EF Core asks a SQL Server sequence for the next value; with the default increment of 10 that value reserves ten ids for this process. It then assigns ids from that block in memory, and only goes back to the sequence when the block is used up. Every instance does the same, and because the sequence hands out each block once, ids stay unique across all of them.

The benefit is that an entity has its key as soon as it is added, so I can wire up references or return the id before saving, and inserts batch cleanly because EF Core does not need identity values back from each row. For a large import it cuts round trips.

The costs: gaps, because a process that restarts throws away the rest of its block; ids that are not in creation order across instances, so they are no good for sorting by time; and the sequence becomes part of the contract, so a stored procedure or SqlBulkCopy must call NEXT VALUE FOR too. I size the increment to throughput, for example 100 for an import service, accepting bigger gaps. Today, for many systems Guid.CreateVersion7() gives the same 'key before save' benefit without the shared sequence, so I treat HiLo as a choice for integer keys specifically."


Trade-offs

Identity HiLo GUID v7
Key before SaveChanges No Yes Yes
Round trips for keys Read back per batch One per block None
Gaps On rollback Per unused block n/a
Ordered across instances Yes Roughly per block Roughly by time
Size 4/8 bytes 4/8 bytes 16 bytes
modelBuilder.HasSequence<long>("OrderSeq").IncrementsBy(100);
modelBuilder.Entity<Order>().Property(o => o.Id).UseHiLo("OrderSeq");

Production example

An import of [2 million] rows with HiLo at increment 100 needed [~20,000] sequence calls instead of reading back identities per batch, and cut import time by [~30%]; a nightly restart left gaps that nobody cared about once the team agreed ids carry no meaning.

🔍 What the interviewer is checking

Checks understanding of client-side key generation.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me