Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C.R.a.C in Spring - I Froze My Server! (15 Minu...

C.R.a.C in Spring - I Froze My Server! (15 Minute Session For NLJUG Speaker Academy)

Starting up complex enterprise Java applications may take a long time and you may not want to wait that long when scaling or deploying. Project CRaC, meaning Coordinated Restore at Checkpoin…
Full description

More Decks by João Filipe Sabino Esperancinha

Other Decks in Programming

Transcript

  1. C.R.a.C in Spring I just froze my service! By João

    Esperancinha 23rd October 2024 Master speaker academy
  2. Who am I ? • João Esperancinha • ISEL (Instituto

    Superior de Engenharia de Lisboa) • Telecommunications and Computers Engineering • OCP11 - VMWare Spring Professional 2021 - Kong Champion • Plants, Furniture making, Nature • JESPROTECH channel on YouTube
  3. History • 2010s ◦ CRIU (Checkpoint Restore in Userspace) -

    Linux ◦ Mitigation of fault tolerance and processes • 2021 ◦ CRaC started with coordinated checkpoint and restore operations ◦ Prepare checkpoint ◦ Save state ◦ Restore later • 2022 - 2023 ◦ Experimentation and tests in frameworks like Spring and Quarkus • 2023 ◦ In April Azul releases the first JDK 17 supporting C.R.a.C.
  4. Coordinated Restore at Checkpoint • Application resources and JDK coordinated

    at checkpoints • Close files, connections and dump cache before checkpoint • Restores, files, connections and cache after checkpoint • A checkpoint is where we freeze the service • The restore doesn’t start the runtime from the checkpoint
  5. What do we need for it? • CRAC library: org.crac:crac:<latest

    version>, 1.4.0 • Spring Framework : ◦ Spring web: org.springframework.boot:spring-boot-starter-web:<latest_version> ◦ Spring boot maven plugin: org.spring.framework:spring-boot-maven-plugin:<latest_version> • A JDK that supports project C.R.a.C.: openjdk version "21.0.4" 2024-07-16 LTS OpenJDK Runtime Environment Zulu21.36+19-CRaC-CA (build 21.0.4+7-LTS) OpenJDK 64-Bit Server VM Zulu21.36+19-CRaC-CA (build 21.0.4+7-LTS, mixed mode, sharing)
  6. What do we need to configure for it? • The

    JDK itself: ◦ sudo chown root:root $(JAVA_HOME)/lib/criu ◦ sudo chmod u+s $(JAVA_HOME)/lib/criu • The jar MUST be compiled with CRaC compatible JDK • The jar MUST run with CRaC compatible Runtime
  7. How can we create a checkpoint? • Programmatically: ◦ Core.checkpointRestore();

    • Or via the command line: ◦ jcmd target/crac-service-java.jar JDK.checkpoint • Start the service indicating the target dump directory: ◦ java -XX:CRaCCheckpointTo=./tmp_checkpoint -jar target/crac-service-java.jar • Use a tool to make requests to the application ◦ siege -c 1 -r 100000 -b http://localhost:8080
  8. How can we monitor the start and restore? • Monitoring

    can be done with Resource: public class CracRunner implements Resource { public CracRunner() { Core.getGlobalContext().register(this); } @Override public void beforeCheckpoint(Context<? extends Resource> context) throws Exception { System.out.println("Before Checkpoint!"); } @Override public void afterRestore(Context<? extends Resource> context) throws Exception { System.out.println("After Restore!"); } }
  9. How can we restore a checkpoint ? • It has

    to be via the command line: ◦ java -XX:CRaCRestoreFrom=./tmp_checkpoint ◦ java -XX:CRaCRestoreFrom=./tmp_checkpoint -cp target/crac-runner-java.jar org.jesperancinha.smtd.CracRunner
  10. Why would we need this? • Faster Startup Times •

    Reduced Warm-Up Time for JVM • Cost Savings in Cloud Environments • Improved Elasticity for Microservices • Enabling Stateful Workloads
  11. Resources • https://mbien.dev/blog/entry/crac-coordinated-restore-at-checkpoint • https://spring.io/blog/2023/10/16/runtime-efficiency-with-spring • https://wiki.openjdk.org/display/crac • https://foojay.io/today/springboot-3-2-crac/ •

    https://en.wikipedia.org/wiki/CRIU • https://openjdk.org/projects/crac/ • https://www.youtube.com/watch?v=sVXUx_Y4hRU • https://foojay.io/today/how-to-run-a-java-application-with-crac-in-a-docker-container/
  12. The lost presentations • Could Virtual Threads cast away the

    usage of Kotlin Coroutines? What experience can tell us • Integrating Arrow Optics in a Real Life Scenario, my personal life experience in a parallel world • Kotlin Mysteries found from years working with Kotlin • Finally, we can simplify our code with Java Virtual Threads and do away with Kotlin Coroutines! • Improve startup times with Spring CRaC by freezing your spring service!
  13. About me • Homepage - https://joaofilipesabinoesperancinha.nl • Threads - https://www.threads.net/@joaofisaes

    • LinkedIn - https://www.linkedin.com/in/joaoesperancinha/ • YouTube - https://www.youtube.com/@jesprotech • Bluesky - https://bsky.app/profile/jesperancinha.bsky.social • Mastodon - https://masto.ai/@jesperancinha • GitHub - https://github.com/jesperancinha • Hackernoon - https://hackernoon.com/u/jesperancinha • DevTO - https://dev.to/jofisaes • Medium - https://medium.com/@jofisaes