In February 1979, Communist China invaded Communist Vietnam, baffling western audiences who had been told during the Vietnam War that the two were close allies. The Chinese stomped around northern Vietnam for a month, declared that the road to the capital, Hanoi, was open, but then left. The Chinese leadership said their actions were designed to "teach a lesson" to the Vietnamese leadership, and they proceeded to reinforce that lesson throughout the subsequent decade by frequently triggering large brawls on the China-Vietnam border, seemingly at random. When the Soviet Union collapsed and the Cold War ended, the whole thing drew to a close. This was not a coincidence, for the whole conflict had roots in aspects of the Cold War not commonly understood at the time or remembered since.
When the French finally left Vietnam in 1954, they left it partitioned in two - Communist North Vietnam and South Vietnam, ideology unspecified. North Vietnam and sympathetic individuals in the South planned eventually to overthrow the South's government and unify the whole country under a Communist regime, but first the Communists focused on consolidating their own rule in the North by carrying out basic Communist chores like collectivizing land, shooting landlords, spreading their ideology among the peasants, and the like. In doing so they relied heavily on the Chinese for cash, military equipment and technical expertise - North Vietnam almost certainly would have failed without Chinese help in those early days, just like the South would have failed without U.S. help.
This was an uncomfortable situation for North Vietnam, whose leadership were not only Communist but strongly nationalist as well. They needed the Chinese but they didn't want to admit it or to give too much away to them, a bit like Europe and the United States today. Vietnam's relation to China is psychologically difficult for the Vietnamese, as their culture is an offshoot of Chinese traditions but the Chinese have a history of dominating Vietnam and looking down on its inhabitants. This has, no doubt, been one motivation for the predilection that the Vietnamese have shown in turn for dominating their own neighbours and annexing territory, a process known as Nam Tiến. These national subtleties ultimately overrode the shared Communism of the countries in ways that western analysts didn't always perceive as they focused on what they thought was a unified Communist threat.
China supported North Vietnam throughout its attempts to take over South Vietnam with enormous quantities of military and economic aid. Chinese anti-air artillery units frequently attacked U.S. planes over North Vietnam, and Chinese troops guarded the country so that North Vietnam's own army could carry on the tradition of Nam Tiến. This reliance on China only deepened North Vietnam's psychological problems, and state media there began to portray China as a potential enemy which was not to be trusted and whose influence needed to be watched carefully in case they tried to take over the country. The Chinese were understandably annoyed after everything they had done for North Vietnam, and the relationship began to sour. To compensate for increasing Chinese hostility, the North Vietnamese turned to the Soviet Union for help.
This was even more worrying for the Chinese, whose hostility towards the Soviet Union had been growing for years as they fought for the mantle of leadership in the Communist world and argued over ideology and economics. In 1969, the two countries fought a huge battle on their border which raised the spectre of nuclear war between two Communist countries. For the Vietnamese to turn to the enemy after they had united their country, as they did in 1978 by signing a 25-year mutual defence treaty with the Soviets, was just too much for the Chinese to swallow. It looked like they had taken Chinese help when they needed it andt then turned their back. When Vietnam occupied Cambodia in 1978 to unseat the murderous Khmer Rouge, China was faced with the prospect of an erstwhile ally on their southern border, backed by their worst enemy, annexing its neighbours. Both their pride and their interests were hurt, and they snapped. This explains the bizarre war aim to "teach a lesson" to the Vietnamese.
It doesn't seem the Chinese ever intended to take over or annex Vietnam, although the Communists in Hanoi may have believed that they did. Instead tens of thousands of their troops poured over the border and occupied several provincial towns, trying to engage and destroy Vietnamese military units with human wave attacks and destroy their infrastructure. After wrecking havoc in the provinces for a few weeks, they taunted the Vietnamese by saying that they could take Hanoi if they wanted, and then left. Message sent, you'd think.
Or not. The Chinese army actually performed poorly in the conflict as they fought North Vietnamese troops and guerrillas who had decades of experience fighting other Vietnamese, the French, and the Americans. A Chinese general wrote dismissively after the war about how easy it had been to destroy entrenched Vietnamese positions, but this reflected how thoughtlessly he was willing to expend his men's lives in massive human wave attacks rather than the fighting ability of the Vietnamese. That was not a sustainable way to win a war. The Vietnamese then quickly melted into the countryside, forcing the Chinese to resort to huge search and destroy operations which were reminiscent of futile American tactics during their own intervention in Vietnam. The Chinese left Vietnam boasting but severely wounded, and they hadn't managed to dispel their image as a paper tiger.
Not that the Soviet Union came out of the war smelling of roses either, as they completely failed to invoke their mutual defence treaty with Vietnam and attack China in response. Fearing just such an eventuality, the Chinese had posted over a million troops on high alert near the Soviet border, and the Soviets backed down. As a result, they also emerged from this war with their reputation as an ally diminished. America fought its own war to defend South Vietnam - and delayed ending the war for so long - largely because it wanted to show that American words on treaties meant something. Communist countries now had reason to doubt what value exactly they could place on Soviet words.
Not that this stopped Vietnam deepening its relationship with the Soviet Union. Later in 1979, the Vietnamese leased the American-built naval base at Cam Ranh Bay to the Soviets, giving them an important foothold in the Pacific - exactly where the Chinese didn't want them. Nor did Vietnam withdraw from Cambodia, as the Chinese wished. Relations between China and Vietnam remained terrible all throughout the 1980s, with the two countries frequently fighting border conflicts which were mostly initiated by China. When the Soviet Union eventually collapsed and Vietnam pulled out of Cambodia, the main irritants in the relationship were removed and China warmed slightly to its little brother once more. But with trouble brewing in the South China Sea over control of its vast energy deposits, which Vietnam and China both claim a right to, Beijing must be hoping they don't have to face the Vietnamese army again.