How to survive a Kingside Attack in Chess? When I play the game of chess, I feel that I am quite weak when my kingside is being subjected to attack.

I have two games to demonstrate as an example. I am black in both cases. Can someone place their annotations with analysis to determine my strategic mistakes, and what I should have done earlier to prevent the attack from happening in the first place?

Game 1 

[fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"]
1.e4 c5 2.Nf3 d6 3.Bc4 Nc6 4.d4 cxd4 5.Nxd4 Bd7 6.Nc3 Nf6 7.Be3 a6 8.h3 e6 9.Qd2 b5 10.Bd3 Be7 11.a3 Rc8 12.Nf3 Na5 13.Rd1 Qc7 14.Ne2 Nc4 15.Qc1 Nxe3  16.Qxe3 Bc6 17.Ng3 O-O 18.O-O Nd7 19.Qe2 Bf6 20.c3 g6 21.Rfe1 Rfe8 22.Qd2 d5 23.Nh2 Ne5 24.Ng4 Nxg4 25.hxg4 Rcd8 26.Qe2 Be5 27.Nf1 Qe7 28.g3 d4 29.f4 Bc7 30.c4 Bb6 31.Kg2 b4 32.Ra1 a5 33.Nd2 a4 34.Rh1 bxa3 35.Rxa3 Bc5 36.Ra2 Rb8 37.Bc2 Rb4 38.Nf3 f6 39.g5 f5 40.Ne5 Ba8 41.Rh6 Qg7 42.Kf1 Reb8 43.Bd3 Bd6 44.Nxg6 hxg6 45.exf5 Qxh6 46.Qxe6+ Kh7 47.gxh6   1-0


Game 2

[fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"]
1.e4 c5 2.c3 Nc6 3.d4 cxd4 4.cxd4 d5 5.e5 Bf5 6.Nc3 e6 7.Nf3 Bb4 8.Bd3 Bxc3+ 9.bxc3 Nge7 10.O-O O-O 11.Bg5 Bxd3 12.Qxd3 h6 13.Bh4 Rc8 14.Rab1 b6 15.Nd2 Na5 16.f4 Qc7 17.Rbc1 Nf5 18.Bf2 Nc4 19.Nf3 Nb2 20.Qc2 Nc4 21.Qe2 Qc6 22.g4 Ne7 23.Qd3 Ng6 24.Bg3 Ne7 25.f5 f6 26.Nh4 fxe5 27.dxe5 Rcd8 28.f6 Nc8 29.Qg6 Rf7 30.fxg7 Rdd7 31.Qxh6 Rxf1+ 32.Rxf1 Rxg7 33.Ng6 Qc5+ 34.Kh1   *
1st game

As in the second game you gained a good position out of the opening, exchanging the strong dark-squared bishop for a knight. Around move 25 black just seems better having the bishop pair, dominating the dark squares and having no apparent weaknesses.


Around move 25 you seem to start playing without clear plan. A natural plan would be to play a5-b4 (after preparation) which would make white's queenside pawns very weak
around move 30/31 could think of playing bxc4 31 Bxc4 a5 and work against the b2 pawn, or alternatively think of closing the kingside by playing e5 or even g5.
32.. a5 You certainly do not want to recapture a5xb4 (since this would give white a protected passed pawn on c4 the open a file...). That's why perhaps 32.. bxa3 33 Rxa3 a5 (and work against the b2 pawn) or 33 bxa3 (and white has weak a and c pawns) might have been better.
around move 35: Black is still better. You created a weak pawn on b2 and nothing should happen on the kingside since the white king is rather weak. Not sure whether black can achieve much from here. Perhaps you need to create some tactical ideas with a3 later on. First of all I would try to close the kingside/center with e5 which would also make the white bishop even weaker than it is. After black's e5, because of the weak pawn on e4 white is basically forced to respond with f5 after which nothing dangerous should happen to you on the kingside. If needed you can always play f6, g5 and defend the h pawn with heavy pieces from the 7th rank.
36..Rb8!? allows 37 Nf3 which makes closing the center more difficult
around move 40: you allowed the white knight to get to e5. Compare this to the position after blocking the center with black pawns on e5, f6, g5 where white would have virtually no counterplay. With the pinned pawn on e4 black should still be better but now you need to be more careful with tactics compared to the blocked kingside variation.
41..Qg7 Using a queen for the only purpose of defending a pawn  is not something you want to do, but seems to be the only normal  defense. A better (but not that easy to see) choice would have been to play 41..d3 42 Bxd3 Bd4 which prevents the knight sacrifice on g6 because now the square h8 (which in some lines allows the white queen to enter) is covered by the bishop.
43..a3 works here and would allow your rooks to get very active. After 44 bxa3 you can play 44..Rb1+ and white is lost if playing 45 Bxb1
44..hxg6 loses. You could have tried fxe4, but even then white should be much better.


Summary: Out of the opening black had a good position and also created some targets (weak pawns) on the queenside. Intentionally or not intentionally (?) black chose to not close the kingside and to keep the pawn structure flexible. This is not necessarily a bad idea considering the weak white king. Basically you made a choice between: (a) closing the kingside and playing safely for a win (with admittedly decent drawing chances for white) or (b) Keeping the structure flexible allowing additional options in the center in case black cannot achieve anything on the queenside. Unfortunately, black then lost to a tactics (Nxg6). I would not really consider this game an example for a kingside attack, because basically you lost because of that tactics only.

2nd game

You achieved a quite decent position around move 12, having exchanged the strong white bishop on d3 and created a weak pawn on c3. There does not seem to be any attack on the king side threatened soon. Black should be slightly better at that point. 


At move 13 you should consider how to continue your play on the queenside. Obviously a rook or two will go on the c-file, the knight seems to have a nice square on c4, but also you might want to use a knight to attack the pawn on c3 from b5 or a4. The queen would have a good square on a5 from where it attacks the pawns on a2 and c3. You started with 13 .. Rc8, which is certainly not wrong, but Qa5 might be more correct as it un-pins the knight on e7 and prevents being locking in the queen after b6.
14 .. b6 makes your play on the queenside much more difficult as your queen cannot go to a5 anymore. And also it prevents activating rooks via Rc6-Rb6 or Ra6. 14.. Na5 might have been better.
18 .. h5 could have been an idea in order to prevent white playing g4.
19 ... Nb2 seems useless unless you want to move the knight to a4. 
Activating the queen via Qe7-Qa3 (this does not work yet because after g4 you'd lose the knight on f5, but could be prepared, with h5) or via Qb7-Qa6-Qa3 or Qa5 could be a long-term plan. But first of all you really need  to prevent white from playing g4-f5 here, which is basically the only way for white to gain any counter-play. 19 .. h5 seems like a logical move here, unless you want to go for a passive setup with g6, Ng7 which does not seem attractive to me.
23 ... Ng6!? does not seem like a good square to me. White will want to play f5 anyway and this just gives him an extra tempo since the knight will be attacked.
24 ... Ne7 You noticed your mistake, but as with the Nb2-Nc4 maneuver, you are wasting tempi in a position where you should either play very strongly on the queenside or think of defenses.
25 ... f6? Not a good move as it weakens the light squares and gives the bad bishop on g3 lots of squares if white plays  exf6. Your best option might have been to play with rook against two minor pieces after 25..exf5 26 gxf5 Nxf5 27 Qxf5 Ne3 With the weak white bishop and weak pawn on c3 chances might not have been that bad.
27 .. Tcd8? White is threatening f6 which does look pretty deadly. 27 ... Qc5+ with the idea to play Qe3 looks like an option since exchange of queens would be good for black. If white responds to the check with 28 Bf2 this would block the rook on f1 from supporting the f6 push and would give black another tempo to play Qc7 attacking the e5 pawn again. In fact white's only good option might be to return with the bishop to g3, making this a move repetition.. 
28 f6 Black is basically lost after this move, so there is no point analyzing further.


Summary: Out of the opening black had a good position. You did not really get anywhere on the queenside though (perhaps because of b6) and also you wasted a few tempi moving knights back and forth. White has basically only one plan in this position which is a kingside attack with g4-f5-f6... When breaking through on the queenside seemed impossible you should have focused on how to prevent white from carrying out this plan. You missed two defensive tactics which could have given you a draw.

A few ideas on what to do in general against this plan in positions like this:


Play h5 to prevent g4 and to strengthen the position of your knight on f5. If white plays h3 with the idea to play g4 sometimes you can play h4 or you can go for a setup (after g4 hxg4 hxg4) with knights on h6 and e7 and queen on d7 all working against f5
Put a rook on the 6th rank (c6 in your case) which could help to prevent f6 later
Escape with the king to the center
Open the center. This is kind of what you tried playing 25 .. f6. However this would have been more effective e.g. with the rook from c8 on e8 defending the knight and putting pressure on the center. Generally opening a center is a good response to a pawn storm.
Be faster on the queenside.


What should you do?

Looking at the two games, I would say that in the second you really encountered a kingside attack which is not easy to defend against and is perfectly possible to lose even with best play. There is no reason that you should lose to the first game's "kingside attack" though, because: you have very good counterplay (b2, e4 pawns), have the option of preventing any of white's activity by closing the center/kingside, don't have many threats to defend against.

Some ideas on how to improve your play against kingside attacks:


Work on your tactics, which is always a good idea. In game 1 you missed 41..d3 and 43..a3; in game 2 you missed 27..Qc5+ and the tactics around moves 25-27 (exchange of two pieces for a rook).
You could also think about changing your opening repertoire to playing for instance 1 e4 e5 which does not run into problems with such kingside attacks as easily. In many of the Sicilians you do run into such kingside pawn storms, similarly in many French and in the Modern/Pirc.
Study how masters handle kingside attacks for pawn structures that occur in your games.
Consider whether you can close the pawn structure on the kingside.
Be quicker with your queenside counterplay (don't fool around).