30 Java & Spring Boot Scenario-Based Interview Questions | Real-World Production Problems | Part 1 — Transcript
Full transcript
- 0:00If you are preparing for a senior Java
- 0:01developer, tech lead or a solution
- 0:03architect interview, there is something
- 0:05you need to understand. At this level,
- 0:08interviewers are usually not interested
- 0:10in hearing a textbook definition. They
- 0:12don't want you to simply tell them what
- 0:14at the rate transactional does or what
- 0:17an index is or what caching means.
- 0:19Instead, they are giving you a
- 0:21production problem. An API suddenly
- 0:23becomes slow, a payment get processed
- 0:25twice or one application instance
- 0:27crashes with out of memory error or a
- 0:30database query that was fast yesterday
- 0:33suddenly taking 10 seconds. These type
- 0:35of questions they will ask and then they
- 0:37ask you one simple question, what would
- 0:40you do? That's when the senior level
- 0:41thinking really matters. In this video,
- 0:44I'm going to walk through 30 real world
- 0:46scenario based interview questions
- 0:48covering Java, Spring Boot, Hibernate,
- 0:50PostgreSQL, Kafka, transactions,
- 0:52concurrency, performance and some
- 0:54architecture related questions as well.
- 0:56And for every scenario, I will explain
- 0:58not just what solution to use, but more
- 1:01importantly, how a senior engineer would
- 1:03investigate the problem and why that
- 1:05solution should be chosen. Let's start.
- 1:07Imagine your Spring Boot API normally
- 1:10responded in around 200 milliseconds,
- 1:12but suddenly in a production the
- 1:13response time increased between 5 to 10
- 1:16seconds. Now CPU looks completely
- 1:18normal, but your database connection
- 1:20pool is exhausted. Now interviewer is
- 1:22asking, what would you investigate first
- 1:24in this? Before giving an answer, jump
- 1:27into any conclusion, just think about
- 1:29it. If interviewer will ask to me,
- 1:31possibly my answer would be, I would not
- 1:34immediately increase the database
- 1:35connection. First, I would find out why
- 1:37the existing connections are being held
- 1:39for so long. I would start with the
- 1:41connection pool matrixes. I wanted to
- 1:44know how many connections are active,
- 1:46how many are ideal, what are the maximum
- 1:48pool sizes and most importantly, whether
- 1:51the request are waiting for a connection
- 1:53or not. If all those things are looks
- 1:55normal, then we'll go to the next and I
- 1:57would look at the database monitoring
- 1:59and identify query is might be slowing.
- 2:01We will identify that any suspicious
- 2:03queries is there. I will use explain
- 2:05analyze in the post case SQL and
- 2:07actually doing the analysis of that
- 2:09query. Then I would check the long
- 2:11running transactions whether there will
- 2:13be any transaction which is long running
- 2:15and blocking the other to take a
- 2:18connection from a DB. Then I will
- 2:20investigate this will there hibernate
- 2:22having any N plus one query related
- 2:24issue or not because an API request can
- 2:27unexpectedly indicate hundreds of
- 2:29thousands of queries due to this N plus
- 2:32one query issue. And another important
- 2:34thing I would check in is whether the
- 2:36external API is happening while the
- 2:39database transaction is just open up. If
- 2:41an open transaction update the database
- 2:44call a third party API that makes a five
- 2:47second and only then commit a database
- 2:49connection may remains occupied for
- 2:51entire five second and it's enough
- 2:53request to this simultaneously the
- 2:55connection pool get exhausted. If the
- 2:57evidence is still not clear I would take
- 2:59a trade dump and identify whether the
- 3:02requests are actually blocked or not. So
- 3:05this way the senior level answer would
- 3:07be expecting. Don't increase the pool
- 3:09size and before understanding why the
- 3:11connections are being held because
- 3:13increasing the connection pool might
- 3:15temporarily hide the symptoms while
- 3:17making the database itself even more
- 3:19overloaded. And this is an important
- 3:21interview pattern. Whenever an
- 3:23interviewer give a performance problem
- 3:25don't jump directly to the configuration
- 3:27changes. First find out the resources
- 3:29that is being exhausted and then find
- 3:32why it is being exhausted. Now let's
- 3:34look at another problem where the
- 3:35performance is not the issue but the
- 3:37correctness is. Now in this question
- 3:40suppose a customer makes a payment and a
- 3:42client send the request to your API but
- 3:44because of the network timeout it didn't
- 3:46receive the responses. A client assumes
- 3:49the request failed and retries. Now the
- 3:51server receives the same payment request
- 3:53twice. Now the interviewer ask how would
- 3:56you prevent a duplicate payment? Now in
- 3:58this particular question there is a
- 4:00issue of requesting twice the same
- 4:02request. Now, first, the solution that
- 4:04comes into your mind is item potency.
- 4:06Now, I would ask the client to send the
- 4:09item potency key with the payment
- 4:11request. For example, item potency key
- 4:13is a b c or one two three like that.
- 4:15When the server receives the request, it
- 4:17stores that key along with the payment
- 4:19result. The database should also enforce
- 4:21a unique constraint on that key. Now,
- 4:23imagine the first request creates a
- 4:25payment successfully. When the retry
- 4:27arrives with the same key, the
- 4:29application detects that the this
- 4:31operation has been already processed and
- 4:33instead of creating another payment, it
- 4:35simply returns the original result. But
- 4:37there is an important architecture
- 4:39detail here. I would not rely only on
- 4:42the application level check because two
- 4:44identical requests can arrive at most
- 4:47exactly the same time. Both requests
- 4:49would check, does this key exist? Both
- 4:51might get no and both would then create
- 4:54a payment. And that's why the database
- 4:55uniqueness constraint is extremely
- 4:57important. It gives us a final
- 4:59consistency boundaries. And remember
- 5:01this principle, we tries are normal in
- 5:03distributed system. So whenever you
- 5:05design a payment or order or message or
- 5:08even event processing, always ask what
- 5:10happens if same request arrives twice.
- 5:13If your design has a good answer to that
- 5:15question, you are already thinking like
- 5:17a senior engineer. Now consider this
- 5:19scenario. You have a method annotation
- 5:21with at the rate transactional. Inside
- 5:23the method, you save an order, then you
- 5:26save a payment information, then an
- 5:28exception occurs. But surprisingly, the
- 5:30order is still committed. Now the
- 5:32interviewer asks, what could be the
- 5:34wrong? So in this such case, we need to
- 5:36check whether the transactions spring
- 5:38transaction proxy. We need in this the
- 5:41first thing we would investigate the
- 5:43first thing we would investigate is
- 5:45spring transaction proxy. At the rate
- 5:47transactional is normally implemented
- 5:50using a spring proxy. So we should ask
- 5:52is the method being called through
- 5:55another spring managed bean or is there
- 5:57a self invocation? Self invocation means
- 6:00like this dot create order. Now when a
- 6:02method inside the same object calls
- 6:05another transactional method directly.
- 6:07The spring proxy can be bypassed. I
- 6:10would also check the exception type. By
- 6:12default, string rollback transactions
- 6:14for unchecked exceptions such as runtime
- 6:16exceptions, but checked exceptions do
- 6:19not automatically triggered rollback in
- 6:21the same way. If the business
- 6:22requirement requires a rollback for a
- 6:25checked exception, we may need something
- 6:27like rollback for I would also verify
- 6:29that the correct transactional manager
- 6:32is being used especially in application
- 6:34involving multiple data sources. So we
- 6:37wouldn't simply say that add at the rate
- 6:39transactional. I would investigate
- 6:41whether the transaction actually exist
- 6:43around the operation or not. This is the
- 6:45exactly the difference between the
- 6:47mid-level and senior level answer. A
- 6:49mid-level engineer says that use at the
- 6:52rate transactional. A senior level
- 6:54engineer says that is a transaction
- 6:56actually being created and is the method
- 6:58being invoked through the spring proxy.
- 7:01Now I will explain about the spring
- 7:03proxy more. Now suppose we have a one
- 7:06class called order and another class
- 7:09called payment. Now the this particular
- 7:11class is having one method called create
- 7:13order and inside this create order at
- 7:16the transactional I am calling a payment
- 7:19class process payment method. Now if the
- 7:21process payment method is having at the
- 7:24rate transactional and it is defined in
- 7:26different bean, then there will be a if
- 7:28any exceptions will occur, then this
- 7:30particular transaction will be rolled
- 7:32back. The process payment transaction or
- 7:34receive transaction payment will be
- 7:35rolled back. But if this process payment
- 7:38process payment transaction method
- 7:40defined in the same bean like in order
- 7:42bean is sent, then the spring proxy will
- 7:45be bypassed and it will not roll back.
- 7:47Now this n plus one query problem. Here
- 7:49we are calling find all method which is
- 7:52loading thousand of orders. After this
- 7:54query, we have written that order dot
- 7:57get customer dot get name and that is
- 7:59also suddenly loading thousand plus
- 8:01queries again. Now the interviewer will
- 8:03ask that this is the n plus one query
- 8:05problem. Now how would you resolve this
- 8:07n plus query problem? Now, first we
- 8:09would confirm the problem using SQL
- 8:11logs, hibernate statistics, or APM
- 8:14traces. We don't want to optimize
- 8:16something that is not actually the
- 8:18bottleneck. So, once the confirmation
- 8:20will be done, there are several possible
- 8:22solutions for that. One of the solution
- 8:24is using @ entity graph. For example, we
- 8:27can explicitly tell JPA that the
- 8:29customer relationship is required for
- 8:32this particular use cases. Now, another
- 8:34option will be the fetch join. This
- 8:36allow us to retrieve the required
- 8:38related data as a part of the same
- 8:40query. Or we'd heavy APIs, we might
- 8:42prefer a DTO projections. Instead of
- 8:44loading complete entity graph, we
- 8:46retrieve only the fields that API
- 8:49actually needed. What I would avoid is
- 8:51blindly changing the relationship to
- 8:53eager. So, that can solve N+ query
- 8:55problem while creating another
- 8:57performance problem somewhere else
- 8:59because now every use of that entity may
- 9:02automatically load additional data. So,
- 9:04remember, don't ask should this
- 9:06relationship be eager? Ask what data
- 9:09does this specific use case actually
- 9:11need? This shift from entity-centric
- 9:14thinking to use case-centric thinking is
- 9:16very important at architect level. Now,
- 9:19we all know that in microservice
- 9:21architecture, there are number of
- 9:23instances are running for a particular
- 9:25microservice. So, suppose we have five
- 9:28different Spring Boot instances running
- 9:31into a production and out of these five
- 9:33different instances, four are healthy
- 9:36and one instance is repeatedly crashing
- 9:38with out of memory error. Now, in this
- 9:40particular scenario, interviewer might
- 9:42be asked how would you investigate why
- 9:44this is happening? Now, the first
- 9:46question would be why is only one
- 9:49instance affected? So, we would compare
- 9:51the traffic and memory consumption
- 9:54across all five instances. Maybe one
- 9:56instance is receiving significantly more
- 9:59traffic or maybe an load balancer is not
- 10:01distributing traffic evenly or perhaps
- 10:04that instance is processing a specific
- 10:06type of request. Then we would
- 10:08investigate the heap usage, old
- 10:10generation growth, garbage collection
- 10:12behavior, threads count, thread local
- 10:14usage, static references, cache growth,
- 10:17and an unusually large collections. We
- 10:20would also capture a heap dump from
- 10:22affected instances if possible. And the
- 10:25heap dump can help to identify which
- 10:28objects are consuming memory and whether
- 10:30some objects are unexpectedly being
- 10:33retained. We would also compare the
- 10:35cache size between healthy and unhealthy
- 10:38instances. The key is to determine
- 10:40whether it is a memory leak, an even
- 10:43profit, or a cache growth problem, or an
- 10:45instance spreading workload. We would
- 10:47not immediately increase the heap size
- 10:49because that would simply delay the
- 10:51crashes. Now here is the architect
- 10:53mindset. When only one instance fails,
- 10:56don't immediately blame the entire
- 10:58application. Compare the healthy
- 11:00instances with the unhealthy one, and
- 11:02the difference between them often
- 11:04contain the answer of this question. Now
- 11:06imagine two users read the same account
- 11:09balance, both see the same value, both
- 11:11make changes, and one user update
- 11:14overrides the other update. Now
- 11:15interviewer will ask, how would you
- 11:17prevent the last updates? Common
- 11:19solution is optimistic locking. In JPA,
- 11:22we can add a version field using @
- 11:24version. For example, we have a private
- 11:27long version field and hibernate that
- 11:29includes the version in the update
- 11:30condition. So effectively the update
- 11:33says update this row only if the version
- 11:36is still the same version I originally
- 11:38read. If another transaction has already
- 11:40modified the record, the version will be
- 11:42changed and the update fails. Now at
- 11:45this point, the application can retry
- 11:47the operation or return a conflict to
- 11:49the user. This work very well when
- 11:52contention is relatively low. If there
- 11:54is a extremely high contention and we
- 11:57genuinely need a row level
- 11:59serialization, pessimistic locking may
- 12:01be a more appropriate. But I wouldn't
- 12:03automatically choose pessimistic locking
- 12:05because it can reduce the concurrency
- 12:08and introduce lock contention. So the
- 12:10interviewer answer is not simply use at
- 12:13the rate version. The better answer is
- 12:16use optimistic locking when contention
- 12:18is reasonable and choose pessimistic
- 12:21locking when the business actually
- 12:23requires strong serialization. Now in
- 12:25this scenario, your spring boot API
- 12:27works perfectly on your system, but
- 12:30after deployment to the production,
- 12:32requests start timing out. Now in this
- 12:34case, interviewer will ask what is your
- 12:36approach to check this issue? So we
- 12:39would trace the entire request path
- 12:41first and then the client, load
- 12:43balancer, network and security layer,
- 12:46application instances, database and
- 12:48external APIs. I would verify
- 12:51environment variable and active spring
- 12:52profiles, then credentials and secrets,
- 12:55then possibly whether the production
- 12:57endpoint is correct or not, then the DNS
- 12:59resolution, maybe the TLS configuration,
- 13:02port, security group, firewall, network
- 13:04routing, everything. The application
- 13:06might be trying to connect to a database
- 13:08endpoint or which can be locally
- 13:11accessible but blocked in the production
- 13:13subnet. So we we need to check all the
- 13:15possibilities here. In apart from this
- 13:18mentioning that only a code related
- 13:20issue. So a production profile might
- 13:22pointing to a different external
- 13:24services as well. So these things also
- 13:26we need to be checked. So would avoid
- 13:28assuming that spring boot itself is a
- 13:30problem. In this particular scenario
- 13:33based question, we need to define a
- 13:35generic methodology where all the
- 13:38possible things we need to be checked
- 13:40before we say that this is the problem
- 13:42related to a spring boot itself. So
- 13:44production only problem is often a
- 13:46system problem, not a code problem. That
- 13:48would be the basic idea you would have.
- 13:51Now don't just inspect the Java
- 13:52application blindly. Trace the entire
- 13:55path first from the client to the
- 13:56dependency and that would be your
- 13:58generic answer in this particular
- 14:00scenario because nobody can provide a
- 14:02perfect answer in this particular
- 14:03scenario based question. So the problem
- 14:05might be at particular level, but we
- 14:08need to be lean to that particular
- 14:10problem. So to get that particular
- 14:13issue, we have to check all possible
- 14:16scenarios in this case. Now this
- 14:17question in the scenario where your
- 14:20third party API is getting slow. So
- 14:22suppose you have API calls a payment
- 14:24service. Sometimes the provider taken 10
- 14:27seconds to respond. Your own API becomes
- 14:30slow because of it. Now interviewer will
- 14:32ask how would you make your API
- 14:33resilient? So for synchronous
- 14:35communication, I would start the strip
- 14:37connection and read timeouts then the
- 14:39bounded retries, not unlimited retries.
- 14:42Retries should generally use exponential
- 14:45backoff. So I would also consider a
- 14:47circuit breaker if the payment provider
- 14:49is consistently failing or timeout. The
- 14:52circuit breaker can prevent every
- 14:54application request from waiting for a
- 14:57some failure dependencies. So bulkhead
- 14:59isolation is another important pattern.
- 15:02The goal is to prevent one slow
- 15:04dependency from consuming all
- 15:06application resources. If the business
- 15:08allows a synchronous processing, I would
- 15:10consider a completely different
- 15:11architecture. The API is calling or
- 15:14pushing the messages into Q and Kafka
- 15:16broker then the payments provider. Now
- 15:18the client doesn't have to keep an HTTP
- 15:21connection open while waiting for an
- 15:23external provider. Now this is a classic
- 15:25distributed system principle. Never
- 15:27allow an external dependency to consume
- 15:30unlimited resources inside your
- 15:32application. Timeouts, retries, circuit
- 15:34breakers, bulkheads, asynchronous
- 15:36processing are the tools to protect your
- 15:38system from someone else's failure. Now
- 15:41this question is related to @ async.
- 15:43Suppose you have added @ async to a sent
- 15:46email method, but when you send a test
- 15:48it, the method is still running
- 15:50synchronously. Now the interviewer will
- 15:52ask that what could be the possible root
- 15:54cause for this? Now first they would
- 15:56check whether the asynchronous
- 15:57processing is enabled with @ enable
- 16:00async or not. Then we would check
- 16:02whether the method is being invoked
- 16:04through the spring proxy or not. Like
- 16:06suppose you have been called send email
- 16:08method using this this dot send email
- 16:11method and this actually bypassing the
- 16:13proxy. Instead this asynchronous method
- 16:16should normally be invoked through
- 16:17another spring managed bean. You would
- 16:20also look at the executor
- 16:21configurations. In production, we
- 16:23wouldn't rely blindly on an unbounded
- 16:25default configuration. We would
- 16:27configure a bounded thread pool task
- 16:30executor with appropriate core threads,
- 16:32maximum threads, and queue capacity
- 16:34because the asynchronous does not mean
- 16:36unlimited and that's simply move the
- 16:38bottleneck somewhere else unless you
- 16:40control the executor. So whenever you
- 16:43hear at the rate async immediately think
- 16:46that proxy plus executor plus capacity.
- 16:48Now in this question, suppose the
- 16:50configuration data changes only once per
- 16:53day but and your API received 50,000
- 16:56request every hour that repeatedly read
- 16:58that configuration. Now interviewer will
- 17:00ask what would be your approach in this
- 17:03particular case? So this is a good cache
- 17:05candidate. So this is a good caching
- 17:08candidate. Now we would cache the
- 17:10expensive reads, for example, using
- 17:12string cache at the rate cacheable and a
- 17:15configuration key would be passed over
- 17:16here. Now for a single application
- 17:18instance, caffeine can be a very good
- 17:20local cache option but if we have a
- 17:22multiple application instances and all
- 17:25instances need a shared view of the
- 17:27cache, we might consider a radish or
- 17:29another distributed cache like ignite as
- 17:32well. So however, we wouldn't choose a
- 17:34cache simply because it's fast. We would
- 17:36evaluate how frequently date does the
- 17:39data changes, how stale can the data be,
- 17:42how validation will be happening, how
- 17:45much memory will the cache consume, and
- 17:48what happen if the cache is unavailable,
- 17:50and how many application instances need
- 17:53to be that particular data. So the
- 17:55senior level caching question is not
- 17:57like should I use radish? It is like
- 18:00what the consistency, latency,
- 18:02invalidation, scalability requirement
- 18:04does this data have? Now this question
- 18:06is dedicated to the Kafka. Spring Boot
- 18:09Kafka consumer occasionally receive the
- 18:11same event twice. Now the interviewer
- 18:13will ask him how do you make sure the
- 18:16processing will be safe? Now we would
- 18:18make the a operation idempotent. Every
- 18:21event should have a unique event ID.
- 18:23Before processing the event, the
- 18:25consumer check whether that the event ID
- 18:27has been already been processed or not.
- 18:29If it has, the consumer can safely
- 18:32ignore the duplicate. If it hasn't, then
- 18:34the process the event with the event ID.
- 18:36We would enforce uniqueness at the
- 18:38database level as well. The important
- 18:40point is that you would not rely purely
- 18:43on messaging system to guarantee exactly
- 18:46once the business behavior will occur
- 18:48because even if message delivery
- 18:50semantics are strong, the business
- 18:52operations may involve external systems
- 18:55or database operations as well. So the
- 18:57business operation itself should be
- 18:58designed to tolerate duplicate delivery.
- 19:01Whenever we are designing the Kafka
- 19:03consumers, remember at least once
- 19:06delivery means duplicates are a normal
- 19:09possibility. Do design your consumer
- 19:12assuming that Kafka will never send this
- 19:14message twice. But design it so that the
- 19:16processing it twice is harmless. Time
- 19:19scenario, suppose an API returns a huge
- 19:22list around 1 lakh records. The memory
- 19:25usage is high. Response time is slow.
- 19:27Now the interviewer is asking, what
- 19:29would you make a change in this case?
- 19:31First, I would avoid returning all
- 19:34records in a single request. We would
- 19:36introduce a pagination for moderate data
- 19:39set, traditional limits and offset
- 19:41pagination can work. But for very large
- 19:43data set, key set pagination is often
- 19:45better. For example, you have written a
- 19:48query where you have mentioned where ID
- 19:50is greater than last ID followed by
- 19:53order by ID and limit this particular
- 19:56size. Now instead of asking the database
- 19:58to skip the hundred of thousands of
- 20:00rows, we will continue from a known
- 20:03position. I would also return only the
- 20:06field required by an API rather than
- 20:09loading complete entity. So when someone
- 20:12say the API is slow because there is a
- 20:15too much data, don't just increase the
- 20:17memory. Ask why are we transferring and
- 20:19processing the data that the client does
- 20:22not need. So have to shrink the data in
- 20:25the API which only are required for a
- 20:28particular API response. That way we can
- 20:32make this API faster. Also the
- 20:34pagination would be the best approach in
- 20:37case of processing data with a huge
- 20:39load.
- 20:40Now here this particular scenario is
- 20:42based on a CPU spike. Now suppose we
- 20:45have a database CPU is normal but one
- 20:48spring boot instance suddenly reaches
- 20:4995% CPU. Now the interviewer is asking
- 20:52how would you troubleshoot this problem?
- 20:55Now we would start with the evidence.
- 20:57First the CPU and the thread matrix.
- 20:59Then we would capture a thread dump as
- 21:01well and we want to identify which
- 21:04threads are consuming the CPU and what
- 21:07they are doing. By that way we are going
- 21:10to lean to the problem so that we can
- 21:12find identify the root cause and solve
- 21:15the issue. Now we would investigate the
- 21:17infinite loop, expensive sorting,
- 21:19serialization, regular expression, busy
- 21:22polling and garbage collections as well.
- 21:24If required we would use profiling to
- 21:27identify the hot code path as well. For
- 21:30example, maybe a piece of business logic
- 21:32is accidentally performing an expensive
- 21:34operation thousands of times. So we
- 21:36would not start changing JVM flag before
- 21:39identifying the actual code path. Now
- 21:41the high CPU is a symptom. The important
- 21:44question is not how do I reduce the CPU
- 21:47utilization? The important question is
- 21:49which code is consuming the CPU and why?
- 21:52And that brings us the end of part one.
- 21:54We have covered 15 real world Java
- 21:56Spring Boot, JPA, Hibernate and
- 21:58Postgress interview scenario but we have
- 22:00only done halfway through. As part of
- 22:03second we will tackle next 15 questions
- 22:06and
- 22:06they are even more interesting and
- 22:08challenging. If you found this useful,
- 22:10please subscribe the channel and comment
- 22:12below which scenarios you have found
- 22:14most interesting. Thank you for
- 22:16watching. I will see you in next part.
- 22:18uid:0
About this transcript
This page contains the full transcript of 30 Java & Spring Boot Scenario-Based Interview Questions | Real-World Production Problems | Part 1 by TechFestJava, generated from the public captions YouTube serves with the video. The transcript has 3,698 words across 591 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.