The Enlightenment philosopher Gottfried Leibniz, in his 1710 work Essais de Théodicée sur la bonté de Dieu, la liberté de l'homme et l'origine du mal (Essays on the Goodness of God, the Freedom of Man and the Origin of Evil), attempted to address this problem. Leibniz took on this central question of theodicy:
If God is all-good (omnibenevolent), all-powerful (omnipotent), and all-knowing (omniscient), how do we explain the existence of suffering and injustice in the world?
Leibniz's solution casts God as a kind of "optimizer" of the collection of all possibilities. This world could have been constructed in a number of different ways. Since God is all-good and all-powerful, and since He chose this world out of all possibilities, this world must be good, even if we can't see it as such. In fact, this world must be the best of all possible worlds.
Voltaire constructed Candide partially for the purpose of entertainment, but mostly to satirize the fallacy in Leibniz's theory of optimism. Throughout the course of his saga, Voltaire juxtaposes the raw, unrepressed optimism of one character in the story with exaggerated real-world adventures of pessimism and gloom. From gruesome war to disease, sedition, and deceit, Voltaire misses none of the bad in "the best of all possible worlds."
Leibniz argued that our world was the epitome of perfection, and all evils that transpire are for the betterment and evolution of our ideal society. God, he believed, was perfect, and as Earth is the conception of God, it must maintain such sound imperfections. However Voltaire chooses to begin writing on these premises alone to further accentuate his satire, purposefully overlooking the inescapable evil that comes with the "best of all possible worlds". Leibniz was actually not suggesting our world was perfect, but rather was the best of all the worlds available to God. Voltaire disregarded this in Candide".
Dr. Pangloss is introduced as the literary symbolic representation of Leibniz's theory. Conceived early in the novella, the then-naive Candide reveres Pangloss as the supreme authority on all philosophical matters and follows his teachings without question. Candide enters the world with this very closed-minded philosophy to further exaggerate Voltaire's satirical aim, but throughout the story he experiences a fierce internal conflict between his childhood schooling and the realities of society. Voltaire also created the character of Martin, a very pessimistic antagonist acting as the devil whispering palpable cynicism into Candide's ear. As their journey progresses, Candide finds it harder and harder to support the unyielding raw optimism of Pangloss.
Sources:
Maugans, Harry. "Voltaire's Candide." Harry Maugans: Passion, Dedication, Obsession, March 30, 2006, www.harrymaugans.com/2006/03/30/voltaires-candide/
Franklin, James. "Leibniz's Solution to the Problem of Evil." Think vol. 5 (Autumn 2003), pp. 97-101, web.maths.unsw.edu.au/~jim/think.pdf.
"Best of all possible worlds." en.wikipedia.org/wiki/Best_of_all_possible_worlds.