I have seen many doctors, including sleep specialists, regarding insomnia. They all pointed to one source as the reason for the sleep issues: stress. And they all wanted to put me on prescription sleeping pills. I said no to that. Sleeping pills are addictive and you have to take them for the rest of your life. As a software developer, I am used to finding and fixing the underlying problem as opposed to the quickfixes these doctors were offering me.
After much research I figured out the underlying problem, and the fix for it. The underlying problem is magnesium deficiency. As a software developer I am using my brain more intensely than most people. This is the stress the doctors are talking about. Stress depletes magnesium. The cells in our body depend on two essential minerals for normal function: Calcium and magnesium. Cells go into ON state when calcium goes in, and OFF state when calcium goes out. Calcium doesn't go out on its own: magnesium has to go in and displace the calcium. When you are low on magnesium, cells can't go into OFF state. When that happens your muscles become stiff and you need massages, and your brain can't turn off and you can't sleep. The solution is magnesium supplements. This fixed my muscle stiffness issues and my sleep issues. A special compound of magnesium called magnesium l-threonate is especially helpful for sleep because it can penetrate what is known as the "blood brain barrier".