Centralized exchanges, both in traditional equity markets and in crypto, operate by means of the order book model. That means that a buyer or seller posts an order, and then this order is executed when someone else fills it with a corresponding buy/sell order. This works well only because centralized exchanges use market makers, i.e. entities that agree to always buy or sell at a certain price (and they make money due to the difference between the buy and sell price). Without market makers, an exchange wouldn’t be liquid enough, because it would take a very long time for an order to be filled.
Decentralized exchanges, on the other hand, need to work differently. Implementing an order book model on most blockchains (e.g. Ethereum in its current state) would be impractical, as every transaction costs a gas fee, and an order book exchange relies on the ability to place a large number of orders (many of which don’t end up being filled). It's worth nothing that there are order-book DEXes that are being developed on some blockchains that have a higher throughput (most notably Solana), but these work in much the same way as centralized exchanges in terms of the user experience, except that users trade directly from their wallets. Here, we'll focus on the most popular DEXes, especially on Ethereum and Binance Smart Chain, which don't use order books.
Because DEXes can’t make use of the order book model in a practical way, they rely on liquidity pools to provide a liquid market for a given pair. The way this works is that, in order to create a market for a given pair, the two assets are deposited into the protocol at a specific ratio (most commonly with the value of the assets being equal). Then, any user can trade the pair directly from the pool, by swapping one asset for the other.
Unlike the order book model, the price isn’t determined by bid and ask orders, but rather automatically responds to supply and demand via an automated market maker (AMM). The simplest AMM is the constant product model, which is used by Uniswap, and which regulates the price by keeping the product of the amounts of the two assets constant. Therefore, when there is heavy demand for one asset, its price automatically increases in order to make sure the product remains the same.
Furthermore, the price always reflects the prices on other exchanges because of arbitrage: as soon as there is a discrepancy, arbitrageurs (often automated programs) will step in and make a profit on the difference, driving the price to the prevailing market level.
The trading fees determined by the protocol, rather than being collected by a centralized exchange, are left in the liquidity pool and distributed to the liquidity providers according to the share of liquidity that they provide. This means that providing liquidity can be a decent way to make a passive income in DeFi.
To take a specific example of this based on how Uniswap works: if ETH costs 1000 DAI, then anyone can provide liquidity to the pool by providing the two assets in a 1:1000 ratio. In return, the liquidity provider is given LP (liquidity providing) tokens, which represent their share of the liquidity pool. When they withdraw the underlying ETH and DAI in exchange for the LP tokens, the assets will also include a proportional share of the fees that have been accumulated.
This process isn’t free from risk, however, as providing liquidity always incurs the risk of impermanent loss. What this means is that, if the price of the assets changes, liquidity providers can end up with less money compared to just holding the underlying assets. This is shown on the chart above.
Now that you know how DEXes work, how do you use them in practice? To start with, you need an Ethereum wallet such as MetaMask (in the case of Ethereum DEXes, which we’re taking as an example here; the process is more or less the same on other chains as well) with some funds on it. Then, open the exchange dApp and connect your wallet (this simply enables the DEX to see your public address and doesn’t incur a gas fee).
When you find the pair you want to trade, type in the amount and swap, and then you will then have to confirm the transaction in your wallet. Be careful with the gas fee, though: if it’s high, you can consider setting it manually after checking the current average gas price on Etherscan or another similar website. Bear in mind that, the lower the fee, the longer it will take for the transaction to be confirmed.
If you’re buying a newly listed coin, one that isn’t on the DEX’s default list, you need to be extremely careful that you have the correct link to the trading pair. Make sure that you get it from the project’s official website or social media pages (and not just a page that looks like the official one) – anyone can create an ERC20 (or other chain) token, and there have been many cases where similar but completely worthless scam tokens have been sold to investors.