This is a question discussed in every law school course on professional responsibility. And informally among law students and young lawyers.
Most criminal defense attorneys, I think, have made peace with this scenario. They say that their job is to serve justice in gross, not necessarily in each case. It's important to criminal justice for every person to have access to a (ideally) high-quality legal defense. Making the prosecution work hard to prove its case is considered a feature, not a bug. It's (hopefully) a protection against skullduggery by the prosecutors and the police. It's true that we don't always get equal justice, nor do prosecutors and police always follow the law or go after the right people for the right reasons. On the other hand, we have a bunch of legal protections built into our system that aren't necessarily easy to notice because they are so common -- but they are there because otherwise criminal attorneys will have a field day.
For instance, forensics labs are pretty high quality, from what I understand. Yes, there are high profile fuckups (wasn't there one in NC, where an employee of the SBI was mucking with tests?) but for the most part, forensic analysis is pretty good and technically sound (there are exceptions, of course). Forensic testing, for instance, frequently rules out suspects who might otherwise be hounded or prosecuted. We also have Brady requirements, which are honored most of the time. The Fourth Amendment also offers protections.
Arguably, none of these protections would exist without criminal defense attorneys who will pound on every deficiency. Without criminal defense attorneys bringing Fourth Am claims, the courts wouldn't be able to enforce the Fourth Am and police departments would be able to run wild with searches. If there are fuckups in forensic labs, lots of convicted people could go free -- with defense attorneys, of course -- which creates pressure on state agencies to make sure their forensic labs are of sufficient quality.
So yeah, it sucks to represent a monstrous client. I don't do criminal defense work, so I wouldn't know (though I had to represent some unsavory characters in appellate practice at Jones Day), but the argument that the defense attorney is serving the system as much as the client isn't wrong, in my view. I'm not sure just how convincing it is, but it's valid. My impression of mob lawyers is that they aren't very good people (see, e.g., Roy Cohn), but probably the majority of criminal defense attorneys are good people who don't see their job as helping guilty people avoid justice.
Since we have a right not to self-incriminate, there is no way for the justice system to deny representation to the most heinous of criminals.