Quick revision
The short answer
We enable HTTP/2 with server.http2.enabled=true, and because browsers only speak HTTP/2 over TLS, we configure SSL as well; Tomcat also supports h2c, cleartext HTTP/2, for clients that use it. We enable response compression with server.compression.enabled=true and tune server.compression.mime-types and server.compression.min-response-size. In many setups both are better done at a proxy or CDN in front of the service.
What to remember
In this answer, we enabled HTTP/2 with server.http2.enabled=true and configured TLS, because browsers only use HTTP/2 over TLS, and saw that Tomcat also serves h2c to cleartext clients. We enabled compression with server.compression.enabled=true and tuned mime-types and min-response-size, avoiding already compressed formats and responses that mix secrets with attacker input. We also saw why both are often better done at a proxy or CDN in front of the service.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We enable HTTP/2 with server.http2.enabled=true, and because browsers only speak HTTP/2 over TLS, we configure SSL as well; Tomcat also supports h2c, cleartext HTTP/2, for clients that use it. We enable response compression with server.compression.enabled=true and tune server.compression.mime-types and server.compression.min-response-size. In many setups both are better done at a proxy or CDN in front of the service.
Background to this question
HTTP/2 and compression are two of the cheapest ways to make responses arrive faster: one lets many requests share a single connection, the other makes each response smaller. Spring Boot can do both with a few properties, but each has conditions that catch people out, such as HTTP/2 silently falling back to HTTP/1.1 or compression never happening for small responses. In this answer, we will recap what each feature does, configure HTTP/2 with TLS, look at cleartext h2c, configure and verify compression, and then decide when the work belongs in front of the service instead.
1A Quick Recap of HTTP/2 and Compression
HTTP/1.1 sends one request at a time on a connection, so a browser opens several connections to load a page in parallel. HTTP/2 multiplexes: many requests and responses travel at the same time over one connection, as separate streams, and headers are compressed. The result is fewer connections and less waiting, especially for pages with many small resources.
Response compression is a different idea. If the client sends Accept-Encoding: gzip, the server may compress the body and add Content-Encoding: gzip, and the client decompresses it. Text formats such as JSON and HTML often shrink to a fifth of their size, which saves bandwidth and time on slow networks, at the cost of some CPU on both sides.
2Enabling HTTP/2 With TLS
Browsers negotiate HTTP/2 during the TLS handshake, using an extension called ALPN, and none of them support HTTP/2 without TLS. So for browser traffic, HTTP/2 means HTTPS. In Spring Boot that is two settings: turn on HTTP/2 and give the server a certificate.
Notice that HTTP/2 is offered, not forced. A client that does not ask for it keeps using HTTP/1.1 on the same port, so enabling it does not break older clients. On current JDKs, ALPN is built in, so Tomcat needs no extra library; Jetty needs its HTTP/2 server module on the classpath, so check the Jetty section of the Boot documentation for your version.
3Cleartext HTTP/2 With h2c
Inside a data centre, traffic between a proxy and a service often runs without TLS, and some clients, such as gRPC clients or a service mesh sidecar, speak HTTP/2 in cleartext. That is called h2c. Tomcat supports it: with server.http2.enabled=true and no SSL configured, it accepts h2c from clients that use it, either by starting directly with HTTP/2 or by upgrading an HTTP/1.1 connection.
So h2c is useful between services and proxies, never for browsers. If a proxy in front terminates TLS and talks h2c to us, we get HTTP/2 end to end without managing certificates in the application.
4Enabling and Tuning Compression
Compression is off by default. When we turn it on, two properties decide which responses are compressed: the content type must be in the mime-types list, and the body must be at least min-response-size.
- server.compression.enabled=true?YesCheck the clientNoSent uncompressed
- Client sent Accept-Encoding: gzip?YesCheck the typeNoSent uncompressed
- Content type in mime-types?YesCheck the sizeNoSent uncompressed
- Body at least min-response-size?YesGzip it, add Content-EncodingNoSent uncompressed
4 steps in order. Play them, or step through them one at a time.
The minimum size exists for a reason. Gzip adds a header and costs CPU, and for a 300 byte response the saving is a few hundred bytes at most, smaller than the TCP packet anyway. Already compressed formats, such as JPEG, PNG, ZIP or video, should never be in the list, because compressing them again wastes CPU and can even make them larger.
Compression also interacts with security. Compressing responses that mix secrets, such as CSRF tokens, with input an attacker controls, over TLS, can leak the secret through response sizes, a BREACH-style attack. So the list of compressed types should be a choice, not a blanket rule.
5When a Proxy or CDN Should Do It Instead
Most production services sit behind something: an ingress controller, a load balancer or a CDN. Those components are built for exactly this work. A CDN can serve compressed static files from its cache without touching our service at all, and can use better algorithms such as Brotli. A proxy that terminates TLS can speak HTTP/2 to browsers while it talks HTTP/1.1 or h2c to our pods, so the application never deals with certificates or ALPN.
Doing it once at the edge also gives one place to set the policy, including the security exceptions. So my default is: HTTP/2 and compression at the proxy or CDN, and in the application only when the service is reached directly, or when a measured gain, such as a large JSON API to mobile clients on slow networks, justifies it.
| Situation | HTTP/2 | Compression |
|---|---|---|
| Browser reaches the app directly | server.http2 with TLS | server.compression, tuned list |
| Behind a TLS-terminating proxy | At the proxy; h2c inside if useful | At the proxy |
| Static files to the world | At the CDN | At the CDN, cached |
| Service to service, mesh | h2c or mesh mTLS | Usually not needed |
6Follow-up questions
Why does my browser still show HTTP/1.1 after enabling server.http2?
Most often because the site is served over plain HTTP, where browsers never use HTTP/2, or because a proxy in front terminates TLS and speaks HTTP/1.1 to the browser. Check the protocol column in the browser's developer tools against the proxy configuration.
Does compression slow the service down?
It costs CPU, roughly a few milliseconds per hundred kilobytes, which matters only for very busy services or very large responses. For most JSON APIs the time saved on the network is larger than the CPU spent, but it is worth measuring under load before enabling it everywhere.
Is HTTP/2 always faster for APIs?
Not always. Its biggest gain is many parallel requests over one connection, typical of browsers. A backend client making one call at a time over a keep-alive connection gains little, so it is a good default, not a guaranteed speed-up.
7Conclusion
In this answer, we enabled HTTP/2 with server.http2.enabled=true and configured TLS, because browsers only use HTTP/2 over TLS, and saw that Tomcat also serves h2c to cleartext clients. We enabled compression with server.compression.enabled=true and tuned mime-types and min-response-size, avoiding already compressed formats and responses that mix secrets with attacker input. We also saw why both are often better done at a proxy or CDN in front of the service.
Open with
For HTTP/2, I set server.http2.enabled=true. The condition is that browsers only use HTTP/2 over TLS, negotiated with ALPN in the handshake, so I also configure SSL, nowadays with an SSL bundle and server.ssl.bundle.
Then, if they want more
Clients that do not ask for HTTP/2 keep using HTTP/1.1 on the same port, so nothing breaks. Without TLS, Tomcat still supports h2c, cleartext HTTP/2, which is useful for proxies, gRPC clients or sidecars that speak it, but browsers never use it. For compression, I set server.compression.enabled=true, which is off by default.
Then a response is gzipped only if the client sends Accept-Encoding gzip, its content type is in server.compression.mime-types, and its body is at least server.compression.min-response-size, 2KB by default, because tiny responses gain nothing.
I keep already compressed formats like images out of the list, and I am careful with responses that mix secrets and user input over HTTPS, because of BREACH-style attacks.
In production, though, I usually prefer doing both at the proxy or CDN in front of the service: the proxy speaks HTTP/2 to browsers, a CDN can cache compressed static files, and there is one place to set the policy.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you enable HTTP/2 and response compression in a Spring Boot application, and what...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/9f5ea65ecf/