John Steinbeck had to work out the plot for his novel before writing it. One thing he probably had in mind at the outset was that he wanted to end the story with one man killing his friend out of compassion. The author needed to establish that George had access to a weapon, but it could not be a weapon that George actually owned. Why not? Because if Lennie were killed with a weapon known to belong to George, then George could be arrested and convicted for murder. Steinbeck must have invented the character of Carlson primarily to establish the existence of a suitable weapon, a German Luger, which is a very distinctive-looking automatic pistol. When George pulls the Luger out of his coat pocket in the final chapter, the reader is expected to recognize it as the one that was missing from George's bunk at the ranch.
Steinbeck planned to turn his short novel into a stage play (see the Introduction in eNotes Study Guide via reference link below). The book is written in such a way that it could obviously be adapted into a script with a minimum of time and trouble. For example, there is hardly any prose exposition in the book. The characters provide information for the reader and the future audience through their dialogue. To take another example, the book is extremely short, suggesting that the author's intention was to make everything fit into the dimensions of a stage play lasting only around an hour and a half. The "shotgun ending" of the story brings it to a conclusion; otherwise a story about the hard lives of farm workers could go on indefinitely. The ending puts a "frame" around the story.
Carlson is a middle-aged man. He probably owns the Luger because he served in World War I and either took it from a captured German officer or bought it from another soldier. Carlson keeps the handgun partly as a souvenir and partly for protection. Life was dangerous for itinerant farm workers during the Great Depression. Candy probably owned a dog for the same reason that Carlson owned a pistol. It was dangerous to be riding on freight cars with strangers, dangerous to be sleeping in hobo jungles, dangerous to be tramping the highways, and dangerous to own anything of value. George probably kept Lennie as a buddy for protection as well as for companionship. Lennie was a big, powerful man who would do anything George told him to do.
When Carlson shoots Candy's dog, George learns how to kill quickly and painlessly with a pistol. He also observes how Candy works the mechanism when he cleans the weapon after using it on the dog. And he sees where Carlson keeps it under his mattress. The weapon George uses to kill Lennie had to be a pistol because George had to keep it concealed.
Carlson is a bitter man. He didn't expect to end up doing back-breaking farm labor for room and board plus fifty dollars a month in wages. The smell of Candy's dog irritates him because it reminds him of his miserable existence. He is angry at the world and takes it out on the old dog.