{"record":{"id":"05b6b6e1ca07e0a8","repo":"mongodb/node-mongodb-native","slug":"selected-server-does-not-support-retryable-writes","errorCode":null,"errorMessage":"Selected server does not support retryable writes","messagePattern":"Selected server does not support retryable writes","errorType":"exception","errorClass":"MongoUnexpectedServerResponseError","httpStatus":null,"severity":"error","filePath":"src/operations/execute_operation.ts","lineNumber":360,"sourceCode":"        (operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError) &&\n          topology.s.options.enableOverloadRetargeting)\n      ) {\n        deprioritizedServers.add(server.description);\n      }\n\n      server = await topology.selectServer(selector, {\n        session,\n        operationName: operation.commandName,\n        deprioritizedServers,\n        signal: operation.options.signal\n      });\n\n      if (\n        hasWriteAspect &&\n        !supportsRetryableWrites(server) &&\n        !operationError.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)\n      ) {\n        throw new MongoUnexpectedServerResponseError(\n          'Selected server does not support retryable writes'\n        );\n      }\n\n      // Batched operations must reset the batch before retry,\n      // otherwise building a command will build the _next_ batch, not the current batch.\n      if (operation.hasAspect(Aspect.COMMAND_BATCHING)) {\n        operation.resetBatch();\n      }\n    }\n  }\n\n  throw (\n    error ??\n    new MongoRuntimeError(\n      'Should never happen: operation execution loop terminated but no error was recorded.'\n    )\n  );","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/operations/execute_operation.ts#L342-L378","documentation":"Thrown as a MongoUnexpectedServerResponseError during the retry loop after a new server is selected for a retryable write, when supportsRetryableWrites(server) returns false for the newly selected server. supportsRetryableWrites is false for standalone servers or servers lacking a logicalSessionTimeoutMinutes (no sessions support). This indicates the topology shifted under retry to a node that cannot honor retryable writes.","triggerScenarios":"A retryable write fails with a retryable error (e.g. network error), the retry selects a different server (failover), and that server is a standalone or otherwise lacks retryable-writes support; the guard at execute_operation.ts:360 then fires. Also possible in mixed topologies where a node description reports no session support.","commonSituations":"Failover from a replica set to a standalone member; misconfigured sharded cluster with a standalone mongos-less node; topology description lagging behind reality; connecting to a single standalone while retryWrites defaults on.","solutions":["Disable retryWrites (retryWrites=false) if the deployment genuinely cannot support it.","Ensure all members of the deployment are part of a replica set or sharded cluster with sessions enabled.","Check that no standalone mongod is reachable at the configured addresses.","Verify the server version supports sessions (3.6+) and that logicalSessionTimeoutMinutes is reported."],"exampleFix":"// before\nconst uri = 'mongodb://host:27017/'; // standalone, retryWrites default\n\n// after\nconst uri = 'mongodb://host:27017/?retryWrites=false';","handlingStrategy":"try-catch","validationCode":"import { MongoClient } from 'mongodb';\n\nconst client = new MongoClient(uri);\nawait client.connect();\n// If the deployment may contain standalones, disable retryWrites up front\nif (!client.topology?.description) {\n  // unable to determine; be conservative\n  throw new Error('Could not verify topology supports retryable writes');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await collection.insertOne({ a: 1 });\n} catch (err) {\n  if (err instanceof MongoUnexpectedServerResponseError && /retryable writes/i.test(err.message)) {\n    // failover selected an unsupported server; retry once with retryWrites off or report\n  } else throw err;\n}","preventionTips":["Keep all cluster members at versions supporting sessions (3.6+).","Avoid mixing standalone and replica-set members in one connection target.","Consider disabling retryWrites if the topology cannot guarantee support across failovers."],"tags":["retryable-writes","failover","server-selection","topology","sessions"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}