Such censuses were indeed performed in ancient Rome, and they did indeed require for people who were not located at their regular place of residence at the moment to return there for the census.In those days Caesar Augustus issued a decree that a census should be taken of the entire Roman world. 2 (This was the first census that took place while Quirinius was governor of Syria.) 3 And everyone went to their own town to register.4 So Joseph also went up from the town of Nazareth in Galilee to Judea, to Bethlehem the town of David, because he belonged to the house and line of David.
However, while this is a known historical fact, this passage actually heavily contradicts this in a rather drastic manner. It says that Joseph went to Bethlehem because he belonged to the house and line of David. Not because his residence was in Bethlehem.
If the fact that Joseph's permanent residence was not Bethlehem but Nazareth is unclear, Luke himself confirms this a few verses later:
39 When Joseph and Mary had done everything required by the Law of the Lord, they returned to Galilee to their own town of Nazareth.There are two blatant contradictions here. One is in the text itself. Verses 3 and 4 say "and everyone went to their own town to register. So Joseph also went up from the town of Nazareth in Galilee to Judea, to Bethlehem the town of David..." Then verse 39 says: "they returned to Galilee to their own town of Nazareth." In other words, Joseph went away from his own town to Bethlehem, even though the census required everyone to go to their own town, as stated by the text itself.
The other contradiction is with the historical fact that Roman censuses required everybody to return to their permanent residence. There's absolutely nothing in history that would indicate in any way that the census would require anybody to move to the town of a very distant ancestor (in this case an ancestor of over a thousand years prior.)
The most likely explanation for this contradiction is that, very probably, there were two widespread but conflicting myths by the time that the gospel of Luke was written. The first was that the promised messiah would be born in Bethlehem (there are indications that Jewish rabbis had this interpretation at that time). The second was that Jesus was being called "Jesus of Nazareth". (In those times it was common practice to "surname" people by either their father's name or the town they were born, in order to distinguish them from other people with the same name.)
Therefore the author of the gospel had a dilemma: Jesus had to be born both in Bethlehem (due to the interpretation that the promised messiah would be born there) and also in Nazareth (because that's what Jesus was widely referred to.) Therefore he concocted this clever way around the dilemma: Write that Jesus was physically born in Bethlehem (due to the alleged sensus) but his "official childhood residence" was in Nazareth (so that his "surname" would make sense.)
Using a sensus as an excuse for Joseph and Mary to be in Bethlehem at that precise moment seems to be pretty sloppy, though. There are myriads of less contradictory reasons why they could have been there.
(Of course I'm talking from the narrative point of view here. The entire story is most probably completely fictional.)