Implement the following plan: Metricstore NATS Slow Consumer Analysis & Improvements…
- User
Implement the following plan:
Metricstore NATS Slow Consumer Analysis & Improvements
Context
The metricstore NATS subscription triggers "slow consumer, messages dropped" warnings under load. The root cause is that the NATS message callback blocks for too long, causing the nats.go client to detect a slow consumer and drop messages from its internal pending buffer.
Root Cause Analysis
There are three compounding bottlenecks in the ingestion path:
1. Single-threaded inline decoding (primary cause)
metricstore.go:187callsReceiveNats(ms, 1, ctx)— workers=1- With workers=1,
DecodeLine()runs directly inside the NATS callback (lineprotocol.go:82-87) - The NATS client cannot deliver the next message until the callback returns
- Each
DecodeLinecall parses line protocol, acquires locks on the level tree, and may block on the WAL channel
2. Blocking WAL channel send
lineprotocol.go:351:WALMessages <- &WALMessage{...}is a blocking send- The
WALMessageschannel has a buffer of only 4096 (walCheckpoint.go:85) - If the single WALStaging goroutine can't drain fast enough (file I/O), this blocks
DecodeLine, which blocks the NATS callback - Under sustained load, the WAL channel fills up and becomes the dominant bottleneck
3. Single WALStaging goroutine with per-message I/O
WALStaging()(walCheckpoint.go:118-234) runs as a single goroutine- It processes messages sequentially: open/lookup file handle → serialize → write via bufio
- While bufio batches actual syscalls, the goroutine still handles one message at a time
- File handle lookups (map access) and potential
MkdirAllcalls add latency
How nats.go detects slow consumers
The nats.go library maintains an internal per-subscription message queue. When the callback blocks, messages accumulate. Once the pending count/bytes exceed limits (default: 65536 messages / 64MB), it drops messages and reports "slow consumer".
Proposed Improvements
Change 1: Increase worker count and channel buffer
File:
pkg/metricstore/metricstore.go:187andpkg/metricstore/lineprotocol.go:56 Assistant
Let me read the files that need modification.
Assistant
Let me check the config struct to understand
Keys.NumWorkers.Assistant
Now I have all the context. Let me also check for a
walDroppedcounter or similar logging pattern to understand if there's existing drop tracking.Assistant
Good, no existing drop tracking. Now let me implement all four changes.
Change 1: Use
Keys.NumWorkersand increase channel buffer inReceiveNats.Assistant
Change 2: Increase WAL channel buffer.
Assistant
Change 3: Non-blocking WAL send with overflow logging.
Assistant
Now add the dropped counter and replace the blocking WAL send:
Assistant
Now replace the blocking WAL send with non-blocking:
Assistant
Change 4: Batch WAL drain in
WALStaging. Replace the main loop to drain multiple messages per iteration:Assistant
Now let me verify it compiles and tests pass:
Assistant
All compiles and tests pass. Here's a summary of the four changes:
Changes made
-
metricstore.go:187— ChangedReceiveNats(ms, 1, ctx)toReceiveNats(ms, Keys.NumWorkers, ctx). This parallelizesDecodeLine()across multiple goroutines instead of running it inline in the NATS callback. -
lineprotocol.go:56— Increased themsgschannel buffer fromworkers*2tomax(workers*256, 8192). This decouples the NATS callback from the decode workers with a much larger buffer. -
lineprotocol.go:353-368— Made the WAL channel send non-blocking withselect/default. Dropped messages are counted via an atomic counter and logged every 10,000 drops. Since the in-memory write succeeds first, dropped WAL messages are recovered by the next binary snapshot. -
walCheckpoint.go:219-247— Added batch drain loop inWALStaging: after processing one message, it drains up to 256 more without blocking, then flushes all buffered writers. This reducesselectoverhead and batches I/O.
-
- User
Does a similar problem occur on the metricstore REST API path?
Assistant
No, the REST path does not have the same problem.
Looking at
internal/api/metricstore.go:91-108, the RESTwriteMetricshandler callsDecodeLine()synchronously within the HTTP request goroutine. Each HTTP request gets its own goroutine from Go'snet/httpserver, so:- No single-threaded bottleneck — Go's HTTP server spawns a goroutine per request, so multiple concurrent POST requests to
/api/write/decode in parallel naturally. - No callback blocking — There's no subscription callback to block. If
DecodeLineis slow, it only delays that one HTTP response, which is fine. - WAL channel send still applies — The blocking WAL send (now fixed to non-blocking) could have stalled an HTTP handler goroutine, but it wouldn't cause message drops — it would just increase HTTP response latency. With the non-blocking change, this is no longer an issue either.
The slow consumer problem is specific to the NATS ingestion path because the nats.go client uses a single callback delivery model with an internal pending buffer that drops messages when the callback can't keep up.
- No single-threaded bottleneck — Go's HTTP server spawns a goroutine per request, so multiple concurrent POST requests to
- User
How many os level threads are used in this application? It is currently configurable?
Assistant
No,
GOMAXPROCSis never set explicitly in this application. It uses the Go default, which equals the number of logical CPUs visible to the process (i.e.,runtime.NumCPU()). It is not configurable viaconfig.jsonor any flag.To clarify the distinction:
- OS threads (
GOMAXPROCS): Controls how many goroutines can execute simultaneously on OS threads. Defaults toruntime.NumCPU(). Not set or configurable anywhere in cc-backend. NumWorkers(config.go:124): Controls the number of goroutines for checkpoint/archive worker pools. Defaults tomin(NumCPU/2+1, 10). Configurable via"num-workers"inconfig.json. After our change, this also controls the NATS decode worker count.
So on a 16-core machine you'd get
GOMAXPROCS=16(up to 16 OS threads running goroutines) andNumWorkers=9(9 goroutines in each worker pool). The goroutines are multiplexed onto the OS threads by the Go scheduler — they're not 1:1.Would you like to make
GOMAXPROCSconfigurable, or is the current default sufficient?- OS threads (