"Trust in the Lord and do good." God said to David, "If you see me doing good to the wicked, don't be so upset. Keep doing good and there is no good except for repentance, as it is said, 'Take words with you and return to the Lord' (Hosea 14:3). What if the wicked, who are provoking me and are of no use to me, I did good to them?
With you, who trust in me, engage in Torah study, perform justice and charity, the verse testifies about you, as it says, 'David acted justly and righteously' (2 Samuel 8:15). How much more so in your case, trust in the Lord and do good. Why was David compared to a worker who worked all his days for the king and was not paid his wages? This worker was distressed and said, "Perhaps I will not get anything for my labor."
After some time, the king hired another worker who worked with him for only one day, fed him and gave him his wages. The first worker said, "What is this? He worked only one day and got paid, whereas I worked with the king all my days and got nothing!" He began to rejoice in his heart.
Similarly, David said, "You have put joy in my heart more than when their grain and new wine abound" (Psalms 4:8). When did joy come to my heart? When I saw what you did to the wicked, more than when their grain and new wine abounded. Rabbi Elazar says, "From the suffering of the wicked in this world, you can infer how great the reward of the righteous will be in the world to come."
How much more so if the wicked are like this, the righteous are even more so. This is what is written, "How great is your goodness, which you have stored up for those who fear you" (Psalms 31:20).