Amor fati and the eternal recurrence
Nietzsche may have encountered the idea of the Eternal Recurrence in the works of Heinrich Heine, who speculated that one day a person would be born with the same thought-processes as himself, and that the same applied to every other individual. Nietzsche expanded on this thought to form his theory, which he put forth in The Gay Science and developed in Thus Spoke Zarathustra. Schopenhauer directly influenced this theory. Schopenhauer postulated that a person who unconditionally affirms life would do so even if everything that has happened were to happen again repeatedly.
Nietzsche's view on eternal return is similar to that of Hume: "the idea that an eternal recurrence of blind, meaningless variation—chaotic, pointless shuffling of matter and law—would inevitably spew up worlds whose evolution through time would yield the apparently meaningful stories of our lives. This idea of eternal recurrence became a cornerstone of his nihilism, and thus part of the foundation of what became existentialism." Nietzsche was so impressed by this idea, that he at first thought he had discovered a new scientific proof of the greatest importance, referring to it as the "most scientific of hypotheses". He gradually backed-off of this view, and in later works referred to it as a thought-experiment. "Nietzsche viewed his argument for eternal recurrence as a proof of the absurdity or meaninglessness of life, a proof that no meaning was given to the universe from on high."
What if a demon were to creep after you one day or night, in your loneliest loneness, and say: "This life which you live and have lived, must be lived again by you, and innumerable times more. And mere will be nothing new in it, but every pain and every joy and every thought and every sigh—everything unspeakably small and great in your life—must come again to you, and in the same sequence and series. . . . The eternal hourglass will again and again be turned—and you with it, dust of dust!" Would you not throw yourself down and curse the demon who spoke to you thus? Or have you once experienced a tremendous moment, in which you would answer him: "Thou art a god, and never have I heard anything more divine!" [The Gay Science (1882), p. 341 (passage translated in Danto 1965, p. 210).]