In 993, Khitans under General Xiao Sunning invaded Korea, but retreated after truce negotiations with minister Seo Hui of Goryeo, establishing friendly relations between the two nations. In 1004, the Liao dynasty repulsed Song China. Subsequently, as part of peace negotiations, Song China to paid tribute to the Liao emperor. However, the tension between Goryeo and Liao created another war. In 1009, General Gang Jo of Goryeo led a coup against King Mokjong, killing the king and establishing military rule. In 1010, the alliance was broken and Liao attacked Korea for General Gang's treason. Also, Khitans claimed six garrison settlements east of the Yalu River, which Goryeo claimed as its territory in truce negotiations during 993. After suffering numerous and heavy casualties against the Goryeo army, the Khitans managed to defeat Gangjo, who had begun to underestimate them. Despite this defeat, the Goryeo king managed to retain his claim on the six garrison settlements. The Khitans retreated from the peninsula, with no great prize for the war, but they did not give up their hope of gaining the six garrison settlements and making Goryeo pay tribute. However, beginning in the summer of 1018, the Khitans constructed a bridge across the Yalu River.
In December 1018, 100,000 Khitan soldiers under the command of General Xiao Baiya crossed the bridge into Goryeo territory, but were met by an ambush of Goryeo soldiers. King Hyeonjong had heard the news of invasion, and ordered his troops into battle against the Khitan invaders. General Gang Gam-chan, who did not have any military experience since he was a government official, became a commander of the Goryeo army of about 208,000 men, and marched toward Yalu River. Near the Garrison Settlement of Heunghwajin, there was a small stream. General Gang ordered the stream blocked until the Khitans began to cross it, and when the Khitans were mid-way across, he ordered that the dam be destroyed so that the water would drown much of the Khitan army. The damage was great, but the Khitans did not abandon their campaign in spite of the challenges represented by defenders and by winter conditions in the northwest. They pushed through to approach Kaesong, the capital, but were defeated by a force led by General Gang Gam Chan. The Goryeo forces led a massive attack that all but annihilated the Khitan army. Barely a few thousand of the Liao troops survived the bitter defeat at Kusong. Many more were captured after surrendering along the river banks. Four years later, Koryo and the Liao dynasty reached a negotiated peace agreement and established normal relations. The Khitan never again invaded Goryeo. The surrendered Khitan troops were divided up among the provinces of Goryeo and settled in isolated and guarded communities. These prisoners were valued for their skill in hunting, butchering, skinning, and leather tanning. Over the next few centuries, they evolved into the Baekjeong class, who came to form the lowest caste of the Korean people.
Aftermath
This was the last invasion of Goryeo by the Khitan, as the two reached a peace agreement four years thereafter. Both the Liao Dynasty and Goryeo enjoyed a time of peace, and their cultures were at their height. However, as the balance of power on the Liao-Goryeo border shifted, the Jurchens, who lived around the border between the two nations, began to expand their power. Finally, in 1115, Jurchen chief Wányán Āgǔdǎ founded the Jin Dynasty in Manchuria, and began to attack the Khitans. In 1125, Jurchen troops captured the Liao king with help from the Chinese, who encouraged the Jurchens in the hope of gaining territories they lost to the Khitans before. Most Khitans fled to Central Asia, where they established the Kingdom of Western Liao. Many of them were forced to surrender to the Jurchens.