The way the Chinese have traditionally viewed the world is a helpful lens to understand their ambitions in North Korea, as well. Simply put, the Chinese believe very strongly in their sphere of influence, and they object strenuously to anyone else interfering in that sphere. In the modern world, the sphere is quite tightly limited - Russia became antagonistic towards them following the Sino-Soviet split, India, Bangladesh and such also oppose them; Taiwan is literally their oldest enemy. Vietnam and other areas in Indochina have a deep-seated distrust of China due to the ambitions of the Imperial state, and they fought an on-again, off again war until 1990. This leaves pretty much one country in which China can exercise a legitimate sphere of influence - North Korea.
In Imperial days, China controlled several smaller nations via a series of methods, but the most instructional one is their meddlings in Mongolia during the pre-Genghis Khan years. Basically, China ensured nobody could gain dominance there, both out of a desire to defend themselves from the potential power in a Mongolian unified nation, but also to ensure there was no others influencing the region. It remained just dangerous enough that nobody else wanted to fuck with it, even though it was a clear and present danger in some circumstances. That's similar to the line China walks right now. China plans a long game, and there's no way they're going to let anyone else gain influence in North Korea - even if that means some proverbial egg on their face.