I thought this was quite interesting as Stephen Fry might add!
Rhyming slang is believed to have originated in the mid-19th century in the East End of London, with several sources suggesting some time in the 1840s. it dates from around 1840 and arose in the East End of London, however John Camden Hotten in his 1859 Dictionary of Modern Slang, Cant and Vulgar Words states that (English) rhyming slang originated "about twelve or fifteen years ago" (i.e. in the 1840s) with 'chaunters' and 'patterers' in the Seven Dials area of Westminster. (The reference is to travelling salesmen of certain kinds. Chaunters sold sheet music and patterers offered cheap, tawdry goods at fairs and markets up and down the country). Hotten's Dictionary included a "Glossary of the Rhyming Slang", the first known such work. It included later mainstays such as "Frog and toad—the main road" and "Apples and pears—stairs" as well as many that later grew more obscure, e.g. "Battle of the Nile—a tile (vulgar term for a hat)", "Duke of York—take a walk", and "Top of Rome—home".
It remains a matter of speculation whether rhyming slang was a linguistic accident, a game, or a cryptolect developed intentionally to confuse non-locals. If deliberate, it may also have been used to maintain a sense of community. It is possible that it was used in the marketplace to allow traders to talk amongst themselves in order to facilitate collusion, without customers knowing what they were saying. Another suggestion is that it may have been used by criminals (see thieves' cant) to confuse the police.