21 small fish in a 30 gallon tank should not be spiking nitrates to 40 either and you should not have to be doing water changes 3 times a week. Sounds like an overfeeding problem to me.

Nitrates can also raise if the filter is too dirty and needs a rinsing. You didn't mention the filter so I would also wonder if enough oxygen is getting to the fish.