Preheat oven to 400°F (200°C) and grease a 9x13-inch baking dish.
Melt butter in a saucepan over medium heat. Add onions and garlic, cooking until softened.
Stir in the flour and cook for 1–2 minutes, stirring constantly.
Gradually whisk in the cream and milk, bringing to a simmer until slightly thickened.
Add 1 1/2 cups of cheddar cheese, salt, pepper, and cayenne. Stir until cheese melts.
Layer half the sliced potatoes in the prepared dish. Pour over half the cheese sauce.
Repeat with the remaining potatoes and cheese sauce.
Sprinkle the top with remaining 1/2 cup cheddar cheese.
Cover with foil and bake for 45 minutes. Uncover and bake an additional 20–25 minutes, or until golden and bubbly.
Let rest for 10 minutes before serving.