When and why is violence necessary in a good piece of fiction?
The tougher answer is this: You need to write violence into your stories when you character would be violent or when your plot needs something violent to happen. Just how much and how graphic that violence becomes depends on the internal rules you've established for your story. A cozy mystery can be filled with murders but also with little to no blood. A street-wise thriller can -- on the other hand -- show you every bullet hole and each tendon as it is ripped by knives.
Or, if you need to move to shock and awe tactics as an author, you can introduce more graphic violence into a traditional cozy.
There's no science to this. It's completely an art, and you learn by doing it. Over and over again.