[{"data":1,"prerenderedAt":2315},["ShallowReactive",2],{"blog-en-designing-idempotent-apis":3,"blog-related-en-designing-idempotent-apis":881},{"id":4,"title":5,"author":6,"body":7,"date":864,"description":865,"extension":866,"featured":867,"image":868,"meta":869,"navigation":119,"path":870,"readingTime":123,"seo":871,"slug":872,"stem":873,"tags":874,"__hash__":880},"blog\u002Fblog\u002Fen\u002Fdesigning-idempotent-apis.md","Designing Idempotent APIs: How to Prevent Duplicate Payments & Orders","Adam Abdel-Djamal",{"type":8,"value":9,"toc":858},"minimark",[10,14,17,30,33,38,45,72,79,145,147,151,154,162,164,168,171,808,810,814,854],[11,12,13],"p",{},"A user clicks \"Pay $49.00\". Their mobile connection blips. The UI hangs for two seconds, so they click \"Pay\" again.",[11,15,16],{},"Without an idempotency layer, their card is charged twice, two identical orders are created in your database, and your support team spends hours issuing refunds.",[11,18,19,20,24,25,29],{},"In distributed systems, ",[21,22,23],"strong",{},"network timeouts are ambiguous",". Did the request fail before reaching the server, or did it succeed but the response was dropped on the wire? In this article, we'll design a production-grade idempotency layer using ",[26,27,28],"code",{},"Idempotency-Key"," and Redis.",[31,32],"hr",{},[34,35,37],"h2",{"id":36},"_1-what-is-idempotency","1. What is Idempotency?",[11,39,40,41,44],{},"An operation is ",[21,42,43],{},"idempotent"," if applying it once produces the exact same outcome as applying it ten times.",[46,47,48,62],"ul",{},[49,50,51,54,55,54,58,61],"li",{},[26,52,53],{},"GET",", ",[26,56,57],{},"PUT",[26,59,60],{},"DELETE"," are inherently idempotent by HTTP specification.",[49,63,64,67,68,71],{},[26,65,66],{},"POST"," is ",[21,69,70],{},"non-idempotent"," by default — each invocation creates a new resource.",[11,73,74,75,78],{},"To make ",[26,76,77],{},"POST \u002Fapi\u002Fcheckout"," idempotent, the client must send a unique identifier with the request:",[80,81,86],"pre",{"className":82,"code":83,"language":84,"meta":85,"style":85},"language-http shiki shiki-themes github-light github-dark","POST \u002Fapi\u002Fcheckout HTTP\u002F1.1\nHost: api.yourapp.com\nIdempotency-Key: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d\nContent-Type: application\u002Fjson\n\n{\n  \"cartId\": \"cart_1234\",\n  \"amount\": 4900\n}\n","http","",[26,87,88,96,102,108,114,121,127,133,139],{"__ignoreMap":85},[89,90,93],"span",{"class":91,"line":92},"line",1,[89,94,95],{},"POST \u002Fapi\u002Fcheckout HTTP\u002F1.1\n",[89,97,99],{"class":91,"line":98},2,[89,100,101],{},"Host: api.yourapp.com\n",[89,103,105],{"class":91,"line":104},3,[89,106,107],{},"Idempotency-Key: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d\n",[89,109,111],{"class":91,"line":110},4,[89,112,113],{},"Content-Type: application\u002Fjson\n",[89,115,117],{"class":91,"line":116},5,[89,118,120],{"emptyLinePlaceholder":119},true,"\n",[89,122,124],{"class":91,"line":123},6,[89,125,126],{},"{\n",[89,128,130],{"class":91,"line":129},7,[89,131,132],{},"  \"cartId\": \"cart_1234\",\n",[89,134,136],{"class":91,"line":135},8,[89,137,138],{},"  \"amount\": 4900\n",[89,140,142],{"class":91,"line":141},9,[89,143,144],{},"}\n",[31,146],{},[34,148,150],{"id":149},"_2-the-idempotency-workflow-with-redis","2. The Idempotency Workflow with Redis",[11,152,153],{},"Here is the life cycle of an idempotent request:",[80,155,160],{"className":156,"code":158,"language":159,"meta":85},[157],"language-text","Client (Sends Idempotency-Key)\n   │\n   ▼\n[Redis Lock Check]\n   ├─► Key exists & Status is COMPLETED: Return cached response immediately (No duplicate work!)\n   ├─► Key exists & Status is PROCESSING: Return 409 Conflict (Concurrent in-flight duplicate)\n   └─► Key does NOT exist: Acquire lock with TTL (e.g. 120s) -> Execute transaction -> Store response in Redis\n","text",[26,161,158],{"__ignoreMap":85},[31,163],{},[34,165,167],{"id":166},"_3-implementation-express-nodejs-middleware","3. Implementation: Express \u002F Node.js Middleware",[11,169,170],{},"Here is the clean middleware implementation using Redis atomic primitives:",[80,172,176],{"className":173,"code":174,"language":175,"meta":85,"style":85},"language-typescript shiki shiki-themes github-light github-dark","import { Request, Response, NextFunction } from 'express'\nimport Redis from 'ioredis'\n\nconst redis = new Redis(process.env.REDIS_URL!)\n\nexport async function idempotencyMiddleware(req: Request, res: Response, next: NextFunction) {\n  const idempotencyKey = req.header('Idempotency-Key')\n  if (!idempotencyKey) return next()\n\n  const redisKey = `idempotency:${req.user?.id || 'anon'}:${idempotencyKey}`\n\n  \u002F\u002F Atomic SET with NX (Only set if Not eXists) and 120-second TTL\n  const acquired = await redis.set(redisKey, JSON.stringify({ status: 'PROCESSING' }), 'EX', 120, 'NX')\n\n  if (!acquired) {\n    const cachedData = await redis.get(redisKey)\n    if (!cachedData) return res.status(500).json({ error: 'Idempotency state corrupted' })\n\n    const { status, statusCode, body } = JSON.parse(cachedData)\n\n    if (status === 'PROCESSING') {\n      return res.status(409).json({ error: 'A request with this idempotency key is already in progress.' })\n    }\n\n    \u002F\u002F Replay original response without re-executing charges\n    return res.status(statusCode).json(body)\n  }\n\n  \u002F\u002F Intercept the response to cache the result once completed\n  const originalJson = res.json.bind(res)\n  res.json = (body: any) => {\n    redis.set(\n      redisKey,\n      JSON.stringify({ status: 'COMPLETED', statusCode: res.statusCode, body }),\n      'EX',\n      86400 \u002F\u002F Keep cached response for 24 hours\n    )\n    return originalJson(body)\n  }\n\n  next()\n}\n","typescript",[26,177,178,195,207,211,242,246,296,319,341,345,384,389,396,450,455,467,488,529,534,571,576,592,618,624,629,635,653,659,664,670,689,717,728,734,752,761,770,776,785,790,795,803],{"__ignoreMap":85},[89,179,180,184,188,191],{"class":91,"line":92},[89,181,183],{"class":182},"szBVR","import",[89,185,187],{"class":186},"sVt8B"," { Request, Response, NextFunction } ",[89,189,190],{"class":182},"from",[89,192,194],{"class":193},"sZZnC"," 'express'\n",[89,196,197,199,202,204],{"class":91,"line":98},[89,198,183],{"class":182},[89,200,201],{"class":186}," Redis ",[89,203,190],{"class":182},[89,205,206],{"class":193}," 'ioredis'\n",[89,208,209],{"class":91,"line":104},[89,210,120],{"emptyLinePlaceholder":119},[89,212,213,216,220,223,226,230,233,236,239],{"class":91,"line":110},[89,214,215],{"class":182},"const",[89,217,219],{"class":218},"sj4cs"," redis",[89,221,222],{"class":182}," =",[89,224,225],{"class":182}," new",[89,227,229],{"class":228},"sScJk"," Redis",[89,231,232],{"class":186},"(process.env.",[89,234,235],{"class":218},"REDIS_URL",[89,237,238],{"class":182},"!",[89,240,241],{"class":186},")\n",[89,243,244],{"class":91,"line":116},[89,245,120],{"emptyLinePlaceholder":119},[89,247,248,251,254,257,260,263,267,270,273,275,278,280,283,285,288,290,293],{"class":91,"line":123},[89,249,250],{"class":182},"export",[89,252,253],{"class":182}," async",[89,255,256],{"class":182}," function",[89,258,259],{"class":228}," idempotencyMiddleware",[89,261,262],{"class":186},"(",[89,264,266],{"class":265},"s4XuR","req",[89,268,269],{"class":182},":",[89,271,272],{"class":228}," Request",[89,274,54],{"class":186},[89,276,277],{"class":265},"res",[89,279,269],{"class":182},[89,281,282],{"class":228}," Response",[89,284,54],{"class":186},[89,286,287],{"class":265},"next",[89,289,269],{"class":182},[89,291,292],{"class":228}," NextFunction",[89,294,295],{"class":186},") {\n",[89,297,298,301,304,306,309,312,314,317],{"class":91,"line":129},[89,299,300],{"class":182},"  const",[89,302,303],{"class":218}," idempotencyKey",[89,305,222],{"class":182},[89,307,308],{"class":186}," req.",[89,310,311],{"class":228},"header",[89,313,262],{"class":186},[89,315,316],{"class":193},"'Idempotency-Key'",[89,318,241],{"class":186},[89,320,321,324,327,329,332,335,338],{"class":91,"line":135},[89,322,323],{"class":182},"  if",[89,325,326],{"class":186}," (",[89,328,238],{"class":182},[89,330,331],{"class":186},"idempotencyKey) ",[89,333,334],{"class":182},"return",[89,336,337],{"class":228}," next",[89,339,340],{"class":186},"()\n",[89,342,343],{"class":91,"line":141},[89,344,120],{"emptyLinePlaceholder":119},[89,346,348,350,353,355,358,360,363,366,369,372,375,378,381],{"class":91,"line":347},10,[89,349,300],{"class":182},[89,351,352],{"class":218}," redisKey",[89,354,222],{"class":182},[89,356,357],{"class":193}," `idempotency:${",[89,359,266],{"class":186},[89,361,362],{"class":193},".",[89,364,365],{"class":186},"user",[89,367,368],{"class":193},"?.",[89,370,371],{"class":186},"id",[89,373,374],{"class":182}," ||",[89,376,377],{"class":193}," 'anon'}:${",[89,379,380],{"class":186},"idempotencyKey",[89,382,383],{"class":193},"}`\n",[89,385,387],{"class":91,"line":386},11,[89,388,120],{"emptyLinePlaceholder":119},[89,390,392],{"class":91,"line":391},12,[89,393,395],{"class":394},"sJ8bj","  \u002F\u002F Atomic SET with NX (Only set if Not eXists) and 120-second TTL\n",[89,397,399,401,404,406,409,412,415,418,421,423,426,429,432,435,438,440,443,445,448],{"class":91,"line":398},13,[89,400,300],{"class":182},[89,402,403],{"class":218}," acquired",[89,405,222],{"class":182},[89,407,408],{"class":182}," await",[89,410,411],{"class":186}," redis.",[89,413,414],{"class":228},"set",[89,416,417],{"class":186},"(redisKey, ",[89,419,420],{"class":218},"JSON",[89,422,362],{"class":186},[89,424,425],{"class":228},"stringify",[89,427,428],{"class":186},"({ status: ",[89,430,431],{"class":193},"'PROCESSING'",[89,433,434],{"class":186}," }), ",[89,436,437],{"class":193},"'EX'",[89,439,54],{"class":186},[89,441,442],{"class":218},"120",[89,444,54],{"class":186},[89,446,447],{"class":193},"'NX'",[89,449,241],{"class":186},[89,451,453],{"class":91,"line":452},14,[89,454,120],{"emptyLinePlaceholder":119},[89,456,458,460,462,464],{"class":91,"line":457},15,[89,459,323],{"class":182},[89,461,326],{"class":186},[89,463,238],{"class":182},[89,465,466],{"class":186},"acquired) {\n",[89,468,470,473,476,478,480,482,485],{"class":91,"line":469},16,[89,471,472],{"class":182},"    const",[89,474,475],{"class":218}," cachedData",[89,477,222],{"class":182},[89,479,408],{"class":182},[89,481,411],{"class":186},[89,483,484],{"class":228},"get",[89,486,487],{"class":186},"(redisKey)\n",[89,489,491,494,496,498,501,503,506,509,511,514,517,520,523,526],{"class":91,"line":490},17,[89,492,493],{"class":182},"    if",[89,495,326],{"class":186},[89,497,238],{"class":182},[89,499,500],{"class":186},"cachedData) ",[89,502,334],{"class":182},[89,504,505],{"class":186}," res.",[89,507,508],{"class":228},"status",[89,510,262],{"class":186},[89,512,513],{"class":218},"500",[89,515,516],{"class":186},").",[89,518,519],{"class":228},"json",[89,521,522],{"class":186},"({ error: ",[89,524,525],{"class":193},"'Idempotency state corrupted'",[89,527,528],{"class":186}," })\n",[89,530,532],{"class":91,"line":531},18,[89,533,120],{"emptyLinePlaceholder":119},[89,535,537,539,542,544,546,549,551,554,557,560,563,565,568],{"class":91,"line":536},19,[89,538,472],{"class":182},[89,540,541],{"class":186}," { ",[89,543,508],{"class":218},[89,545,54],{"class":186},[89,547,548],{"class":218},"statusCode",[89,550,54],{"class":186},[89,552,553],{"class":218},"body",[89,555,556],{"class":186}," } ",[89,558,559],{"class":182},"=",[89,561,562],{"class":218}," JSON",[89,564,362],{"class":186},[89,566,567],{"class":228},"parse",[89,569,570],{"class":186},"(cachedData)\n",[89,572,574],{"class":91,"line":573},20,[89,575,120],{"emptyLinePlaceholder":119},[89,577,579,581,584,587,590],{"class":91,"line":578},21,[89,580,493],{"class":182},[89,582,583],{"class":186}," (status ",[89,585,586],{"class":182},"===",[89,588,589],{"class":193}," 'PROCESSING'",[89,591,295],{"class":186},[89,593,595,598,600,602,604,607,609,611,613,616],{"class":91,"line":594},22,[89,596,597],{"class":182},"      return",[89,599,505],{"class":186},[89,601,508],{"class":228},[89,603,262],{"class":186},[89,605,606],{"class":218},"409",[89,608,516],{"class":186},[89,610,519],{"class":228},[89,612,522],{"class":186},[89,614,615],{"class":193},"'A request with this idempotency key is already in progress.'",[89,617,528],{"class":186},[89,619,621],{"class":91,"line":620},23,[89,622,623],{"class":186},"    }\n",[89,625,627],{"class":91,"line":626},24,[89,628,120],{"emptyLinePlaceholder":119},[89,630,632],{"class":91,"line":631},25,[89,633,634],{"class":394},"    \u002F\u002F Replay original response without re-executing charges\n",[89,636,638,641,643,645,648,650],{"class":91,"line":637},26,[89,639,640],{"class":182},"    return",[89,642,505],{"class":186},[89,644,508],{"class":228},[89,646,647],{"class":186},"(statusCode).",[89,649,519],{"class":228},[89,651,652],{"class":186},"(body)\n",[89,654,656],{"class":91,"line":655},27,[89,657,658],{"class":186},"  }\n",[89,660,662],{"class":91,"line":661},28,[89,663,120],{"emptyLinePlaceholder":119},[89,665,667],{"class":91,"line":666},29,[89,668,669],{"class":394},"  \u002F\u002F Intercept the response to cache the result once completed\n",[89,671,673,675,678,680,683,686],{"class":91,"line":672},30,[89,674,300],{"class":182},[89,676,677],{"class":218}," originalJson",[89,679,222],{"class":182},[89,681,682],{"class":186}," res.json.",[89,684,685],{"class":228},"bind",[89,687,688],{"class":186},"(res)\n",[89,690,692,695,697,699,701,703,705,708,711,714],{"class":91,"line":691},31,[89,693,694],{"class":186},"  res.",[89,696,519],{"class":228},[89,698,222],{"class":182},[89,700,326],{"class":186},[89,702,553],{"class":265},[89,704,269],{"class":182},[89,706,707],{"class":218}," any",[89,709,710],{"class":186},") ",[89,712,713],{"class":182},"=>",[89,715,716],{"class":186}," {\n",[89,718,720,723,725],{"class":91,"line":719},32,[89,721,722],{"class":186},"    redis.",[89,724,414],{"class":228},[89,726,727],{"class":186},"(\n",[89,729,731],{"class":91,"line":730},33,[89,732,733],{"class":186},"      redisKey,\n",[89,735,737,740,742,744,746,749],{"class":91,"line":736},34,[89,738,739],{"class":218},"      JSON",[89,741,362],{"class":186},[89,743,425],{"class":228},[89,745,428],{"class":186},[89,747,748],{"class":193},"'COMPLETED'",[89,750,751],{"class":186},", statusCode: res.statusCode, body }),\n",[89,753,755,758],{"class":91,"line":754},35,[89,756,757],{"class":193},"      'EX'",[89,759,760],{"class":186},",\n",[89,762,764,767],{"class":91,"line":763},36,[89,765,766],{"class":218},"      86400",[89,768,769],{"class":394}," \u002F\u002F Keep cached response for 24 hours\n",[89,771,773],{"class":91,"line":772},37,[89,774,775],{"class":186},"    )\n",[89,777,779,781,783],{"class":91,"line":778},38,[89,780,640],{"class":182},[89,782,677],{"class":228},[89,784,652],{"class":186},[89,786,788],{"class":91,"line":787},39,[89,789,658],{"class":186},[89,791,793],{"class":91,"line":792},40,[89,794,120],{"emptyLinePlaceholder":119},[89,796,798,801],{"class":91,"line":797},41,[89,799,800],{"class":228},"  next",[89,802,340],{"class":186},[89,804,806],{"class":91,"line":805},42,[89,807,144],{"class":186},[31,809],{},[34,811,813],{"id":812},"_4-crucial-edge-cases-to-handle","4. Crucial Edge Cases to Handle",[815,816,817,834,844],"ol",{},[49,818,819,822,823,825,826,829,830,833],{},[21,820,821],{},"Payload Mismatch",": If a client sends the same ",[26,824,28],{}," with a completely different payload or amount, hash the request body (",[26,827,828],{},"SHA-256",") and verify it against the cached hash. Reject with ",[26,831,832],{},"422 Unprocessable Entity"," if the payload mutated.",[49,835,836,839,840,843],{},[21,837,838],{},"Server Crashes During Execution",": If your server crashes mid-flight while the status is ",[26,841,842],{},"PROCESSING",", the short 120s TTL ensures the key automatically expires rather than permanently locking the user out.",[49,845,846,849,850,853],{},[21,847,848],{},"Database Transactions",": Always wrap state-altering database changes in atomic ACID transactions (",[26,851,852],{},"BEGIN ... COMMIT",") before updating the idempotency cache.",[855,856,857],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":85,"searchDepth":98,"depth":98,"links":859},[860,861,862,863],{"id":36,"depth":98,"text":37},{"id":149,"depth":98,"text":150},{"id":166,"depth":98,"text":167},{"id":812,"depth":98,"text":813},"2026-08-25","How to implement Idempotency-Key headers with Redis to guarantee that network retries or double-clicks never create duplicate transactions or charges.","md",false,null,{},"\u002Fblog\u002Fen\u002Fdesigning-idempotent-apis",{"title":5,"description":865},"designing-idempotent-apis","blog\u002Fen\u002Fdesigning-idempotent-apis",[875,876,877,878,879],"api","architecture","redis","backend","security","6K_Mmospvh-bxbnf2IQJk51_kdS49k4H9JFbR_SMft0",[882,1701],{"id":883,"title":884,"author":6,"body":885,"date":1691,"description":1692,"extension":866,"featured":867,"image":868,"meta":1693,"navigation":119,"path":1694,"readingTime":123,"seo":1695,"slug":868,"stem":1696,"tags":1697,"__hash__":1700},"blog\u002Fblog\u002Fen\u002Fapi-common-mistakes.md","6 Common API Mistakes (And How to Fix Them)",{"type":8,"value":886,"toc":1682},[887,890,892,896,905,934,953,955,959,966,1046,1051,1170,1172,1176,1179,1239,1242,1244,1248,1251,1384,1391,1393,1397,1400,1574,1577,1579,1583,1586,1620,1627,1629,1633,1636,1679],[11,888,889],{},"Building an API feels straightforward until your first production incident. Most bugs aren't complex — they're the same six mistakes repeated across thousands of codebases. Here's what they are and how to stop making them.",[31,891],{},[34,893,895],{"id":894},"_1-using-the-wrong-http-method","1. Using the Wrong HTTP Method",[11,897,898,899,901,902,904],{},"The most visible mistake. Using ",[26,900,53],{}," to delete a resource, or ",[26,903,66],{}," for everything because \"it works.\"",[80,906,908],{"className":82,"code":907,"language":84,"meta":85,"style":85},"# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\nGET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n\n# ✅ Correct\nDELETE \u002Fapi\u002Fusers\u002F42\n",[26,909,910,915,920,924,929],{"__ignoreMap":85},[89,911,912],{"class":91,"line":92},[89,913,914],{},"# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\n",[89,916,917],{"class":91,"line":98},[89,918,919],{},"GET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n",[89,921,922],{"class":91,"line":104},[89,923,120],{"emptyLinePlaceholder":119},[89,925,926],{"class":91,"line":110},[89,927,928],{},"# ✅ Correct\n",[89,930,931],{"class":91,"line":116},[89,932,933],{},"DELETE \u002Fapi\u002Fusers\u002F42\n",[11,935,936,937,939,940,942,943,945,946,949,950,952],{},"HTTP methods carry semantic meaning that clients, proxies, and caches rely on. ",[26,938,53],{}," must be idempotent and safe (no side effects). ",[26,941,66],{}," creates. ",[26,944,57],{},"\u002F",[26,947,948],{},"PATCH"," update. ",[26,951,60],{}," removes. Violating these conventions breaks caching, browser history, and every HTTP client that follows the spec.",[31,954],{},[34,956,958],{"id":957},"_2-wrong-status-codes","2. Wrong Status Codes",[11,960,961,962,965],{},"Returning ",[26,963,964],{},"200 OK"," for an error response is the most common mistake in junior APIs.",[80,967,970],{"className":968,"code":969,"language":519,"meta":85,"style":85},"language-json shiki shiki-themes github-light github-dark","\u002F\u002F ❌ Status 200, but it's an error\n{\n  \"success\": false,\n  \"error\": \"User not found\"\n}\n\n\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n{\n  \"error\": \"User not found\",\n  \"code\": \"USER_NOT_FOUND\"\n}\n",[26,971,972,977,981,994,1004,1008,1012,1017,1021,1032,1042],{"__ignoreMap":85},[89,973,974],{"class":91,"line":92},[89,975,976],{"class":394},"\u002F\u002F ❌ Status 200, but it's an error\n",[89,978,979],{"class":91,"line":98},[89,980,126],{"class":186},[89,982,983,986,989,992],{"class":91,"line":104},[89,984,985],{"class":218},"  \"success\"",[89,987,988],{"class":186},": ",[89,990,991],{"class":218},"false",[89,993,760],{"class":186},[89,995,996,999,1001],{"class":91,"line":110},[89,997,998],{"class":218},"  \"error\"",[89,1000,988],{"class":186},[89,1002,1003],{"class":193},"\"User not found\"\n",[89,1005,1006],{"class":91,"line":116},[89,1007,144],{"class":186},[89,1009,1010],{"class":91,"line":123},[89,1011,120],{"emptyLinePlaceholder":119},[89,1013,1014],{"class":91,"line":129},[89,1015,1016],{"class":394},"\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n",[89,1018,1019],{"class":91,"line":135},[89,1020,126],{"class":186},[89,1022,1023,1025,1027,1030],{"class":91,"line":141},[89,1024,998],{"class":218},[89,1026,988],{"class":186},[89,1028,1029],{"class":193},"\"User not found\"",[89,1031,760],{"class":186},[89,1033,1034,1037,1039],{"class":91,"line":347},[89,1035,1036],{"class":218},"  \"code\"",[89,1038,988],{"class":186},[89,1040,1041],{"class":193},"\"USER_NOT_FOUND\"\n",[89,1043,1044],{"class":91,"line":386},[89,1045,144],{"class":186},[11,1047,1048],{},[21,1049,1050],{},"The codes that matter most:",[1052,1053,1054,1067],"table",{},[1055,1056,1057],"thead",{},[1058,1059,1060,1064],"tr",{},[1061,1062,1063],"th",{},"Code",[1061,1065,1066],{},"When to use it",[1068,1069,1070,1081,1091,1101,1111,1121,1131,1141,1151,1161],"tbody",{},[1058,1071,1072,1078],{},[1073,1074,1075],"td",{},[26,1076,1077],{},"200",[1073,1079,1080],{},"Success with a body",[1058,1082,1083,1088],{},[1073,1084,1085],{},[26,1086,1087],{},"201",[1073,1089,1090],{},"Resource created (POST)",[1058,1092,1093,1098],{},[1073,1094,1095],{},[26,1096,1097],{},"204",[1073,1099,1100],{},"Success, no body (DELETE)",[1058,1102,1103,1108],{},[1073,1104,1105],{},[26,1106,1107],{},"400",[1073,1109,1110],{},"Invalid input from the client",[1058,1112,1113,1118],{},[1073,1114,1115],{},[26,1116,1117],{},"401",[1073,1119,1120],{},"Not authenticated",[1058,1122,1123,1128],{},[1073,1124,1125],{},[26,1126,1127],{},"403",[1073,1129,1130],{},"Authenticated but not authorized",[1058,1132,1133,1138],{},[1073,1134,1135],{},[26,1136,1137],{},"404",[1073,1139,1140],{},"Resource doesn't exist",[1058,1142,1143,1148],{},[1073,1144,1145],{},[26,1146,1147],{},"422",[1073,1149,1150],{},"Input syntactically valid but semantically wrong",[1058,1152,1153,1158],{},[1073,1154,1155],{},[26,1156,1157],{},"429",[1073,1159,1160],{},"Rate limit exceeded",[1058,1162,1163,1167],{},[1073,1164,1165],{},[26,1166,513],{},[1073,1168,1169],{},"Server error (never the client's fault)",[31,1171],{},[34,1173,1175],{"id":1174},"_3-trusting-client-input","3. Trusting Client Input",[11,1177,1178],{},"Every value sent by a client is a potential attack vector.",[80,1180,1184],{"className":1181,"code":1182,"language":1183,"meta":85,"style":85},"language-php shiki shiki-themes github-light github-dark","\u002F\u002F ❌ Never do this\n$id = $_GET['id'];\n$query = \"SELECT * FROM users WHERE id = $id\";\n\u002F\u002F SQL injection: ?id=1 OR 1=1\n\n\u002F\u002F ✅ Always validate and sanitize\n$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\nif (!$id) {\n    return response()->json(['error' => 'Invalid ID'], 400);\n}\n$user = User::findOrFail($id); \u002F\u002F parameterized query\n","php",[26,1185,1186,1191,1196,1201,1206,1210,1215,1220,1225,1230,1234],{"__ignoreMap":85},[89,1187,1188],{"class":91,"line":92},[89,1189,1190],{},"\u002F\u002F ❌ Never do this\n",[89,1192,1193],{"class":91,"line":98},[89,1194,1195],{},"$id = $_GET['id'];\n",[89,1197,1198],{"class":91,"line":104},[89,1199,1200],{},"$query = \"SELECT * FROM users WHERE id = $id\";\n",[89,1202,1203],{"class":91,"line":110},[89,1204,1205],{},"\u002F\u002F SQL injection: ?id=1 OR 1=1\n",[89,1207,1208],{"class":91,"line":116},[89,1209,120],{"emptyLinePlaceholder":119},[89,1211,1212],{"class":91,"line":123},[89,1213,1214],{},"\u002F\u002F ✅ Always validate and sanitize\n",[89,1216,1217],{"class":91,"line":129},[89,1218,1219],{},"$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\n",[89,1221,1222],{"class":91,"line":135},[89,1223,1224],{},"if (!$id) {\n",[89,1226,1227],{"class":91,"line":141},[89,1228,1229],{},"    return response()->json(['error' => 'Invalid ID'], 400);\n",[89,1231,1232],{"class":91,"line":347},[89,1233,144],{},[89,1235,1236],{"class":91,"line":386},[89,1237,1238],{},"$user = User::findOrFail($id); \u002F\u002F parameterized query\n",[11,1240,1241],{},"Validate: type, format, length, range. Sanitize before any database or filesystem operation. Never trust what arrives in the request body, headers, or query string.",[31,1243],{},[34,1245,1247],{"id":1246},"_4-leaking-sensitive-data","4. Leaking Sensitive Data",[11,1249,1250],{},"Returning the full database object without filtering is an invitation to a data breach.",[80,1252,1254],{"className":968,"code":1253,"language":519,"meta":85,"style":85},"\u002F\u002F ❌ Returning the raw User model\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"password\": \"$2b$12$...\",\n  \"stripe_customer_id\": \"cus_xxx\",\n  \"internal_notes\": \"flagged for review\",\n  \"created_at\": \"2024-01-01\"\n}\n\n\u002F\u002F ✅ Return only what the client actually needs\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"created_at\": \"2024-01-01\"\n}\n",[26,1255,1256,1261,1265,1277,1289,1301,1313,1325,1335,1339,1343,1348,1352,1362,1372,1380],{"__ignoreMap":85},[89,1257,1258],{"class":91,"line":92},[89,1259,1260],{"class":394},"\u002F\u002F ❌ Returning the raw User model\n",[89,1262,1263],{"class":91,"line":98},[89,1264,126],{"class":186},[89,1266,1267,1270,1272,1275],{"class":91,"line":104},[89,1268,1269],{"class":218},"  \"id\"",[89,1271,988],{"class":186},[89,1273,1274],{"class":218},"42",[89,1276,760],{"class":186},[89,1278,1279,1282,1284,1287],{"class":91,"line":110},[89,1280,1281],{"class":218},"  \"email\"",[89,1283,988],{"class":186},[89,1285,1286],{"class":193},"\"user@example.com\"",[89,1288,760],{"class":186},[89,1290,1291,1294,1296,1299],{"class":91,"line":116},[89,1292,1293],{"class":218},"  \"password\"",[89,1295,988],{"class":186},[89,1297,1298],{"class":193},"\"$2b$12$...\"",[89,1300,760],{"class":186},[89,1302,1303,1306,1308,1311],{"class":91,"line":123},[89,1304,1305],{"class":218},"  \"stripe_customer_id\"",[89,1307,988],{"class":186},[89,1309,1310],{"class":193},"\"cus_xxx\"",[89,1312,760],{"class":186},[89,1314,1315,1318,1320,1323],{"class":91,"line":129},[89,1316,1317],{"class":218},"  \"internal_notes\"",[89,1319,988],{"class":186},[89,1321,1322],{"class":193},"\"flagged for review\"",[89,1324,760],{"class":186},[89,1326,1327,1330,1332],{"class":91,"line":135},[89,1328,1329],{"class":218},"  \"created_at\"",[89,1331,988],{"class":186},[89,1333,1334],{"class":193},"\"2024-01-01\"\n",[89,1336,1337],{"class":91,"line":141},[89,1338,144],{"class":186},[89,1340,1341],{"class":91,"line":347},[89,1342,120],{"emptyLinePlaceholder":119},[89,1344,1345],{"class":91,"line":386},[89,1346,1347],{"class":394},"\u002F\u002F ✅ Return only what the client actually needs\n",[89,1349,1350],{"class":91,"line":391},[89,1351,126],{"class":186},[89,1353,1354,1356,1358,1360],{"class":91,"line":398},[89,1355,1269],{"class":218},[89,1357,988],{"class":186},[89,1359,1274],{"class":218},[89,1361,760],{"class":186},[89,1363,1364,1366,1368,1370],{"class":91,"line":452},[89,1365,1281],{"class":218},[89,1367,988],{"class":186},[89,1369,1286],{"class":193},[89,1371,760],{"class":186},[89,1373,1374,1376,1378],{"class":91,"line":457},[89,1375,1329],{"class":218},[89,1377,988],{"class":186},[89,1379,1334],{"class":193},[89,1381,1382],{"class":91,"line":469},[89,1383,144],{"class":186},[11,1385,1386,1387,1390],{},"Use serializers, DTOs, or API Resources (Laravel) to explicitly define what gets exposed. The rule: ",[21,1388,1389],{},"opt-in, not opt-out."," Start with nothing, add what's needed.",[31,1392],{},[34,1394,1396],{"id":1395},"_5-inconsistent-error-format","5. Inconsistent Error Format",[11,1398,1399],{},"When every endpoint returns errors differently, every client has to handle every format.",[80,1401,1403],{"className":968,"code":1402,"language":519,"meta":85,"style":85},"\u002F\u002F ❌ Endpoint A\n{ \"message\": \"Not found\" }\n\n\u002F\u002F ❌ Endpoint B\n{ \"error\": true, \"msg\": \"Validation failed\", \"fields\": [\"email\"] }\n\n\u002F\u002F ❌ Endpoint C\n\"Internal server error\"\n\n\u002F\u002F ✅ One format, everywhere\n{\n  \"error\": {\n    \"code\": \"VALIDATION_FAILED\",\n    \"message\": \"The request contains invalid fields.\",\n    \"details\": [\n      { \"field\": \"email\", \"issue\": \"Invalid email format\" }\n    ]\n  }\n}\n",[26,1404,1405,1410,1426,1430,1435,1471,1475,1480,1485,1489,1494,1498,1505,1517,1529,1537,1561,1566,1570],{"__ignoreMap":85},[89,1406,1407],{"class":91,"line":92},[89,1408,1409],{"class":394},"\u002F\u002F ❌ Endpoint A\n",[89,1411,1412,1415,1418,1420,1423],{"class":91,"line":98},[89,1413,1414],{"class":186},"{ ",[89,1416,1417],{"class":218},"\"message\"",[89,1419,988],{"class":186},[89,1421,1422],{"class":193},"\"Not found\"",[89,1424,1425],{"class":186}," }\n",[89,1427,1428],{"class":91,"line":104},[89,1429,120],{"emptyLinePlaceholder":119},[89,1431,1432],{"class":91,"line":110},[89,1433,1434],{"class":394},"\u002F\u002F ❌ Endpoint B\n",[89,1436,1437,1439,1442,1444,1447,1449,1452,1454,1457,1459,1462,1465,1468],{"class":91,"line":116},[89,1438,1414],{"class":186},[89,1440,1441],{"class":218},"\"error\"",[89,1443,988],{"class":186},[89,1445,1446],{"class":218},"true",[89,1448,54],{"class":186},[89,1450,1451],{"class":218},"\"msg\"",[89,1453,988],{"class":186},[89,1455,1456],{"class":193},"\"Validation failed\"",[89,1458,54],{"class":186},[89,1460,1461],{"class":218},"\"fields\"",[89,1463,1464],{"class":186},": [",[89,1466,1467],{"class":193},"\"email\"",[89,1469,1470],{"class":186},"] }\n",[89,1472,1473],{"class":91,"line":123},[89,1474,120],{"emptyLinePlaceholder":119},[89,1476,1477],{"class":91,"line":129},[89,1478,1479],{"class":394},"\u002F\u002F ❌ Endpoint C\n",[89,1481,1482],{"class":91,"line":135},[89,1483,1484],{"class":193},"\"Internal server error\"\n",[89,1486,1487],{"class":91,"line":141},[89,1488,120],{"emptyLinePlaceholder":119},[89,1490,1491],{"class":91,"line":347},[89,1492,1493],{"class":394},"\u002F\u002F ✅ One format, everywhere\n",[89,1495,1496],{"class":91,"line":386},[89,1497,126],{"class":186},[89,1499,1500,1502],{"class":91,"line":391},[89,1501,998],{"class":218},[89,1503,1504],{"class":186},": {\n",[89,1506,1507,1510,1512,1515],{"class":91,"line":398},[89,1508,1509],{"class":218},"    \"code\"",[89,1511,988],{"class":186},[89,1513,1514],{"class":193},"\"VALIDATION_FAILED\"",[89,1516,760],{"class":186},[89,1518,1519,1522,1524,1527],{"class":91,"line":452},[89,1520,1521],{"class":218},"    \"message\"",[89,1523,988],{"class":186},[89,1525,1526],{"class":193},"\"The request contains invalid fields.\"",[89,1528,760],{"class":186},[89,1530,1531,1534],{"class":91,"line":457},[89,1532,1533],{"class":218},"    \"details\"",[89,1535,1536],{"class":186},": [\n",[89,1538,1539,1542,1545,1547,1549,1551,1554,1556,1559],{"class":91,"line":469},[89,1540,1541],{"class":186},"      { ",[89,1543,1544],{"class":218},"\"field\"",[89,1546,988],{"class":186},[89,1548,1467],{"class":193},[89,1550,54],{"class":186},[89,1552,1553],{"class":218},"\"issue\"",[89,1555,988],{"class":186},[89,1557,1558],{"class":193},"\"Invalid email format\"",[89,1560,1425],{"class":186},[89,1562,1563],{"class":91,"line":490},[89,1564,1565],{"class":186},"    ]\n",[89,1567,1568],{"class":91,"line":531},[89,1569,658],{"class":186},[89,1571,1572],{"class":91,"line":536},[89,1573,144],{"class":186},[11,1575,1576],{},"Define your error envelope once. Apply it everywhere via a global exception handler. Clients — including your own frontend — will thank you.",[31,1578],{},[34,1580,1582],{"id":1581},"_6-no-api-versioning","6. No API Versioning",[11,1584,1585],{},"Changing an endpoint response breaks every client using it. Without versioning, you can never evolve the API.",[80,1587,1589],{"className":82,"code":1588,"language":84,"meta":85,"style":85},"# ❌ No version — any breaking change affects all clients immediately\nGET \u002Fapi\u002Fusers\n\n# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\nGET \u002Fapi\u002Fv1\u002Fusers\nGET \u002Fapi\u002Fv2\u002Fusers\n",[26,1590,1591,1596,1601,1605,1610,1615],{"__ignoreMap":85},[89,1592,1593],{"class":91,"line":92},[89,1594,1595],{},"# ❌ No version — any breaking change affects all clients immediately\n",[89,1597,1598],{"class":91,"line":98},[89,1599,1600],{},"GET \u002Fapi\u002Fusers\n",[89,1602,1603],{"class":91,"line":104},[89,1604,120],{"emptyLinePlaceholder":119},[89,1606,1607],{"class":91,"line":110},[89,1608,1609],{},"# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\n",[89,1611,1612],{"class":91,"line":116},[89,1613,1614],{},"GET \u002Fapi\u002Fv1\u002Fusers\n",[89,1616,1617],{"class":91,"line":123},[89,1618,1619],{},"GET \u002Fapi\u002Fv2\u002Fusers\n",[11,1621,1622,1623,1626],{},"Version from day one, even if you only have v1. The cost of adding ",[26,1624,1625],{},"\u002Fv1\u002F"," later — after clients are in production — is much higher than the cost of including it from the start.",[31,1628],{},[34,1630,1632],{"id":1631},"the-takeaway","The Takeaway",[11,1634,1635],{},"None of these mistakes require advanced knowledge to fix. They require discipline: a checklist before every endpoint ships.",[46,1637,1640,1649,1655,1661,1667,1673],{"className":1638},[1639],"contains-task-list",[49,1641,1644,1648],{"className":1642},[1643],"task-list-item",[1645,1646],"input",{"disabled":119,"type":1647},"checkbox"," HTTP method matches the operation's semantics",[49,1650,1652,1654],{"className":1651},[1643],[1645,1653],{"disabled":119,"type":1647}," Status code is accurate",[49,1656,1658,1660],{"className":1657},[1643],[1645,1659],{"disabled":119,"type":1647}," All input is validated before use",[49,1662,1664,1666],{"className":1663},[1643],[1645,1665],{"disabled":119,"type":1647}," Response only exposes necessary fields",[49,1668,1670,1672],{"className":1669},[1643],[1645,1671],{"disabled":119,"type":1647}," Errors follow the global format",[49,1674,1676,1678],{"className":1675},[1643],[1645,1677],{"disabled":119,"type":1647}," URL includes an API version",[855,1680,1681],{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":85,"searchDepth":98,"depth":98,"links":1683},[1684,1685,1686,1687,1688,1689,1690],{"id":894,"depth":98,"text":895},{"id":957,"depth":98,"text":958},{"id":1174,"depth":98,"text":1175},{"id":1246,"depth":98,"text":1247},{"id":1395,"depth":98,"text":1396},{"id":1581,"depth":98,"text":1582},{"id":1631,"depth":98,"text":1632},"2026-06-04","From wrong HTTP methods to leaking sensitive data — the errors most developers make when building their first API, and the concrete fixes.",{},"\u002Fblog\u002Fen\u002Fapi-common-mistakes",{"title":884,"description":1692},"blog\u002Fen\u002Fapi-common-mistakes",[875,878,1698,1699],"rest","best-practices","z2NMprz3c9WpXKotgDMzEF9WM_LLLolyFqidBZu4I6s",{"id":1702,"title":1703,"author":6,"body":1704,"date":2305,"description":2306,"extension":866,"featured":867,"image":868,"meta":2307,"navigation":119,"path":2308,"readingTime":123,"seo":2309,"slug":2310,"stem":2311,"tags":2312,"__hash__":2314},"blog\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis.md","API Rate Limiting in Practice: Token Bucket vs. Sliding Window with Redis",{"type":8,"value":1705,"toc":2296},[1706,1709,1712,1723,1725,1729,1734,1741,1745,1748,1756,1760,1763,1765,1769,1776,2226,2228,2232,2235,2291,2294],[11,1707,1708],{},"Without API rate limiting, a single rogue script or misconfigured webhook client can fire 10,000 requests per second at your login or checkout endpoint, exhausting your database connection pool and taking your entire application down.",[11,1710,1711],{},"Rate limiting is the first line of defense against bot attacks, credential stuffing, and API resource abuse. But which algorithm should you implement?",[11,1713,1714,1715,1718,1719,1722],{},"In this article, we compare the two industry-standard algorithms — ",[21,1716,1717],{},"Token Bucket"," and ",[21,1720,1721],{},"Sliding Window Log"," — and show how to implement a high-speed sliding window rate limiter in Redis.",[31,1724],{},[34,1726,1728],{"id":1727},"_1-comparing-the-algorithms","1. Comparing the Algorithms",[1730,1731,1733],"h3",{"id":1732},"a-fixed-window-counter-the-flawed-default","A. Fixed Window Counter (The Flawed Default)",[11,1735,1736,1737,1740],{},"Divides time into fixed buckets (e.g. 10:00 to 10:01). If a user can make 100 requests per minute, they can send 100 requests at 10:00:59, and another 100 requests at 10:01:01. That means ",[21,1738,1739],{},"200 requests in 2 seconds"," — double the intended burst limit!",[1730,1742,1744],{"id":1743},"b-token-bucket","B. Token Bucket",[11,1746,1747],{},"A bucket holds a fixed number of tokens (burst capacity). Tokens are continuously replenished at a constant rate. Each incoming request consumes one token.",[46,1749,1750],{},[49,1751,1752,1755],{},[21,1753,1754],{},"Great for",": APIs that want to allow legitimate temporary traffic spikes while capping sustained throughput.",[1730,1757,1759],{"id":1758},"c-sliding-window-counter-the-production-standard","C. Sliding Window Counter (The Production Standard)",[11,1761,1762],{},"Combines the low memory usage of fixed windows with the smoothness of a rolling window. It calculates the request rate by weighting the request count of the previous window and the current window.",[31,1764],{},[34,1766,1768],{"id":1767},"_2-sliding-window-rate-limiter-with-redis-sorted-sets","2. Sliding Window Rate Limiter with Redis Sorted Sets",[11,1770,1771,1772,1775],{},"Redis Sorted Sets (",[26,1773,1774],{},"ZSET",") allow us to track requests by timestamp with microsecond accuracy:",[80,1777,1779],{"className":173,"code":1778,"language":175,"meta":85,"style":85},"import Redis from 'ioredis'\n\nconst redis = new Redis(process.env.REDIS_URL!)\n\nexport async function checkRateLimit(identifier: string, limit = 60, windowInSeconds = 60) {\n  const now = Date.now()\n  const windowStart = now - (windowInSeconds * 1000)\n  const key = `ratelimit:${identifier}`\n\n  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n  const multi = redis.multi()\n  \u002F\u002F 1. Remove all request timestamps older than the active window\n  multi.zremrangebyscore(key, 0, windowStart)\n  \u002F\u002F 2. Count requests currently in the active window\n  multi.zcard(key)\n  \u002F\u002F 3. Add the current timestamp as a new request\n  multi.zadd(key, now, `${now}-${Math.random()}`)\n  \u002F\u002F 4. Set key expiration to auto-clean idle users\n  multi.expire(key, windowInSeconds)\n\n  const results = await multi.exec()\n  const currentCount = (results?.[1]?.[1] as number) || 0\n\n  if (currentCount >= limit) {\n    return {\n      allowed: false,\n      remaining: 0,\n      retryAfter: Math.ceil(windowInSeconds - ((now - windowStart) \u002F 1000))\n    }\n  }\n\n  return {\n    allowed: true,\n    remaining: limit - currentCount - 1,\n    retryAfter: 0\n  }\n}\n",[26,1780,1781,1791,1795,1815,1819,1861,1878,1904,1920,1924,1929,1945,1950,1967,1972,1982,1987,2021,2026,2036,2040,2059,2096,2100,2113,2119,2128,2137,2165,2169,2173,2177,2184,2193,2210,2218,2222],{"__ignoreMap":85},[89,1782,1783,1785,1787,1789],{"class":91,"line":92},[89,1784,183],{"class":182},[89,1786,201],{"class":186},[89,1788,190],{"class":182},[89,1790,206],{"class":193},[89,1792,1793],{"class":91,"line":98},[89,1794,120],{"emptyLinePlaceholder":119},[89,1796,1797,1799,1801,1803,1805,1807,1809,1811,1813],{"class":91,"line":104},[89,1798,215],{"class":182},[89,1800,219],{"class":218},[89,1802,222],{"class":182},[89,1804,225],{"class":182},[89,1806,229],{"class":228},[89,1808,232],{"class":186},[89,1810,235],{"class":218},[89,1812,238],{"class":182},[89,1814,241],{"class":186},[89,1816,1817],{"class":91,"line":110},[89,1818,120],{"emptyLinePlaceholder":119},[89,1820,1821,1823,1825,1827,1830,1832,1835,1837,1840,1842,1845,1847,1850,1852,1855,1857,1859],{"class":91,"line":116},[89,1822,250],{"class":182},[89,1824,253],{"class":182},[89,1826,256],{"class":182},[89,1828,1829],{"class":228}," checkRateLimit",[89,1831,262],{"class":186},[89,1833,1834],{"class":265},"identifier",[89,1836,269],{"class":182},[89,1838,1839],{"class":218}," string",[89,1841,54],{"class":186},[89,1843,1844],{"class":265},"limit",[89,1846,222],{"class":182},[89,1848,1849],{"class":218}," 60",[89,1851,54],{"class":186},[89,1853,1854],{"class":265},"windowInSeconds",[89,1856,222],{"class":182},[89,1858,1849],{"class":218},[89,1860,295],{"class":186},[89,1862,1863,1865,1868,1870,1873,1876],{"class":91,"line":123},[89,1864,300],{"class":182},[89,1866,1867],{"class":218}," now",[89,1869,222],{"class":182},[89,1871,1872],{"class":186}," Date.",[89,1874,1875],{"class":228},"now",[89,1877,340],{"class":186},[89,1879,1880,1882,1885,1887,1890,1893,1896,1899,1902],{"class":91,"line":129},[89,1881,300],{"class":182},[89,1883,1884],{"class":218}," windowStart",[89,1886,222],{"class":182},[89,1888,1889],{"class":186}," now ",[89,1891,1892],{"class":182},"-",[89,1894,1895],{"class":186}," (windowInSeconds ",[89,1897,1898],{"class":182},"*",[89,1900,1901],{"class":218}," 1000",[89,1903,241],{"class":186},[89,1905,1906,1908,1911,1913,1916,1918],{"class":91,"line":135},[89,1907,300],{"class":182},[89,1909,1910],{"class":218}," key",[89,1912,222],{"class":182},[89,1914,1915],{"class":193}," `ratelimit:${",[89,1917,1834],{"class":186},[89,1919,383],{"class":193},[89,1921,1922],{"class":91,"line":141},[89,1923,120],{"emptyLinePlaceholder":119},[89,1925,1926],{"class":91,"line":347},[89,1927,1928],{"class":394},"  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n",[89,1930,1931,1933,1936,1938,1940,1943],{"class":91,"line":386},[89,1932,300],{"class":182},[89,1934,1935],{"class":218}," multi",[89,1937,222],{"class":182},[89,1939,411],{"class":186},[89,1941,1942],{"class":228},"multi",[89,1944,340],{"class":186},[89,1946,1947],{"class":91,"line":391},[89,1948,1949],{"class":394},"  \u002F\u002F 1. Remove all request timestamps older than the active window\n",[89,1951,1952,1955,1958,1961,1964],{"class":91,"line":398},[89,1953,1954],{"class":186},"  multi.",[89,1956,1957],{"class":228},"zremrangebyscore",[89,1959,1960],{"class":186},"(key, ",[89,1962,1963],{"class":218},"0",[89,1965,1966],{"class":186},", windowStart)\n",[89,1968,1969],{"class":91,"line":452},[89,1970,1971],{"class":394},"  \u002F\u002F 2. Count requests currently in the active window\n",[89,1973,1974,1976,1979],{"class":91,"line":457},[89,1975,1954],{"class":186},[89,1977,1978],{"class":228},"zcard",[89,1980,1981],{"class":186},"(key)\n",[89,1983,1984],{"class":91,"line":469},[89,1985,1986],{"class":394},"  \u002F\u002F 3. Add the current timestamp as a new request\n",[89,1988,1989,1991,1994,1997,2000,2002,2005,2008,2010,2013,2016,2019],{"class":91,"line":490},[89,1990,1954],{"class":186},[89,1992,1993],{"class":228},"zadd",[89,1995,1996],{"class":186},"(key, now, ",[89,1998,1999],{"class":193},"`${",[89,2001,1875],{"class":186},[89,2003,2004],{"class":193},"}-${",[89,2006,2007],{"class":186},"Math",[89,2009,362],{"class":193},[89,2011,2012],{"class":228},"random",[89,2014,2015],{"class":193},"()",[89,2017,2018],{"class":193},"}`",[89,2020,241],{"class":186},[89,2022,2023],{"class":91,"line":531},[89,2024,2025],{"class":394},"  \u002F\u002F 4. Set key expiration to auto-clean idle users\n",[89,2027,2028,2030,2033],{"class":91,"line":536},[89,2029,1954],{"class":186},[89,2031,2032],{"class":228},"expire",[89,2034,2035],{"class":186},"(key, windowInSeconds)\n",[89,2037,2038],{"class":91,"line":573},[89,2039,120],{"emptyLinePlaceholder":119},[89,2041,2042,2044,2047,2049,2051,2054,2057],{"class":91,"line":578},[89,2043,300],{"class":182},[89,2045,2046],{"class":218}," results",[89,2048,222],{"class":182},[89,2050,408],{"class":182},[89,2052,2053],{"class":186}," multi.",[89,2055,2056],{"class":228},"exec",[89,2058,340],{"class":186},[89,2060,2061,2063,2066,2068,2071,2074,2077,2079,2082,2085,2088,2090,2093],{"class":91,"line":594},[89,2062,300],{"class":182},[89,2064,2065],{"class":218}," currentCount",[89,2067,222],{"class":182},[89,2069,2070],{"class":186}," (results?.[",[89,2072,2073],{"class":218},"1",[89,2075,2076],{"class":186},"]?.[",[89,2078,2073],{"class":218},[89,2080,2081],{"class":186},"] ",[89,2083,2084],{"class":182},"as",[89,2086,2087],{"class":218}," number",[89,2089,710],{"class":186},[89,2091,2092],{"class":182},"||",[89,2094,2095],{"class":218}," 0\n",[89,2097,2098],{"class":91,"line":620},[89,2099,120],{"emptyLinePlaceholder":119},[89,2101,2102,2104,2107,2110],{"class":91,"line":626},[89,2103,323],{"class":182},[89,2105,2106],{"class":186}," (currentCount ",[89,2108,2109],{"class":182},">=",[89,2111,2112],{"class":186}," limit) {\n",[89,2114,2115,2117],{"class":91,"line":631},[89,2116,640],{"class":182},[89,2118,716],{"class":186},[89,2120,2121,2124,2126],{"class":91,"line":637},[89,2122,2123],{"class":186},"      allowed: ",[89,2125,991],{"class":218},[89,2127,760],{"class":186},[89,2129,2130,2133,2135],{"class":91,"line":655},[89,2131,2132],{"class":186},"      remaining: ",[89,2134,1963],{"class":218},[89,2136,760],{"class":186},[89,2138,2139,2142,2145,2148,2150,2153,2155,2158,2160,2162],{"class":91,"line":661},[89,2140,2141],{"class":186},"      retryAfter: Math.",[89,2143,2144],{"class":228},"ceil",[89,2146,2147],{"class":186},"(windowInSeconds ",[89,2149,1892],{"class":182},[89,2151,2152],{"class":186}," ((now ",[89,2154,1892],{"class":182},[89,2156,2157],{"class":186}," windowStart) ",[89,2159,945],{"class":182},[89,2161,1901],{"class":218},[89,2163,2164],{"class":186},"))\n",[89,2166,2167],{"class":91,"line":666},[89,2168,623],{"class":186},[89,2170,2171],{"class":91,"line":672},[89,2172,658],{"class":186},[89,2174,2175],{"class":91,"line":691},[89,2176,120],{"emptyLinePlaceholder":119},[89,2178,2179,2182],{"class":91,"line":719},[89,2180,2181],{"class":182},"  return",[89,2183,716],{"class":186},[89,2185,2186,2189,2191],{"class":91,"line":730},[89,2187,2188],{"class":186},"    allowed: ",[89,2190,1446],{"class":218},[89,2192,760],{"class":186},[89,2194,2195,2198,2200,2203,2205,2208],{"class":91,"line":736},[89,2196,2197],{"class":186},"    remaining: limit ",[89,2199,1892],{"class":182},[89,2201,2202],{"class":186}," currentCount ",[89,2204,1892],{"class":182},[89,2206,2207],{"class":218}," 1",[89,2209,760],{"class":186},[89,2211,2212,2215],{"class":91,"line":754},[89,2213,2214],{"class":186},"    retryAfter: ",[89,2216,2217],{"class":218},"0\n",[89,2219,2220],{"class":91,"line":763},[89,2221,658],{"class":186},[89,2223,2224],{"class":91,"line":772},[89,2225,144],{"class":186},[31,2227],{},[34,2229,2231],{"id":2230},"_3-returning-standard-http-rate-limit-headers","3. Returning Standard HTTP Rate Limit Headers",[11,2233,2234],{},"Whenever your server answers a request, communicate rate limit states in the response headers (following IETF draft standards):",[80,2236,2238],{"className":82,"code":2237,"language":84,"meta":85,"style":85},"HTTP\u002F1.1 429 Too Many Requests\nRetry-After: 32\nX-RateLimit-Limit: 60\nX-RateLimit-Remaining: 0\nX-RateLimit-Reset: 1773489240\nContent-Type: application\u002Fjson\n\n{\n  \"error\": \"Too Many Requests\",\n  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n}\n",[26,2239,2240,2245,2250,2255,2260,2265,2269,2273,2277,2282,2287],{"__ignoreMap":85},[89,2241,2242],{"class":91,"line":92},[89,2243,2244],{},"HTTP\u002F1.1 429 Too Many Requests\n",[89,2246,2247],{"class":91,"line":98},[89,2248,2249],{},"Retry-After: 32\n",[89,2251,2252],{"class":91,"line":104},[89,2253,2254],{},"X-RateLimit-Limit: 60\n",[89,2256,2257],{"class":91,"line":110},[89,2258,2259],{},"X-RateLimit-Remaining: 0\n",[89,2261,2262],{"class":91,"line":116},[89,2263,2264],{},"X-RateLimit-Reset: 1773489240\n",[89,2266,2267],{"class":91,"line":123},[89,2268,113],{},[89,2270,2271],{"class":91,"line":129},[89,2272,120],{"emptyLinePlaceholder":119},[89,2274,2275],{"class":91,"line":135},[89,2276,126],{},[89,2278,2279],{"class":91,"line":141},[89,2280,2281],{},"  \"error\": \"Too Many Requests\",\n",[89,2283,2284],{"class":91,"line":347},[89,2285,2286],{},"  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n",[89,2288,2289],{"class":91,"line":386},[89,2290,144],{},[11,2292,2293],{},"This ensures honest third-party API clients and mobile SDKs can automatically back off and retry without overwhelming your infrastructure.",[855,2295,857],{},{"title":85,"searchDepth":98,"depth":98,"links":2297},[2298,2303,2304],{"id":1727,"depth":98,"text":1728,"children":2299},[2300,2301,2302],{"id":1732,"depth":104,"text":1733},{"id":1743,"depth":104,"text":1744},{"id":1758,"depth":104,"text":1759},{"id":1767,"depth":98,"text":1768},{"id":2230,"depth":98,"text":2231},"2026-09-14","Comparing rate-limiting algorithms to protect your backend against scrapers, credential stuffing, and bot spam with minimal latency overhead.",{},"\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis",{"title":1703,"description":2306},"api-rate-limiting-redis","blog\u002Fen\u002Fapi-rate-limiting-redis",[875,879,877,878,2313],"performance","KXTsuQOwj895dkbCDObhyO_wOncRKtB2ufJbjc-AUyc",1792236100709]