This problem was solved at least as far back as ancient Rome. The solution was and is the secret ballot. If nobody gets to see your ballot before it goes into the box, and if it can't be tied back to you, nobody can hold you to a vote. Thus, even if someone threatens your life, bribes you, etc, the secret ballot preserves your ability to vote your conscience.
https://en.wikipedia.org/wiki/Secret_ballot#Ancient