Germany scraped through to a World Cup quarterfinal meeting with France despite being given a real scare by Algeria on a thrilling night of football in Porto Alegre.
Andre Schurrle and Mesut Ozil scored as Germany won 2-1 after extra time, but for much of the night, it seemed Joachim Low's men were heading for what would have been the shock defeat of the tournament so far.
Algeria spotted Germany's vulnerable high-line defence from the first whistle and took advantage with a series of long-ball counter-attacks.
Germany goalkeeper Manuel Neuer played sweeper for much of the match in the Estadio Beira-Rio, racing out of his box to clear on four occasions.
Islam Slimani was a constant threat for the African nation, who looked like World Cup contenders, rather than knockout stage debutants.
Sofiane Feghouli and Faouzi Ghoulam were guilty of missing first-half chances for Algeria and Mehdi Mostefa also shot wide in extra time.
Rais M'Bolhi had a superb game in the Algeria goal, saving from Philipp Lahm, Bastian Schweinsteiger and Schurrle in normal time.
But his efforts to avenge the 1982 "Disgrace of Gijon" -- when West Germany and Austria conspired to eliminate Algeria from the World Cup -- were ultimately ended by Ozil, who swept home in the second period of extra time.
Abdelmoumene Djabou scored in the 120th minute, but Algeria could not find another as a massively relieved Germany sealed a quarter-final meeting with France in the Maracana.
Algeria's plan to hit Germany on the break was clear from the off.
Slimani sneaked in behind Per Mertesacker following a long punt up field, but luckily for the Arsenal defender, Neuer sprinted off his line and put in a superb sliding tackle well outside his area.
Feghouli then showed Jerome Boateng up, turning the former Manchester City defender in the box, but he shot wide.
Algeria thought all the early pressure had paid off when Slimani headed El Arbi Soudani's cross in. Algeria coach Vahid Halilhodzic flung his arms in the air, but the goal was ruled out for offside.
Still, Algeria continued to pile on the pressure on the counter.
Soudani played Ghoulam in behind, but he failed to test Neuer.
Once again, Algeria punted the ball long. Mertesacker failed to control the ball and Feghouli raced free only for Neuer to dash out of his box and clear.
M'Bolhi then kept Algeria in it with two saves in quick succession just before a frantic first half came to an end.
Schurrle came on for Mario Gotze at the break and made an immediate impact, latching on to Thomas Muller's pass, but his deflected shot flew just wide.
Germany were dominating possession, but they could not beat M'Bolhi, who pulled off an outstanding save to deny Lahm on the hour.
The Algerians' pace still made them the most dangerous side in the second half. Neuer had to come racing out of his area for a third time to deny Slimani with his head.
Germany had two great chances to win with 10 minutes of the 90 left, but M'Bolhi saved Muller's header and Schurrle's follow-up was blocked on the line.
Neuer's sweeping skills were called on again with two minutes to go as he denied Feghouli.
Schweinsteiger should have won the game in the final minute of normal time, but he fired a free header straight at the Algeria goalkeeper.
Moments earlier Muller fell over as part of a free-kick routine. Nothing was going right for the Germans.
That all changed less than two minutes into extra-time.
Muller pinched the ball off Algeria full-back Aissa Mandi and squared for Schurrle, who flicked the ball beyond M'Bolhi with his instep.
The Algerians tired, but they kept looking for the equaliser. Mostefa shot just wide when he should have tested Neuer.
Ozil scored in the final minute to give Germany a two-goal cushion, but Djabou then turned home Feghouli's cross to give Algeria a glimmer of hope.
There was not enough time for Algeria to find another though and Germany progressed.