用 Node.js 和 PostgreSQL 实现事务级 API 幂等
📊 核心数据与事实(2026-11-27):
作者用 Node.js 24.21、Express 5.3.0 和 PostgreSQL 18.6 复现了 Redis 存幂等键时的重复扣款:进程在写入支付后、更新键前崩溃,锁过期后同一 Idempotency-Key 重试会再插入一条支付记录。
• 原文实测切片:In real life this is a deploy, an out-of-memory kill or a pod being rescheduled: async function createPayment(db, amount) { const { rows } = await db.query( `INSERT INTO payments (amount, status) VALUES ($1, 'succeeded') RETURNING id, amount,…
• 原文实测切片:null)) .digest('hex') const db = await pool.connect() try { await db.query('BEGIN') // claim the key: a duplicate waits here until the first transaction ends const claim = await db.query( `INSERT INTO idempotency_keys (key, fingerprint) VALUES…
• 原文实测切片:SET LOCAL limits it to the current transaction: await db.query('BEGIN') await db.query("SET LOCAL lock_timeout = '500ms'") } catch (err) { await db.query('ROLLBACK') if (err.code === '55P03') { // lock_timeout: the first request still holds…
💡 对独立开发者的启发:
1/ 供给端造工具的门槛已被 AI 和开源抹平,独立开发者真正的护城河是「找准垂直业务场景 + 做好分发」。
2/ 面对新模型或开源组件先看三件事:许可证是否商用安全、API/推理边际成本降了多少、能不能直接嵌进现有变现工作流。
3/ 别去卷通用开发者轮子,把底层新能力包装成面向具体行业人群(电商、创作者、中小商家)傻瓜式交付结果的产品。
📅 日期:2026-11-27(北京时间)
📰 信息源出处:HackerNoon · Douglas Matoso(hackernoon.com)
🔗 原文:https://hackernoon.com/implementing-transactional-api-idempotency-with-nodejs-and-postgresql?source=rss
📡 归档:https://billionare.me/items/vznyf47v0pd47i5vqdk2n9psm
社媒成稿工作台 · 一键复制
飞书同步推送 · 含数据/日期/出处/启发用 Node.js 和 PostgreSQL 实现事务级 API 幂等 📊 核心数据与事实(2026-11-27): 作者用 Node.js 24.21、Express 5.3.0 和 PostgreSQL 18.6 复现了 Redis 存幂等键时的重复扣款:进程在写入支付后、更新键前崩溃,锁过期后同一 Idempotency-Key 重试会再插入一条支付记录。 • 原文实测切片:In real life this is a deploy, an out-of-memory kill or a pod being rescheduled: async function createPayment(db, amount) { const { rows } = await db.query( `INSERT INTO payments (amount, status) VALUES ($1, 'succeeded') RETURNING id, amount,… • 原文实测切片:null)) .digest('hex') const db = await pool.connect() try { await db.query('BEGIN') // claim the key: a duplicate waits here until the first transaction ends const claim = await db.query( `INSERT INTO idempotency_keys (key, fingerprint) VALUES… • 原文实测切片:SET LOCAL limits it to the current transaction: await db.query('BEGIN') await db.query("SET LOCAL lock_timeout = '500ms'") } catch (err) { await db.query('ROLLBACK') if (err.code === '55P03') { // lock_timeout: the first request still holds… 💡 对独立开发者的启发: 1/ 供给端造工具的门槛已被 AI 和开源抹平,独立开发者真正的护城河是「找准垂直业务场景 + 做好分发」。 2/ 面对新模型或开源组件先看三件事:许可证是否商用安全、API/推理边际成本降了多少、能不能直接嵌进现有变现工作流。 3/ 别去卷通用开发者轮子,把底层新能力包装成面向具体行业人群(电商、创作者、中小商家)傻瓜式交付结果的产品。 📅 日期:2026-11-27(北京时间) 📰 信息源出处:HackerNoon · Douglas Matoso(hackernoon.com) 🔗 原文:https://hackernoon.com/implementing-transactional-api-idempotency-with-nodejs-and-postgresql?source=rss 📡 归档:https://billionare.me/items/vznyf47v0pd47i5vqdk2n9psm