When do you lock the castle?

Hi there, Just wondering when you should lock the castle? I am not too far into the game yet, and want to know the best times to lock it. Thanks :) Also, if you kill a boss, do you need to lock the castle in order to keep your progress?