This guide will explain the various ways to protect or defend yourself against enemy (rally) attacks.
In the first version of this guide I offered the option of Garrison your troops in an alliance building as a way of protecting yourself. To do this while offline is extremly risky, it turns out. I had assumed the Alliance Fortress of Towers would provide some extra defense boosts, but I was wrong. I don't know the details of how it works, but I have had a recent experience where a bunch of us had garrisoned the Alliance Fortress with near 300k troops in total. All were killed by one solo attack from a (very!) strong player. In the battle report it was shown as if one of the players was attacked (although via the coordinates in the right top of the report you could still see it was the Alliance Fortress). I am not sure whether that was only a reporting issue or whether it actually affected the battle, but the reported boosts were from that one player, who happened to not have used his dragon, and have very low boosts.
For me this means that using the Garrison option to keep your troops in an Alliance Fortress or Tower went from a situation where I thought it was very likely you would get significant defensive boosts when attacked, to a situation that I am sure does not have any benefits, and possibly/probably even makes you more vulnerable than if you are in your own castle, so see the alliance buildings still as an option to temporarily store your troops while you are offline if you want to empty out your castle before an attack, but know the risks of leaving your troops there while offline.
A small positive side note, my experience with being attacked while Garrisoning the Alliance Fortress did confirm that, like with reinforcing allies, your troops end up in your hospitals, not lost.
The game favours attackers. Let's as an example look at a situation of two alliances each with few big players with buildings level 20, and the rest small players with buildings level 15. When one of the big players leads a rally against a smaller player, the big player can have a rally size of 500,000 with a Hall of War lvl 20, which can be filled by fellow alliance members, big and small. Looking at a small player it is a lot more difficult to get an army of 500,000 troops to defend yourself at level 15. Training so many troops yourself with become more and more difficult as you have more due to the upkeep. An Embassy level 15 only allows your alliance members to reinforce you with 90,000 troops. The Trap Capacity for a Trap Factory level 15 is 22,500. This means even if you have been able to build an army of 90,000 your defenders are outnumbered significantly.
The amount of troops are an important factor, but the level 20 rally leader also would normally be able to have much better combat boosts from Dragon Skill, Lord Talent Points Projects, Research Projects, and equipment, so the attacking troops would also have much better stats. Realize that the combined effect of these can be quite huge.
If you are online when attacked, you still have the option to use some items to increase Attack and Defense, but if not, this is also favoring the Attacker.
The above means that unless you really have a huge army and you are fully reinforced, you do not stand a chance against incoming rally attacks. The difference is so huge that the attacker can wipe out most castles without taking significant losses.
The only way to really project yourself is to use a peace shield. Putting up a Peace Shield means nobody can attack you, so your resources and troops are save. However peace shields are not cheap (e.g. an 8 hour peace shield costs 600 gold or 60,000 alliance loyalty), and kill events last 3 days, not even considering attacks outside of kill events. It is going to be expensive to stay shielded whenever you are not online.
When an attack is incoming and you are online and you do not put up a shield, you can chose to defend against the attack. Depending on your Watchtower level you get information about the attack. This can help you in deciding whether defending against the attack is the best option. If you decide to defend you can consider the following: ask your alliance members to reinforce your embassy, you can make sure your equipment and lord talent points are set for combat, and you can use items to boost your attack and defense stats. Keep in mind that the attacker is online for sure and can call off the attack.
Consider your hospital capacity and alliance hospital capacity, make sure you have enough to store all your troops, you might need it.
When an attack is incoming you can also opt to empty your castle. This means send your troops outside and make sure the resources you have are below Storehouse levels. You can use your resources by for example starting a building upgrade, research project and troop trainings. You can also decide to send some to the alliance storehouse. You can send out your troops to gather resources, reinforce an alliance member, or garrison one of the alliance buildings. Even attacking a monster if you make sure it keeps them away from your castle long enough is ok. It seems your traps will not engage in battle when there are no troops in your castle. They will not inflict any kills, but also not get damaged.
The Dragon Altar Alliance Building offers some spells to defend the alliance, which can help against incoming attacks as well. Most useful is probably the spell that casts a 4 hour shield to all alliance members.
When you are not online you do not have the option to react on incoming attacks, you need to make a decision in advance. As stated before, staying shielded whenever you are offline is expensive and for most players not an option. Because of the inbalance between attack and defense leaving your troops in your castle is costly. Sure, if you have enough hospital capacity none of your troops will die, but it also takes a lot of resources to heal your troops, and a lot of time.
However not leaving your troops in your castle has some disadvantages as well, it means people can just attack you and take your excess resources, making it difficult to build up resources for larger building upgrades and research projects.
It is a difficult choice to make, but basically you will probably decide to not keep your troops in your castle anymore if you are hit too often and have to heal all your troops. If that is the case you might opt to send your troops to an alliance member that is shielded (or a very strong alliance member if no one puts up a shield).
If you have sufficient troops and march size it might be a good option to send a full march to the alliance resource building as well. You can even temporarily change your dragon skills to lower (meaning remove any dragon skills to increase gathering speed) resource gathering speed and increase march capacity, so that your troops will take longer to come back from the alliance resource building.
I suggested to garrison an alliance building in an earlier version of this guide, but I no longer see that as a safe option.
The best way to protect yourself is putting up a shield, but since that is quite expensive you might decide to reinforce a shielded alliance member, or send your troops out gathering to the alliance resource building, before you go offline for longer times.
If you have any questions or other tips to defend or protect yourself, please leave a comment.