The answer is Metaphor.
The figure of speech used in the verse 3 of the Psalm 71 is Metaphor, which is the comparison of two things that are not related, in an implicit or hidden way. The psalmist is comparing God to a solid rock, which is not easily moved ("my rock") and is a refuge for him; he also compares God to a well-protected castle ("my strong habitation and my fortress"), he feels secure and protected.