There is an excellent article about using and understanding raster interrupts on the C64 at https://www.retro-programming.de/programming/nachschlagewerk/interrupts/der-rasterzeileninterrupt/. Joern does a very good job in explaining how this concept works.
My contribution is that I converted his code to assembly code that can be digested by Turbo Macro Pro and Turbo Assembler on the Commodore C64. I also added (a lot of) English comments to make the example more accessible. Please see my GitHub repro at https://github.com/jklingel/Raster-Interrupts for the code.