<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Doogal Simpson</title>
    <description>The latest articles on DEV Community by Doogal Simpson (@doogal).</description>
    <link>https://dev.to/doogal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3657111%2Fac69c96a-33e1-4023-99ef-ee3059b3ccb6.jpeg</url>
      <title>DEV Community: Doogal Simpson</title>
      <link>https://dev.to/doogal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/doogal"/>
    <language>en</language>
    <item>
      <title>How WhatsApp Scaled to 450M Users Using Erlang</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Wed, 25 Nov 2026 13:28:05 +0000</pubDate>
      <link>https://dev.to/doogal/how-whatsapp-scaled-to-450m-users-using-erlang-1m35</link>
      <guid>https://dev.to/doogal/how-whatsapp-scaled-to-450m-users-using-erlang-1m35</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; WhatsApp supported 450 million active users with just 32 engineers by running on Erlang. Erlang allocates tiny, user-space virtual machine processes that require only 2.6 kilobytes of memory per connection, compared to roughly 1 megabyte per thread in traditional environments like Java. This runtime efficiency allowed single servers to handle millions of persistent sockets simultaneously.&lt;/p&gt;

&lt;p&gt;Imagine running an on-call rotation where every single engineer on your team is personally responsible for 14 million active users. Back in 2014, when Facebook acquired WhatsApp for $19 billion, that was the exact reality for their 32-person engineering team.&lt;/p&gt;

&lt;p&gt;Keeping a global messaging platform online at that scale sounds like a nightmare that would demand hundreds of engineers and sprawling server farms. WhatsApp pulled it off with a skeleton crew because of an architectural decision made at the runtime level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did WhatsApp need millions of persistent connections open at once?
&lt;/h2&gt;

&lt;p&gt;WhatsApp requires continuous, bidirectional communication between mobile devices and backend servers to deliver messages instantly without battery-draining polling. To achieve this, every active phone maintains an open, long-lived TCP socket directly to WhatsApp's infrastructure. When hundreds of millions of users are online, the system must hold millions of concurrent connections open indefinitely.&lt;/p&gt;

&lt;p&gt;In standard request-response web architectures, a client connects, grabs data, and disconnects. Persistent messaging doesn't work that way. If your phone drops its connection, you miss push notifications and messages queue up. Holding that socket open means the server must allocate memory to track the state of that connection for hours, days, or weeks at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Erlang handle concurrent connections differently than Java?
&lt;/h2&gt;

&lt;p&gt;Erlang manages concurrency using tiny user-space processes inside the BEAM virtual machine rather than relying on heavy operating system threads. While a traditional thread in a platform like Java typically reserves around 1 megabyte of stack space, an Erlang process starts with an overhead of roughly 2.6 kilobytes.&lt;/p&gt;

&lt;p&gt;That difference in footprint changes your hardware requirements entirely. Consider what happens when you need to hold 1,000,000 concurrent socket connections on a single server:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Java (Thread-per-connection model):&lt;/strong&gt; 1,000,000 connections multiplied by 1 MB per thread requires roughly 1 terabyte of memory just to keep the connections idle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Erlang (BEAM process-per-connection model):&lt;/strong&gt; 1,000,000 connections multiplied by 2.6 KB per process requires approximately 2.6 gigabytes of memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fitting 2.6 GB of idle connection state into a commodity server's RAM is trivial. Fitting 1 TB of memory into a single machine in 2014 was prohibitively expensive, if not physically impossible. Erlang turned a distributed systems hardware bottleneck into a simple in-memory workload.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concurrency Characteristic&lt;/th&gt;
&lt;th&gt;Erlang (BEAM Process)&lt;/th&gt;
&lt;th&gt;Traditional Java (OS Thread)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Allocation Layer&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;User-space virtual machine&lt;/td&gt;
&lt;td&gt;Operating system kernel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Initial Memory Footprint&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~2.6 KB&lt;/td&gt;
&lt;td&gt;~1 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;RAM for 1 Million Sockets&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~2.6 GB&lt;/td&gt;
&lt;td&gt;~1 TB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Context Switching Overhead&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Extremely low (cooperative/preemptive in VM)&lt;/td&gt;
&lt;td&gt;High (requires CPU kernel context switch)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What makes Erlang VM processes so lightweight?
&lt;/h2&gt;

&lt;p&gt;Erlang processes are lightweight because they are isolated software entities managed entirely by the BEAM runtime scheduler, bypassing kernel-level thread management. Each process has its own tiny private heap and stack that grow and shrink dynamically on demand, rather than pre-allocating a large contiguous block of memory up front.&lt;/p&gt;

&lt;p&gt;Because there is no shared memory between processes, garbage collection occurs per process rather than across the entire application runtime. If a user connection disconnects, only its tiny 2.6 KB process is reclaimed, eliminating the massive, stop-the-world garbage collection pauses that plague heavy thread-based enterprise runtimes under load. This isolation also means an unhandled error inside one user's connection cannot cascade and crash adjacent connections.&lt;/p&gt;

&lt;p&gt;By matching the underlying communication protocol—one socket per user—to an runtime primitive that costs almost nothing to spawn, WhatsApp avoided building layers of defensive infrastructure. A small team could focus entirely on uptime and throughput.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What server software did WhatsApp modify to handle these connections?
&lt;/h3&gt;

&lt;p&gt;WhatsApp heavily modified the open-source ejabberd XMPP server, which is written in Erlang. They stripped out unused XMPP features, customized the backend database integration with Mnesia, and tuned the FreeBSD kernel to handle millions of open file descriptors per box.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does a standard operating system thread consume so much memory?
&lt;/h3&gt;

&lt;p&gt;Operating system threads reserve a fixed, contiguous stack block (often 512 KB to 1 MB) to prevent stack overflows during deep method calls. Even if an idle socket uses only a fraction of that capacity, the virtual memory overhead remains dedicated to that specific thread.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can modern runtimes like Go match this connection density?
&lt;/h3&gt;

&lt;p&gt;Yes. Modern runtimes with user-space concurrency primitives, such as Go's goroutines, start with small stacks (around 2 KB to 4 KB) and grow dynamically. This provides a similar density profile to Erlang, allowing single machines to maintain millions of idle connections.&lt;/p&gt;

</description>
      <category>erlang</category>
      <category>systemdesign</category>
      <category>architecture</category>
      <category>backend</category>
    </item>
    <item>
      <title>How Banks Create Money: Double-Entry Mechanics</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Wed, 25 Nov 2026 08:59:05 +0000</pubDate>
      <link>https://dev.to/doogal/how-banks-create-money-double-entry-mechanics-144h</link>
      <guid>https://dev.to/doogal/how-banks-create-money-double-entry-mechanics-144h</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; Commercial banks do not lend out existing customer deposits. Instead, they create new digital money whenever they issue a loan by writing simultaneous, offsetting asset and liability entries to their balance sheet. When a borrower pays off the principal, those entries contract, and that created money is permanently destroyed.&lt;/p&gt;

&lt;p&gt;Most people assume commercial banks act like simple custodial brokers. The mental model is intuitive: Alice deposits £10,000, the bank locks it in a vault, and Bob borrows £10,000 from that exact pile while paying a sliver of interest. &lt;/p&gt;

&lt;p&gt;If you have ever built or audited a ledger system, you know that is not how modern money mechanics work. In reality, banks do not shuffle pre-existing funds between accounts. When you take out a loan, the bank creates brand-new money directly into your account out of thin air.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does bank loan money actually come from?
&lt;/h2&gt;

&lt;p&gt;Bank loan money does not originate from other customers' savings or a central reserve vault. Instead, it is newly minted commercial bank money generated at the exact moment credit is approved. The bank expands its balance sheet rather than reallocating an existing pool of capital.&lt;/p&gt;

&lt;p&gt;Imagine your engineering team is building a double-entry ledger service for a platform. If you want to transfer value, you normally debit one funded account and credit another. The total supply of units remains invariant. &lt;/p&gt;

&lt;p&gt;Commercial banks, however, operate under fractional reserve banking and modern monetary frameworks where they act as money creators. When a loan contract is signed, the bank does not move money out of a reserve account. The bank simply enters a new deposit balance under the borrower's name. That deposit is spendable legal tender across the entire economy, increasing the broader money supply (M1/M2) on demand.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does a bank create money out of nothing?
&lt;/h2&gt;

&lt;p&gt;A bank creates money by writing an asset and an offsetting liability simultaneously onto its balance sheet, netting out to zero. The asset is your legal contract promising repayment with interest, while the liability is the newly credited deposit balance the bank owes you.&lt;/p&gt;

