Yes, they keep them forever, but ONLY if you sell via the Barter system. If you sell directly, then no.

I suppose the reasoning for this is in case you sell something you didn't mean to and realize 9 hours later that you had, so you wouldn't lose 9 hours of progress. Like when it happened to me when I accidentally sold the Idol of Sylvanus and then 7 hours later noticed it's gone from my inventory. So it does serve a purpose.

As for the technical aspects and possibility of problems, there wouldn't be any. Divinity Original Sin 2's merchants also stored all items you sold and there were no issues.