Use the Azure Cosmos DB Emulator for local development and testing

Binaries Download MSI
Docker Docker Hub
Docker source Github

The Azure Cosmos DB Emulator provides a local environment that emulates the Azure Cosmos DB service for development purposes. Using the Azure Cosmos DB Emulator, you can develop and test your application locally, without creating an Azure subscription or incurring any costs. When you're satisfied with how your application is working in the Azure Cosmos DB Emulator, you can switch to using an Azure Cosmos DB account in the cloud.

This article covers the following tasks:

  • Installing the Emulator
  • Authenticating requests
  • Using the Data Explorer in the Emulator
  • Exporting SSL certificates
  • Calling the Emulator from the command line
  • Running the Emulator on Docker for Windows
  • Collecting trace files
  • Troubleshooting

We recommend getting started by watching the following video, where Kirill Gavrylyuk shows how to get started with the Azure Cosmos DB Emulator. Note that the video refers to the emulator as the DocumentDB Emulator, but the tool itself has been renamed the Azure Cosmos DB Emulator since taping the video. All information in the video is still accurate for the Azure Cosmos DB Emulator.

How the Emulator works

The Azure Cosmos DB Emulator provides a high-fidelity emulation of the Azure Cosmos DB service. It supports identical functionality as Azure Cosmos DB, including support for creating and querying JSON documents, provisioning and scaling collections, and executing stored procedures and triggers. You can develop and test applications using the Azure Cosmos DB Emulator, and deploy them to Azure at global scale by just making a single configuration change to the connection endpoint for Azure Cosmos DB.

While we created a high-fidelity local emulation of the actual Azure Cosmos DB service, the implementation of the Azure Cosmos DB Emulator is different than that of the service. For example, the Azure Cosmos DB Emulator uses standard OS components such as the local file system for persistence, and HTTPS protocol stack for connectivity. This means that some functionality that relies on Azure infrastructure like global replication, single-digit millisecond latency for reads/writes, and tunable consistency levels are not available via the Azure Cosmos DB Emulator.

Note

At this time the Data Explorer in the emulator only supports the creation of SQL API collections and MongoDB collections. The Data Explorer in the emulator does not currently support the creation of tables and graphs.

Differences between the Emulator and the service

Because the Azure Cosmos DB Emulator provides an emulated environment running on a local developer workstation, there are some differences in functionality between the emulator and an Azure Cosmos DB account in the cloud:

  • The Azure Cosmos DB Emulator supports only a single fixed account and a well-known master key. Key regeneration is not possible in the Azure Cosmos DB Emulator.
  • The Azure Cosmos DB Emulator is not a scalable service and will not support a large number of collections.
  • The Azure Cosmos DB Emulator does not simulate different Azure Cosmos DB consistency levels.
  • The Azure Cosmos DB Emulator does not simulate multi-region replication.
  • The Azure Cosmos DB Emulator does not support the service quota overrides that are available in the Azure Cosmos DB service (e.g. document size limits, increased partitioned collection storage).
  • As your copy of the Azure Cosmos DB Emulator might not be up to date with the most recent changes with the Azure Cosmos DB service, please Azure Cosmos DB capacity planner to accurately estimate production throughput (RUs) needs of your application.

System requirements

The Azure Cosmos DB Emulator has the following hardware and software requirements:

  • Software requirements
    • Windows Server 2012 R2, Windows Server 2016, or Windows 10
  • Minimum Hardware requirements
    • 2 GB RAM
    • 10 GB available hard disk space

Installation

You can download and install the Azure Cosmos DB Emulator from the Microsoft Download Center or you can run the emulator on Docker for Windows. For instructions on using the Emulator on Docker for Windows, see Running on Docker.

Note

To install, configure, and run the Azure Cosmos DB Emulator, you must have administrative privileges on the computer.

Running on Windows

To start the Azure Cosmos DB Emulator, select the Start button or press the Windows key. Begin typing Azure Cosmos DB Emulator, and select the emulator from the list of applications.

Select the Start button or press the Windows key, begin typing **Azure Cosmos DB Emulator**, and select the emulator from the list of applications

When the emulator is running, you'll see an icon in the Windows taskbar notification area. Azure Cosmos DB local emulator taskbar notification

The Azure Cosmos DB Emulator by default runs on the local machine ("localhost") listening on port 8081.

The Azure Cosmos DB Emulator is installed by default to the C:\Program Files\Azure Cosmos DB Emulator directory. You can also start and stop the emulator from the command-line. See command-line tool reference for more information.

Start Data Explorer