&lt;p&gt;From an accounting perspective, the transaction balances out instantly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bank Asset (+£10,000):&lt;/strong&gt; The loan agreement. The bank now owns a claim on your future earnings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bank Liability (+£10,000):&lt;/strong&gt; The newly created customer deposit credited to your checking account, which the bank must honour upon withdrawal or transfer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Net Change to Bank Equity:&lt;/strong&gt; Exactly £0.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Ledger Party&lt;/th&gt;
&lt;th&gt;Entry Type&lt;/th&gt;
&lt;th&gt;Amount&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bank Balance Sheet&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Asset (Loan Contract)&lt;/td&gt;
&lt;td&gt;+£10,000&lt;/td&gt;
&lt;td&gt;Legal claim on borrower's future payments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bank Balance Sheet&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Liability (Customer Deposit)&lt;/td&gt;
&lt;td&gt;+£10,000&lt;/td&gt;
&lt;td&gt;New spendable money credited to checking account&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Borrower Balance Sheet&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Asset (Bank Deposit)&lt;/td&gt;
&lt;td&gt;+£10,000&lt;/td&gt;
&lt;td&gt;Spendable liquidity available for immediate use&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Borrower Balance Sheet&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Liability (Debt Owed)&lt;/td&gt;
&lt;td&gt;-£10,000&lt;/td&gt;
&lt;td&gt;Obligation to repay principal plus interest&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because both sides expand by equivalent amounts, the bank does not need to source £10,000 before signing the agreement. The money supply expands simply through bilateral balance sheet expansion.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens to the money supply when a loan is paid back?
&lt;/h2&gt;

&lt;p&gt;When a borrower repays the principal of a loan, that money is effectively destroyed and removed from circulation. The repayment cancels the bank's asset against its deposit liability, shrinking the institution's balance sheet back down.&lt;/p&gt;

&lt;p&gt;When you make a principal payment, money does not transfer into a vault for the bank to spend on bonuses or office furniture. The bank debits your deposit balance (destroying the spendable liability) and marks down your outstanding loan balance (destroying the asset).&lt;/p&gt;

&lt;p&gt;Interest payments, however, are treated differently. The portion of your payment covering interest is retained by the bank as revenue to cover operating expenses, capital requirements, and profit. But the principal itself vanishes just as cleanly as it appeared. Money creation and money destruction are two sides of the same transactional lifecycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Do banks need any deposits at all to issue loans?
&lt;/h3&gt;

&lt;p&gt;Yes, but not as direct funding for the loan itself. Banks need customer deposits and capital reserves to settle interbank transfers when borrowers spend their new funds, as well as to meet regulatory liquidity ratios set by central banks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does paying off a mortgage reduce the total money supply in the economy?
&lt;/h3&gt;

&lt;p&gt;Yes. Repaying loan principal permanently eliminates commercial bank deposits from circulation. If the rate of total loan repayments across an economy exceeds the rate of new loan issuance, the broad money supply actively contracts.&lt;/p&gt;

&lt;h3&gt;
  
  
  What prevents banks from creating infinite money through loans?
&lt;/h3&gt;

&lt;p&gt;Banks are constrained by regulatory capital requirements, reserve ratios, borrower creditworthiness, and central bank interest rates. If a bank issues too many risky loans, defaults will erode its equity capital, leading to insolvency.&lt;/p&gt;

</description>
      <category>fintech</category>
      <category>softwareengineering</category>
      <category>systemdesign</category>
      <category>banking</category>
    </item>
    <item>
      <title>Why Amazon SQS Duplicates Messages (and How to Fix It)</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Mon, 23 Nov 2026 11:15:39 +0000</pubDate>
      <link>https://dev.to/doogal/why-amazon-sqs-duplicates-messages-and-how-to-fix-it-37ll</link>
      <guid>https://dev.to/doogal/why-amazon-sqs-duplicates-messages-and-how-to-fix-it-37ll</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; Amazon SQS uses an at-least-once delivery model. When a worker takes longer to process a job than the queue's configured Visibility Timeout, SQS assumes the worker crashed and makes the message visible to another consumer. To prevent duplicate side effects, you must make your message consumers idempotent and extend message visibility during long-running tasks.&lt;/p&gt;




&lt;p&gt;If you have ever had a user get billed twice for a single order or receive two identical confirmation emails, there is a good chance Amazon SQS was doing exactly what it was designed to do.&lt;/p&gt;

&lt;p&gt;SQS has a specific behavioral quirk that catches teams off guard: messages can and will be processed multiple times if your workers run too slowly. Here is why that happens under the hood and how to make sure your architecture handles it cleanly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Does Amazon SQS Deliver Duplicate Messages?
&lt;/h2&gt;

&lt;p&gt;Amazon SQS delivers duplicate messages because it operates on an "at-least-once" delivery guarantee, relying on a timer called the Visibility Timeout to detect failed consumers. SQS does not know if a worker is actively processing a job or if it crashed; it only knows whether the message was deleted before the timer expired.&lt;/p&gt;

&lt;p&gt;When a worker pulls a message from SQS, that message does not get removed from the queue immediately. Instead, SQS hides the message from every other worker for a set window—by default, 30 seconds. The queue assumes your worker will pick up the payload, finish the job, and send a delete request back to SQS well within that window.&lt;/p&gt;

&lt;p&gt;If the worker process dies midway through, this mechanism works as intended: 30 seconds pass, the message becomes visible again, and a fresh worker picks it up to keep your pipeline moving. But if the worker does not die and instead gets bogged down, SQS assumes the worst.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens When Worker Processing Exceeds the Visibility Timeout?
&lt;/h2&gt;

&lt;p&gt;When worker processing exceeds the visibility timeout, SQS automatically returns the in-flight message to the queue while the original worker is still processing it. A second worker then picks up the exact same payload, resulting in concurrent execution on the same job.&lt;/p&gt;

&lt;p&gt;Imagine a scenario where a worker picks up an order processing event. Part of its job involves calling a third-party payment gateway. If that payment provider is having an outage or responding with high latency, your worker might sit waiting on an HTTP response for 35 seconds. &lt;/p&gt;

&lt;p&gt;Here is how that race condition plays out across your system:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Timestamp&lt;/th&gt;
&lt;th&gt;Worker A Activity&lt;/th&gt;
&lt;th&gt;SQS Message State&lt;/th&gt;
&lt;th&gt;Worker B Activity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;T + 00s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Receives message, starts payment call&lt;/td&gt;
&lt;td&gt;Invisible to consumers&lt;/td&gt;
&lt;td&gt;Idle / waiting for work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;T + 30s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Still awaiting payment API response&lt;/td&gt;
&lt;td&gt;Visibility Timeout expires; becomes visible&lt;/td&gt;
&lt;td&gt;Checks queue, receives message&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;T + 31s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Receives API success, executes charge&lt;/td&gt;
&lt;td&gt;Invisible to consumers&lt;/td&gt;
&lt;td&gt;Starts duplicate payment call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;T + 35s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Deletes original message from queue&lt;/td&gt;
&lt;td&gt;Deleted&lt;/td&gt;
&lt;td&gt;Still running duplicate charge&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because Worker A took 35 seconds instead of 30, both Worker A and Worker B run the exact same operation simultaneously, leading to duplicated database writes, duplicated payments, or corrupted application state.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Do You Prevent Duplicate Processing in SQS Workers?
&lt;/h2&gt;

&lt;p&gt;To prevent duplicate processing in SQS workers, you must design your downstream handlers to be strictly idempotent using unique message identifiers. Additionally, long-running tasks should actively heartbeat back to SQS to extend their Visibility Timeout before it expires.&lt;/p&gt;

&lt;p&gt;Idempotency means that processing the same event multiple times yields the exact same outcome as processing it once. When a message arrives, check the unique business ID (such as an &lt;code&gt;order_id&lt;/code&gt; or &lt;code&gt;payment_id&lt;/code&gt;) against a fast, consistent data store like Redis or DynamoDB. If that ID is already flagged as "in-flight" or "completed," your worker can short-circuit, skip the heavy work, and safely delete the message from SQS.&lt;/p&gt;

&lt;p&gt;For tasks that legitimately take longer than your visibility window, do not simply crank the queue timeout up to 15 minutes. Instead, have your worker periodically call &lt;code&gt;ChangeMessageVisibility&lt;/code&gt; to tell SQS: "I am still alive, give me another 30 seconds." If the worker actually crashes, the message returns to the queue within seconds instead of minutes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does an SQS FIFO Queue Guarantee Exactly-Once Processing?
&lt;/h3&gt;

&lt;p&gt;No. SQS FIFO queues guarantee exactly-once &lt;em&gt;delivery&lt;/em&gt; to the consumer pool, but consumer failures or timeouts can still result in a message being re-dispatched. Your application code must still remain idempotent even when using FIFO queues.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Long Should You Set the SQS Visibility Timeout?
&lt;/h3&gt;

&lt;p&gt;Set your Visibility Timeout to roughly six times the expected average processing time of your tasks. This buffer gives workers ample headroom for network blips without leaving failed messages trapped in an invisible state for too long.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is the Difference Between Message Retention and Visibility Timeout in SQS?
&lt;/h3&gt;

&lt;p&gt;Message retention defines how long an unconsumed message lives in the queue before SQS permanently deletes it (up to 14 days). Visibility timeout is simply the temporary hiding period applied to a message while a worker is currently processing it.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>backend</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Firestore Offset vs. Cursor Pagination: Cut Read Costs</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Mon, 23 Nov 2026 10:02:54 +0000</pubDate>
      <link>https://dev.to/doogal/firestore-offset-vs-cursor-pagination-cut-read-costs-55hh</link>
      <guid>https://dev.to/doogal/firestore-offset-vs-cursor-pagination-cut-read-costs-55hh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; When you run an offset query in Google Cloud Firestore, you are billed for every skipped document read up to your offset count, plus the documents returned. To avoid unexpected read spikes, replace &lt;code&gt;offset()&lt;/code&gt; with cursor-based pagination using &lt;code&gt;startAfter()&lt;/code&gt; or &lt;code&gt;startAt()&lt;/code&gt;, which leverages Firestore's internal indexes to fetch records without paying for skipped data.&lt;/p&gt;

