Writing a mystery is not easy. It seems as if it should be. The formula is straight forward enough, there is a murder or some other type of event that needs to be solved. There is a detective who will solve it by the end and there are suspects. The problem is that within all of that there are numerous pitfalls and problems that you can fall into.
The first thing to understand is that more so than in any other form of writing the writer of the mystery must have a plan. You can go into most other forms of writing with just a vague idea of where you are going and make it work, but in a mystery the reader is trying to find the clues that the writer left behind and that requires a plan.
Next, you have to introduce both the detective and the person who committed the crime early on. People want to guess and to do that you need to have a suspect. In addition to this you will need to introduce other possible suspects as well. Each of these characters should be well enough described to make them believable. Make certain that your culprit is visible throughout the story. The reason that the butler did it is such a commonly used clich is that people hate it. Having a minor character who hasn't showed up for a hundred pages is just going to annoy the reader who has been trying to figure it out.
Next you should make certain that the detective solves the crime using his own wits. No matter if it is CSI or Sherlock Holmes readers want the detective to figure out the crime not have the answer given to him with DNA, divine proclamation or any other device.
The clues should exist to solve the crime. The best mysteries are the ones that you feel like you should have figured out. Often the clue to the entire book can be found on the first page. This can be dangerous if the reader figures it out but if it well written and surrounded by other clues the odds are that they won't.
With any story you want the climax near the end, but in the detective story once the culprit is revealed there is typically very little reason to keep going. Keep the reveal until end, the last page if possible.
Writing a good mystery is as fun as reading one. You can play with crime, study how things are done, even imagine the murder of someone you don't like but you still have to put in the work. Understand what the expectations are then meet or change them them and anything else you do will simply be bonus.