I would like to know what happens when you set Settings > Messages > Keep Messages to something other than forever? (I.e. 30 days) I was under the assumption that messages are stored both in the cloud and locally on the phone. Will it remove both versions or just the version on your phone allowing you to redownload those messages at a later time?
tl;dr: There is no online version, so it will only remove the messages on that specific device.
From what I've seen, iMessages are stored locally, not on iCloud, with the exception of iCloud backups which do include iMessages from whatever you had stored on your device. In fact, the option for how long to save messages applies both to SMS and to iMessage texts, even though your cellular provider does not save your SMS messages. (If iMessages were stored online, you would probably also expect a Messages app on iCloud.com, which there isn't.)
Possibly you are under the impression that it saves messages online because when you restored from an iCloud backup it included your messages, and when you scroll up to old parts of conversations it loads messages that hadn't displayed prior, but it's loading the extra messages that were already saved on your device. You can test this by opening a conversation thread while in Airplane Mode and scrolling to load old messages.
Further, if you set up a new device, and you do not restore anything from a backup (so it doesn't load your settings) but you still sign in to your Apple account, Messages would not automatically download your old messages.
If you change how long your device saves iMessages for, that will only affect that one device. Maybe you can have one device save messages for only 30 days to save room, and on another device you can have it save messages permanently.