&lt;p&gt;Serverless databases feel like magic right up until you receive an invoice for reading documents your users never actually saw.&lt;/p&gt;

&lt;p&gt;I have a lot of time for Firestore. It is a solid choice when you need a database that handles spiky traffic without forcing you to pay for an expensive, idle database instance running 24/7. But because Firestore bills on granular operations—per read, per write, and per delete—standard relational database habits will catch you off guard.&lt;/p&gt;

&lt;p&gt;The biggest trap? Traditional offset pagination.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does Firestore charge for offset queries?
&lt;/h2&gt;

&lt;p&gt;Firestore bills you for every document evaluated up to the offset, plus the documents returned in your limit. If you execute a query with an offset of 100 and a limit of 10, Firestore charges you for 110 document reads, even though your client application only receives 10.&lt;/p&gt;

&lt;p&gt;Think of it like hiring a courier to fetch page 50 of a report. Instead of turning directly to page 50, the courier reads aloud pages 1 through 49, charges you a fee for every page they glance at, and then hands you the single page you requested.&lt;/p&gt;

&lt;p&gt;If a user clicks through to page 10 of a 20-item list in your UI, that single request bills you for 200 reads. Let that UI pattern scale across thousands of daily active users, and you will rack up millions of unnecessary document reads on skipped data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does Firestore charge for skipped documents?
&lt;/h2&gt;

&lt;p&gt;Firestore is a multi-tenant, serverless document store that does not maintain a dedicated virtual machine or cache buffer for your open connection. To satisfy an offset query, the underlying storage nodes must still locate, walk through, and process each index entry and matching document before discarding it.&lt;/p&gt;

&lt;p&gt;Because Firestore allocates compute and I/O resources to evaluate each document in that sequence, it passes that operational cost directly to you. It is designed around the premise that you pay strictly for the work the system does under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Offset pagination vs. cursor pagination: How do they compare?
&lt;/h2&gt;

