A defense process found in many bacteria and archaea that protects the organism from invading foreign DNA by cleaving it with a restriction endonuclease. The organism's own DNA is protected by methylation of a specific nucleotide, which occurs immediately following replication, in the same target site as the restriction enzyme.