Edinburgh Castle is a fortress which dominates the skyline of the city of Edinburgh, Scotland, from its position atop the volcanic Castle Rock. Human habitation of the site is dated back as far as the 9th century BC, although the nature of early settlement is unclear. There has been a royal castle here since at least the reign of David I in the 12th century, and the site continued to be a royal residence until the Union of the Crowns in 1603. From the 15th century the castle's residential role declined, and by the 17th century its principal role was as a military base with a large garrison. Its importance as a historic monument was recognised from the 19th century, and various restoration programmes have been carried out since. As one of the most important fortresses in the Kingdom of Scotland, Edinburgh Castle was involved in many historical conflicts, from the Wars of Scottish Independence in the 14th century, up to the Jacobite Rising of 1745, and has been besieged, both successfully and unsuccessfully, on several occasions.
Few of the present buildings pre-date the Lang Siege of the 16th century, when the medieval fortifications were largely destroyed by artillery bombardment. The most notable exceptions are St Margaret's Chapel, which dates from the early 12th century and is the oldest surviving building in Edinburgh, the Royal Palace, and the early-16th-century Great Hall. The castle also houses the Honours of Scotland, the Scottish National War Memorial, and the National War Museum of Scotland.
Edinburgh Castle is in the care of Historic Scotland, and is Scotland's most-visited paid tourist attraction, with over 1.3 million visitors in 2011. The British Army is responsible for some parts of the castle, although its presence is largely ceremonial and administrative, including a number of regimental museums. As the backdrop to the annual Edinburgh Military Tattoo it has become a recognisable symbol of Edinburgh and of Scotland.
The castle stands upon the plug of an extinct volcano, which is estimated to have risen some 350 million years ago during the lower Carboniferous period. The Castle Rock is the remains of a volcanic pipe, which cut through the surrounding sedimentary rock, before cooling to form very hard dolerite, a type of basalt. Subsequent glacial erosion was resisted by the dolerite, which protected the softer rock to the east, leaving a crag and tail formation.
The summit of the Castle Rock is 130 metres (430 ft) above sea level, with rocky cliffs to the south, west and north, rearing up to 80 metres (260 ft) from the surrounding landscape. This means that the only readily accessible route to the castle lies to the east, where the ridge slopes more gently. The defensive advantage of such a site is clear, but the geology of the rock also presents difficulties, since basalt is an extremely poor aquifer. Providing water to the Upper Ward of the castle was problematic, and despite the sinking of a 28-metre (92 ft) deep well, the water supply often ran out during drought or siege, for example during the Lang Siege of 1573. celebrations.