Punishment, Revenge and Deterrance
Punishment is a psychological concept. Basically, if you engage in a behavior and it results in something unpleasant, you're less likely to do that again. The motivation behind administering punishment is to reduce the future frequency of that behavior in that person. An example would be if a parent catches a kid stealing a cookie and decides to put the kid in time-out, hoping that the kid will refrain from stealing cookies in the future.
Deterrance is a bit like punishment, except that it's not the wrong-doer's behavior that they're intending to change, instead it's an onlooker's behavior. This is also a psychological concept - if you see a behavior result in a bad outcome for someone else, you're less likely to imitate them. An example would be if the parent puts the cookie-thief in time-out in full view of a younger sibling, hoping that the younger sibling will refrain from stealing cookies.
Revenge is an emotion-driven action. It's not about changing anyone's behavior, instead, you're expressing the anger and hurt that the behavior caused, trying to make yourself (or someone else) feel better by causing equivalent suffering in the perpetrator. An example would be if the kid took the last cookie, that the parent was planning to eat, and the parent got upset at being deprived a cookie and decided to put the kid in time out for that reason.
Obviously, the same action can serve all three purposes. But it doesn't always do so. For example, some actions prevent the person from being able to repeat the behavior - life imprisonment and execution are two examples. These could still deter others or serve as revenge. Sometimes people really don't mind the person's behavior but feel that it should be punished anyway, such as some of the hilarious antics that children engage in. Sometimes no one else knows what the consequence of that action is.
In addition, since both punishment and deterrance have purposes, they could fail at their purpose - maybe the person is insensitive to punishment or unable to control the behavior, or others assume they won't be caught. Psychopaths are usually insensitive to punishment, for example, and drug addicts have strong drives to do certain actions which overwhelm their fear of punishment.
Revenge is purposeless. In my opinion, purposelessly causing suffering in others is morally wrong, no matter who they are. It's wrong when the perpetrator does it, and it's wrong when someone does it back to them. So if the only reason for providing a negative consequence is revenge, then you shouldn't do it