Deterrence's purpose in punishment is to prevent future criminal activity by virtue of the unpleasantness of crime's consequences. While it bears some resemblance to retribution, deterrence is a purpose with measurable utility, and would seem to have different origins than retribution. If deterrence seeks to injure the criminal offender, it is primarily with the aim of impressing him or her with the undesirability of a life of crime compared to a law-abiding existence. Criminology distinguishes two kinds of deterrence, individual and general.
Individual deterrence aims to convince the criminal that repeating his or her crime - or any crime, really - is not worth the risk of repeating the punishment, let alone incurring a stiffer one. The inconvenience, discomfort, humiliation and pain of punishments are carefully deliberated on by legislators and judiciaries to match the crime and, perhaps more importantly in this case, the criminal. Even after committing exactly the same crime, two men may require vastly different levels of severity in their sentence to deter them from future offenses. It's obviously important that an offender's sentence not be too light, or else it will not deter future crime. But it's also crucial not to impose sentences that are too severe, or punishment may fail to deter and actually cause more harm than good.
While deterrence can be in good harmony with retribution, it has the potential to interfere with rehabilitation. A sentence should be miserable enough to convince people that they don't want to repeat the experience, but problems can arise when punishment is so severe that meaningful rehabilitation becomes impossible. It's a very difficult balance to strike, and failure can potentially create hardened criminals where before there were only criminal offenders.
To illustrate, excessive fines, lengthy incarceration, wretched prison conditions and harsh treatment by guards can all serve the purposes of deterrence. But these conditions also have the potential to encourage people to think of themselves as something separate from and opposed to the society that's punishing them, something that society might even regard as somewhat less than human, rather than as a citizens who's actions have incurred a debt to society which must be repaid before being permitted to rejoin. The later attitude has the potential to inspire reformation. The former is the mindset of a criminal. As long as a person thinks of himself or herself as a criminal at heart it can be argued that rehabilitation has failed. Furthermore, criminals in general tend to commit crimes no matter how unpleasant the punishments are. If punishment produces criminals where before there were none then it not only fails to deter but actually encourages crime.
Of course this is no argument for abolishing unpleasantness from punishment. Imagine if prisons were replaced by a sort of friendly, sunny rehab center, tax-payer-supported, in which burglers, rapists and murderes are counciled and encouraged to think of themselves as potentially productive members of society. Who would try to avoid that? It is important, however, to appreciate the complexities and contradictions inherent in our society's attempts to successfully address criminal activity.