Can I setup DNS in such a way that if one IP address fails, it uses the other IP?

No. Or not in general.

The DNS does load balancing by default, not fail over. If you have multiple data for a given name and record type, applications are supposed to use all of that data. But since they are sets, not lists, there is no guarantee of order. And records are cached, so through all this applications do not have an "immediate" way to fail over from a broken case to a working one.

But they are exceptions. MX records are ordered through a priority field, so applications (that is SMTP clients or MUA/MTAs) are expected to automatically switch to second record if first one fails, and so on. But this is because it is backed in the specifications of the MX record, and because it has a specific piece of information in the record, the priority.

Same for SRV records that allows to put in place load balancing and fail over... as long as applications are developed to use those records, which for example is not the case with web browsers, for some now historical reasons, which ought to be solved in the future with the new HTTPS and SVCB record types.

Load balancers in hot/hot or hot/cold configurations have specific protocols to resolve those issues. You typically use either IP anycast with some clever BGP routing, or some "virtual" IP address that is floating between various systems, which needs to be synchronized to make sure the IP address is claimed only by one at any single point in time. Look at VRRP for example, or at https://www.haproxy.com/blog/failover-and-worst-case-management-with-haproxy/ for an example when using HAProxy.