For the last several years, the United States has been gripped in a sharp debate over the flow of immigrants into our country. It is easy to get demoralized about the inability to reach a consensus, but understanding the complexity of the challenge could help us appreciate what stands in the way of reform — and what needs to happen before change can occur.

And make no mistake: We must resolve these issues if we are to experience a virtuous cycle of greater openness, wealth and human development, rather than falling back into a vicious cycle that leads the world into greater anarchy, poverty, disorder and war.

Every day, tens of millions of people cross borders, adding up to roughly 2 billion annually. Managing those flows is a huge challenge for nation-states, whose governments must make choices about who can enter — and exit — and on what terms. This is where politics enters the equation.

To reap the benefits of immigration, such as new sources of human capital and labor, nation-states must accept the long-term costs of social integration, the short-term fiscal burdens of concentrated immigrant populations in some regions and localities, and the security costs that come with living in an age of drug cartels and domestic and international terrorism. The strong feelings on all sides of those issues make it painstakingly hard to create actual policies.

Liberal democracies also must contend with the rights of migrants, including their legalization, naturalization and citizenship. It is one thing to need foreign labor, but is it right to admit foreign workers without granting them protections against exploitation?

This is another place where so much of our nation’s debate has stalled, especially when it comes to determining whether unauthorized immigrants should have a shot at legalization. Until we find a way to legalize their status, we risk undermining a social contract that extends rights in return for labor and long-term residence.

The United States is not the only free society that faces this dilemma. The economic need for openness versus the political and legal pressures for a closed society are what I call the “liberal paradox.” Dynamic economies need immigrant labor, and open societies are stronger than closed societies. But openness comes with a price. We must be willing to grant foreign workers and their families a basic package of human and civil rights that enables them to flourish, settle and become full members of our society.

The policy game becomes infinitely more complex when a country feels threatened, physically or culturally. In times of war and political crisis, a focus on markets and rights gives way to a concern about culture and security. This is what has happened in the first year of the Trump administration and in recent years in Europe.

President Trump’s immigration and refugee policy, for example, is couched in cultural, ethnic and civilizational terms. Christians and Jews are pitted against Muslims, and Mexicans and Hispanics are pitted against whites. Symbolic and racial politics allow him to shore up elements of his political base, but the president has created a perfect storm of opposition to his policies at the international, national, state and local levels.

The immigration policy game, which Tip O’Neill once likened to “political death,” involves tradeoffs between markets, rights, culture and security. All these factors combine to make immigration reform difficult, but pressures to modernize immigration systems are only going to mount. That’s because international migration will likely increase in coming decades.

A cataclysmic international event, like a war or depression, could change that forecast, but even after 9/11 and the Great Recession, the United States still admits more than 1 million immigrants annually. And despite Europe’s debt crisis, 2.4 million people immigrated to the EU in 2015 from non-EU countries. Growing demand for basic manpower and fierce competition for the highly skilled worker, when coupled with a demographic decline in the receiving countries, have created economic opportunities for migrants.

The extension of rights to migrants also has become an important part of the immigration story. If a migrant can establish some claim to residence in a liberal state, his or her chances of being able to remain and settle will increase. Witness Central American children arriving at the U.S./Mexico border, surrendering to border control agents, and immediately requesting asylum.

How well powerful nations like the United States and Germany, regional organizations like the European Union, and the international community manage migration will determine whether the world remains open and prosperous or falls back into poverty and war. As states struggle to meet this challenge, which remains a fundamental feature of our world, they must keep balancing security, economic, legal and foreign policy interests. They are likely to continue to encounter a liberal paradox as they do.