Turkmenchay treaty has been signed as a result of 2nd Russia-Iran war between Russia and Iran in Turkmenchay village on 10 February 1828. Signing ceremony was attended by famous Russian writer A.S.Griboyedov and Azerbaijan's progressive figure A.A.Bakikhanov, who was interpreting at the signing ceremony. Initial treaty on dividing Azerbaijan between Russia and Iran was signed in 1813 and is called Gulustan treaty. (info taken from Wikipedia in Azeri)
As it is more or less obvious by now this was the only way to exist for small and rich Azerbaijan...its territory was divided into khanlıqs (territories ruled by Khans) and each khan preferred to be on its own, pseudo-independent, rather than unite with any other local khan...neighbours were big and strong...and Azerbaijan with Caspian Sea shore, no general goal and strategy, rich natural resources...no army.
Currently in ex Soviet Republic, Azerbaijan, reside around 8 million 700 people...I am not sure about number of Azerbaijanis in Iran, some data say around 20 million.