There has been a royal castle on the rock since at least the reign of David I in the 12th century, and the site continued to be a royal residence until 1633. From the 15th century the castle's residential role declined, and by the 17th century it was principally used as military barracks with a large garrison. Its importance as a part of Scotland's national heritage was recognised increasingly from the early 19th century onwards, and various restoration programmes have been carried out over the past century and a half. As one of the most important strongholds in the Kingdom of Scotland, Edinburgh Castle was involved in many historical conflicts from the Wars of Scottish Independence in the 14th century to the Jacobite Rising of 1745. Research undertaken in 2014 identified 26 sieges in its 1100-year-old history, giving it a claim to having been "the most besieged place in Great Britain and one of the most attacked in the world".
Few of the present buildings pre-date the Lang Siege of the 16th century, when the medieval defences were largely destroyed by artillery bombardment. The most notable exceptions are St Margaret's Chapel from the early 12th century, which is regarded as the oldest building in Edinburgh, the Royal Palace and the early-16th-century Great Hall, although the interiors have been much altered from the mid-Victorian period onwards. The castle also houses the Scottish regalia, known as the Honours of Scotland and is the site of the Scottish National War Memorial and the National War Museum of Scotland. The British Army is still responsible for some parts of the castle, although its presence is now largely ceremonial and administrative. Some of the castle buildings house regimental museums which contribute to its presentation as a tourist attraction.
The castle, in the care of Historic Scotland, is Scotland's most-visited paid tourist attraction, with over 1.4 million visitors in 2013. As the backdrop to the Edinburgh Military Tattoo during the annual Edinburgh International Festival the castle has become a recognisable symbol of Edinburgh and of Scotland and indeed, it is Edinburgh's most frequently visited visitor attraction—according to the Edinburgh Visitor Survey, more than 70% of leisure visitors to Edinburgh visited the castle.