Aside from its distinctive flavor, coconut milk bears a remarkable resemblance to conventional dairy milk. It has a similar mixture of liquids, fat and protein, and it even separates into thinner "milk" and thicker "cream" if left to sit for a time. Unfortunately, like dairy milk, coconut milk can also curdle as it cooks. The grainy lumps don't alter the taste of your dish but do mar its appearance, so curdling is usually avoided if possible.
Milking a Coconut
Coconut milk is made by peeling and shredding the nut's meat, and then steeping it in very hot water. The water dissolves the coconut's oil and extracts proteins and a variety of other solids from the meat, keeping them together in a thick suspension. When the tough, fibrous residue is strained from the mixture, what's left is the coconut milk. The coconut's oil wouldn't ordinarily be able to bond with water, because water and fats repel each other, but the protein and other solids play matchmaker and help them remain bound to each other.
Unraveling In the Heat
When you heat the coconut milk in your favorite sauce or soup, the heat upsets this delicate balance. Ordinarily the protein molecules in the coconut are relatively compact, but as they're heated, they begin to unwind and take up more space. For comparison purposes, visualize walking through a crowded room with your arms at your sides and then doing the same with your arms spread out. The unwound or "denatured" proteins are more likely to collide with each other and to form molecular bonds when they do. If enough of them cluster together, they'll form lumps and curdle your sauce.
Things to Avoid
You can do a few basic but important things to minimize the risk of curdling. First and most importantly, simmer your coconut milk gently rather than boiling it vigorously. The harder it boils, the more the proteins will be thrown together and the more likely it is that they'll clump into curds. Whenever possible, cook your coconut milk without a lid over the pot. Under a lid, it could easily boil and curdle before you have an opportunity to react. If your recipe calls for acidic ingredients such as lemon or lime juice, add them last. The acidity actively denatures your coconut milk's proteins, making it much more likely the milk will curdle.
Things to Do
Aside from avoiding factors that contribute to curdling, you can take positive steps to discourage the formation of curds. The simplest is just to keep stirring. Stirring the milk frequently breaks up bonds between the protein molecules even as they form, helping minimize the risk of curdling. A more potent option is to mix a small amount of cornstarch into the cold coconut milk before heating it. As the coconut milk comes to a simmer, the cornstarch thickens and stabilizes it. The thickened liquid makes it difficult for the protein molecules to encounter and bind with each other.