{"record":{"id":"7b31a5e2d645ad50","repo":"nats-io/nats-server","slug":"last-sequence-mismatch","errorCode":null,"errorMessage":"last sequence mismatch","messagePattern":"last sequence mismatch","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/stream.go","lineNumber":6373,"sourceCode":"\thdr = removeHeaderStatusIfPresent(hdr)\n\tif mt, traceOnly := c.isMsgTraceEnabled(); mt != nil {\n\t\t// If message is delivered, we need to disable the message trace headers\n\t\t// to prevent a trace event to be generated when a stored message\n\t\t// is delivered to a consumer and routed.\n\t\tif !traceOnly {\n\t\t\thdr = setHeader(MsgTraceDest, MsgTraceDestDisabled, hdr)\n\t\t}\n\t\t// This will add the jetstream event while in the client read loop.\n\t\t// Since the event will be updated in a different go routine, the\n\t\t// tracing object will have a separate reference to the JS trace\n\t\t// object.\n\t\tmt.addJetStreamEvent(mset.name())\n\t}\n\tmset.queueInbound(mset.msgs, subject, reply, hdr, msg, nil, c.pa.trace)\n}\n\nvar (\n\terrLastSeqMismatch   = errors.New(\"last sequence mismatch\")\n\terrMsgIdDuplicate    = errors.New(\"msgid is duplicate\")\n\terrStreamClosed      = errors.New(\"stream closed\")\n\terrInvalidMsgHandler = errors.New(\"undefined message handler\")\n\terrStreamMismatch    = errors.New(\"expected stream does not match\")\n\terrMsgTTLDisabled    = errors.New(\"message TTL disabled\")\n)\n\n// processJetStreamMsg is where we try to actually process the stream msg.\n// needIsolation should be false only if the caller already holds isolateMu\n// across a whole atomic batch; mset.mu must NOT be held by the caller.\nfunc (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, lseq uint64, ts int64, mt *msgTrace, sourced bool, needIsolation bool) error {\n\treturn mset.processJetStreamMsgWithBatch(subject, reply, hdr, msg, lseq, ts, mt, sourced, needIsolation, nil)\n}\n\nfunc (mset *stream) processJetStreamMsgWithBatch(subject, reply string, hdr, msg []byte, lseq uint64, ts int64, mt *msgTrace, sourced bool, needIsolation bool, fastBatch *FastBatch) (retErr error) {\n\tif mt != nil {\n\t\t// Only the leader/standalone will have mt!=nil. On exit, send the\n\t\t// message trace event.","sourceCodeStart":6355,"sourceCodeEnd":6391,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/stream.go#L6355-L6391","documentation":"errLastSeqMismatch: a publish carried an expected last sequence (Nats-Expected-Last-Sequence header) that does not equal the stream's current last sequence — an optimistic concurrency check. In clustered catch-up code the same sentinel indicates raft stream divergence and triggers the cluster-reset recovery path.","triggerScenarios":"Thrown at server/stream.go:6373 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fetch the current stream state and retry with the correct expected sequence","Use Nats-Expected-Last-Subject-Sequence if only per-subject ordering matters","For cluster occurrences, ensure raft/meta health and let the server reset or catch up the stream"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}