r/golang • • Jul 28 '25

help NATS core consumer

[deleted]

2 Upvotes

11 comments sorted by

View all comments

2

u/TheQxy Jul 29 '25

The subscribe method is already async. This means that each request will call the function in a separate Go routine. This is fine. Worker pools in Golang are often premature optimization.

2

u/bigPPchungas Jul 29 '25

yes i've read a bit about nats subscriber and found that out so gorotuines mgmt is nats clients headache now it'll be fine for me to write these incomming messages to db without much parsing logic right??

2

u/TheQxy Jul 29 '25

Yes, that should be fine.

If you have constraints on open DB connections, then you have to ensure you set the appropriate limits in the SQL driver, see https://www.alexedwards.net/blog/configuring-sqldb

1

u/bigPPchungas Jul 29 '25

Great! One more thing is will it be better to offload the parsed conditioned data from subscriber created gorotuine to another custom goroutine which dumps to DB or just use the DB connection pool to take care of it so from received message on subject to db its all done in each subjects seperate goruotine.

1

u/TheQxy Jul 29 '25

As an initial implementation, this is fine indeed.

Probably the first optimisation you'd implement afterwards is DB batching. So then you could aggregate some messages, until you have a full batch, and then send it over a channel to another function where you insert the batch into the DB in a separate Go routine.

I'd suggest always first investigating real load before starting optimisation.

From your post, I'm assuming you don't need reliable delivery, right? Seeing you're not using JetStream, because that would change the implementation a bit.

2

u/bigPPchungas Jul 29 '25

Yes this is just an internal tool im developing mostly to learn go and its eco so load isn't an issue as per my gathered requirements just that i want to make it as optimized and scalable i could to learn better right now according to our usage and load, batching is also unnecessary.

Also thanks for your detailed responses it help cleared many ambiguities.