October 24, 2016 by Kenneth Fisher
You’ll hear these terms thrown around in programming all the time, but what do they mean and why do we care? Starting with simple definitions first:
- occurring at the same time; coinciding in time; contemporaneous; simultaneous.
- going on at the same rate and exactly together; recurring together.
- Physics, Electricity. having the same frequency and zero phase difference.
- Computers, Telecommunications. of, relating to, or operating using fixed-time intervals controlled by a clock (opposed to asynchronous ).
- Aerospace. geostationary.
- not occurring at the same time.
- (of a computer or other electrical machine) having each operation started only after the preceding operation is completed.
- Computers, Telecommunications. of or relating to operation without the use of fixed time intervals (opposed to synchronous ).
Honestly that’s less helpful than I expected. So I’m going to try my own definitions related specifically to running code along with an example of each.
Synchronous – Code that runs one one line at a time. Each line of code is completed before the next one starts. If an external call is made then it is completed before the next line of code runs.
Asynchronous – Code that is launched and runs separately from the initial code. If a SQL job is launched from inside a batch of code (using sp_start_job for example) then the job is running in parallel (at the same time as) to the remainder of the batch of code.
Ok, so why do we care?
Because sometimes we want to be able to start a piece of code without having to wait for it. Agent jobs and service broker are great ways to do this btw. For example inserting data into a table that needs to be processed but you don’t want to have to wait on the processing to commit the insert. On the other hand there are times when we want to start a job and wait for it to finish.
There are a lot of ways to handle this from both directions and it really depends on what you are doing. The important thing is to remember that the concept exists. That both options exist and know which you need.