When the Azure Cosmos DB emulator launches it automatically opens the Azure Cosmos DB Data Explorer in your browser. The address appears as https://localhost:8081/_explorer/index.html. If you close the explorer and would like to re-open it later, you can either open the URL in your browser or launch it from the Azure Cosmos DB Emulator in the Windows Tray Icon as shown below.

Azure Cosmos DB local emulator data explorer launcher

Checking for updates

Data Explorer indicates if there is a new update available for download.

Note

Data created in one version of the Azure Cosmos DB Emulator is not guaranteed to be accessible when using a different version. If you need to persist your data for the long term, it is recommended that you store that data in an Azure Cosmos DB account, rather than in the Azure Cosmos DB Emulator.

Authenticating requests

Just as with Azure Cosmos DB in the cloud, every request that you make against the Azure Cosmos DB Emulator must be authenticated. The Azure Cosmos DB Emulator supports a single fixed account and a well-known authentication key for master key authentication. This account and key are the only credentials permitted for use with the Azure Cosmos DB Emulator. They are:

Account name: localhost:<port>
Account key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

Note

The master key supported by the Azure Cosmos DB Emulator is intended for use only with the emulator. You cannot use your production Azure Cosmos DB account and key with the Azure Cosmos DB Emulator.

Note

If you have started the emulator with the /Key option, then use the generated key instead of "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="

Additionally, just as the Azure Cosmos DB service, the Azure Cosmos DB Emulator supports only secure communication via SSL.

Running on a local network

You can run the emulator on a local network. To enable network access, specify the /AllowNetworkAccess option at the command line, which also requires that you specify /Key=key_string or /KeyFile=file_name. You can use /GenKeyFile=file_name to generate a file with a random key upfront. Then you can pass that to /KeyFile=file_name or /Key=contents_of_file.

To enable network access for the first time the user should shutdown the emulator and delete the emulator’s data directory (C:\Users\user_name\AppData\Local\CosmosDBEmulator).

Developing with the Emulator

Once you have the Azure Cosmos DB Emulator running on your desktop, you can use any supported Azure Cosmos DB SDK or the Azure Cosmos DB REST API to interact with the Emulator. The Azure Cosmos DB Emulator also includes a built-in Data Explorer that lets you create collections for the SQL and MongoDB APIs, and view and edit documents without writing any code.

// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(
    new Uri("https://localhost:8081"), 
    "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");

If you're using Azure Cosmos DB protocol support for MongoDB, please use the following connection string:

mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==@localhost:10255/admin?ssl=true

You can use existing tools like Azure DocumentDB Studio to connect to the Azure Cosmos DB Emulator. You can also migrate data between the Azure Cosmos DB Emulator and the Azure Cosmos DB service using the Azure Cosmos DB Data Migration Tool.

Note

If you have started the emulator with the /Key option, then use the generated key instead of "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="

Using the Azure Cosmos DB emulator, by default, you can create up to 25 single partition collections or 1 partitioned collection. For more information about changing this value, see Setting the PartitionCount value.

Export the SSL certificate

.NET languages and runtime use the Windows Certificate Store to securely connect to the Azure Cosmos DB local emulator. Other languages have their own method of managing and using certificates. Java uses its own certificate store whereas Python uses socket wrappers.

In order to obtain a certificate to use with languages and runtimes that do not integrate with the Windows Certificate Store you will need to export it using the Windows Certificate Manager. You can start it by running certlm.msc or follow the step by step instructions in Export the Azure Cosmos DB Emulator Certificates. Once the certificate manager is running, open the Personal Certificates as shown below and export the certificate with the friendly name "DocumentDBEmulatorCertificate" as a BASE-64 encoded X.509 (.cer) file.

Azure Cosmos DB local emulator SSL certificate

The X.509 certificate can be imported into the Java certificate store by following the instructions in Adding a Certificate to the Java CA Certificates Store. Once the certificate is imported into the certificate store, Java and MongoDB applications will be able to connect to the Azure Cosmos DB Emulator.

When connecting to the emulator from Python and Node.js SDKs, SSL verification is disabled.

Command-line tool reference

From the installation location, you can use the command-line to start and stop the emulator, configure options, and perform other operations.

Command-line syntax

CosmosDB.Emulator.exe [/Shutdown] [/DataPath] [/Port] [/MongoPort] [/DirectPorts] [/Key] [/EnableRateLimiting] [/DisableRateLimiting] [/NoUI] [/NoExplorer] [/?]

To view the list of options, type CosmosDB.Emulator.exe /? at the command prompt.

Option Description Command Arguments
[No arguments] Starts up the Azure Cosmos DB Emulator with default settings. CosmosDB.Emulator.exe
[Help] Displays the list of supported command-line arguments. CosmosDB.Emulator.exe /?
Shutdown Shuts down the Azure Cosmos DB Emulator. CosmosDB.Emulator.exe /Shutdown
DataPath Specifies the path in which to store data files. Default is %LocalAppdata%\CosmosDBEmulator. CosmosDB.Emulator.exe /DataPath=<datapath> <datapath>: An accessible path
Port Specifies the port number to use for the emulator. Default is 8081. CosmosDB.Emulator.exe /Port=<port> <port>: Single port number
MongoPort Specifies the port number to use for MongoDB compatibility API. Default is 10255. CosmosDB.Emulator.exe /MongoPort=<mongoport> <mongoport>: Single port number
DirectPorts Specifies the ports to use for direct connectivity. Defaults are 10251,10252,10253,10254. CosmosDB.Emulator.exe /DirectPorts:<directports> <directports>: Comma-delimited list of 4 ports
Key Authorization key for the emulator. Key must be the base-64 encoding of a 64-byte vector. CosmosDB.Emulator.exe /Key:<key> <key>: Key must be the base-64 encoding of a 64-byte vector
EnableRateLimiting Specifies that request rate limiting behavior is enabled. CosmosDB.Emulator.exe /EnableRateLimiting
DisableRateLimiting Specifies that request rate limiting behavior is disabled. CosmosDB.Emulator.exe /DisableRateLimiting
NoUI Do not show the emulator user interface. CosmosDB.Emulator.exe /NoUI
NoExplorer Don't show document explorer on startup. CosmosDB.Emulator.exe /NoExplorer
PartitionCount Specifies the maximum number of partitioned collections. See Change the number of collections for more information. CosmosDB.Emulator.exe /PartitionCount=<partitioncount> <partitioncount>: Maximum number of allowed single partition collections. Default is 25. Maximum allowed is 250.
DefaultPartitionCount Specifies the default number of partitions for a partitioned collection. CosmosDB.Emulator.exe /DefaultPartitionCount=<defaultpartitioncount> <defaultpartitioncount> Default is 25.
AllowNetworkAccess Enables access to the emulator over a network. You must also pass /Key=<key_string> or /KeyFile=<file_name> to enable network access. CosmosDB.Emulator.exe /AllowNetworkAccess /Key=<key_string>

or

CosmosDB.Emulator.exe /AllowNetworkAccess /KeyFile=<file_name>
NoFirewall Don't adjust firewall rules when /AllowNetworkAccess is used. CosmosDB.Emulator.exe /NoFirewall
GenKeyFile Generate a new authorization key and save to the specified file. The generated key can be used with the /Key or /KeyFile options. CosmosDB.Emulator.exe /GenKeyFile=<path to key file>
Consistency Set the default consistency level for the account. CosmosDB.Emulator.exe /Consistency=<consistency> <consistency>: Value must be one of the following consistency levels: Session, Strong, Eventual, or BoundedStaleness. The default value is Session.
? Show the help message.

Change the number of collections

By default, you can create up to 25 single partition collections, or 1 partitioned collection using the Azure Cosmos DB Emulator. By modifying the PartitionCount value, you can create up to 250 single partition collections or 10 partitioned collections, or any combination of the two that does not exceed 250 single partitions (where 1 partitioned collection = 25 single partition collection).

If you attempt to create a collection after the current partition count has been exceeded, the emulator throws a ServiceUnavailable exception, with the following message.

Sorry, we are currently experiencing high demand in this region, 
and cannot fulfill your request at this time. We work continuously 
to bring more and more capacity online, and encourage you to try again. 
Please do not hesitate to email docdbswat@microsoft.com at any time or 
for any reason. ActivityId: 29da65cc-fba1-45f9-b82c-bf01d78a1f91

To change the number of collections available to the Azure Cosmos DB Emulator, do the following:

  1. Delete all local Azure Cosmos DB Emulator data by right-clicking the Azure Cosmos DB Emulator icon on the system tray, and then clicking Reset Data….
  2. Delete all emulator data in this folder C:\Users\user_name\AppData\Local\CosmosDBEmulator.
  3. Exit all open instances by right-clicking the Azure Cosmos DB Emulator icon on the system tray, and then clicking Exit. It may take a minute for all instances to exit.
  4. Install the latest version of the Azure Cosmos DB Emulator.
  5. Launch the emulator with the PartitionCount flag by setting a value <= 250. For example: C:\Program Files\Azure CosmosDB Emulator>CosmosDB.Emulator.exe /PartitionCount=100.

Running on Docker

The Azure Cosmos DB Emulator can be run on Docker for Windows. The Emulator does not work on Docker for Oracle Linux.

Once you have Docker for Windows installed, switch to Windows containers by right-clicking the Docker icon on the toolbar and selecting Switch to Windows containers.

Next, pull the Emulator image from Docker Hub by running the following command from your favorite shell.

docker pull microsoft/azure-cosmosdb-emulator 

To start the image, run the following commands.

From the command line:

md %LOCALAPPDATA%\CosmosDBEmulatorCert 2>null
docker run -v %LOCALAPPDATA%\CosmosDBEmulatorCert:c:\CosmosDBEmulator\CosmosDBEmulatorCert -P -t -i -m 2GB microsoft/azure-cosmosdb-emulator 

From PowerShell:

md $env:LOCALAPPDATA\CosmosDBEmulatorCert 2>null
docker run -v $env:LOCALAPPDATA\CosmosDBEmulatorCert:c:\CosmosDBEmulator\CosmosDBEmulatorCert -P -t -i -m 2GB microsoft/azure-cosmosdb-emulator 

The response looks similar to the following:

Starting Emulator
Emulator Endpoint: https://172.20.229.193:8081/
Master Key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
Exporting SSL Certificate
You can import the SSL certificate from an administrator command prompt on the host by running:
cd /d %LOCALAPPDATA%\CosmosDBEmulatorCert
powershell .\importcert.ps1
--------------------------------------------------------------------------------------------------
Starting interactive shell

Now use the endpoint and master key in from the response in your client and import the SSL certificate into your host. To import the SSL certificate, do the following from an admin command prompt:

cd %LOCALAPPDATA%\CosmosDBEmulatorCert
powershell .\importcert.ps1

Closing the interactive shell once the Emulator has been started will shutdown the Emulator’s container.

To open the Data Explorer navigate to the following URL in your browser. The emulator endpoint is provided in the response message shown above.

https://<emulator endpoint provided in response>/_explorer/index.html

Troubleshooting

Use the following tips to help troubleshoot issues you encounter with the Azure Cosmos DB emulator:

  • If you installed a new version of the Emulator and are experiencing errors, ensure you reset your data. You can reset your data by right-clicking the Azure Cosmos DB Emulator icon on the system tray, and then clicking Reset Data…. If that does not fix the errors, you can uninstall and reinstall the app. See Uninstall the local emulator for instructions.

  • If the Azure Cosmos DB emulator crashes, collect dump files from c:\Users\user_name\AppData\Local\CrashDumps folder, compress them, and attach them to an email to askcosmosdb@microsoft.com.

  • If you experience crashes in CosmosDB.StartupEntryPoint.exe, run the following command from an admin command prompt: lodctr /R

  • If you encounter a connectivity issue, collect trace files, compress them, and attach them to an email to askcosmosdb@microsoft.com.

  • If you receive a Service Unavailable message, the emulator might be failing to initialize the network stack. Check to see if you have the Pulse secure client or Juniper networks client installed, as their network filter drivers may cause the problem. Uninstalling third party network filter drivers typically fixes the issue.

Collect trace files

To collect debugging traces, run the following commands from an administrative command prompt:

  1. cd /d "%ProgramFiles%\Azure Cosmos DB Emulator"
  2. CosmosDB.Emulator.exe /shutdown. Watch the system tray to make sure the program has shut down, it may take a minute. You can also just click Exit in the Azure Cosmos DB emulator user interface.
  3. CosmosDB.Emulator.exe /starttraces
  4. CosmosDB.Emulator.exe
  5. Reproduce the problem. If Data Explorer is not working, you only need to wait for the browser to open for a few seconds to catch the error.
  6. CosmosDB.Emulator.exe /stoptraces
  7. Navigate to %ProgramFiles%\Azure Cosmos DB Emulator and find the docdbemulator_000001.etl file.
  8. Send the .etl file along with repro steps to askcosmosdb@microsoft.com for debugging.

Uninstall the local Emulator

  1. Exit all open instances of the local Emulator by right-clicking the Azure Cosmos DB Emulator icon on the system tray, and then clicking Exit. It may take a minute for all instances to exit.
  2. In the Windows search box, type Apps & features and click on the Apps & features (System settings) result.
  3. In the list of apps, scroll to Azure Cosmos DB Emulator, select it, click Uninstall, then confirm and click Uninstall again.
  4. When the app is uninstalled, navigate to C:\Users<user>\AppData\Local\CosmosDBEmulator and delete the folder.

Next steps

In this tutorial, you've done the following:

  • Installed the local Emulator
  • Rand the Emulator on Docker for Windows
  • Authenticated requests
  • Used the Data Explorer in the Emulator
  • Exported SSL certificates
  • Called the Emulator from the command line
  • Collected trace files

In this tutorial, you've learned how to use the local Emulator for free local development. You can now proceed to the next tutorial and learn how to export Emulator SSL certificates.