&lt;p&gt;Offset pagination relies on counting past a numeric volume of records, which creates linear read costs (&lt;code&gt;O(N)&lt;/code&gt; reads where &lt;code&gt;N&lt;/code&gt; is the offset). Cursor-based pagination uses an anchor point—a specific document snapshot or indexed field value—to jump straight to that location in &lt;code&gt;O(1)&lt;/code&gt; read cost.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Offset Pagination (&lt;code&gt;offset()&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;Cursor Pagination (&lt;code&gt;startAfter()&lt;/code&gt;)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Read Cost&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Skipped docs + Returned docs&lt;/td&gt;
&lt;td&gt;Only returned docs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cost at Page 50 (Limit 10)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;510 reads&lt;/td&gt;
&lt;td&gt;10 reads&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Degrades as offset grows&lt;/td&gt;
&lt;td&gt;Constant speed via index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Query Mechanism&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Evaluates and drops records&lt;/td&gt;
&lt;td&gt;Seeks direct index position&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Recommended Use Case&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Small datasets (&amp;lt; 50 items)&lt;/td&gt;
&lt;td&gt;Production apps, infinite scroll&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  How do you implement cursor-based pagination in Firestore?
&lt;/h2&gt;

&lt;p&gt;To paginate without burning reads, pass the last visible document from your current result set into &lt;code&gt;startAfter()&lt;/code&gt;. Firestore requires indexes for every query, so it uses that document reference to jump directly to the correct index position without scanning prior records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fetch the initial page&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;firstBatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'createdAt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'desc'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;lastVisibleDoc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstBatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;firstBatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Fetch next page without paying for earlier reads&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextBatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'createdAt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'desc'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startAfter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastVisibleDoc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By pointing Firestore to an exact document anchor, you pay only for the 10 documents you actually use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can you implement traditional page numbers (e.g., page 1, 2, 3) with cursors?
&lt;/h3&gt;

&lt;p&gt;Not easily. Cursor-based pagination is inherently sequential, making it ideal for "Next/Previous" buttons or infinite scrolling. If your UI strictly requires jumping directly to arbitrary page numbers, you must either accept the &lt;code&gt;offset()&lt;/code&gt; read billing penalty or pre-aggregate page boundaries in separate index documents.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens if the cursor document is deleted before the next fetch?
&lt;/h3&gt;

&lt;p&gt;If you pass a DocumentSnapshot that has since been deleted, Firestore can still extract the field values and order key from that snapshot to navigate the index. Alternatively, you can use &lt;code&gt;startAfter()&lt;/code&gt; with literal field values (such as a timestamp or unique ID) instead of the full document snapshot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does client-side caching reduce offset read costs?
&lt;/h3&gt;

&lt;p&gt;If you use the Firebase Web or Mobile SDK with offline persistence enabled, subsequent identical reads can sometimes be served from local cache. However, backend server SDKs (such as Node.js or Go) do not maintain client cache and will incur full read costs on every offset invocation.&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>firestore</category>
      <category>database</category>
      <category>backend</category>
    </item>
    <item>
      <title>How Excel's 65K Row Limit Lost 16,000 COVID Tests</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Thu, 19 Nov 2026 12:36:25 +0000</pubDate>
      <link>https://dev.to/doogal/how-excels-65k-row-limit-lost-16000-covid-tests-3pm5</link>
      <guid>https://dev.to/doogal/how-excels-65k-row-limit-lost-16000-covid-tests-3pm5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; In October 2020, Public Health England lost nearly 16,000 positive COVID-19 test results because an automated data pipeline converted raw CSV files into legacy Excel (.xls) spreadsheets. The format's hard 65,536-row cap silently truncated records exceeding that boundary, illustrating why data pipelines must validate data boundaries and fail loudly rather than relying on legacy serialization formats.&lt;/p&gt;

&lt;p&gt;I think the COVID era gave us some of the most surreal software post-mortems in recent memory, but this particular incident always sticks with me. &lt;/p&gt;

&lt;p&gt;Imagine an automated batch job processing incoming data during a massive surge. No alert triggers, every process returns a clean exit code &lt;code&gt;0&lt;/code&gt;, and server metrics look completely healthy. Yet behind the scenes, twenty percent of your inbound records have silently vanished into thin air. That isn't a contrived stress-test scenario—it actually happened in production during the height of the pandemic.&lt;/p&gt;

&lt;h2&gt;
  
  
  How did an Excel format drop nearly 16,000 COVID test results?
&lt;/h2&gt;

&lt;p&gt;Public Health England lost roughly 16,000 positive COVID-19 test results because an automated batch job ingested data using the legacy Excel 97-2003 &lt;code&gt;.xls&lt;/code&gt; format. Because &lt;code&gt;.xls&lt;/code&gt; imposes a hard ceiling of 65,536 rows, any test records past that cutoff were silently discarded without triggering an ingestion error.&lt;/p&gt;

&lt;p&gt;The system itself seemed straightforward on paper. Commercial testing labs processed patient swabs, compiled their daily positive test results into plain CSV text files, and sent them to Public Health England (PHE). A CSV is simply a flat text file without an inherent length ceiling; as long as you have the disk space to store it, it can hold millions of lines.&lt;/p&gt;

&lt;p&gt;However, PHE had an automated script running behind the scenes that converted those incoming CSVs into an Excel template before pushing the data into their central contact tracing system. Instead of using modern formats, the script saved them out as legacy &lt;code&gt;.xls&lt;/code&gt; files. When infection numbers spiked, the daily test logs blew past 65,536 rows. Because the legacy format physically could not support a 65,537th row, the script simply chopped off the rest. The rows past the boundary were discarded, thousands of infected individuals never knew they had tested positive, and their contacts were never notified.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do legacy intermediate formats create hidden pipeline bottlenecks?
&lt;/h2&gt;

&lt;p&gt;Legacy file formats introduce hidden pipeline bottlenecks because they carry hardcoded dimensional limits from earlier computing eras. When a pipeline uses desktop-oriented file formats as intermediate transport layers, it risks catastrophic data loss whenever traffic spikes exceed those vintage specifications.&lt;/p&gt;

&lt;p&gt;To me, this is like taking an industrial freight train full of goods and funneling it into a pickup truck halfway through the journey. The origin and the destination can both handle massive volume, but the intermediate vehicle spills everything onto the pavement the moment the payload exceeds its tailgate.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File Format&lt;/th&gt;
&lt;th&gt;Maximum Row Limit&lt;/th&gt;
&lt;th&gt;Behavior on Overflow&lt;/th&gt;
&lt;th&gt;Safe for Pipeline Ingestion?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CSV (Raw Text)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unlimited (Disk-bound)&lt;/td&gt;
&lt;td&gt;Streams continuously&lt;/td&gt;
&lt;td&gt;Yes, with schema validation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Excel (.xls)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;65,536 rows&lt;/td&gt;
&lt;td&gt;Silent truncation&lt;/td&gt;
&lt;td&gt;No (Legacy binary format)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Excel (.xlsx)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1,048,576 rows&lt;/td&gt;
&lt;td&gt;Out-of-memory or file error&lt;/td&gt;
&lt;td&gt;No (Avoid proprietary file types for ETL)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Parquet / Arrow&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unlimited (Chunk-bound)&lt;/td&gt;
&lt;td&gt;Preserves schema and volume&lt;/td&gt;
&lt;td&gt;Yes (Industry standard for analytics)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;What fascinates me most about this failure is that the ingestion script had likely been humming along quietly in the background for years without a single hitch. It had simply never been asked to process more than 65,000 rows of anything before. The vulnerability sat dormant until the real-world scale of the pandemic finally surfaced it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can engineering teams prevent silent data truncation?
&lt;/h2&gt;

&lt;p&gt;Engineering teams can prevent silent data truncation by implementing row-count reconciliation, deprecating proprietary desktop formats in automated batch jobs, and configuring ingestion pipelines to fail loudly whenever an ingestion boundary is reached.&lt;/p&gt;

&lt;p&gt;To me, silent failure is the absolute worst failure mode an engineer can encounter. If a script crashes, you get an alert, a stack trace, and a clear problem to resolve. If a script silently discards data while reporting success, you assume everything works as intended until real-world consequences catch up with you.&lt;/p&gt;

&lt;p&gt;Instead of trusting that intermediary tools process files intact, data pipelines must enforce row-count reconciliation at every step. Before any transformation begins, the system should count the raw source lines. Once the transform or import completes, the system must verify that the destination datastore received the exact same number of records. If the input count was 80,000 and the committed count is 65,536, the pipeline must immediately abort the transaction, flag the mismatch, and wake somebody up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why was the .xls format used instead of .xlsx?
&lt;/h3&gt;

&lt;p&gt;Public Health England relied on an older automated script written to interface with legacy reporting infrastructure that had never been modernized to use the Office Open XML standard. As a result, the pipeline inherited the strict 65,536-row cap that Microsoft introduced back with Excel 97.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the maximum row limit in modern Microsoft Excel?
&lt;/h3&gt;

&lt;p&gt;Modern Excel workbooks (&lt;code&gt;.xlsx&lt;/code&gt;) support up to 1,048,576 rows and 16,384 columns. While that ceiling is sixteen times higher than the legacy &lt;code&gt;.xls&lt;/code&gt; format, it remains an arbitrary boundary that makes desktop spreadsheet formats unsuited for automated, high-volume data ingestion.&lt;/p&gt;

&lt;h3&gt;
  
  
  What formats should be used for automated data ingestion?
&lt;/h3&gt;

&lt;p&gt;Automated data pipelines should rely on open, streaming-compatible formats such as raw CSV, newline-delimited JSON (NDJSON), or columnar binary formats like Apache Parquet. These formats stream data efficiently without imposing arbitrary row cutoffs on your datasets.&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>softwareengineering</category>
      <category>backend</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Why SELECT Before INSERT Causes Duplicate Records</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Mon, 16 Nov 2026 12:42:32 +0000</pubDate>
      <link>https://dev.to/doogal/why-select-before-insert-causes-duplicate-records-3bna</link>
      <guid>https://dev.to/doogal/why-select-before-insert-causes-duplicate-records-3bna</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; Checking whether a record exists with a &lt;code&gt;SELECT&lt;/code&gt; query before executing an &lt;code&gt;INSERT&lt;/code&gt; fails under concurrent traffic. When two requests arrive simultaneously, both queries see zero records and proceed to insert duplicates. To reliably prevent duplicate entries, enforce a database-level unique constraint and handle the resulting violation error gracefully in your application layer.&lt;/p&gt;

&lt;p&gt;We have all seen this pattern in codebases: you have a table where a column like &lt;code&gt;email&lt;/code&gt; must stay unique. The most intuitive approach is to query the database first. If the row does not exist, your application proceeds with the insert. It works smoothly in local development and passes single-threaded unit tests. However, the moment your service handles concurrent production traffic, duplicate records inevitably appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does checking for an existing row before inserting cause duplicates?
&lt;/h2&gt;

&lt;p&gt;When two requests arrive at nearly the identical millisecond, both execute the &lt;code&gt;SELECT&lt;/code&gt; query before either has performed the &lt;code&gt;INSERT&lt;/code&gt;. Because neither query finds an existing record, both transactions assume the email is available and write duplicate rows. This classic concurrency bug is known as a Time-of-Check to Time-of-Use (TOCTOU) race condition.&lt;/p&gt;

&lt;p&gt;Imagine a single-occupancy fitting room with an unlocked door. Two shoppers walk up at the exact same split-second, peek inside, and see that nobody is there. Because both checked before anyone stepped through the doorway, both walk in simultaneously and bump into each other. Checking state before acting without an atomic lock guarantees collisions under concurrent load.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Thread A checks state&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'alex@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- 0 rows&lt;/span&gt;

&lt;span class="c1"&gt;-- Thread B checks state concurrently&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'alex@example.com'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- 0 rows&lt;/span&gt;

&lt;span class="c1"&gt;-- Both threads believe the email is free and insert duplicates:&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'alex@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'alex@example.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why doesn't SELECT FOR UPDATE stop duplicate rows?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; statement locks existing matching rows, but it cannot lock a row that does not exist yet. If two concurrent transactions query for an email that is not in the database, both lock zero rows, receive an empty result, and proceed with their inserts. &lt;/p&gt;

&lt;p&gt;Engineers often reach for row-level locking when encountering concurrency bugs, assuming it will serialize access. However, database row locks attach to actual index entries or physical records. If no row matches your filter, standard row-locking mechanisms find nothing to hold onto. Both connections continue unblocked, making the lock completely ineffective for preventing initial duplicate writes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the correct way to prevent duplicate inserts?
&lt;/h2&gt;

&lt;p&gt;The correct solution is enforcing a database-level unique constraint on the column and catching the constraint violation error in your application. Letting the database engine act as the single source of truth guarantees atomicity regardless of how many requests hit your API at the same millisecond.&lt;/p&gt;

&lt;p&gt;Here is how common strategies compare when handling simultaneous writes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Strategy&lt;/th&gt;
&lt;th&gt;Concurrency Safe?&lt;/th&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;Failure Mode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SELECT then INSERT&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Application-level check&lt;/td&gt;
&lt;td&gt;Race condition allows duplicate rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SELECT FOR UPDATE&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Row-level write lock&lt;/td&gt;
&lt;td&gt;Cannot lock non-existent records&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Unique Constraint&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Database index validation&lt;/td&gt;
&lt;td&gt;Rejects the second write with an error&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Once you add a unique constraint, the storage engine guarantees that only one transaction succeeds. The second transaction triggers a unique violation error (such as PostgreSQL error code &lt;code&gt;23505&lt;/code&gt;). Your application code simply needs to catch that specific database exception and return the appropriate response—matching what you would have returned had the pre-existing row been detected upfront.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How should an API respond to a unique constraint violation?
&lt;/h3&gt;

&lt;p&gt;An API should generally return an HTTP 409 Conflict status code or an idempotent HTTP 200 OK, depending on the endpoint's semantics. If the client intends to ensure an entity exists idempotently, catching the duplicate violation and returning the existing resource representation creates a seamless client experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can serializable database transactions prevent duplicates without constraints?
&lt;/h3&gt;

&lt;p&gt;Serializable isolation can detect write conflicts and abort one of the transactions, but it does not remove the need for error handling. Because serializable transactions throw serialization failures when race conditions occur, you still have to catch errors and execute retry logic, making an explicit unique constraint far simpler and more performant.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is an UPSERT statement better than handling unique constraint errors?
&lt;/h3&gt;

&lt;p&gt;Yes, native UPSERT clauses like &lt;code&gt;INSERT ... ON CONFLICT DO NOTHING&lt;/code&gt; in PostgreSQL or &lt;code&gt;INSERT IGNORE&lt;/code&gt; in MySQL handle collisions atomically in a single database round-trip. They rely on the underlying unique index while sparing your application from catching database driver exceptions.&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>backend</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>The 7 Threads Running Inside Every Node.js Process</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Fri, 13 Nov 2026 09:24:13 +0000</pubDate>
      <link>https://dev.to/doogal/the-7-threads-running-inside-every-nodejs-process-938</link>
      <guid>https://dev.to/doogal/the-7-threads-running-inside-every-nodejs-process-938</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; While JavaScript executes strictly on a single thread via the event loop, the Node.js runtime process itself is multi-threaded. An empty Node.js process immediately launches seven OS threads: one main thread for your JavaScript, four V8 engine threads for garbage collection and JIT compilation, one task scheduler thread, and one debug/signal handler.&lt;/p&gt;

&lt;p&gt;Whenever I hear someone say that Node.js is single-threaded, I understand why they say it. It is one of those industry shorthands we lean on when explaining the event loop, reassuring colleagues that they will not have to wrestle with mutexes, deadlocks, or race conditions when writing application logic.&lt;/p&gt;

&lt;p&gt;Yet if I fire up an empty Node script in my terminal and inspect the process at the operating system level, I do not see a single thread. I see seven OS threads running immediately.&lt;/p&gt;

&lt;p&gt;Here is what those threads actually do, and why that distinction matters when you are reasoning about your runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does an empty Node.js process need multiple threads?
&lt;/h2&gt;

&lt;p&gt;An empty Node.js process requires multiple threads because modern runtimes offload background system operations to keep the main execution loop unblocked. While your JavaScript logic runs strictly on one thread, the underlying V8 engine and platform primitives need dedicated threads for memory management, code compilation, and operating system signaling.&lt;/p&gt;

&lt;p&gt;Consider how single-threaded database engines like Redis operate. Redis guarantees atomic, linear execution by running its core command processing on a single thread, sparing you from concurrency conflicts. Under the hood, however, Redis spins up auxiliary threads to handle disk writes, memory defragmentation, and asynchronous key unlinking out of band.&lt;/p&gt;

&lt;p&gt;Node.js uses a very similar architectural pattern. If V8 had to pause your JavaScript execution to optimize bytecode, compact the memory heap, or wait on OS signals, your application latency would spike unpredictably. By offloading those infrastructure tasks to dedicated background threads, the engine ensures the main thread spends its cycles purely executing your event loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the 7 threads running inside Node.js?
&lt;/h2&gt;

&lt;p&gt;When you start a Node process, the OS provisions seven distinct threads to manage the environment before your code executes. These threads handle the main event loop, V8 garbage collection, JIT compilation, task scheduling, and OS signal management.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Thread&lt;/th&gt;
&lt;th&gt;Primary Responsibility&lt;/th&gt;
&lt;th&gt;Managed By&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. Main Thread&lt;/td&gt;
&lt;td&gt;Executes your JavaScript and powers the event loop&lt;/td&gt;
&lt;td&gt;Node.js Runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2–5. V8 Worker Threads (x4)&lt;/td&gt;
&lt;td&gt;Handles garbage collection (GC) and JIT optimization&lt;/td&gt;
&lt;td&gt;Google V8 Engine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6. Task Scheduler&lt;/td&gt;
&lt;td&gt;Manages delayed execution, internal timers, and background scheduling&lt;/td&gt;
&lt;td&gt;Google V8 Engine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7. Debug &amp;amp; Signal Handler&lt;/td&gt;
&lt;td&gt;Monitors OS signals (e.g., SIGINT, SIGTERM) and debug hooks&lt;/td&gt;
&lt;td&gt;Node.js Runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here is how that breakdown works in practice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Main Thread (1):&lt;/strong&gt; This is the thread everyone knows. It runs the event loop, executes your JavaScript sequentially, and processes incoming callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The V8 Worker Threads (4):&lt;/strong&gt; The V8 engine spins up four helper threads right away. They handle concurrent marking and sweeping for garbage collection and manage just-in-time (JIT) compilation, compiling hot JavaScript paths into optimized machine code without stalling code execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Task Scheduler (1):&lt;/strong&gt; V8 reserves a dedicated thread for delayed task scheduling, handling internal engine timers and task coordination.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Signal/Debug Handler (1):&lt;/strong&gt; This thread listens for operating system signals (such as &lt;code&gt;SIGINT&lt;/code&gt; from pressing Ctrl+C or &lt;code&gt;SIGTERM&lt;/code&gt; from a process orchestrator) and debugging hooks, ensuring the process can exit cleanly even if the main thread is busy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How does this differ from the libuv worker thread pool?
&lt;/h2&gt;

&lt;p&gt;The default seven startup threads belong strictly to engine maintenance and signal handling, whereas the libuv worker thread pool exists specifically to handle asynchronous system I/O. The background V8 and signal threads run completely independent of the &lt;code&gt;UV_THREADPOOL_SIZE&lt;/code&gt; pool used for heavy crypto or file system tasks.&lt;/p&gt;

&lt;p&gt;I frequently see engineers conflate these two layers. When someone mentions that Node uses threads, the conversation usually jumps straight to the libuv thread pool—the worker threads Node uses to handle &lt;code&gt;fs&lt;/code&gt; operations, &lt;code&gt;crypto.pbkdf2&lt;/code&gt;, or DNS lookups.&lt;/p&gt;

&lt;p&gt;However, these baseline seven threads exist before libuv ever queues an I/O task. You do not need to read a file or hash a password to trigger them; they are baked directly into the startup lifecycle of the V8 engine and the Node runtime itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does this mean our JavaScript is secretly multi-threaded?
&lt;/h2&gt;

&lt;p&gt;No, your JavaScript execution remains strictly single-threaded. You still write sequential code, and you cannot mutate shared memory across threads without explicitly using dedicated features like &lt;code&gt;worker_threads&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The distinction I always emphasize is simple: &lt;strong&gt;JavaScript execution is single-threaded, but the process executing it is multi-threaded.&lt;/strong&gt; You get the developer ergonomics of single-threaded code—deterministic execution without lock contention—while your host machine's hardware threads keep the engine healthy behind the scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How can I inspect the active threads in a running Node.js process?
&lt;/h3&gt;

&lt;p&gt;You can check the thread count using your operating system's native tools. On Linux, run &lt;code&gt;ps -T -p &amp;lt;pid&amp;gt;&lt;/code&gt; to list all threads under your Node process ID. On macOS, you can inspect the process inside Activity Monitor or run &lt;code&gt;top -pid &amp;lt;pid&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I disable the extra V8 threads to make Node truly single-threaded?
&lt;/h3&gt;

&lt;p&gt;No, you cannot disable the background helper threads. V8 requires these threads to optimize code compilation and perform generational garbage collection safely without halting process execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Are the four V8 threads the same as the libuv thread pool?
&lt;/h3&gt;

&lt;p&gt;No. The four V8 background threads handle internal engine tasks like garbage collection and JIT optimization. The libuv thread pool (controlled via the &lt;code&gt;UV_THREADPOOL_SIZE&lt;/code&gt; environment variable) is completely separate and manages asynchronous file I/O, DNS lookups, and specific crypto functions.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Why ASP.NET Core Accepts Expired JWTs (And How to Fix It)</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Thu, 12 Nov 2026 09:48:45 +0000</pubDate>
      <link>https://dev.to/doogal/why-aspnet-core-accepts-expired-jwts-and-how-to-fix-it-2mdi</link>
      <guid>https://dev.to/doogal/why-aspnet-core-accepts-expired-jwts-and-how-to-fix-it-2mdi</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; ASP.NET Core accepts expired JWT tokens for up to five minutes past their expiration (&lt;code&gt;exp&lt;/code&gt;) timestamp by default. This behavior is deliberate and compliant with RFC 7519, which permits clock skew leeway between servers. While Node.js, Python, and Go default to zero leeway, ASP.NET Core grants a default five-minute grace period.&lt;/p&gt;

&lt;p&gt;Imagine you issue a JSON Web Token (JWT) with a strict expiration time of sixty seconds. You wait ninety seconds, fire a request at your ASP.NET Core backend, and to your surprise, it responds with a crisp &lt;code&gt;200 OK&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you have ever encountered this behavior, your first instinct was probably to check whether your authentication middleware was completely broken. I have seen plenty of engineers run into this exact surprise. But this is not a bug—it is an intentional architectural decision baked straight into the JWT specification.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does ASP.NET Core allow expired JWT tokens?
&lt;/h2&gt;

&lt;p&gt;ASP.NET Core allows expired tokens because its default token validation parameters apply a five-minute clock skew leeway. This grace period prevents requests from failing due to minor time synchronization discrepancies between the machine that issued the token and the machine verifying it.&lt;/p&gt;

&lt;p&gt;Think of it like agreeing to meet someone at a coffee shop at 2:00 PM. If your watch says 2:00 PM and their watch says 1:58 PM, you do not instantly cancel the meeting. You allow a buffer of a few minutes for imperfect clocks. &lt;/p&gt;

&lt;p&gt;In distributed architectures, servers face the exact same problem. Even with Network Time Protocol (NTP) syncing your infrastructure, clocks across different cloud providers, data centers, and containers drift by milliseconds or seconds. The official JWT specification (RFC 7519) explicitly accounts for this reality, allowing token checkers to grant a margin of leeway when validating timestamps like &lt;code&gt;exp&lt;/code&gt; (expiration) and &lt;code&gt;nbf&lt;/code&gt; (not before).&lt;/p&gt;

&lt;h2&gt;
  
  
  How do different backend frameworks handle JWT clock skew?
&lt;/h2&gt;

&lt;p&gt;Frameworks differ dramatically in how much clock skew leeway they permit by default. While languages like Go, Python, and Node.js enforce strict zero-leeway validation out of the box, enterprise frameworks like Spring Security and ASP.NET Core build in default buffers.&lt;/p&gt;

&lt;p&gt;Here is how common backend environments compare when handling token expiration:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Framework / Runtime&lt;/th&gt;
&lt;th&gt;Default Clock Skew Leeway&lt;/th&gt;
&lt;th&gt;Effective Lifetime of a 60-Minute Token&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Node.js (&lt;code&gt;jsonwebtoken&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;0 seconds&lt;/td&gt;
&lt;td&gt;60 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Go (&lt;code&gt;golang-jwt&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;0 seconds&lt;/td&gt;
&lt;td&gt;60 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python (&lt;code&gt;PyJWT&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;0 seconds&lt;/td&gt;
&lt;td&gt;60 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spring Security (Java)&lt;/td&gt;
&lt;td&gt;60 seconds&lt;/td&gt;
&lt;td&gt;61 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ASP.NET Core (.NET)&lt;/td&gt;
&lt;td&gt;300 seconds (5 minutes)&lt;/td&gt;
&lt;td&gt;65 minutes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This discrepancy creates real edge cases in polyglot environments. Suppose your auth provider is written in Node.js, while your downstream microservices are a mix of Go and ASP.NET Core. A token that expired two minutes ago will fail validation on the Go service, but pass without friction on the .NET service.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you disable clock skew in ASP.NET Core?
&lt;/h2&gt;

&lt;p&gt;You can disable clock skew in ASP.NET Core by setting the &lt;code&gt;ClockSkew&lt;/code&gt; property to &lt;code&gt;TimeSpan.Zero&lt;/code&gt; inside your authentication configuration. This instructs the underlying identity model to reject tokens the exact second their &lt;code&gt;exp&lt;/code&gt; timestamp passes.&lt;/p&gt;

&lt;p&gt;Here is how to configure it in your &lt;code&gt;Program.cs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddAuthentication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JwtBearerDefaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AuthenticationScheme&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddJwtBearer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TokenValidationParameters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TokenValidationParameters&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;ValidateLifetime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;ClockSkew&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TimeSpan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Zero&lt;/span&gt; &lt;span class="c1"&gt;// Overrides the 5-minute default&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your architecture relies on short-lived access tokens—say, five to ten minutes—leaving the default five-minute clock skew enabled effectively extends your token lifetime by 50% to 100%. In scenarios where fast revocation matters, reducing or eliminating this window is an essential hardening step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is clock skew in JWT authentication?
&lt;/h3&gt;

&lt;p&gt;Clock skew is the time difference between the system clock of the authentication server that generated the JWT and the system clock of the API server validating it. Validation libraries allow a leeway window so valid requests are not rejected due to minor timing drifts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does the JWT specification mandate a 5-minute clock skew?
&lt;/h3&gt;

&lt;p&gt;No, RFC 7519 does not specify a mandatory duration. The specification merely states that implementations may provide a small leeway to account for clock drift, leaving the actual threshold up to individual library authors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I always set ClockSkew to zero in ASP.NET Core?
&lt;/h3&gt;

&lt;p&gt;Not necessarily. If your systems run in multi-region environments or use external third-party identity providers where NTP drift can occur, setting clock skew to zero can cause intermittent validation rejections. A balanced middle ground is often 30 to 60 seconds.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>aspnetcore</category>
      <category>jwt</category>
      <category>websecurity</category>
    </item>
    <item>
      <title>A Day Isn't 24 Hours: Fixing Date Math &amp; DST Bugs</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Wed, 11 Nov 2026 10:03:54 +0000</pubDate>
      <link>https://dev.to/doogal/a-day-isnt-24-hours-fixing-date-math-dst-bugs-3kfi</link>
      <guid>https://dev.to/doogal/a-day-isnt-24-hours-fixing-date-math-dst-bugs-3kfi</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; Adding 24 hours to a timestamp does not reliably move a clock forward by one calendar day. Because of Daylight Saving Time transitions, a calendar day can span 23, 24, or 25 hours. To prevent brittle tests and production bugs, always perform date math using calendar intervals rather than fixed durations of hours or seconds.&lt;/p&gt;

&lt;p&gt;There is nothing quite as humbling as watching an entire CI pipeline turn red on code nobody has touched in months.&lt;/p&gt;

&lt;p&gt;A test suite runs cleanly for weeks, pull requests merge without incident, and then—out of nowhere on a random Sunday night—builds start failing across unrelated branches. When code breaks without anyone modifying it, the culprit is almost always an external assumption leaking into your test suite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did our CI build fail without any code changes?
&lt;/h2&gt;

&lt;p&gt;The CI pipeline failed because an automated test relied on a hidden environmental dependency: the calendar. When Daylight Saving Time occurred, a unit test assuming every day contains exactly 24 hours broke across all builds.&lt;/p&gt;

&lt;p&gt;In our test suite, someone wrote an assertion verifying scheduled events. It looked innocent enough:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Capture the current timestamp (&lt;code&gt;now&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Add 24 hours to that timestamp.&lt;/li&gt;
&lt;li&gt;Assert that the resulting time equals the exact same wall-clock time tomorrow.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For roughly 363 days a year, that test passes without complaint. But on the morning the clocks moved for Daylight Saving Time, the assertion completely collapsed. The test failed on every single deploy because the system was doing exactly what the engineer told it to do—add 24 hours—rather than what they meant: advance the calendar by one day.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is a day not always 24 hours?
&lt;/h2&gt;

&lt;p&gt;A calendar day is a human convention tied to local civil time, whereas 24 hours is a fixed physical duration of 86,400 seconds. When Daylight Saving Time begins or ends, local clocks shift forward or backward by an hour, producing calendar days that are actually 23 or 25 hours long.&lt;/p&gt;

&lt;p&gt;Think of it like an egg timer versus a hotel reservation. If you set an egg timer for 24 hours, it counts down raw elapsed physical seconds. It does not care what timezone you are in or what the sun is doing. A hotel reservation, however, cares about civil calendar days. When you book a room for "tomorrow," you expect to arrive at the same local hour, regardless of whether civil authorities stole or added an hour to the night.&lt;/p&gt;

&lt;p&gt;When we hardcode &lt;code&gt;24 * 60 * 60 * 1000&lt;/code&gt; milliseconds into our logic, we are using an egg timer to schedule a hotel stay.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fragile: Assumes every day has 86,400,000 milliseconds&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tomorrowDuration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Resilient: Adds a calendar day according to local civil rules&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tomorrowCalendar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;tomorrowCalendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tomorrowCalendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How should developers handle date arithmetic across Daylight Saving Time?
&lt;/h2&gt;

&lt;p&gt;Developers should separate duration math from calendar math. When an operation needs to land on the same local time tomorrow, manipulate semantic calendar units like days and months instead of adding fixed blocks of hours or seconds.&lt;/p&gt;

&lt;p&gt;Here is how fixed duration math compares against calendar arithmetic across different points in the year:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Fixed Duration Math (+24 Hours)&lt;/th&gt;
&lt;th&gt;Calendar Unit Math (+1 Day)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Standard Day&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Advances exactly 24 hours to the same time.&lt;/td&gt;
&lt;td&gt;Advances 1 calendar day to the same time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Spring Forward (DST Start)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Skips ahead: lands 1 hour later than current local time.&lt;/td&gt;
&lt;td&gt;Preserves local wall-clock time (23-hour day).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Fall Back (DST End)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Lags behind: lands 1 hour earlier than current local time.&lt;/td&gt;
&lt;td&gt;Preserves local wall-clock time (25-hour day).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Primary Use Case&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Rate limiting, session timeouts, token expiry.&lt;/td&gt;
&lt;td&gt;Daily reminders, billing cycles, calendar events.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you want an event to happen 86,400 seconds from right now—like an authentication token expiration—use fixed durations. But if you want something to run "at the same time tomorrow," tell your runtime to add one day, not 24 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does adding 86,400 seconds cause bugs during Daylight Saving Time?
&lt;/h3&gt;

&lt;p&gt;Because 86,400 seconds is strictly 24 hours of elapsed physical time. During DST transitions, a civil day is either 23 hours (82,800 seconds) or 25 hours (90,000 seconds), meaning an 86,400-second offset will cause your timestamp to shift by an hour relative to local wall-clock time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does storing timestamps in UTC prevent date math bugs?
&lt;/h3&gt;

&lt;p&gt;No, UTC only eliminates ambiguity when storing and logging past events. If you take a UTC timestamp and add 24 hours to schedule a recurring 9:00 AM meeting, that meeting will still display at 8:00 AM or 10:00 AM locally once the user's timezone enters or exits Daylight Saving Time.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do modern date libraries solve this problem?
&lt;/h3&gt;

&lt;p&gt;Modern libraries distinguish between physical durations (like durations of elapsed seconds) and calendar periods (like days, months, and years). When you ask libraries like &lt;code&gt;date-fns&lt;/code&gt;, Java's &lt;code&gt;java.time&lt;/code&gt;, or the ECMAScript &lt;code&gt;Temporal&lt;/code&gt; proposal to add a day, they resolve the operation against civil calendar rules rather than raw elapsed seconds.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why SQL UNIQUE Constraints Allow Duplicate NULLs</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Tue, 10 Nov 2026 17:03:40 +0000</pubDate>
      <link>https://dev.to/doogal/why-sql-unique-constraints-allow-duplicate-nulls-56n</link>
      <guid>https://dev.to/doogal/why-sql-unique-constraints-allow-duplicate-nulls-56n</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; In SQL, unique constraints permit multiple NULL values because NULL represents an unknown state rather than an empty value, meaning two NULLs are never equal (&lt;code&gt;NULL = NULL&lt;/code&gt; evaluates to &lt;code&gt;UNKNOWN&lt;/code&gt;). While this prevents unintended collisions on optional single columns, it silently compromises composite keys unless you explicitly configure your index with &lt;code&gt;NULLS NOT DISTINCT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Imagine running a migration to enforce strict uniqueness on a database column, only to look at your production data a week later and find twenty rows that appear identical. Your initial instinct might be to suspect a transaction isolation bug or a race condition in your application layer. &lt;/p&gt;

&lt;p&gt;In reality, your database is following the SQL standard to the letter. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why does SQL permit duplicate NULL values in a UNIQUE column?
&lt;/h2&gt;

&lt;p&gt;SQL relies on three-valued logic (True, False, and Unknown), which means a NULL value represents missing or unknown data rather than an empty string or zero. Because the database cannot confirm what an unknown value actually is, evaluating whether one NULL matches another yields &lt;code&gt;UNKNOWN&lt;/code&gt; rather than &lt;code&gt;TRUE&lt;/code&gt;, allowing the unique check to pass.&lt;/p&gt;

&lt;p&gt;Think of a NULL like an unlabeled package sitting in a delivery warehouse. If two packages arrive without shipping labels, you cannot declare that they contain the exact same item. You simply do not know what is inside either box. Because the contents are uncertain, the system cannot definitively prove a collision, so it leaves them alone.&lt;/p&gt;

&lt;p&gt;Here is how that plays out in a standard table definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;INT&lt;/span&gt; &lt;span class="no"&gt;GENERATED&lt;/span&gt; &lt;span class="no"&gt;ALWAYS&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;IDENTITY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Both inserts will succeed without an error:&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;members&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a single column like an optional secondary email address, this behavior is usually desirable. You want to enforce uniqueness whenever an address is actually supplied, but you do not want to block multiple accounts from leaving the field blank.&lt;/p&gt;

&lt;h2&gt;
  
  
  When do duplicate NULLs break your data integrity?
&lt;/h2&gt;

&lt;p&gt;Duplicate NULLs become hazardous when you introduce composite unique constraints across two or more columns, such as &lt;code&gt;(org_id, team_id)&lt;/code&gt;. If &lt;code&gt;team_id&lt;/code&gt; is nullable to represent org-wide assets that do not belong to a specific team, the database allows endless identical &lt;code&gt;(org_id, NULL)&lt;/code&gt; records because it cannot prove the NULLs represent the same entity.&lt;/p&gt;

&lt;p&gt;Let's say you are building an access control system where a permission can be scoped to an entire organization or narrowed down to a specific team within that organization. If your schema uses a unique constraint across &lt;code&gt;(org_id, team_id)&lt;/code&gt;, you might expect an organization to only ever have one org-wide default record where &lt;code&gt;team_id&lt;/code&gt; is NULL. &lt;/p&gt;

&lt;p&gt;Instead, your database will happily accept dozens of records for the exact same &lt;code&gt;org_id&lt;/code&gt; as long as &lt;code&gt;team_id&lt;/code&gt; remains NULL. You end up with orphaned, ambiguous configuration rows that defy your business rules.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value 1&lt;/th&gt;
&lt;th&gt;Value 2&lt;/th&gt;
&lt;th&gt;Standard SQL Equality (&lt;code&gt;=&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;Triggers Unique Violation?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'alice@test.com'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'alice@test.com'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;TRUE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'alice@test.com'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'bob@test.com'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;FALSE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NULL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;'alice@test.com'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UNKNOWN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NULL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;NULL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UNKNOWN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  How do you enforce unique NULL values in modern SQL?
&lt;/h2&gt;

&lt;p&gt;Starting in PostgreSQL 15, you can add the &lt;code&gt;NULLS NOT DISTINCT&lt;/code&gt; clause directly to a unique constraint or unique index. This clause modifies the standard comparison behavior, instructing the query engine to treat all NULL values as identical for the purposes of that constraint.&lt;/p&gt;

&lt;p&gt;If you are on an engine that supports this syntax, fixing composite constraints takes a single modifier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;org_permissions&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;org_id&lt;/span&gt; &lt;span class="kt"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;team_id&lt;/span&gt; &lt;span class="kt"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;unique_org_team&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NULLS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;org_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;team_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;NULLS NOT DISTINCT&lt;/code&gt; applied, the database permits only one row where &lt;code&gt;team_id&lt;/code&gt; is NULL for any given &lt;code&gt;org_id&lt;/code&gt;. A second attempt to insert &lt;code&gt;(42, NULL)&lt;/code&gt; will immediately raise a unique violation error, protecting your multi-column logic without forcing you into artificial workarounds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does standard SQL require NULLs to be distinct in unique constraints?
&lt;/h3&gt;

&lt;p&gt;The SQL-92 standard left the treatment of NULLs in unique constraints somewhat ambiguous, but SQL:2008 introduced the optional &lt;code&gt;UNIQUE NULLS DISTINCT&lt;/code&gt; and &lt;code&gt;UNIQUE NULLS NOT DISTINCT&lt;/code&gt; clauses. By default, most engines (including Postgres, MySQL, and SQLite) default to treating NULLs as distinct.&lt;/p&gt;

&lt;h3&gt;
  
  
  How can you prevent duplicate NULLs in older PostgreSQL versions?
&lt;/h3&gt;

&lt;p&gt;If you are running PostgreSQL 14 or earlier, you can prevent duplicate NULLs by creating a partial unique index. For example, create one unique index on &lt;code&gt;(org_id, team_id) WHERE team_id IS NOT NULL&lt;/code&gt; and a second partial index on &lt;code&gt;(org_id) WHERE team_id IS NULL&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Microsoft SQL Server treat NULLs the same way as PostgreSQL?
&lt;/h3&gt;

&lt;p&gt;No. SQL Server historically diverges from the standard default behavior: a standard &lt;code&gt;UNIQUE&lt;/code&gt; constraint in SQL Server allows only one NULL value across the entire column. To mimic PostgreSQL's default behavior in SQL Server, developers typically have to build a filtered index that excludes NULLs.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>databasedesign</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Why Uppercasing a String Can Change Its Length</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Sat, 07 Nov 2026 18:06:55 +0000</pubDate>
      <link>https://dev.to/doogal/why-uppercasing-a-string-can-change-its-length-5gkp</link>
      <guid>https://dev.to/doogal/why-uppercasing-a-string-can-change-its-length-5gkp</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; Yes, uppercasing a string can increase its length. Under standard Unicode rules, certain characters—like the German sharp S (&lt;code&gt;ß&lt;/code&gt;) in "Straße"—expand into two characters ("SS") when converted to uppercase. A 6-character word becomes 7 characters in languages like JavaScript, Python, and Java, though behavior varies across runtime environments like C#.&lt;/p&gt;

&lt;p&gt;Most of us write code under the quiet assumption that string casing is a 1:1 operation. You take a lowercase &lt;code&gt;a&lt;/code&gt;, flip a bit in your head, and get an uppercase &lt;code&gt;A&lt;/code&gt;. The character count stays identical, index positions don't budge, and your memory allocations stay neat. &lt;/p&gt;

&lt;p&gt;Unicode, however, does not care about our tidy assumptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does uppercasing a string change its length?
&lt;/h2&gt;

&lt;p&gt;Uppercasing can increase string length because Unicode casing rules reflect real-world orthography rather than simple character-code offsets. Certain lowercase characters, such as the German eszett (&lt;code&gt;ß&lt;/code&gt;), do not have a standard single-character uppercase equivalent in traditional typography. As a result, standard Unicode mappings expand that single glyph into the two-character sequence "SS".&lt;/p&gt;

&lt;p&gt;Think of it like a foldout couch. In its lowercase state, the German &lt;code&gt;ß&lt;/code&gt; sits tucked away as a single character representing a double-s sound. When you tell a runtime to convert the string "Straße" to uppercase, the engine doesn't just swap the glyph; it unpacks that foldout couch into two separate letters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Straße&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;upper_word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# 6
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper_word&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# 7
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upper_word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# STRASSE
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You start with six characters and end with seven. The word grew simply because you shouted it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do different programming languages handle the German sharp S?
&lt;/h2&gt;

&lt;p&gt;Most modern runtimes follow standard Unicode casing tables and expand &lt;code&gt;ß&lt;/code&gt; to &lt;code&gt;SS&lt;/code&gt;, increasing character length. However, C# historically diverges from this behavior under default invariant settings by leaving the lowercase &lt;code&gt;ß&lt;/code&gt; unchanged during an uppercase transform.&lt;/p&gt;

&lt;p&gt;If you pass "Straße" into uppercase methods across standard environments, you will see inconsistent results depending on the language's implementation of Unicode standards:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;th&gt;Length Change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.upper()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Straße&lt;/td&gt;
&lt;td&gt;STRASSE&lt;/td&gt;
&lt;td&gt;+1 (6 to 7)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.toUpperCase()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Straße&lt;/td&gt;
&lt;td&gt;STRASSE&lt;/td&gt;
&lt;td&gt;+1 (6 to 7)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.toUpperCase()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Straße&lt;/td&gt;
&lt;td&gt;STRASSE&lt;/td&gt;
&lt;td&gt;+1 (6 to 7)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;C# (.NET)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.ToUpper()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Straße&lt;/td&gt;
&lt;td&gt;STRAßE&lt;/td&gt;
&lt;td&gt;0 (6 to 6)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In JavaScript, Python, and Java, the engine respects the default Unicode special casing rules, expanding the character. In C#, &lt;code&gt;"Straße".ToUpper()&lt;/code&gt; returns &lt;code&gt;"STRAßE"&lt;/code&gt; because the runtime traditionally avoided length-modifying casing in its default string operations unless explicit culture rules were specified.&lt;/p&gt;

&lt;h2&gt;
  
  
  What breaks when string case conversion alters length?
&lt;/h2&gt;

&lt;p&gt;Code that couples character indexing, buffer allocation, or length validation to pre-cased strings can silently fail when length changes. If a system validates input length before casing, an expanding character can violate downstream database column sizes or break substring offsets.&lt;/p&gt;

&lt;p&gt;Imagine your service validates user input against a &lt;code&gt;VARCHAR(6)&lt;/code&gt; column in a database. The user submits &lt;code&gt;Straße&lt;/code&gt;. The validation passes because the string length is 6. If your application normalizes the input using &lt;code&gt;.toUpperCase()&lt;/code&gt; before storing it, the string expands to &lt;code&gt;STRASSE&lt;/code&gt; (length 7), triggering an unhandled database exception.&lt;/p&gt;

&lt;p&gt;Similarly, highlighting matches or calculating cursor positions breaks down quickly. If an algorithm searches for an offset in a lowercase string and tries to slice the uppercase version using the same index, every character after the expansion point will be misaligned by one position.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why doesn't Unicode uppercase ß to the capital sharp S (ẞ)?
&lt;/h3&gt;

&lt;p&gt;Unicode introduced the capital sharp S (&lt;code&gt;ẞ&lt;/code&gt;, U+1E9E) in 2008, but standard default casing rules maintain the legacy &lt;code&gt;SS&lt;/code&gt; mapping. This ensures backward compatibility with centuries of German typographic tradition and prevents breaking existing systems that expect standard capitalization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does lowercasing ever change string length?
&lt;/h3&gt;

&lt;p&gt;Yes, casing operations are asymmetrical. While less common than uppercase expansions, certain characters—like the uppercase Greek letter Omega with rough breathing and an iota subscript—can change length depending on the target case and the normalization form applied.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the safest way to perform case-insensitive string comparisons?
&lt;/h3&gt;

&lt;p&gt;Avoid calling &lt;code&gt;.toUpperCase()&lt;/code&gt; or &lt;code&gt;.toLowerCase()&lt;/code&gt; on both strings to compare them with equality operators. Instead, use native, culture-aware comparison methods like &lt;code&gt;string.Equals(a, b, StringComparison.OrdinalIgnoreCase)&lt;/code&gt; in C# or &lt;code&gt;a.localeCompare(b, undefined, { sensitivity: 'accent' })&lt;/code&gt; in JavaScript.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>softwareengineering</category>
      <category>unicode</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Float to Int Conversion: Why Large Numbers Break</title>
      <dc:creator>Doogal Simpson</dc:creator>
      <pubDate>Fri, 06 Nov 2026 14:18:36 +0000</pubDate>
      <link>https://dev.to/doogal/float-to-int-conversion-why-large-numbers-break-39i6</link>
      <guid>https://dev.to/doogal/float-to-int-conversion-why-large-numbers-break-39i6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Quick Answer:&lt;/strong&gt; Converting large floating-point numbers to integers is unpredictable across languages. While small decimals truncate their fractional parts cleanly, floats can represent magnitudes far exceeding fixed integer limits (like 10^20). Depending on whether you run Java, JavaScript, C, or Python, your runtime will silently clamp the value, truncate bits, trigger undefined behavior, or scale to arbitrary precision.&lt;/p&gt;

&lt;p&gt;Converting a decimal into an integer seems like one of the simplest operations in programming. You take the value after the decimal point, chop it off, and keep the whole number. If you pass in &lt;code&gt;3.7&lt;/code&gt;, you get back &lt;code&gt;3&lt;/code&gt;. It feels intuitive, deterministic, and safe.&lt;/p&gt;

&lt;p&gt;Things fall apart when scale enters the picture. Floating-point numbers can represent values vastly larger than standard integers can hold. If you take a number like 10^20 and force it into an integer type, how your system responds depends entirely on the language you are using.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does casting large floats to integers break?
&lt;/h2&gt;

&lt;p&gt;Floating-point standards like IEEE 754 sacrifice absolute precision to represent an enormous dynamic range, scaling up to roughly 1.8 × 10^308 for standard 64-bit doubles. In contrast, fixed-width integers rely on a fixed bucket of bits (typically 32 or 64 bits) with hard mathematical boundaries. When you attempt to force an astronomical float into a small integer container, the bits simply have nowhere to go.&lt;/p&gt;

&lt;p&gt;Think of it like trying to pack the cargo of a freight train into the trunk of a sedan. A 32-bit signed integer caps out at 2,147,483,647 (roughly 2.14 billion). A double can hold 10^20 without batting an eye. When you tell a runtime to truncate that double into a 32-bit integer, the language must choose what to do with the missing capacity.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do different programming languages handle float-to-int overflow?
&lt;/h2&gt;

&lt;p&gt;Different languages handle float-to-int overflow using completely different strategies, ranging from silent value clamping to memory-level undefined behavior. Some runtimes attempt to provide a safe fallback, while others pass the problem straight down to CPU instructions.&lt;/p&gt;

&lt;p&gt;Here is how common languages handle casting a float holding 10^20 to an integer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JavaScript 32-bit bitwise conversion&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;largeFloat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nx"&gt;e20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;largeFloat&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs an unexpected 32-bit wrapped value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Language&lt;/th&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;Result for 10^20 cast to standard int&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Clamping&lt;/td&gt;
&lt;td&gt;Returns &lt;code&gt;Integer.MAX_VALUE&lt;/code&gt; (2,147,483,647)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;JavaScript&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Bitwise 32-bit truncation (&lt;code&gt;bitwise OR 0&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Bitwise wrap (outputs garbage like ~1.6 billion or negative ints)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;C (C99/C11)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cast operator &lt;code&gt;(int)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Undefined behavior (hardware-dependent result or traps)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;int()&lt;/code&gt; function&lt;/td&gt;
&lt;td&gt;Returns exact integer: 100,000,000,000,000,000,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In Java, the runtime effectively gives up and returns the largest value it can manage. It sees a massive number, realizes it cannot fit it into 32 bits, and hands you 2.1 billion. It is not the correct number, but the runtime refuses to crash.&lt;/p&gt;

&lt;p&gt;JavaScript takes another path if you use common conversion tricks like the bitwise OR hack (&lt;code&gt;val | 0&lt;/code&gt;). Bitwise operators in JavaScript force operands into 32-bit signed integers. When 10^20 passes through that pipeline, the high bits are discarded, leaving you with an unintuitive wrapped integer value.&lt;/p&gt;

&lt;p&gt;C hands the problem to the underlying architecture. The C specification states that casting a float to an integer when the value cannot fit within the destination type produces undefined behavior. Your program might return zero, yield the minimum integer, or execute arbitrary CPU-level artifacts.&lt;/p&gt;

&lt;p&gt;Python sidesteps fixed-width limits altogether. Python 3 integers feature arbitrary precision by default. When you cast a float holding 10^20 via &lt;code&gt;int()&lt;/code&gt;, Python allocates enough memory to store every single zero, giving you the real integer representation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can you safely convert large floating-point numbers?
&lt;/h2&gt;

&lt;p&gt;To safely convert large floating-point values, always validate that the float sits within the target integer type's bounds before casting. If your application handles numbers that exceed standard 32-bit or 64-bit bounds, avoid primitive casts and use dedicated large-number libraries.&lt;/p&gt;

&lt;p&gt;Imagine you are building a billing engine that calculates usage-based charges. If you aggregate metrics as floating-point values and cast the total to a standard integer for ledger storage, a sudden traffic spike can quietly clamp charges to 2.1 billion or wrap your balance into negative numbers.&lt;/p&gt;

&lt;p&gt;Before executing a cast, check boundaries explicitly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare the floating-point number against your destination maximum (for example, ensuring the value does not exceed standard integer limits).&lt;/li&gt;
&lt;li&gt;Use arbitrary-precision libraries (such as &lt;code&gt;BigInt&lt;/code&gt; in JavaScript or &lt;code&gt;BigDecimal&lt;/code&gt; in Java) when precision cannot be compromised.&lt;/li&gt;
&lt;li&gt;Avoid bitwise conversion shortcuts on data whose upper bounds are unknown.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does C treat float-to-int overflow as undefined behavior?
&lt;/h3&gt;

&lt;p&gt;C prioritizes raw execution speed over defensive runtimes. Checking whether a float fits within integer bounds on every cast introduces branch instructions, so C delegates the conversion directly to native CPU instructions, which behave inconsistently across hardware architectures.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does JavaScript BigInt handle large float conversions?
&lt;/h3&gt;

&lt;p&gt;JavaScript does not allow direct implicit casting between standard floating-point numbers and &lt;code&gt;BigInt&lt;/code&gt;. Passing a float directly to &lt;code&gt;BigInt()&lt;/code&gt; throws a &lt;code&gt;RangeError&lt;/code&gt; if the float has a fractional part, requiring you to round or truncate explicitly first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Python's int() always preserve full precision from floats?
&lt;/h3&gt;

&lt;p&gt;No. While Python integers support arbitrary precision, floats do not. A 64-bit float only carries 53 bits of precision (roughly 15 to 17 significant decimal digits). Casting 10^20 works, but smaller trailing digits beyond the 53-bit limit are already lost before the cast occurs.&lt;/p&gt;

</description>
      <category>softwareengineering</category>
      <category>programming</category>
      <category>computerscience</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
