Learning center

64-bit IDs that a JavaScript consumer rounds to another row

A JavaScript number holds integers exactly only up to 2⁵³ − 1. When a new release starts issuing 64-bit IDs above that, such as time-based IDs, and sends them as JSON numbers, a Node.js consumer parses them to a nearby value. In our lab, a lookup for event 9007199254740993 found nothing, and the update meant for it changed event 9007199254740992 instead. Send IDs above 2⁵³ as strings before any release issues them.

Where the value changes#

The events table has a bigint primary key. A release in Python sends a list of event IDs to a Node.js service as JSON. Python's json.dumps writes integers as numbers of any size:

>>> json.dumps([{"id": 9007199254740993}])
'[{"id": 9007199254740993}]'

The Node.js service, on Node 24, parses it:

JSON.parse('[{"id":4200},{"id":9007199254740993},{"id":1859263849173811201}]').map(e => e.id)
// [ 4200, 9007199254740992, 1859263849173811200 ]
Number.isSafeInteger(9007199254740992)   // false

JSON.parse produced the nearest representable number without an error. Lookups with the parsed IDs, through node-postgres 8.13 on PostgreSQL 16.15, found 4200 and missed the other two.

When the rounded value exists#

Missing rows are the visible outcome. When the rounded ID belongs to another row, the consumer acts on it:

const id = JSON.parse('{"id":9007199254740993}').id;   // 9007199254740992
await db.query('UPDATE events SET kind = $1 WHERE id = $2 RETURNING id::text, kind',
               ['signup_confirmed', id]);
// [ { id: '9007199254740992', kind: 'signup_confirmed' } ]

Event 9007199254740992 was a password reset, and it is now recorded as a confirmed signup. Two IDs one apart are likely with time-based or sequence-based generators, which issue neighbors within the same millisecond.

Strings end to end#

The database driver already does the right thing: node-postgres returns bigint columns as strings by default, and our query returned '9007199254740993'. The loss happened in JSON. Parsed from {"id":"9007199254740993"}, the string reached the query intact and found the right row.

Change the producer to send IDs as strings, and the consumer to treat them as strings, in releases that ship before any ID above 2⁵³ − 1 exists. During the rollout, the consumer has to accept both forms, and only the string form is exact. If you cannot change the producer, a JSON parser that reads large integers as BigInt works, but every consumer needs it, including browser code.

Check the headroom#

SELECT max(id), max(id) > 9007199254740991 AS beyond_javascript FROM events;

Run it, and know which generator will cross the line and when. The ID ranges a database sequence can reach are covered in how much ID room is left.

The compatibility test#

Send an ID of 9007199254740993 from the producer to every JavaScript consumer, including web clients, and have each echo it back. Pass when every echo is the same string. More mixed-release checks are in moving data while two application releases share one database.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading