A CD early withdrawal penalty could cost you a good chunk of cash, but if you understand the terms and how this savings tool works, you can avoid getting hurt.

Banks issue CDs for a set amount of money and a set amount of time. (Credit unions do, too, but they call them “share certificates.”) Some have minimum opening deposit requirements, but the best ones don’t. As with any savings account, you’ll also want to look for CDs with high interest rates.

Banks pay interest on your deposit on a regular basis — typically monthly — until the CD matures. That period of time is called the term length, and it usually ranges from six months to five years. You might be able to find some as short as four weeks or as long as 10 years, though. Generally speaking, the longer the term length, the higher the rate.

» MORE: NerdWallet’s best CD rates tool

The earlier you withdraw money from your CD, the less interest you’ll earn. And in most cases, you’ll also have to pay some sort of penalty. That could be a specified number of months’ worth of interest.

Say you have a two-year CD and you cash it after seven months. You might have to forfeit six months of interest for early withdrawal, leaving you with very little in the way of a return. You probably won’t be able to avoid this fee, even if you need to withdraw only a small amount, as many banks stipulate that no partial withdrawals are allowed.

There may be exceptions to this rule, but either way, you should understand your CD’s withdrawal conditions, just in case the need arises. These aren’t always the same across the board, so be sure to reach out to your credit union or bank before signing on any dotted lines.

» MORE: What is a CD?

Avoiding CD early withdrawal fees starts long before you even have one. For starters, you shouldn’t open a CD unless you can afford to hold the money for the full term length.

Though most CDs carry very basic terms, you’ll find a variety of options available at certain banks and credit unions. Some allow penalty-free withdrawals, although they typically come with lower rates than standard CDs do.

If you want to lock in the higher rates of a five-year CD but don’t want to tie your money up for so long, CD laddering might be right for you. Instead of putting $5,000 in a five-year CD, you’d put $1,000 each into a one-, a two-, a three-, a four- and a five-year CD. Once the shortest certificate matures, you’ll have the opportunity to reinvest your earnings in a long-term CD or move the money back into your checking account.

CD laddering can provide a time cushion between maturity dates, and it gives you more immediate access to your savings in case of a sudden emergency. Plus, your CDs won’t be locked in for just one rate of return, a good thing if interest rates start to climb.

Now that you know how CDs work — and how to avoid early withdrawal fees — find one that has the best rates.

Tony Armstrong is a staff writer at NerdWallet, a personal finance website. Email: tony@nerdwallet.com. Twitter: @tonystrongarm.

Updated Feb. 23, 2017.

The article How to Avoid a CD Early Withdrawal Penalty originally appeared on NerdWallet.