If you put together your mountain bike yourself, you will probably buy the cassette in a component group. This means that you also buy the shifting group and derailleurs. However, if your derailleur is worn out, you can also replace it with a new one. Read more about replacing the MTB cassette here.